You are here

protected function JsonApiDefaultsFunctionalTest::assertPagination in JSON:API Extras 8.3

Checks if pagination links on a jsonapi response are working as expected.

Parameters

array $jsonapi_response: The parsed response from the jsonapi endpoint.

int $page_limit: Limit for amount of items displayed per page.

1 call to JsonApiDefaultsFunctionalTest::assertPagination()
JsonApiDefaultsFunctionalTest::testPagination in modules/jsonapi_defaults/tests/src/Functional/JsonApiDefaultsFunctionalTest.php
Checks standard pagination and page limit overrides.

File

modules/jsonapi_defaults/tests/src/Functional/JsonApiDefaultsFunctionalTest.php, line 161

Class

JsonApiDefaultsFunctionalTest
The test class for the JSON API Defaults functionality.

Namespace

Drupal\Tests\jsonapi_defaults\Functional

Code

protected function assertPagination(array $jsonapi_response, $page_limit) {
  $this
    ->assertArrayHasKey('data', $jsonapi_response);
  $this
    ->assertCount($page_limit, $jsonapi_response['data']);
  $first_node_uuid = $jsonapi_response['data'][0]['attributes']['internalId'];
  $this
    ->assertArrayHasKey('links', $jsonapi_response);
  $this
    ->assertArrayHasKey('next', $jsonapi_response['links']);
  $this
    ->assertArrayNotHasKey('prev', $jsonapi_response['links']);
  $this
    ->assertPagerLink($jsonapi_response['links']['next']['href'], 1, $page_limit);
  $response = $this
    ->drupalGet($jsonapi_response['links']['next']['href']);
  $jsonapi_response = Json::decode($response);
  $this
    ->assertCount($page_limit, $jsonapi_response['data']);
  $this
    ->assertNotEqual($first_node_uuid, $jsonapi_response['data'][0]['attributes']['internalId']);
  $this
    ->assertArrayHasKey('next', $jsonapi_response['links']);
  $this
    ->assertArrayHasKey('prev', $jsonapi_response['links']);
  $this
    ->assertPagerLink($jsonapi_response['links']['next']['href'], 2, $page_limit);
  $this
    ->assertPagerLink($jsonapi_response['links']['prev']['href'], 0, $page_limit);
}