function SelectPagerDefaultTest::testOddPagerQuery

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Database/SelectPagerDefaultTest.php \Drupal\Tests\system\Functional\Database\SelectPagerDefaultTest::testOddPagerQuery()
  2. 10 core/modules/system/tests/src/Functional/Database/SelectPagerDefaultTest.php \Drupal\Tests\system\Functional\Database\SelectPagerDefaultTest::testOddPagerQuery()
  3. 11.x core/modules/system/tests/src/Functional/Database/SelectPagerDefaultTest.php \Drupal\Tests\system\Functional\Database\SelectPagerDefaultTest::testOddPagerQuery()

Confirms that a pager query returns the correct results.

Note that we have to make an HTTP request to a test page handler because the pager depends on GET parameters.

File

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

Class

SelectPagerDefaultTest
Tests the pager query select extender.

Namespace

Drupal\Tests\system\Functional\Database

Code

public function testOddPagerQuery() {
    // To keep the test from being too brittle, we determine up front
    // what the page count should be dynamically, and pass the control
    // information forward to the actual query on the other side of the
    // HTTP request.
    $limit = 2;
    $count = Database::getConnection()->select('test_task')
        ->countQuery()
        ->execute()
        ->fetchField();
    $correct_number = $limit;
    $num_pages = floor($count / $limit);
    // If there is no remainder from rounding, subtract 1 since we index from 0.
    if (!($num_pages * $limit < $count)) {
        $num_pages--;
    }
    for ($page = 0; $page <= $num_pages; ++$page) {
        $this->drupalGet('database_test/pager_query_odd/' . $limit, [
            'query' => [
                'page' => $page,
            ],
        ]);
        $data = json_decode($this->getSession()
            ->getPage()
            ->getContent());
        if ($page == $num_pages) {
            $correct_number = $count - $limit * $page;
        }
        $this->assertCount($correct_number, $data->names, new FormattableMarkup('Correct number of records returned by pager: @number', [
            '@number' => $correct_number,
        ]));
    }
}

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