View source
<?php
namespace Drupal\Tests\jsonapi_defaults\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\jsonapi\Query\OffsetPage;
use Drupal\jsonapi_extras\Entity\JsonapiResourceConfig;
use Drupal\Tests\jsonapi_extras\Functional\JsonApiExtrasFunctionalTestBase;
use function GuzzleHttp\Psr7\parse_query;
class JsonApiDefaultsFunctionalTest extends JsonApiExtrasFunctionalTestBase {
const PAGE_LIMIT_OVERRIDE_VALUE = 100;
public static $modules = [
'jsonapi_defaults',
];
public function testRead() {
$this
->createDefaultContent(4, 5, TRUE, TRUE, static::IS_NOT_MULTILINGUAL);
$response = $this
->drupalGet('/api/articles');
$parsed_response = Json::decode($response);
$this
->assertArrayHasKey('data', $parsed_response);
$this
->assertCount(1, $parsed_response['data']);
$this
->assertEquals(3, $parsed_response['data'][0]['attributes']['internalId']);
$this
->assertArrayHasKey('included', $parsed_response);
$this
->assertGreaterThan(0, count($parsed_response['included']));
$response = $this
->drupalGet('/api/articles/' . $this->nodes[0]
->uuid() . '/owner');
$parsed_response = Json::decode($response);
$this
->assertArrayHasKey('data', $parsed_response);
$this
->assertEquals('user--user', $parsed_response['data']['type']);
$response = $this
->drupalGet('/api/articles', [
'query' => [
'filter' => [
'i' => [
'condition' => [
'path' => 'internalId',
'value' => '2',
],
],
],
],
]);
$parsed_response = Json::decode($response);
$this
->assertArrayHasKey('data', $parsed_response);
$this
->assertCount(0, $parsed_response['data']);
$response = $this
->drupalGet('/api/articles', [
'query' => [
'include' => '',
],
]);
$parsed_response = Json::decode($response);
$this
->assertArrayNotHasKey('included', $parsed_response);
$this
->setResouceConfigValue([
'default_filter' => [],
]);
$this->nodes[0]
->setTitle('a');
$this->nodes[0]
->save();
$this->nodes[1]
->setTitle('b');
$this->nodes[1]
->save();
$this->nodes[2]
->setTitle('c');
$this->nodes[2]
->save();
$this->nodes[3]
->setTitle('d');
$this->nodes[3]
->save();
$response = $this
->drupalGet('/api/articles');
$parsed_response = Json::decode($response);
$this
->assertEquals('d', $parsed_response['data'][0]['attributes']['title']);
$this
->assertEquals('c', $parsed_response['data'][1]['attributes']['title']);
$this
->assertEquals('b', $parsed_response['data'][2]['attributes']['title']);
$this
->assertEquals('a', $parsed_response['data'][3]['attributes']['title']);
$response = $this
->drupalGet('/api/articles', [
'query' => [
'sort' => [
'title' => [
'path' => 'title',
'direction' => 'ASC',
],
],
],
]);
$parsed_response = Json::decode($response);
$this
->assertEquals('a', $parsed_response['data'][0]['attributes']['title']);
$this
->assertEquals('b', $parsed_response['data'][1]['attributes']['title']);
$this
->assertEquals('c', $parsed_response['data'][2]['attributes']['title']);
$this
->assertEquals('d', $parsed_response['data'][3]['attributes']['title']);
}
public function testPagination() {
$this
->setResouceConfigValue([
'default_filter' => [],
]);
$this
->createDefaultContent(300, 1, FALSE, TRUE, static::IS_NOT_MULTILINGUAL);
$response = $this
->drupalGet('/api/articles');
$this
->assertPagination(Json::decode($response), OffsetPage::SIZE_MAX);
$this
->setResouceConfigValue([
'page_limit' => static::PAGE_LIMIT_OVERRIDE_VALUE,
]);
$response = $this
->drupalGet('/api/articles', [
'query' => [
'page[limit]' => static::PAGE_LIMIT_OVERRIDE_VALUE,
],
]);
$this
->assertPagination(Json::decode($response), static::PAGE_LIMIT_OVERRIDE_VALUE);
$query_override = static::PAGE_LIMIT_OVERRIDE_VALUE + OffsetPage::SIZE_MAX;
$response = $this
->drupalGet('/api/articles', [
'query' => [
'page[limit]' => $query_override,
],
]);
$response = Json::decode($response);
$this
->assertArrayHasKey('data', $response);
$this
->assertNotEqual(count($response['data']), $query_override);
$this
->assertEqual(count($response['data']), static::PAGE_LIMIT_OVERRIDE_VALUE);
}
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);
}
protected function assertPagerLink($url, $page, $page_limit) {
$query = parse_url($url, PHP_URL_QUERY);
$query_params = parse_query($query);
$this
->assertArrayHasKey('page[limit]', $query_params);
$this
->assertArrayHasKey('page[offset]', $query_params);
$this
->assertEquals($query_params['page[offset]'], $page * $page_limit);
$this
->assertEquals($query_params['page[limit]'], $page_limit);
}
protected function setResouceConfigValue(array $values) {
$resource_config = JsonapiResourceConfig::load('node--article');
foreach ($values as $key => $value) {
$resource_config
->setThirdPartySetting('jsonapi_defaults', $key, $value);
}
$resource_config
->save();
}
protected static function overrideResources() {
JsonapiResourceConfig::create([
'id' => 'node--article',
'third_party_settings' => [
'jsonapi_defaults' => [
'default_filter' => [
'filter:nidFilter#condition#path' => 'internalId',
'filter:nidFilter#condition#value' => 3,
],
'default_sorting' => [
'sort:title#path' => 'title',
'sort:title#direction' => 'DESC',
],
'default_include' => [
'tags',
],
],
],
'disabled' => FALSE,
'path' => 'articles',
'resourceType' => 'articles',
'resourceFields' => [
'nid' => [
'fieldName' => 'nid',
'publicName' => 'internalId',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
'uuid' => [
'fieldName' => 'uuid',
'publicName' => 'uuid',
'enhancer' => [
'id' => '',
],
'disabled' => TRUE,
],
'vid' => [
'fieldName' => 'vid',
'publicName' => 'vid',
'enhancer' => [
'id' => '',
],
'disabled' => TRUE,
],
'langcode' => [
'fieldName' => 'langcode',
'publicName' => 'langcode',
'enhancer' => [
'id' => '',
],
'disabled' => TRUE,
],
'type' => [
'fieldName' => 'type',
'publicName' => 'contentType',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
'status' => [
'fieldName' => 'status',
'publicName' => 'isPublished',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
'title' => [
'fieldName' => 'title',
'publicName' => 'title',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
'uid' => [
'fieldName' => 'uid',
'publicName' => 'owner',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
'created' => [
'fieldName' => 'created',
'publicName' => 'createdAt',
'enhancer' => [
'id' => 'date_time',
'settings' => [
'dateTimeFormat' => 'Y-m-d\\TH:i:sO',
],
],
'disabled' => FALSE,
],
'changed' => [
'fieldName' => 'changed',
'publicName' => 'updatedAt',
'enhancer' => [
'id' => 'date_time',
'settings' => [
'dateTimeFormat' => 'Y-m-d\\TH:i:sO',
],
],
'disabled' => FALSE,
],
'promote' => [
'fieldName' => 'promote',
'publicName' => 'isPromoted',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
'sticky' => [
'fieldName' => 'sticky',
'publicName' => 'sticky',
'enhancer' => [
'id' => '',
],
'disabled' => TRUE,
],
'revision_timestamp' => [
'fieldName' => 'revision_timestamp',
'publicName' => 'revision_timestamp',
'enhancer' => [
'id' => '',
],
'disabled' => TRUE,
],
'revision_uid' => [
'fieldName' => 'revision_uid',
'publicName' => 'revision_uid',
'enhancer' => [
'id' => '',
],
'disabled' => TRUE,
],
'revision_log' => [
'fieldName' => 'revision_log',
'publicName' => 'revision_log',
'enhancer' => [
'id' => '',
],
'disabled' => TRUE,
],
'revision_translation_affected' => [
'fieldName' => 'revision_translation_affected',
'publicName' => 'revision_translation_affected',
'enhancer' => [
'id' => '',
],
'disabled' => TRUE,
],
'default_langcode' => [
'fieldName' => 'default_langcode',
'publicName' => 'default_langcode',
'enhancer' => [
'id' => '',
],
'disabled' => TRUE,
],
'path' => [
'fieldName' => 'path',
'publicName' => 'path',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
'body' => [
'fieldName' => 'body',
'publicName' => 'body',
'enhancer' => [
'id' => 'nested',
'settings' => [
'path' => 'value',
],
],
'disabled' => FALSE,
],
'field_link' => [
'fieldName' => 'field_link',
'publicName' => 'link',
'enhancer' => [
'id' => 'uuid_link',
],
'disabled' => FALSE,
],
'field_timestamp' => [
'fieldName' => 'field_timestamp',
'publicName' => 'timestamp',
'enhancer' => [
'id' => 'date_time',
'settings' => [
'dateTimeFormat' => 'Y-m-d\\TH:i:sO',
],
],
'disabled' => FALSE,
],
'comment' => [
'fieldName' => 'comment',
'publicName' => 'comment',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
'field_image' => [
'fieldName' => 'field_image',
'publicName' => 'image',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
'field_recipes' => [
'fieldName' => 'field_recipes',
'publicName' => 'recipes',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
'field_tags' => [
'fieldName' => 'field_tags',
'publicName' => 'tags',
'enhancer' => [
'id' => '',
],
'disabled' => FALSE,
],
],
])
->save();
}
}