You are here

public function FileEntityAdminTest::testFilesAdminSort in File Entity (fieldable files) 8.2

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

File

tests/src/Functional/FileEntityAdminTest.php, line 78

Class

FileEntityAdminTest
Test file administration page functionality.

Namespace

Drupal\Tests\file_entity\Functional

Code

public function testFilesAdminSort() {
  $this
    ->drupalLogin($this->userAdmin);
  $i = 0;
  foreach (array(
    'dd',
    'aa',
    'DD',
    'bb',
    'cc',
    'CC',
    'AA',
    'BB',
  ) as $prefix) {
    $this
      ->createFileEntity(array(
      'filename' => $prefix . $this
        ->randomMachineName(6),
      'created' => $i * 90000,
    ));
    $i++;
  }

  // Test that the default sort by file_managed.created DESC fires properly.
  $files_query = array();
  foreach (\Drupal::entityQuery('file')
    ->sort('created', 'DESC')
    ->execute() as $fid) {
    $files_query[] = FileEntity::load($fid)
      ->label();
  }
  $this
    ->drupalGet('admin/content/files');
  $xpath = '//form[@id="views-form-file-entity-files-overview"]/table[@class="cols-10 responsive-enabled"]/tbody//tr/td[contains(@class, "views-field-filename")]';
  $list = $this
    ->xpath($xpath);
  $entries = [];
  foreach ($list as $entry) {
    $entries[] = trim((string) $entry
      ->getText());
  }
  $this
    ->assertEqual($files_query, $entries, 'Files are sorted in the view according to the default query.');

  // Compare the rendered HTML node list to a query for the files ordered by
  // filename to account for possible database-dependent sort order.
  $files_query = array();
  foreach (\Drupal::entityQuery('file')
    ->sort('filename')
    ->execute() as $fid) {
    $files_query[] = FileEntity::load($fid)
      ->label();
  }
  $this
    ->drupalGet('admin/content/files', array(
    'query' => array(
      'sort' => 'asc',
      'order' => 'filename',
    ),
  ));
  $list = $this
    ->xpath($xpath);
  $entries = [];
  foreach ($list as $entry) {
    $entries[] = trim((string) $entry
      ->getText());
  }
  $this
    ->assertEqual($files_query, $entries, 'Files are sorted in the view the same as they are in the query.');
}