You are here

public function SortTest::testNumericOrdering in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/Handler/SortTest.php \Drupal\Tests\views\Kernel\Handler\SortTest::testNumericOrdering()

Tests numeric ordering of the result set.

File

core/modules/views/tests/src/Kernel/Handler/SortTest.php, line 25

Class

SortTest
Tests for core Drupal\views\Plugin\views\sort\SortPluginBase handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testNumericOrdering() {
  $view = Views::getView('test_view');
  $view
    ->setDisplay();

  // Change the ordering
  $view->displayHandlers
    ->get('default')
    ->overrideOption('sorts', [
    'age' => [
      'order' => 'ASC',
      'id' => 'age',
      'table' => 'views_test_data',
      'field' => 'age',
      'relationship' => 'none',
    ],
  ]);

  // Execute the view.
  $this
    ->executeView($view);

  // Verify the result.
  $this
    ->assertSameSize($this
    ->dataSet(), $view->result, 'The number of returned rows match.');
  $this
    ->assertIdenticalResultset($view, $this
    ->orderResultSet($this
    ->dataSet(), 'age'), [
    'views_test_data_name' => 'name',
    'views_test_data_age' => 'age',
  ]);
  $view
    ->destroy();
  $view
    ->setDisplay();

  // Reverse the ordering
  $view->displayHandlers
    ->get('default')
    ->overrideOption('sorts', [
    'age' => [
      'order' => 'DESC',
      'id' => 'age',
      'table' => 'views_test_data',
      'field' => 'age',
      'relationship' => 'none',
    ],
  ]);

  // Execute the view.
  $this
    ->executeView($view);

  // Verify the result.
  $this
    ->assertSameSize($this
    ->dataSet(), $view->result, 'The number of returned rows match.');
  $this
    ->assertIdenticalResultset($view, $this
    ->orderResultSet($this
    ->dataSet(), 'age', TRUE), [
    'views_test_data_name' => 'name',
    'views_test_data_age' => 'age',
  ]);
}