function ConfigEntityQueryTest::testSortRange

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityQueryTest::testSortRange()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php \Drupal\KernelTests\Core\Entity\ConfigEntityQueryTest::testSortRange()
  3. 11.x 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 457

Class

ConfigEntityQueryTest
Tests Config Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testSortRange() : void {
    // 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));
    // Omit optional parameters for the range and sort.
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->range()
        ->sort('id')
        ->execute();
    $this->assertSame([
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
    ], array_values($this->queryResults));
    // Explicitly pass NULL for the range and sort.
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->range(NULL, NULL)
        ->sort('id')
        ->execute();
    $this->assertSame([
        '1',
        '2',
        '3',
        '4',
        '5',
        '6',
        '7',
    ], array_values($this->queryResults));
    // Omit the optional start parameter for the range.
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->range(NULL, 1)
        ->sort('id')
        ->execute();
    $this->assertSame([
        '1',
    ], array_values($this->queryResults));
    // Omit the optional length parameter for the range.
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->range(4)
        ->sort('id')
        ->execute();
    $this->assertSame([
        '5',
        '6',
        '7',
    ], array_values($this->queryResults));
    // Request an empty range.
    $this->queryResults = $this->entityStorage
        ->getQuery()
        ->range(0, 0)
        ->execute();
    $this->assertEmpty($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));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.