SortTest.php

Same filename in this branch
  1. 9 core/modules/jsonapi/tests/src/Unit/Query/SortTest.php
  2. 9 core/tests/Drupal/Tests/PhpCs/SortTest.php
Same filename in other branches
  1. 8.9.x core/modules/jsonapi/tests/src/Unit/Query/SortTest.php
  2. 8.9.x core/modules/views/tests/src/Kernel/Handler/SortTest.php
  3. 10 core/modules/jsonapi/tests/src/Unit/Query/SortTest.php
  4. 10 core/modules/views/tests/src/Kernel/Handler/SortTest.php
  5. 10 core/tests/Drupal/Tests/PhpCs/SortTest.php
  6. 10 core/tests/Drupal/Tests/CSpell/SortTest.php
  7. 11.x core/modules/jsonapi/tests/src/Unit/Query/SortTest.php
  8. 11.x core/modules/views/tests/src/Kernel/Handler/SortTest.php
  9. 11.x core/tests/Drupal/Tests/PhpCs/SortTest.php
  10. 11.x core/tests/Drupal/Tests/CSpell/SortTest.php

Namespace

Drupal\Tests\views\Kernel\Handler

File

core/modules/views/tests/src/Kernel/Handler/SortTest.php

View source
<?php

namespace Drupal\Tests\views\Kernel\Handler;

use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;

/**
 * Tests for core Drupal\views\Plugin\views\sort\SortPluginBase handler.
 *
 * @group views
 */
class SortTest extends ViewsKernelTestBase {
    
    /**
     * Views used by this test.
     *
     * @var array
     */
    public static $testViews = [
        'test_view',
    ];
    
    /**
     * Tests numeric ordering of the result set.
     */
    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',
        ]);
    }
    
    /**
     * Tests string ordering of the result set.
     */
    public function testStringOrdering() {
        $view = Views::getView('test_view');
        $view->setDisplay();
        // Change the ordering
        $view->displayHandlers
            ->get('default')
            ->overrideOption('sorts', [
            'name' => [
                'order' => 'ASC',
                'id' => 'name',
                'table' => 'views_test_data',
                'field' => 'name',
                '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(), 'name'), [
            'views_test_data_name' => 'name',
            'views_test_data_age' => 'age',
        ]);
        $view->destroy();
        $view->setDisplay();
        // Reverse the ordering
        $view->displayHandlers
            ->get('default')
            ->overrideOption('sorts', [
            'name' => [
                'order' => 'DESC',
                'id' => 'name',
                'table' => 'views_test_data',
                'field' => 'name',
                '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(), 'name', TRUE), [
            'views_test_data_name' => 'name',
            'views_test_data_age' => 'age',
        ]);
    }

}

Classes

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

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