You are here

public function ConfigEntityQueryTest::testSortRange 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::testSortRange()

Tests sorting and range on config entity queries.

File

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

Class

ConfigEntityQueryTest
Tests Config Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testSortRange() {

  // Sort by simple ascending/descending.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->sort('number', 'DESC')
    ->execute();
  $this
    ->assertSame([
    '7',
    '3',
    '5',
    '2',
    '1',
    '4',
    '6',
  ], array_values($this->queryResults));
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->sort('number', 'ASC')
    ->execute();
  $this
    ->assertSame([
    '6',
    '4',
    '1',
    '2',
    '5',
    '3',
    '7',
  ], array_values($this->queryResults));

  // Apply some filters and sort.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', '3', '>')
    ->sort('number', 'DESC')
    ->execute();
  $this
    ->assertSame([
    '7',
    '5',
    '4',
    '6',
  ], array_values($this->queryResults));
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->condition('id', '3', '>')
    ->sort('number', 'ASC')
    ->execute();
  $this
    ->assertSame([
    '6',
    '4',
    '5',
    '7',
  ], array_values($this->queryResults));

  // Apply a pager and sort.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->sort('number', 'DESC')
    ->range('2', '2')
    ->execute();
  $this
    ->assertSame([
    '5',
    '2',
  ], array_values($this->queryResults));
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->sort('number', 'ASC')
    ->range('2', '2')
    ->execute();
  $this
    ->assertSame([
    '1',
    '2',
  ], array_values($this->queryResults));

  // Add a range to a query without a start parameter.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->range(0, '3')
    ->sort('id', 'ASC')
    ->execute();
  $this
    ->assertSame([
    '1',
    '2',
    '3',
  ], array_values($this->queryResults));

  // Apply a pager with limit 4.
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->pager('4', 0)
    ->sort('id', 'ASC')
    ->execute();
  $this
    ->assertSame([
    '1',
    '2',
    '3',
    '4',
  ], array_values($this->queryResults));
}