public function RestfulListTestCase::testPagination in RESTful 7.2
Same name and namespace in other branches
- 7 tests/RestfulListTestCase.test \RestfulListTestCase::testPagination()
Test pagination.
File
- tests/
RestfulListTestCase.test, line 658 - Contains RestfulListTestCase.
Class
- RestfulListTestCase
- Class RestfulListTestCase.
Code
public function testPagination() {
$resource_manager = restful()
->getResourceManager();
foreach (range(1, 9) as $key) {
$settings = array(
'type' => 'article',
'title' => $key,
);
$this
->drupalCreateNode($settings);
}
$handler = $resource_manager
->getPlugin('articles:1.0');
$formatter_handler = restful()
->getFormatterManager()
->negotiateFormatter('');
$instance_configuration = $formatter_handler
->getConfiguration();
$formatter_handler
->setConfiguration(array_merge($instance_configuration ?: array(), array(
'resource' => $handler,
)));
// Set a smaller range for the pagination.
$data_provider = $handler
->getDataProvider();
$data_provider
->setRange(3);
// Check pagination of first page.
$handler
->setRequest(Request::create('', array(
'page' => 1,
), RequestInterface::METHOD_GET));
$handler
->setPath('');
$result = drupal_json_decode($formatter_handler
->format($handler
->process()));
$this
->assertEqual(count($result['data']), 3);
$this
->assertTrue($result['next'], '"Next" link exists on the first page.');
$this
->assertTrue(empty($result['previous']), '"Previous" link does not exist on the first page.');
// Check pagination of middle pages.
$handler
->setRequest(Request::create('', array(
'page' => 2,
), RequestInterface::METHOD_GET));
$result = drupal_json_decode($formatter_handler
->format($handler
->process()));
$this
->assertTrue($result['next'], '"Next" link exists on the middle page.');
$this
->assertEqual($result['next']['href'], $handler
->versionedUrl('', array(
'query' => array(
'page' => array(
'number' => 3,
),
),
)));
$this
->assertTrue($result['previous'], '"Previous" link exists on the middle page.');
$this
->assertEqual($result['previous']['href'], $handler
->versionedUrl('', array(
'query' => array(
'page' => array(
'number' => 1,
),
),
)));
// Check pagination of last page.
$handler
->setRequest(Request::create('', array(
'page' => 3,
), RequestInterface::METHOD_GET));
$result = drupal_json_decode($formatter_handler
->format($handler
->process()));
$this
->assertTrue(empty($result['next']), '"Next" link does not exist on the last page.');
$this
->assertTrue($result['previous'], '"Previous" link exists on the last page.');
// Check other query strings are retained in the _links.
$handler
->setRequest(Request::create('', array(
'page' => 3,
'sort' => '-id',
), RequestInterface::METHOD_GET));
$result = drupal_json_decode($formatter_handler
->format($handler
->process()));
$this
->assertTrue(strpos($result['previous']['href'], 'sort=-id'), 'Query strings are retained in the _links.');
// Check pagination with non-numeric value.
$handler
->setRequest(Request::create('', array(
'page' => 'string',
), RequestInterface::METHOD_GET));
try {
restful()
->getFormatterManager()
->format($handler
->process(), 'json');
$this
->fail('No exception thrown for pagination with non-numeric value.');
} catch (BadRequestException $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.
$handler
->setRequest(Request::create('', array(
'page' => 0,
), RequestInterface::METHOD_GET));
try {
restful()
->getFormatterManager()
->format($handler
->process(), 'json');
$this
->fail('No exception thrown for pagination with 0 value.');
} catch (BadRequestException $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.
$handler
->setRequest(Request::create('', array(
'page' => 100,
), RequestInterface::METHOD_GET));
$result = drupal_json_decode($formatter_handler
->format($handler
->process()));
$this
->assertEqual($result['data'], array(), 'pagination with high number, where there are not items yielded no results.');
// Check total number of results.
$handler
->setRequest(Request::create('', array(
'page' => 3,
), RequestInterface::METHOD_GET));
$result = drupal_json_decode($formatter_handler
->format($handler
->process()));
$this
->assertEqual($result['count'], 9, 'Total count exists and is correct.');
}