function EntityQueryTest::testSort
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testSort()
- 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testSort()
- 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testSort()
Tests sort().
Warning: this is complicated.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ EntityQueryTest.php, line 402
Class
- EntityQueryTest
- Tests Entity Query functionality.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testSort() : void {
$greetings = $this->greetings;
$figures = $this->figures;
// Order up and down on a number.
$this->queryResults = $this->storage
->getQuery()
->accessCheck(FALSE)
->sort('id')
->execute();
$this->assertResult(range(1, 15));
$this->queryResults = $this->storage
->getQuery()
->accessCheck(FALSE)
->sort('id', 'DESC')
->execute();
$this->assertResult(range(15, 1));
$query = $this->storage
->getQuery()
->accessCheck(FALSE)
->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->assertSame(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([
'page' => '0,2',
]);
$request->setSession(new Session(new MockArraySessionStorage()));
\Drupal::getContainer()->get('request_stack')
->push($request);
$this->queryResults = $this->storage
->getQuery()
->accessCheck(FALSE)
->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->storage
->getQuery()
->accessCheck(FALSE)
->sort("{$figures}.color", 'DESC')
->sort("{$greetings}.format", 'DESC')
->sort('id', 'DESC');
$count_query = clone $query;
$this->assertSame(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);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.