You are here

public function SelectPagerDefaultTest::testElementNumbers in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/Database/SelectPagerDefaultTest.php \Drupal\Tests\system\Functional\Database\SelectPagerDefaultTest::testElementNumbers()
  2. 9 core/modules/system/tests/src/Functional/Database/SelectPagerDefaultTest.php \Drupal\Tests\system\Functional\Database\SelectPagerDefaultTest::testElementNumbers()

Confirms that every pager gets a valid, non-overlapping element ID.

File

core/modules/system/tests/src/Functional/Database/SelectPagerDefaultTest.php, line 138

Class

SelectPagerDefaultTest
Tests the pager query select extender.

Namespace

Drupal\Tests\system\Functional\Database

Code

public function testElementNumbers() {
  $request = Request::createFromGlobals();
  $request->query
    ->replace([
    'page' => '3, 2, 1, 0',
  ]);
  \Drupal::getContainer()
    ->get('request_stack')
    ->push($request);
  $connection = Database::getConnection();
  $query = $connection
    ->select('test', 't')
    ->extend(PagerSelectExtender::class)
    ->element(2)
    ->fields('t', [
    'name',
  ])
    ->orderBy('age')
    ->limit(1);
  $this
    ->assertSame(2, $query
    ->getElement());
  $name = $query
    ->execute()
    ->fetchField();
  $this
    ->assertEquals('Paul', $name, 'Pager query #1 with a specified element ID returned the correct results.');

  // Setting an element smaller than the previous one should not collide with
  // the existing pager.
  $query = $connection
    ->select('test', 't')
    ->extend(PagerSelectExtender::class)
    ->element(1)
    ->fields('t', [
    'name',
  ])
    ->orderBy('age')
    ->limit(1);
  $this
    ->assertSame(1, $query
    ->getElement());
  $name = $query
    ->execute()
    ->fetchField();
  $this
    ->assertEquals('George', $name, 'Pager query #2 with a specified element ID returned the correct results.');
  $query = $connection
    ->select('test', 't')
    ->extend(PagerSelectExtender::class)
    ->fields('t', [
    'name',
  ])
    ->orderBy('age')
    ->limit(1);
  $this
    ->assertSame(3, $query
    ->getElement());
  $name = $query
    ->execute()
    ->fetchField();
  $this
    ->assertEquals('John', $name, 'Pager query #3 with a generated element ID returned the correct results.');
}