Skip to content
Extraits de code Groupes Projets
SorterTest.php 16,6 ko
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
    <?php
    /**
     * Piwik - free/libre analytics platform
     *
     * @link http://piwik.org
     * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
     */
    namespace Piwik\Tests\Unit\Metrics;
    
    use Piwik\DataTable;
    use Piwik\DataTable\Row;
    use Piwik\Metrics;
    use Piwik\Metrics\Sorter;
    use Piwik\Metrics\Sorter\Config;
    use Piwik\Tests\Framework\TestCase\UnitTestCase;
    
    /**
     * @group Core
     * @group sort
     */
    class SorterTest extends UnitTestCase
    {
        /**
         * @var Sorter
         */
        private $sorter;
    
        /**
         * @var Config
         */
        private $config;
    
        public function setUp()
        {
            parent::setUp();
    
            $this->config = new Config();
            $this->config->primaryColumnToSort = 'nb_visits';
            $this->config->primarySortOrder = SORT_DESC;
            $this->config->primarySortFlags = SORT_NUMERIC;
            $this->sorter = new Sorter($this->config);
        }
    
        public function test_getPrimarySortOrder_shouldReturnDescByDefault()
        {
            $this->assertSame(SORT_DESC, $this->sorter->getPrimarySortOrder(null));
            $this->assertSame(SORT_DESC, $this->sorter->getPrimarySortOrder('whatever'));
            $this->assertSame(SORT_DESC, $this->sorter->getPrimarySortOrder('desc'));
        }
    
        public function test_getPrimarySortOrder_shouldReturnAscIfRequestedLowerCase()
        {
            $this->assertSame(SORT_ASC, $this->sorter->getPrimarySortOrder('asc'));
            $this->assertSame(SORT_DESC, $this->sorter->getPrimarySortOrder('AsC')); // we require 'asc' to be lowercase
        }
    
        public function test_getSecondarySortOrder_shouldReturnInvertedOrder_IfColumnIsLabel()
        {
            $this->assertSame(SORT_DESC, $this->sorter->getSecondarySortOrder('asc', 'label'));
            $this->assertSame(SORT_ASC, $this->sorter->getSecondarySortOrder('whatever', 'label'));
            $this->assertSame(SORT_ASC, $this->sorter->getSecondarySortOrder('desc', 'label'));
            $this->assertSame(SORT_ASC, $this->sorter->getSecondarySortOrder('AsC', 'label'));
        }
        public function test_getPrimarySortOrder_shouldReturnDescByDefault_IfNotLabelColumnIsRequested()
        {
            $this->assertSame(SORT_DESC, $this->sorter->getSecondarySortOrder(null, 'nb_visits'));
            $this->assertSame(SORT_DESC, $this->sorter->getSecondarySortOrder('whatever', 'nb_visits'));
            $this->assertSame(SORT_DESC, $this->sorter->getSecondarySortOrder('desc', 'nb_visits'));
        }
    
        public function test_getSecondarySortOrder_shouldReturnAscIfRequestedLowerCase_IfNotLabelColumnIsRequested()
        {
            $this->assertSame(SORT_ASC, $this->sorter->getSecondarySortOrder('asc', 'nb_visits'));
            $this->assertSame(SORT_DESC, $this->sorter->getSecondarySortOrder('AsC', 'nb_visits')); // we require 'asc' to be lowercase
        }
    
        /**
         * @dataProvider getPrimaryColumnsToSort
         */
        public function test_getPrimaryColumnToSort_shouldPickCorrectPrimaryColumnAndMapMetricNameToIdIfNeededAndReverse($expectedUsedColumn, $columnToSortBy)
        {
            $table = $this->createDataTable(array(
                array('label' => 'nintendo', 'nb_visits' => false, 'nb_hits' => 0, Metrics::INDEX_NB_VISITS_CONVERTED => false, Metrics::INDEX_BOUNCE_COUNT => 5)
            ));
    
            $this->assertSame($expectedUsedColumn, $this->sorter->getPrimaryColumnToSort($table, $columnToSortBy));
        }
    
        public function getPrimaryColumnsToSort()
        {
            return array(
                array('nb_visits', 'nb_visits'), // it is present in the row and should be used even though the value is false
                array('nb_hits', 'nb_hits'),      // it is present in the row and should be used even though the value is zero
                array(Metrics::INDEX_NB_VISITS_CONVERTED, 'nb_visits_converted'), // the column name is not present but it should find the column id even though the value is false
                array(Metrics::INDEX_NB_VISITS_CONVERTED, Metrics::INDEX_NB_VISITS_CONVERTED),  // if a column is present as id it should still be able to find it
                array(Metrics::INDEX_BOUNCE_COUNT, 'bounce_count'),  // should resolve column name to id, column has a value
                array(Metrics::INDEX_BOUNCE_COUNT, Metrics::INDEX_BOUNCE_COUNT), // should find a column with a value
            );
        }
    
        public function test_getPrimaryColumnToSort_shouldFallbackToNbVisitsIfPossible()
        {
            $table = $this->createDataTable(array(
                array('label' => 'nintendo', 'nb_visits' => false)
            ));
    
            $this->assertSame('nb_visits', $this->sorter->getPrimaryColumnToSort($table, 'any_random_column_that_doesnt_exist'));
        }
    
        public function test_getPrimaryColumnToSort_shouldFallbackToThePassedColumnNameIfColumnCannotBeFoundAndNbVisitsDoesNotExist()
        {
            $table = $this->createDataTable(array(array('label' => 'nintendo')));
    
            $this->assertSame('any_random_column_that_doesnt_exist', $this->sorter->getPrimaryColumnToSort($table, 'any_random_column_that_doesnt_exist'));
        }
    
        public function test_getSecondaryColumnToSort_shouldNotFindASecondaryColumnToSort_IfSortedByLabelButNoVisitsColumnPresent()
        {
            $row = $this->createRow(array('label' => 'nintendo'));
    
            $this->assertNull($this->sorter->getSecondaryColumnToSort($row, 'label'));
        }
    
        public function test_getSecondaryColumnToSort_shouldPreferVisitsColumn_IfColumnIsPresent_EvenIfValueIsFalse()
        {
            $row = $this->createRow(array('label' => 'nintendo', 'nb_visits' => false, 'nb_hits' => 10));
    
            $this->assertSame('nb_visits', $this->sorter->getSecondaryColumnToSort($row, 'nb_hits'));
            $this->assertSame('nb_visits', $this->sorter->getSecondaryColumnToSort($row, 'label'));
        }
    
        public function test_getSecondaryColumnToSort_shouldPreferVisitsColumn_IfColumnIsPresent_EvenIfVisitsColumnIsId()
        {
            $row = $this->createRow(array('label' => 'nintendo', Metrics::INDEX_NB_VISITS => false, 'nb_hits' => 10));
    
            $this->assertSame(Metrics::INDEX_NB_VISITS, $this->sorter->getSecondaryColumnToSort($row, 'nb_hits'));
            $this->assertSame(Metrics::INDEX_NB_VISITS, $this->sorter->getSecondaryColumnToSort($row, 'label'));
        }
    
        public function test_getSecondaryColumnToSort_shouldUseLabelColumn_IfColumnIsPresentButNotNbVisitsColumn()
        {
            $row = $this->createRow(array('label' => 'nintendo', 'nb_hits' => 10));
    
            $this->assertSame('label', $this->sorter->getSecondaryColumnToSort($row, 'nb_hits'));
        }
    
        public function test_getSecondaryColumnToSort_shouldUseLabelColumn_IfPrimaryColumnIsNbVisitsColumn()
        {
            $row = $this->createRow(array('label' => 'nintendo', 'nb_visits' => 10));
    
            $this->assertSame('label', $this->sorter->getSecondaryColumnToSort($row, 'nb_visits'));
            $this->assertSame('label', $this->sorter->getSecondaryColumnToSort($row, Metrics::INDEX_NB_VISITS));
        }
    
        public function test_getSecondaryColumnToSort_shouldNotBeAbleToFallback_IfVisitsColumnIsUsedButThereIsNoLabelColumn()
        {
            $row = $this->createRow(array('nb_visits' => 10, 'nb_hits' => 10));
    
            $this->assertNull($this->sorter->getSecondaryColumnToSort($row, 'nb_visits'));
        }
    
        public function test_getSecondaryColumnToSort_shouldUseVisitsAsSecondaryColumn_IfLabelIsUsedAsPrimaryColumn()
        {
            $row = $this->createRow(array('label' => 'nintendo', 'nb_visits' => false));
    
            $this->assertSame('nb_visits', $this->sorter->getSecondaryColumnToSort($row, 'label'));
        }
    
        /**
         * @dataProvider getLabelsForNaturalSortTest
         */
        public function test_getBestSortFlags_shouldAlwaysPickStringOrNaturalSortCaseInsensitive($label)
        {
            $table = $this->createDataTable(array(array('label' => $label)));
    
            $this->config->naturalSort = false; // even if natural sort is not preferred it should be still used
            $this->assertSame(SORT_STRING | SORT_FLAG_CASE, $this->sorter->getBestSortFlags($table, 'label'));
    
            $this->config->naturalSort = true;
            $this->assertSame(SORT_NATURAL | SORT_FLAG_CASE, $this->sorter->getBestSortFlags($table, 'label'));
        }
    
        public function getLabelsForNaturalSortTest()
        {
            return array(array('nintendo'), array('2015'), array('240.4'), array(2015), array('/test'));
        }
    
        /**
         * @dataProvider getColumnsForBestSortFlagsTest
         */
        public function test_getBestSortFlags($expectedSortFlags, $columnToReadFrom, $naturalSort = false)
        {
            $this->config->naturalSort = $naturalSort;
    
            $table = $this->createDataTable(array(
                array('label' => 'nintendo1', 'nb_visits' => false, 'nb_hits' => 0, Metrics::INDEX_NB_VISITS_CONVERTED => false, Metrics::INDEX_BOUNCE_COUNT => 5),
                array('label' => 'nintendo2', 'nb_visits' => 100, 'nb_pageviews' => 100, Metrics::INDEX_NB_VISITS_CONVERTED => null, 'sum_visit_length' => '5.5s'),
                array('label' => 'nintendo2', Metrics::INDEX_NB_VISITS_CONVERTED => array(), 'min_time_generation' => '5.5')
            ));
    
            $this->assertSame($expectedSortFlags, $this->sorter->getBestSortFlags($table, $columnToReadFrom));
        }
    
        public function getColumnsForBestSortFlagsTest()
        {
            return array(
                array(SORT_NUMERIC, 'nb_visits'), // should find a numeric value in the first row
                array(SORT_NUMERIC, 'nb_pageviews'), // should find a numeric value in the second row
                array(SORT_STRING | SORT_FLAG_CASE, Metrics::INDEX_NB_VISITS_CONVERTED), // should not find any value in any row and use default value
                array(SORT_NATURAL | SORT_FLAG_CASE, Metrics::INDEX_NB_VISITS_CONVERTED, true), // should not find any value in any row and use default value, natural preferred
                array(SORT_STRING | SORT_FLAG_CASE, 'sum_visit_length'), // it is not numeric so should use string as natural is disabled
                array(SORT_NATURAL | SORT_FLAG_CASE, 'sum_visit_length', true), // it is not numeric but natural is preferred so should use natural sort
                array(SORT_NUMERIC, 'min_time_generation') // value is a string but numeric so should use numeric
            );
        }
    
        public function test_sort_shouldNotFailIfNoRowsAreSet()
        {
            $table = $this->createDataTable(array());
    
            $this->sorter->sort($table);
    
            $this->assertSame(0, $table->getRowsCount());
        }
    
        public function test_sort_shouldSetTheSortedColumnNameOnTheTable()
        {
            $table = $this->createDataTable(array(array('nb_test' => 5)));
            $this->config->primaryColumnToSort = 'nb_test';
    
            $this->sorter->sort($table);
    
            $this->assertSame('nb_test', $table->getSortedByColumnName());
        }
    
        public function test_sort_shouldKeepTheAmountOfColumns()
        {
            $table = $this->createDataTableFromValues(array(5, null));
            $table->addSummaryRow($this->createRow(array('nb_test' => 10)));
    
            $this->sorter->sort($table);
    
            $this->assertSame(3, $table->getRowsCount());
            $this->assertSame(2, $table->getRowsCountWithoutSummaryRow());
        }
    
        public function test_sort_shouldNotSortOrChangeTheSummaryRow()
        {
            $table = $this->createDataTableFromValues(array(5, null));
            $table->addSummaryRow($this->createRow(array('nb_test' => 10)));
    
            $this->sorter->sort($table);
    
            $summaryRow = $table->getRowFromId(DataTable::ID_SUMMARY_ROW);
    
            $this->assertSame(10, $summaryRow->getColumn('nb_test'));
        }
    
        public function test_sort_shouldSortNumeric_AndShouldAddEmptyValuesAlwaysAtTheEnd()
        {
            $table = $this->createDataTableFromValues(array(5, null, 61, array(), 10, false, 20, 15));
    
            $this->config->primarySortFlags = SORT_NUMERIC;
            $this->config->primarySortOrder = SORT_ASC;
            $this->sorter->sort($table);
    
            $expected = array(5, 10, 15, 20, 61, false, array(), false);
            $this->assertExpectedRowsOrder($expected, $table);
    
            $this->config->primarySortOrder = SORT_DESC;
            $this->sorter->sort($table);
    
            $expected = array(61, 20, 15, 10, 5, false, array(), false);
            $this->assertExpectedRowsOrder($expected, $table);
        }
    
        public function test_sort_sortNatural_ShoudAddEmptyValuesAlwaysAtTheEnd()
        {
            $table = $this->createDataTableFromValues(array('nintendo', null, 'abc', array(), 'DeF', 'def', false, '1210', 'piwik'));
    
            $this->config->primarySortFlags = SORT_NATURAL;
            $this->config->primarySortOrder = SORT_ASC;
            $this->sorter->sort($table);
    
            $expected = array('1210', 'DeF', 'abc', 'def', 'nintendo', 'piwik', false, array(), false);
            $this->assertExpectedRowsOrder($expected, $table);
    
            $this->config->primarySortOrder = SORT_DESC;
            $this->sorter->sort($table);
    
            $expected = array('piwik', 'nintendo', 'def', 'abc', 'DeF', '1210', false, array(), false);
            $this->assertExpectedRowsOrder($expected, $table);
        }
    
        public function test_sort_ShoudIgnoreASecondColumnSort_IfDisabled()
        {
            $table = $this->createDataTableFromValues(array('abc', 'abc', 'abc', 'abc', 'abc'));
    
            $this->config->primarySortFlags = SORT_NATURAL;
            $this->config->isSecondaryColumnSortEnabled = false;
            $this->sorter->sort($table);
    
            // we make sure the labels order did not change neither when ASC nor DESC
            $expected = array('My Label 0', 'My Label 1', 'My Label 2', 'My Label 3', 'My Label 4');
            $this->assertExpectedRowsOrder($expected, $table, 'label');
    
            $this->config->secondarySortOrder = SORT_DESC;
            $this->sorter->sort($table);
    
            $this->assertExpectedRowsOrder($expected, $table, 'label');
        }
    
        public function test_sort_ShoudIgnoreASecondColumnSort_IfSortIsNumericButNoSecondaryColumnIsSet()
        {
            $table = $this->createDataTableFromValues(array('abc', 'abc', 'abc', 'abc', 'abc'));
    
            $this->config->primarySortFlags = SORT_NUMERIC;
            $this->sorter->sort($table);
    
            // we make sure the labels order did not change neither when ASC nor DESC
            $expected = array('My Label 0', 'My Label 1', 'My Label 2', 'My Label 3', 'My Label 4');
            $this->assertExpectedRowsOrder($expected, $table, 'label');
    
            $this->config->secondarySortOrder = SORT_DESC;
            $this->sorter->sort($table);
    
            $this->assertExpectedRowsOrder($expected, $table, 'label');
        }
    
        public function test_sort_ShoudSortBySecondColumn_IfSortedNumeric()
        {
            $table = $this->createDataTableFromValues(array('abc', 'abc', 'abc', 'abc', 'abc'));
    
            $this->config->primarySortFlags = SORT_NUMERIC;
            $this->config->secondaryColumnToSort = 'label';
            $this->config->secondarySortOrder = SORT_ASC;
            $this->config->secondarySortFlags = SORT_NATURAL;
    
            $this->sorter->sort($table);
    
            // we make sure the labels order did not change neither when ASC nor DESC
            $expected = array('My Label 0', 'My Label 1', 'My Label 2', 'My Label 3', 'My Label 4');
            $this->assertExpectedRowsOrder($expected, $table, 'label');
    
            $this->config->secondarySortOrder = SORT_DESC;
            $this->sorter->sort($table);
    
            $expected = array_reverse($expected);
            $this->assertExpectedRowsOrder($expected, $table, 'label');
        }
    
        public function test_sort_ShoudSortEmptyValues_BySecondColumn_IfSortedNumeric()
        {
            $table = $this->createDataTableFromValues(array(null, null, null, null, null));
    
            $this->config->primarySortFlags = SORT_NUMERIC;
            $this->config->secondaryColumnToSort = 'label';
            $this->config->secondarySortOrder = SORT_ASC;
            $this->config->secondarySortFlags = SORT_NATURAL;
    
            $this->sorter->sort($table);
    
            // we make sure the labels order did not change neither when ASC nor DESC
            $expected = array('My Label 0', 'My Label 1', 'My Label 2', 'My Label 3', 'My Label 4');
            $this->assertExpectedRowsOrder($expected, $table, 'label');
    
            $this->config->secondarySortOrder = SORT_DESC;
            $this->sorter->sort($table);
    
            $expected = array_reverse($expected);
            $this->assertExpectedRowsOrder($expected, $table, 'label');
        }
    
        private function assertExpectedRowsOrder($expectedValuesOrder, $table, $column = 'nb_visits')
        {
            foreach ($table->getRows() as $index => $row) {
                $this->assertSame($expectedValuesOrder[$index], $row->getColumn($column));
            }
        }
    
        private function createDataTableFromValues($values)
        {
            $rows = array();
            foreach ($values as $index => $value) {
                $rows[] = array('nb_visits' => $value, 'label' => 'My Label ' . $index);
            }
    
            return $this->createDataTable($rows);
        }
    
        private function createDataTable($rows)
        {
            $table = new DataTable();
            foreach ($rows as $columns) {
                $table->addRow($this->createRow($columns));
            }
            return $table;
        }
    
        private function createRow($columns)
        {
            return new Row(array(Row::COLUMNS => $columns));
        }
    
    }