function SortDateTest::testDateOrdering

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/SortDateTest.php \Drupal\Tests\views\Kernel\Handler\SortDateTest::testDateOrdering()
  2. 8.9.x core/modules/views/tests/src/Kernel/Handler/SortDateTest.php \Drupal\Tests\views\Kernel\Handler\SortDateTest::testDateOrdering()
  3. 10 core/modules/views/tests/src/Kernel/Handler/SortDateTest.php \Drupal\Tests\views\Kernel\Handler\SortDateTest::testDateOrdering()

Tests numeric ordering of the result set.

File

core/modules/views/tests/src/Kernel/Handler/SortDateTest.php, line 159

Class

SortDateTest
Tests for core Drupal\views\Plugin\views\sort\Date handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testDateOrdering() : void {
    foreach ([
        'second',
        'minute',
        'hour',
        'day',
        'month',
        'year',
    ] as $granularity) {
        foreach ([
            FALSE,
            TRUE,
        ] as $reverse) {
            $view = Views::getView('test_view');
            $view->setDisplay();
            // Change the fields.
            $view->displayHandlers
                ->get('default')
                ->overrideOption('fields', [
                'name' => [
                    'id' => 'name',
                    'table' => 'views_test_data',
                    'field' => 'name',
                    'relationship' => 'none',
                ],
                'created' => [
                    'id' => 'created',
                    'table' => 'views_test_data',
                    'field' => 'created',
                    'relationship' => 'none',
                ],
            ]);
            // Change the ordering
            $view->displayHandlers
                ->get('default')
                ->overrideOption('sorts', [
                'created' => [
                    'id' => 'created',
                    'table' => 'views_test_data',
                    'field' => 'created',
                    'relationship' => 'none',
                    'granularity' => $granularity,
                    'order' => $reverse ? 'DESC' : 'ASC',
                ],
                'id' => [
                    'id' => 'id',
                    'table' => 'views_test_data',
                    'field' => 'id',
                    'relationship' => 'none',
                    'order' => 'ASC',
                ],
            ]);
            // Execute the view.
            $this->executeView($view);
            // Verify the result.
            $this->assertIdenticalResultset($view, $this->expectedResultSet($granularity, $reverse), [
                'views_test_data_name' => 'name',
            ], sprintf('Result is returned correctly when ordering by granularity %s, %s.', $granularity, $reverse ? 'reverse' : 'forward'));
            $view->destroy();
            unset($view);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.