comparison core/modules/jsonapi/tests/src/Functional/EntityTestTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php
2
3 namespace Drupal\Tests\jsonapi\Functional;
4
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\Core\Session\AccountInterface;
7 use Drupal\Core\Url;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
10 use Drupal\user\Entity\User;
11
12 /**
13 * JSON:API integration test for the "EntityTest" content entity type.
14 *
15 * @group jsonapi
16 */
17 class EntityTestTest extends ResourceTestBase {
18
19 use BcTimestampNormalizerUnixTestTrait;
20
21 /**
22 * {@inheritdoc}
23 */
24 public static $modules = ['entity_test'];
25
26 /**
27 * {@inheritdoc}
28 */
29 protected static $entityTypeId = 'entity_test';
30
31 /**
32 * {@inheritdoc}
33 */
34 protected static $resourceTypeName = 'entity_test--entity_test';
35
36 /**
37 * {@inheritdoc}
38 */
39 protected static $patchProtectedFieldNames = [];
40
41 /**
42 * {@inheritdoc}
43 *
44 * @var \Drupal\entity_test\Entity\EntityTest
45 */
46 protected $entity;
47
48 /**
49 * {@inheritdoc}
50 */
51 protected function setUpAuthorization($method) {
52 switch ($method) {
53 case 'GET':
54 $this->grantPermissionsToTestedRole(['view test entity']);
55 break;
56
57 case 'POST':
58 $this->grantPermissionsToTestedRole(['create entity_test entity_test_with_bundle entities']);
59 break;
60
61 case 'PATCH':
62 case 'DELETE':
63 $this->grantPermissionsToTestedRole(['administer entity_test content']);
64 break;
65 }
66 }
67
68 /**
69 * {@inheritdoc}
70 */
71 protected function createEntity() {
72 // Set flag so that internal field 'internal_string_field' is created.
73 // @see entity_test_entity_base_field_info()
74 $this->container->get('state')->set('entity_test.internal_field', TRUE);
75 $field_storage_definition = BaseFieldDefinition::create('string')
76 ->setLabel('Internal field')
77 ->setInternal(TRUE);
78 \Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('internal_string_field', 'entity_test', 'entity_test', $field_storage_definition);
79
80 $entity_test = EntityTest::create([
81 'name' => 'Llama',
82 'type' => 'entity_test',
83 // Set a value for the internal field to confirm that it will not be
84 // returned in normalization.
85 // @see entity_test_entity_base_field_info().
86 'internal_string_field' => [
87 'value' => 'This value shall not be internal!',
88 ],
89 ]);
90 $entity_test->setOwnerId(0);
91 $entity_test->save();
92
93 return $entity_test;
94 }
95
96 /**
97 * {@inheritdoc}
98 */
99 protected function getExpectedDocument() {
100 $self_url = Url::fromUri('base:/jsonapi/entity_test/entity_test/' . $this->entity->uuid())->setAbsolute()->toString(TRUE)->getGeneratedUrl();
101 $author = User::load(0);
102 return [
103 'jsonapi' => [
104 'meta' => [
105 'links' => [
106 'self' => ['href' => 'http://jsonapi.org/format/1.0/'],
107 ],
108 ],
109 'version' => '1.0',
110 ],
111 'links' => [
112 'self' => ['href' => $self_url],
113 ],
114 'data' => [
115 'id' => $this->entity->uuid(),
116 'type' => 'entity_test--entity_test',
117 'links' => [
118 'self' => ['href' => $self_url],
119 ],
120 'attributes' => [
121 'created' => (new \DateTime())->setTimestamp($this->entity->get('created')->value)->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339),
122 'field_test_text' => NULL,
123 'langcode' => 'en',
124 'name' => 'Llama',
125 'entity_test_type' => 'entity_test',
126 'drupal_internal__id' => 1,
127 ],
128 'relationships' => [
129 'user_id' => [
130 'data' => [
131 'id' => $author->uuid(),
132 'type' => 'user--user',
133 ],
134 'links' => [
135 'related' => ['href' => $self_url . '/user_id'],
136 'self' => ['href' => $self_url . '/relationships/user_id'],
137 ],
138 ],
139 ],
140 ],
141 ];
142 }
143
144 /**
145 * {@inheritdoc}
146 */
147 protected function getPostDocument() {
148 return [
149 'data' => [
150 'type' => 'entity_test--entity_test',
151 'attributes' => [
152 'name' => 'Dramallama',
153 ],
154 ],
155 ];
156 }
157
158 /**
159 * {@inheritdoc}
160 */
161 protected function getExpectedUnauthorizedAccessMessage($method) {
162 switch ($method) {
163 case 'GET':
164 return "The 'view test entity' permission is required.";
165
166 case 'POST':
167 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'.";
168
169 default:
170 return parent::getExpectedUnauthorizedAccessMessage($method);
171 }
172 }
173
174 /**
175 * {@inheritdoc}
176 */
177 protected function getSparseFieldSets() {
178 // EntityTest's owner field name is `user_id`, not `uid`, which breaks
179 // nested sparse fieldset tests.
180 return array_diff_key(parent::getSparseFieldSets(), array_flip([
181 'nested_empty_fieldset',
182 'nested_fieldset_with_owner_fieldset',
183 ]));
184 }
185
186 /**
187 * {@inheritdoc}
188 */
189 protected static function getExpectedCollectionCacheability(AccountInterface $account, array $collection, array $sparse_fieldset = NULL, $filtered = FALSE) {
190 $cacheability = parent::getExpectedCollectionCacheability($account, $collection, $sparse_fieldset, $filtered);
191 if ($filtered) {
192 $cacheability->addCacheTags(['state:jsonapi__entity_test_filter_access_blacklist']);
193 }
194 return $cacheability;
195 }
196
197 }