You are here

function EntityQueryTest::testSort in Zircon Profile 8

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

Test sort().

Warning: this is complicated.

File

core/modules/system/src/Tests/Entity/EntityQueryTest.php, line 325
Contains \Drupal\system\Tests\Entity\EntityQueryTest.

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\system\Tests\Entity

Code

function testSort() {
  $greetings = $this->greetings;
  $figures = $this->figures;

  // Order up and down on a number.
  $this->queryResults = $this->factory
    ->get('entity_test_mulrev')
    ->sort('id')
    ->execute();
  $this
    ->assertResult(range(1, 15));
  $this->queryResults = $this->factory
    ->get('entity_test_mulrev')
    ->sort('id', 'DESC')
    ->execute();
  $this
    ->assertResult(range(15, 1));
  $query = $this->factory
    ->get('entity_test_mulrev')
    ->sort("{$figures}.color")
    ->sort("{$greetings}.format")
    ->sort('id');

  // As we do not have any conditions, here are the possible colors and
  // language codes, already in order, with the first occurrence of the
  // entity id marked with *:
  // 8  NULL pl *
  // 12 NULL pl *
  // 4  NULL tr *
  // 12 NULL tr
  // 2  blue NULL *
  // 3  blue NULL *
  // 10 blue pl *
  // 11 blue pl *
  // 14 blue pl *
  // 15 blue pl *
  // 6  blue tr *
  // 7  blue tr *
  // 14 blue tr
  // 15 blue tr
  // 1  red  NULL
  // 3  red  NULL
  // 9  red  pl *
  // 11 red  pl
  // 13 red  pl *
  // 15 red  pl
  // 5  red  tr *
  // 7  red  tr
  // 13 red  tr
  // 15 red  tr
  $count_query = clone $query;
  $this
    ->assertEqual(15, $count_query
    ->count()
    ->execute());
  $this->queryResults = $query
    ->execute();
  $this
    ->assertResult(8, 12, 4, 2, 3, 10, 11, 14, 15, 6, 7, 1, 9, 13, 5);

  // Test the pager by setting element #1 to page 2 with a page size of 4.
  // Results will be #8-12 from above.
  $request = Request::createFromGlobals();
  $request->query
    ->replace(array(
    'page' => '0,2',
  ));
  \Drupal::getContainer()
    ->get('request_stack')
    ->push($request);
  $this->queryResults = $this->factory
    ->get('entity_test_mulrev')
    ->sort("{$figures}.color")
    ->sort("{$greetings}.format")
    ->sort('id')
    ->pager(4, 1)
    ->execute();
  $this
    ->assertResult(15, 6, 7, 1);

  // Now test the reversed order.
  $query = $this->factory
    ->get('entity_test_mulrev')
    ->sort("{$figures}.color", 'DESC')
    ->sort("{$greetings}.format", 'DESC')
    ->sort('id', 'DESC');
  $count_query = clone $query;
  $this
    ->assertEqual(15, $count_query
    ->count()
    ->execute());
  $this->queryResults = $query
    ->execute();
  $this
    ->assertResult(15, 13, 7, 5, 11, 9, 3, 1, 14, 6, 10, 2, 12, 4, 8);
}