You are here

public function SortDateTest::testDateOrdering in Drupal 8

Same name and namespace in other branches
  1. 9 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 158

Class

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

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testDateOrdering() {
  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
        ->assertEqual(count($this
        ->dataSet()), count($view->result), 'The number of returned rows match.');
      $this
        ->assertIdenticalResultset($view, $this
        ->expectedResultSet($granularity, $reverse), [
        'views_test_data_name' => 'name',
      ], new FormattableMarkup('Result is returned correctly when ordering by granularity @granularity, @reverse.', [
        '@granularity' => $granularity,
        '@reverse' => $reverse ? 'reverse' : 'forward',
      ]));
      $view
        ->destroy();
      unset($view);
    }
  }
}