You are here

public function SortDateTest::testDateOrdering in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Handler/SortDateTest.php \Drupal\views\Tests\Handler\SortDateTest::testDateOrdering()

Tests numeric ordering of the result set.

File

core/modules/views/src/Tests/Handler/SortDateTest.php, line 153
Contains \Drupal\views\Tests\Handler\SortDateTest.

Class

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

Namespace

Drupal\views\Tests\Handler

Code

public function testDateOrdering() {
  foreach (array(
    'second',
    'minute',
    'hour',
    'day',
    'month',
    'year',
  ) as $granularity) {
    foreach (array(
      FALSE,
      TRUE,
    ) as $reverse) {
      $view = Views::getView('test_view');
      $view
        ->setDisplay();

      // Change the fields.
      $view->displayHandlers
        ->get('default')
        ->overrideOption('fields', array(
        'name' => array(
          'id' => 'name',
          'table' => 'views_test_data',
          'field' => 'name',
          'relationship' => 'none',
        ),
        'created' => array(
          'id' => 'created',
          'table' => 'views_test_data',
          'field' => 'created',
          'relationship' => 'none',
        ),
      ));

      // Change the ordering
      $view->displayHandlers
        ->get('default')
        ->overrideOption('sorts', array(
        'created' => array(
          'id' => 'created',
          'table' => 'views_test_data',
          'field' => 'created',
          'relationship' => 'none',
          'granularity' => $granularity,
          'order' => $reverse ? 'DESC' : 'ASC',
        ),
        'id' => array(
          '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), array(
        'views_test_data_name' => 'name',
      ), SafeMarkup::format('Result is returned correctly when ordering by granularity @granularity, @reverse.', array(
        '@granularity' => $granularity,
        '@reverse' => $reverse ? 'reverse' : 'forward',
      )));
      $view
        ->destroy();
      unset($view);
    }
  }
}