You are here

function RestfulListTestCase::testPagination in RESTful 7

Same name and namespace in other branches
  1. 7.2 tests/RestfulListTestCase.test \RestfulListTestCase::testPagination()

Test pagination.

File

tests/RestfulListTestCase.test, line 600
Contains RestfulListTestCase

Class

RestfulListTestCase
@file Contains RestfulListTestCase

Code

function testPagination() {
  foreach (range(1, 9) as $key) {
    $settings = array(
      'type' => 'article',
      'title' => $key,
    );
    $this
      ->drupalCreateNode($settings);
  }
  $handler = restful_get_restful_handler('articles');

  // Set a smaller range for the pagination.
  $handler
    ->setRange(3);

  // Check pagination of first page.
  $result = $handler
    ->get('', array(
    'page' => 1,
  ));
  $output = drupal_json_decode($handler
    ->format($result));
  $this
    ->assertEqual(count($result), 3);
  $this
    ->assertTrue($output['next'], '"Next" link exists on the first page.');
  $this
    ->assertTrue(empty($output['previous']), '"Previous" link does not exist on the first page.');

  // Check pagination of middle pages
  $result = $handler
    ->get('', array(
    'page' => 2,
  ));
  $output = drupal_json_decode($handler
    ->format($result));
  $this
    ->assertTrue($output['next'], '"Next" link exists on the middle page.');
  $this
    ->assertEqual($output['next']['href'], $handler
    ->versionedUrl('', array(
    'query' => array(
      'page' => 3,
    ),
  )));
  $this
    ->assertTrue($output['previous'], '"Previous" link exists on the middle page.');
  $this
    ->assertEqual($output['previous']['href'], $handler
    ->versionedUrl('', array(
    'query' => array(
      'page' => 1,
    ),
  )));

  // Check pagination of last page.
  $result = $handler
    ->get('', array(
    'page' => 3,
  ));
  $output = drupal_json_decode($handler
    ->format($result));
  $this
    ->assertTrue(empty($output['next']), '"Next" link does not exist on the last page.');
  $this
    ->assertTrue($output['previous'], '"Previous" link exists on the last page.');

  // Check other query strings are retained in the _links.
  $result = $handler
    ->get('', array(
    'page' => 3,
    'sort' => '-id',
  ));
  $output = drupal_json_decode($handler
    ->format($result));
  $this
    ->assertTrue(strpos($output['previous']['href'], 'sort=-id'), 'Query strings are retained in the _links.');

  // Check pagination with non-numeric value.
  try {
    $handler
      ->get('', array(
      'page' => 'string',
    ));
    $this
      ->fail('No exception thrown for pagination with non-numeric value.');
  } catch (\RestfulBadRequestException $e) {
    $this
      ->pass('Correct exception thrown for pagination with non-numeric value.');
  } catch (\Exception $e) {
    $this
      ->fail('Incorrect exception thrown for pagination with non-numeric value.');
  }

  // Check pagination with 0 value.
  try {
    $handler
      ->get('', array(
      'page' => 0,
    ));
    $this
      ->fail('No exception thrown for pagination with 0 value.');
  } catch (\RestfulBadRequestException $e) {
    $this
      ->pass('Correct exception thrown for pagination with 0 value.');
  } catch (\Exception $e) {
    $this
      ->fail('Incorrect exception thrown for pagination with 0 value.');
  }

  // Check pagination with high number, where there are not items, yielded no
  // results, but is a valid call.
  $result = $handler
    ->get('', array(
    'page' => 100,
  ));
  $this
    ->assertEqual($result, array(), 'pagination with high number, where there are not items yielded no results.');

  // Check total number of results.
  $result = $handler
    ->get('', array(
    'page' => 3,
  ));
  $output = drupal_json_decode($handler
    ->format($result));
  $this
    ->assertEqual($output['count'], 9, 'Total count exists and is correct.');
}