function NodeAdminTest::testContentAdminSort
Same name in other branches
- 9 core/modules/node/tests/src/Functional/NodeAdminTest.php \Drupal\Tests\node\Functional\NodeAdminTest::testContentAdminSort()
- 8.9.x core/modules/node/tests/src/Functional/NodeAdminTest.php \Drupal\Tests\node\Functional\NodeAdminTest::testContentAdminSort()
- 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 87
Class
- NodeAdminTest
- Tests node administration page functionality.
Namespace
Drupal\Tests\node\FunctionalCode
public function testContentAdminSort() : void {
$this->drupalLogin($this->adminUser);
$changed = \Drupal::time()->getRequestTime();
$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,
]));
}
// Verify aria-sort is present and its value matches the sort order.
$this->assertSession()
->elementAttributeContains('css', 'table thead tr th.views-field-title', 'aria-sort', 'ascending');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.