You are here

public function ConfigEntityQueryTest::testTableSort in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityQueryTest::testTableSort()

Tests sorting with tableSort on config entity queries.

File

core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php, line 511

Class

ConfigEntityQueryTest
Tests Config Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testTableSort() {
  $header = [
    [
      'data' => t('ID'),
      'specifier' => 'id',
    ],
    [
      'data' => t('Number'),
      'specifier' => 'number',
    ],
  ];

  // Sort key: id
  // Sorting with 'DESC' upper case
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->tableSort($header)
    ->sort('id', 'DESC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), [
    '7',
    '6',
    '5',
    '4',
    '3',
    '2',
    '1',
  ]);

  // Sorting with 'ASC' upper case
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->tableSort($header)
    ->sort('id', 'ASC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
  ]);

  // Sorting with 'desc' lower case
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->tableSort($header)
    ->sort('id', 'desc')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), [
    '7',
    '6',
    '5',
    '4',
    '3',
    '2',
    '1',
  ]);

  // Sorting with 'asc' lower case
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->tableSort($header)
    ->sort('id', 'asc')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), [
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
  ]);

  // Sort key: number
  // Sorting with 'DeSc' mixed upper and lower case
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->tableSort($header)
    ->sort('number', 'DeSc')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), [
    '7',
    '3',
    '5',
    '2',
    '1',
    '4',
    '6',
  ]);

  // Sorting with 'AsC' mixed upper and lower case
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->tableSort($header)
    ->sort('number', 'AsC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), [
    '6',
    '4',
    '1',
    '2',
    '5',
    '3',
    '7',
  ]);

  // Sorting with 'dEsC' mixed upper and lower case
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->tableSort($header)
    ->sort('number', 'dEsC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), [
    '7',
    '3',
    '5',
    '2',
    '1',
    '4',
    '6',
  ]);

  // Sorting with 'aSc' mixed upper and lower case
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->tableSort($header)
    ->sort('number', 'aSc')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), [
    '6',
    '4',
    '1',
    '2',
    '5',
    '3',
    '7',
  ]);
}