diff --git a/core/DataTable.php b/core/DataTable.php index ba598f3c1d0cc062d9658384d44f34ca4d6fbbd0..e653470c6c142fb283602b90e0bae8bdcc25dc5f 100644 --- a/core/DataTable.php +++ b/core/DataTable.php @@ -698,6 +698,7 @@ class DataTable implements DataTableInterface, \IteratorAggregate, \ArrayAccess */ public function rebuildIndex() { + $this->rowsIndexByLabel = array(); $this->rebuildIndexContinuously = true; foreach ($this->rows as $id => $row) { diff --git a/tests/PHPUnit/Unit/DataTableTest.php b/tests/PHPUnit/Unit/DataTableTest.php index b2eb143518c6f4400ced826ebfb07ee88dad74a0..2fa06a5f6a7920d0a01104d9644b3302c255437a 100644 --- a/tests/PHPUnit/Unit/DataTableTest.php +++ b/tests/PHPUnit/Unit/DataTableTest.php @@ -107,6 +107,35 @@ class DataTableTest extends \PHPUnit_Framework_TestCase $this->assertEquals($table2->getRowFromIdSubDataTable($idTable3), $table2->getLastRow()); } + public function test_rebuildIndex() + { + $labels = array(0 => 'abc', 1 => 'def', 2 => 'ghi', 3 => 'jkl', 4 => 'mno'); + $table = new DataTable(); + + $rows = array(); + foreach ($labels as $label) { + $row = new Row(array(Row::COLUMNS => array('label' => $label))); + $table->addRow($row); + $rows[] = $row; + } + + foreach ($labels as $label) { + $rowVerify1 = $table->getRowFromLabel($label); + $this->assertSame($label, $rowVerify1->getColumn('label')); + } + + $table->setRows(array($rows[2], $rows[3], $rows[4])); + $table->rebuildIndex();// rebuildindex would be called anyway but we force rebuilding the index just to make sure + + // verify still accessible + $rowVerify1 = $table->getRowFromLabel('ghi'); + $this->assertSame('ghi', $rowVerify1->getColumn('label')); + + // verify no longer accessible + $rowVerify3 = $table->getRowFromLabel('abc'); + $this->assertFalse($rowVerify3); + } + public function test_clone_shouldIncreasesTableId() { $table = new DataTable;