You are here

public function RestfulJsonApiTestCase::testPagination in RESTful 7.2

Test pagination.

File

tests/RestfulJsonApiTestCase.test, line 454
Contains \RestfulJsonApiTestCase.

Class

RestfulJsonApiTestCase
Class RestfulJsonApiTestCase.

Code

public function testPagination() {
  $entities = $this
    ->createEntities();
  $count = count($entities);
  for ($index = 0; $index < 3; $index++) {
    $entities[] = entity_create('entity_test', array(
      'name' => 'main',
      'uid' => $this->account->uid,
    ));
    $entities[$count + $index]
      ->save();
  }

  // 1. Test pagination links in the first page.
  $range = 2;
  $this->handler
    ->setRequest(Request::create('', array(
    'range' => $range,
  )));
  $this->handler
    ->setPath('');
  $this->formatter
    ->setResource($this->handler);
  $resource_field_collections = $this->handler
    ->process();
  $result = drupal_json_decode($this->formatter
    ->format($resource_field_collections));
  $links = $result['links'];
  $expected_links = array(
    'self' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'number' => 1,
          'size' => $range,
        ),
      ),
    )),
    'first' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'size' => $range,
        ),
      ),
    )),
    'last' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'number' => 5,
          'size' => $range,
        ),
      ),
    )),
    'next' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'number' => 2,
          'size' => $range,
        ),
      ),
    )),
  );
  $this
    ->assertEqual($links, $expected_links);

  // 2. Test pagination links in the middle page.
  $range = 2;
  $this->handler
    ->setRequest(Request::create('', array(
    'range' => $range,
    'page' => 2,
  )));
  $this->handler
    ->setPath('');
  $this->formatter
    ->setResource($this->handler);
  $resource_field_collections = $this->handler
    ->process();
  $result = drupal_json_decode($this->formatter
    ->format($resource_field_collections));
  $links = $result['links'];
  $expected_links = array(
    'self' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'number' => 2,
          'size' => $range,
        ),
      ),
    )),
    'first' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'size' => $range,
        ),
      ),
    )),
    'previous' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'number' => 1,
          'size' => $range,
        ),
      ),
    )),
    'last' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'number' => 5,
          'size' => $range,
        ),
      ),
    )),
    'next' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'number' => 3,
          'size' => $range,
        ),
      ),
    )),
  );
  $this
    ->assertEqual($links, $expected_links);

  // 3. Test pagination links in the last page.
  $range = 2;
  $this->handler
    ->setRequest(Request::create('', array(
    'page' => array(
      'size' => $range,
      'number' => 5,
    ),
  )));
  $this->handler
    ->setPath('');
  $this->formatter
    ->setResource($this->handler);
  $resource_field_collections = $this->handler
    ->process();
  $result = drupal_json_decode($this->formatter
    ->format($resource_field_collections));
  $links = $result['links'];
  $expected_links = array(
    'self' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'size' => $range,
          'number' => 5,
        ),
      ),
    )),
    'first' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'size' => $range,
        ),
      ),
    )),
    'previous' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'size' => $range,
          'number' => 4,
        ),
      ),
    )),
    'last' => $this->handler
      ->versionedUrl('', array(
      'query' => array(
        'page' => array(
          'size' => $range,
          'number' => 5,
        ),
      ),
    )),
  );
  $this
    ->assertEqual($links, $expected_links);
}