function DatabaseSelectPagerDefaultTestCase::testEvenPagerQuery

Confirm 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

modules/simpletest/tests/database_test.test, line 2571

Class

DatabaseSelectPagerDefaultTestCase

Code

function testEvenPagerQuery() {
    // 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 = db_query('SELECT COUNT(*) FROM {test}')->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_even/' . $limit, array(
            'query' => array(
                'page' => $page,
            ),
        ));
        $data = json_decode($this->drupalGetContent());
        if ($page == $num_pages) {
            $correct_number = $count - $limit * $page;
        }
        $this->assertEqual(count($data->names), $correct_number, format_string('Correct number of records returned by pager: @number', array(
            '@number' => $correct_number,
        )));
    }
}

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