You are here

public function ConfigEntityQueryTest::testTableSort in Drupal 9

Same name and namespace in other branches
  1. 8 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
    ->assertSame([
    '7',
    '6',
    '5',
    '4',
    '3',
    '2',
    '1',
  ], array_values($this->queryResults));

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

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

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

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

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

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

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