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. 10 core/modules/views/tests/src/Functional/Handler/FieldWebTest.php \Drupal\Tests\views\Functional\Handler\FieldWebTest::testClickSorting()
  3. 11.x 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 68

Class

FieldWebTest
Tests fields from within a UI.

Namespace

Drupal\Tests\views\Functional\Handler

Code

public function testClickSorting() {
    $this->drupalGet('test_click_sort');
    $this->assertSession()
        ->statusCodeEquals(200);
    // Only the id and name should be click sortable, but not the name.
    $this->assertLinkByHref(Url::fromRoute('<none>', [], [
        'query' => [
            'order' => 'id',
            'sort' => 'asc',
        ],
    ])->toString());
    $this->assertLinkByHref(Url::fromRoute('<none>', [], [
        'query' => [
            'order' => 'name',
            'sort' => 'desc',
        ],
    ])->toString());
    $this->assertNoLinkByHref(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(t('ID'));
    $href = Url::fromRoute('<none>', [], [
        'query' => [
            'order' => 'id',
            'sort' => 'desc',
        ],
    ])->toString();
    $this->assertLinkByHref($href);
    // Check that the output has the expected order (asc).
    $ids = $this->clickSortLoadIdsFromOutput();
    $this->assertEqual($ids, range(1, 5));
    // Check that the rel attribute has the correct value.
    $result = $this->xpath('//a[@href="' . $href . '"]');
    $this->assertEquals('nofollow', $result[0]->getAttribute('rel'));
    $this->clickLink(t('ID Sort descending'));
    // Check that the output has the expected order (desc).
    $ids = $this->clickSortLoadIdsFromOutput();
    $this->assertEqual($ids, range(5, 1, -1));
}

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