You are here

public function ConfigEntityQueryTest::testSortRange in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/ConfigEntityQueryTest.php \Drupal\system\Tests\Entity\ConfigEntityQueryTest::testSortRange()

Tests sorting and range on config entity queries.

File

core/modules/system/src/Tests/Entity/ConfigEntityQueryTest.php, line 431
Contains \Drupal\system\Tests\Entity\ConfigEntityQueryTest.

Class

ConfigEntityQueryTest
Tests Config Entity Query functionality.

Namespace

Drupal\system\Tests\Entity

Code

public function testSortRange() {

  // Sort by simple ascending/descending.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->sort('number', 'DESC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), array(
    '3',
    '5',
    '2',
    '1',
    '4',
  ));
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->sort('number', 'ASC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), array(
    '4',
    '1',
    '2',
    '5',
    '3',
  ));

  // Apply some filters and sort.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', '3', '>')
    ->sort('number', 'DESC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), array(
    '5',
    '4',
  ));
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->condition('id', '3', '>')
    ->sort('number', 'ASC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), array(
    '4',
    '5',
  ));

  // Apply a pager and sort.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->sort('number', 'DESC')
    ->range('2', '2')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), array(
    '2',
    '1',
  ));
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->sort('number', 'ASC')
    ->range('2', '2')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), array(
    '2',
    '5',
  ));

  // Add a range to a query without a start parameter.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->range(0, '3')
    ->sort('id', 'ASC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), array(
    '1',
    '2',
    '3',
  ));

  // Apply a pager with limit 4.
  $this->queryResults = $this->factory
    ->get('config_query_test')
    ->pager('4', 0)
    ->sort('id', 'ASC')
    ->execute();
  $this
    ->assertIdentical(array_values($this->queryResults), array(
    '1',
    '2',
    '3',
    '4',
  ));
}