From 29e9ed9fee668f5694f5ff136d453460b5c33b46 Mon Sep 17 00:00:00 2001 From: Thomas Steur <tsteur@users.noreply.github.com> Date: Sun, 4 Dec 2016 09:51:18 +1300 Subject: [PATCH] Fix a bug where Piwik returns wrong rows by label (#10947) * Fix a bug where Piwik returns wrong rows by label * fix typo * added test --- core/DataTable.php | 1 + tests/PHPUnit/Unit/DataTableTest.php | 29 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/core/DataTable.php b/core/DataTable.php index ba598f3c1d..e653470c6c 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 b2eb143518..2fa06a5f6a 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; -- GitLab