View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
use Drupal\user\Entity\User;
class EntityTestTest extends ResourceTestBase {
use BcTimestampNormalizerUnixTestTrait;
public static $modules = [
'entity_test',
];
protected static $entityTypeId = 'entity_test';
protected static $resourceTypeName = 'entity_test--entity_test';
protected static $patchProtectedFieldNames = [];
protected $entity;
protected function setUpAuthorization($method) {
switch ($method) {
case 'GET':
$this
->grantPermissionsToTestedRole([
'view test entity',
]);
break;
case 'POST':
$this
->grantPermissionsToTestedRole([
'create entity_test entity_test_with_bundle entities',
]);
break;
case 'PATCH':
case 'DELETE':
$this
->grantPermissionsToTestedRole([
'administer entity_test content',
]);
break;
}
}
protected function createEntity() {
$this->container
->get('state')
->set('entity_test.internal_field', TRUE);
\Drupal::entityDefinitionUpdateManager()
->applyUpdates();
$entity_test = EntityTest::create([
'name' => 'Llama',
'type' => 'entity_test',
'internal_string_field' => [
'value' => 'This value shall not be internal!',
],
]);
$entity_test
->setOwnerId(0);
$entity_test
->save();
return $entity_test;
}
protected function getExpectedDocument() {
$self_url = Url::fromUri('base:/jsonapi/entity_test/entity_test/' . $this->entity
->uuid())
->setAbsolute()
->toString(TRUE)
->getGeneratedUrl();
$author = User::load(0);
$normalization = [
'jsonapi' => [
'meta' => [
'links' => [
'self' => 'http://jsonapi.org/format/1.0/',
],
],
'version' => '1.0',
],
'links' => [
'self' => $self_url,
],
'data' => [
'id' => $this->entity
->uuid(),
'type' => 'entity_test--entity_test',
'links' => [
'self' => $self_url,
],
'attributes' => [
'id' => 1,
'created' => (int) $this->entity
->get('created')->value,
'field_test_text' => NULL,
'langcode' => 'en',
'name' => 'Llama',
'type' => 'entity_test',
'uuid' => $this->entity
->uuid(),
],
'relationships' => [
'user_id' => [
'data' => [
'id' => $author
->uuid(),
'type' => 'user--user',
],
'links' => [
'related' => $self_url . '/user_id',
'self' => $self_url . '/relationships/user_id',
],
],
],
],
];
if (floatval(\Drupal::VERSION) < 8.5) {
unset($normalization['data']['attributes']['internal_string_field']);
}
return $normalization;
}
protected function getPostDocument() {
return [
'data' => [
'type' => 'entity_test--entity_test',
'attributes' => [
'name' => 'Dramallama',
],
],
];
}
protected function getExpectedUnauthorizedAccessMessage($method) {
switch ($method) {
case 'GET':
return "The 'view test entity' permission is required.";
case 'POST':
return "The following permissions are required: 'administer entity_test content' OR 'administer entity_test_with_bundle content' OR 'create entity_test entity_test_with_bundle entities'.";
default:
return parent::getExpectedUnauthorizedAccessMessage($method);
}
}
protected function getSparseFieldSets() {
return array_diff_key(parent::getSparseFieldSets(), array_flip([
'nested_empty_fieldset',
'nested_fieldset_with_owner_fieldset',
]));
}
protected static function getExpectedCollectionCacheability(array $collection, array $sparse_fieldset = NULL, AccountInterface $account, $filtered = FALSE) {
$cacheability = parent::getExpectedCollectionCacheability($collection, $sparse_fieldset, $account, $filtered);
if ($filtered) {
$cacheability
->addCacheTags([
'state:jsonapi__entity_test_filter_access_blacklist',
]);
}
return $cacheability;
}
}