View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTestMapField;
use Drupal\user\Entity\User;
class EntityTestMapFieldTest extends ResourceTestBase {
public static $modules = [
'entity_test',
];
protected static $entityTypeId = 'entity_test_map_field';
protected static $resourceTypeName = 'entity_test_map_field--entity_test_map_field';
protected static $patchProtectedFieldNames = [];
protected $entity;
protected static $mapValue = [
'key1' => 'value',
'key2' => 'no, val you',
'π' => 3.14159,
TRUE => 42,
'nested' => [
'bird' => 'robin',
'doll' => 'Russian',
],
];
protected function setUpAuthorization($method) {
$this
->grantPermissionsToTestedRole([
'administer entity_test content',
]);
}
protected function createEntity() {
$entity = EntityTestMapField::create([
'name' => 'Llama',
'type' => 'entity_test_map_field',
'data' => [
static::$mapValue,
],
]);
$entity
->setOwnerId(0);
$entity
->save();
return $entity;
}
protected function getExpectedDocument() {
$self_url = Url::fromUri('base:/jsonapi/entity_test_map_field/entity_test_map_field/' . $this->entity
->uuid())
->setAbsolute()
->toString(TRUE)
->getGeneratedUrl();
$author = User::load(0);
return [
'jsonapi' => [
'meta' => [
'links' => [
'self' => [
'href' => 'http://jsonapi.org/format/1.0/',
],
],
],
'version' => '1.0',
],
'links' => [
'self' => [
'href' => $self_url,
],
],
'data' => [
'id' => $this->entity
->uuid(),
'type' => 'entity_test_map_field--entity_test_map_field',
'links' => [
'self' => [
'href' => $self_url,
],
],
'attributes' => [
'created' => (new \DateTime())
->setTimestamp($this->entity
->get('created')->value)
->setTimezone(new \DateTimeZone('UTC'))
->format(\DateTime::RFC3339),
'langcode' => 'en',
'name' => 'Llama',
'data' => static::$mapValue,
'drupal_internal__id' => 1,
],
'relationships' => [
'user_id' => [
'data' => [
'id' => $author
->uuid(),
'type' => 'user--user',
],
'links' => [
'related' => [
'href' => $self_url . '/user_id',
],
'self' => [
'href' => $self_url . '/relationships/user_id',
],
],
],
],
],
];
}
protected function getPostDocument() {
return [
'data' => [
'type' => 'entity_test_map_field--entity_test_map_field',
'attributes' => [
'name' => 'Dramallama',
'data' => static::$mapValue,
],
],
];
}
protected function getExpectedUnauthorizedAccessMessage($method) {
return "The 'administer entity_test content' permission is required.";
}
protected function getSparseFieldSets() {
return array_diff_key(parent::getSparseFieldSets(), array_flip([
'nested_empty_fieldset',
'nested_fieldset_with_owner_fieldset',
]));
}
}