function NodeAdminTest::testContentAdminSort

Same name in other branches
  1. 8.9.x core/modules/node/tests/src/Functional/NodeAdminTest.php \Drupal\Tests\node\Functional\NodeAdminTest::testContentAdminSort()
  2. 10 core/modules/node/tests/src/Functional/NodeAdminTest.php \Drupal\Tests\node\Functional\NodeAdminTest::testContentAdminSort()
  3. 11.x core/modules/node/tests/src/Functional/NodeAdminTest.php \Drupal\Tests\node\Functional\NodeAdminTest::testContentAdminSort()

Tests that the table sorting works on the content admin pages.

File

core/modules/node/tests/src/Functional/NodeAdminTest.php, line 86

Class

NodeAdminTest
Tests node administration page functionality.

Namespace

Drupal\Tests\node\Functional

Code

public function testContentAdminSort() {
    $this->drupalLogin($this->adminUser);
    $changed = REQUEST_TIME;
    $connection = Database::getConnection();
    foreach ([
        'dd',
        'aa',
        'DD',
        'bb',
        'cc',
        'CC',
        'AA',
        'BB',
    ] as $prefix) {
        $changed += 1000;
        $node = $this->drupalCreateNode([
            'title' => $prefix . $this->randomMachineName(6),
        ]);
        $connection->update('node_field_data')
            ->fields([
            'changed' => $changed,
        ])
            ->condition('nid', $node->id())
            ->execute();
    }
    // Test that the default sort by node.changed DESC actually fires properly.
    $nodes_query = $connection->select('node_field_data', 'n')
        ->fields('n', [
        'title',
    ])
        ->orderBy('changed', 'DESC')
        ->execute()
        ->fetchCol();
    $this->drupalGet('admin/content');
    foreach ($nodes_query as $delta => $string) {
        // Verify that the node was found in the correct order.
        $this->assertSession()
            ->elementExists('xpath', $this->assertSession()
            ->buildXPathQuery('//table/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [
            ':label' => $string,
        ]));
    }
    // Compare the rendered HTML node list to a query for the nodes ordered by
    // title to account for possible database-dependent sort order.
    $nodes_query = $connection->select('node_field_data', 'n')
        ->fields('n', [
        'title',
    ])
        ->orderBy('title')
        ->execute()
        ->fetchCol();
    $this->drupalGet('admin/content', [
        'query' => [
            'sort' => 'asc',
            'order' => 'title',
        ],
    ]);
    foreach ($nodes_query as $delta => $string) {
        // Verify that the node was found in the correct order.
        $this->assertSession()
            ->elementExists('xpath', $this->assertSession()
            ->buildXPathQuery('//table/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [
            ':label' => $string,
        ]));
    }
}

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