You are here

public function EntityQueryTest::testTableSort in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testTableSort()
  2. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testTableSort()

Tests tablesort().

File

core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php, line 479

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testTableSort() {

  // While ordering on bundles do not give us a definite order, we can still
  // assert that all entities from one bundle are after the other as the
  // order dictates.
  $request = Request::createFromGlobals();
  $request->query
    ->replace([
    'sort' => 'asc',
    'order' => 'Type',
  ]);
  \Drupal::getContainer()
    ->get('request_stack')
    ->push($request);
  $header = [
    'id' => [
      'data' => 'Id',
      'specifier' => 'id',
    ],
    'type' => [
      'data' => 'Type',
      'specifier' => 'type',
    ],
  ];
  $this->queryResults = array_values($this->storage
    ->getQuery()
    ->accessCheck(FALSE)
    ->tableSort($header)
    ->execute());
  $this
    ->assertBundleOrder('asc');
  $request->query
    ->add([
    'sort' => 'desc',
  ]);
  \Drupal::getContainer()
    ->get('request_stack')
    ->push($request);
  $header = [
    'id' => [
      'data' => 'Id',
      'specifier' => 'id',
    ],
    'type' => [
      'data' => 'Type',
      'specifier' => 'type',
    ],
  ];
  $this->queryResults = array_values($this->storage
    ->getQuery()
    ->accessCheck(FALSE)
    ->tableSort($header)
    ->execute());
  $this
    ->assertBundleOrder('desc');

  // Ordering on ID is definite, however.
  $request->query
    ->add([
    'order' => 'Id',
  ]);
  \Drupal::getContainer()
    ->get('request_stack')
    ->push($request);
  $this->queryResults = $this->storage
    ->getQuery()
    ->accessCheck(FALSE)
    ->tableSort($header)
    ->execute();
  $this
    ->assertResult(range(15, 1));
}