You are here

public function NodeAdminTest::testContentAdminSort in Drupal 8

Same name and namespace in other branches
  1. 9 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()

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) {
    $elements = $this
      ->xpath('//table[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [
      ':class' => 'views-table',
      ':label' => $string,
    ]);
    $this
      ->assertTrue(!empty($elements), 'The node was found in the correct order.');
  }

  // 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) {
    $elements = $this
      ->xpath('//table[contains(@class, :class)]/tbody/tr[' . ($delta + 1) . ']/td[2]/a[normalize-space(text())=:label]', [
      ':class' => 'views-table',
      ':label' => $string,
    ]);
    $this
      ->assertTrue(!empty($elements), 'The node was found in the correct order.');
  }
}