function NodeAdminTest::testContentAdminSort

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

File

drupal/core/modules/node/lib/Drupal/node/Tests/NodeAdminTest.php, line 47
Definition of Drupal\node\Tests\NodeAdminTest.

Class

NodeAdminTest
Tests node administration page functionality.

Namespace

Drupal\node\Tests

Code

function testContentAdminSort() {
  $this
    ->drupalLogin($this->admin_user);

  // Create nodes that have different node.changed values.
  $this->container
    ->get('state')
    ->set('node_test.storage_controller', TRUE);
  module_enable(array(
    'node_test',
  ));
  $changed = REQUEST_TIME;
  foreach (array(
    'dd',
    'aa',
    'DD',
    'bb',
    'cc',
    'CC',
    'AA',
    'BB',
  ) as $prefix) {
    $changed += 1000;
    $this
      ->drupalCreateNode(array(
      'title' => $prefix . $this
        ->randomName(6),
      'changed' => $changed,
    ));
  }

  // Test that the default sort by node.changed DESC actually fires properly.
  $nodes_query = db_select('node_field_data', 'n')
    ->fields('n', array(
    '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]', array(
      ':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 = db_select('node_field_data', 'n')
    ->fields('n', array(
    'title',
  ))
    ->orderBy('title')
    ->execute()
    ->fetchCol();
  $this
    ->drupalGet('admin/content', array(
    'query' => array(
      '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]', array(
      ':class' => 'views-table',
      ':label' => $string,
    ));
    $this
      ->assertTrue(!empty($elements), 'The node was found in the correct order.');
  }
}