You are here

public function EntityQueryTest::testTableSort in Drupal 8

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

Test tablesort().

File

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

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()
    ->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()
    ->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()
    ->tableSort($header)
    ->execute();
  $this
    ->assertResult(range(15, 1));
}