You are here

public function NodeAdminTest::testContentAdminSort in Drupal 9

Same name and namespace in other branches
  1. 8 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 83

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[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [
      ':class' => 'views-table',
      ':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[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [
      ':class' => 'views-table',
      ':label' => $string,
    ]));
  }
}