You are here

function SelectPagerDefaultTest::testEvenPagerQuery in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Database/SelectPagerDefaultTest.php \Drupal\system\Tests\Database\SelectPagerDefaultTest::testEvenPagerQuery()

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/src/Tests/Database/SelectPagerDefaultTest.php, line 24
Contains \Drupal\system\Tests\Database\SelectPagerDefaultTest.

Class

SelectPagerDefaultTest
Tests the pager query select extender.

Namespace

Drupal\system\Tests\Database

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
      ->getRawContent());
    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,
    )));
  }
}