function SelectPagerDefaultTest::testElementNumbers

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Database/SelectPagerDefaultTest.php \Drupal\Tests\system\Functional\Database\SelectPagerDefaultTest::testElementNumbers()
  2. 8.9.x core/modules/system/tests/src/Functional/Database/SelectPagerDefaultTest.php \Drupal\Tests\system\Functional\Database\SelectPagerDefaultTest::testElementNumbers()
  3. 11.x 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 141

Class

SelectPagerDefaultTest
Tests the pager query select extender.

Namespace

Drupal\Tests\system\Functional\Database

Code

public function testElementNumbers() : void {
    $request = Request::createFromGlobals();
    $request->query
        ->replace([
        'page' => '3, 2, 1, 0',
    ]);
    $request->setSession(new Session(new MockArraySessionStorage()));
    \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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.