From 3d8f3ce87b8dc58934717792576530dd3c457bfb Mon Sep 17 00:00:00 2001 From: mattab Date: Fri, 5 Dec 2014 14:58:43 +1300 Subject: [PATCH] Fixes #6562 --- core/DataTable/Filter/Sort.php | 48 +++++++------------ .../Unit/DataTable/Filter/SortTest.php | 19 ++++++++ 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/core/DataTable/Filter/Sort.php b/core/DataTable/Filter/Sort.php index 6ffe33bcedb..95052a12013 100644 --- a/core/DataTable/Filter/Sort.php +++ b/core/DataTable/Filter/Sort.php @@ -73,16 +73,8 @@ public function setOrder($order) */ public function numberSort($a, $b) { - $valA = $a->getColumn($this->columnToSort); - $valB = $b->getColumn($this->columnToSort); - - if ($valA === false) { - $valA = null; - } - - if ($valB === false) { - $valB = null; - } + $valA = $this->getColumnValue($a); + $valB = $this->getColumnValue($b); return !isset($valA) && !isset($valB) @@ -118,16 +110,8 @@ public function numberSort($a, $b) */ function naturalSort($a, $b) { - $valA = $a->getColumn($this->columnToSort); - $valB = $b->getColumn($this->columnToSort); - - if ($valA === false) { - $valA = null; - } - - if ($valB === false) { - $valB = null; - } + $valA = $this->getColumnValue($a); + $valB = $this->getColumnValue($b); return !isset($valA) && !isset($valB) @@ -153,16 +137,8 @@ function naturalSort($a, $b) */ function sortString($a, $b) { - $valA = $a->getColumn($this->columnToSort); - $valB = $b->getColumn($this->columnToSort); - - if ($valA === false) { - $valA = null; - } - - if ($valB === false) { - $valB = null; - } + $valA = $this->getColumnValue($a); + $valB = $this->getColumnValue($b); return !isset($valA) && !isset($valB) @@ -179,6 +155,18 @@ function sortString($a, $b) ); } + protected function getColumnValue(Row $table ) + { + $value = $table->getColumn($this->columnToSort); + + if ($value === false + || is_array($value) + ) { + return null; + } + return $value; + } + /** * Sets the column to be used for sorting * diff --git a/tests/PHPUnit/Unit/DataTable/Filter/SortTest.php b/tests/PHPUnit/Unit/DataTable/Filter/SortTest.php index 4aedcb3d7e7..64eab67a103 100644 --- a/tests/PHPUnit/Unit/DataTable/Filter/SortTest.php +++ b/tests/PHPUnit/Unit/DataTable/Filter/SortTest.php @@ -12,6 +12,9 @@ use Piwik\DataTable; use Piwik\DataTable\Row; +/** + * @group SortTest + */ class DataTable_Filter_SortTest extends \PHPUnit_Framework_TestCase { /** @@ -169,4 +172,20 @@ public function testFilterSortNumeric() $filter->filter($table); $this->assertTrue(DataTable::isEqual($table, $expectedtableReverse)); } + + public function test_sortingArrayValues_doesNotError() + { + $table = new DataTable(); + $table->addRowsFromArray(array( + array(Row::COLUMNS => array('label' => 'ask', 'count_array' => array(100, 1, 2) )), + array(Row::COLUMNS => array('label' => 'nintendo', 'count_array' => array(0, 'hello'))), + array(Row::COLUMNS => array('label' => 'yahoo', 'count_array' => array(10, 'test')) + ))); + + $tableOriginal = clone $table; + + $filter = new Sort($table, 'count_array', 'desc'); + $filter->filter($table); + $this->assertTrue(DataTable::isEqual($tableOriginal, $table)); + } }