function FieldWebTest::testClickSorting

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

Tests the click sorting functionality.

File

core/modules/views/tests/src/Functional/Handler/FieldWebTest.php, line 73

Class

FieldWebTest
Tests fields from within a UI.

Namespace

Drupal\Tests\views\Functional\Handler

Code

public function testClickSorting() : void {
    $this->drupalGet('test_click_sort');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Only the id and name should be click sortable, but not the name.
    $this->assertSession()
        ->linkByHrefExists(Url::fromRoute('<none>', [], [
        'query' => [
            'order' => 'id',
            'sort' => 'asc',
        ],
    ])->toString());
    $this->assertSession()
        ->linkByHrefExists(Url::fromRoute('<none>', [], [
        'query' => [
            'order' => 'name',
            'sort' => 'desc',
        ],
    ])->toString());
    $this->assertSession()
        ->linkByHrefNotExists(Url::fromRoute('<none>', [], [
        'query' => [
            'order' => 'created',
        ],
    ])->toString());
    // Check that the view returns the click sorting cache contexts.
    $expected_contexts = [
        'languages:language_interface',
        'theme',
        'url.query_args',
    ];
    $this->assertCacheContexts($expected_contexts);
    // Clicking a click sort should change the order.
    $this->clickLink('ID');
    $href = Url::fromRoute('<none>', [], [
        'query' => [
            'order' => 'id',
            'sort' => 'desc',
        ],
    ])->toString();
    $this->assertSession()
        ->linkByHrefExists($href);
    // Check that the output has the expected order (asc).
    $ids = $this->clickSortLoadIdsFromOutput();
    $this->assertEquals(range(1, 5), $ids);
    // Check that the rel attribute has the correct value.
    $this->assertSession()
        ->elementAttributeContains('xpath', "//a[@href='{$href}']", 'rel', 'nofollow');
    $this->clickLink('ID Sort descending');
    // Check that the output has the expected order (desc).
    $ids = $this->clickSortLoadIdsFromOutput();
    $this->assertEquals(range(5, 1, -1), $ids);
}

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