You are here

public function ConfigEntityQueryTest::testSortRange 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::testSortRange()
  2. 10 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
    ->assertIdentical(array_values($this->queryResults), [
    '7',
    '3',
    '5',
    '2',
    '1',
    '4',
    '6',
  ]);
  $this->queryResults = $this->entityStorage
    ->getQuery()
    ->sort('number', 'ASC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), [
    '6',
    '4',
    '1',
    '2',
    '5',
    '3',
    '7',
  ]);

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

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

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

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