bf0ba7a59194545261c91b3dc329003af597a37c
[yaffs-website] / web / core / modules / rest / tests / src / Functional / EntityResource / Node / NodeResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\rest\Functional\EntityResource\Node;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\node\Entity\NodeType;
7 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
8 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
9 use Drupal\user\Entity\User;
10 use GuzzleHttp\RequestOptions;
11
12 abstract class NodeResourceTestBase extends EntityResourceTestBase {
13
14   use BcTimestampNormalizerUnixTestTrait;
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = ['node', 'path'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected static $entityTypeId = 'node';
25
26   /**
27    * {@inheritdoc}
28    */
29   protected static $patchProtectedFieldNames = [
30     'revision_timestamp',
31     'revision_uid',
32     'created',
33     'changed',
34     'promote',
35     'sticky',
36     'path',
37   ];
38
39   /**
40    * @var \Drupal\node\NodeInterface
41    */
42   protected $entity;
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function setUpAuthorization($method) {
48     switch ($method) {
49       case 'GET':
50         $this->grantPermissionsToTestedRole(['access content']);
51         break;
52       case 'POST':
53         $this->grantPermissionsToTestedRole(['access content', 'create camelids content']);
54         break;
55       case 'PATCH':
56         // Do not grant the 'create url aliases' permission to test the case
57         // when the path field is protected/not accessible, see
58         // \Drupal\Tests\rest\Functional\EntityResource\Term\TermResourceTestBase
59         // for a positive test.
60         $this->grantPermissionsToTestedRole(['access content', 'edit any camelids content']);
61         break;
62       case 'DELETE':
63         $this->grantPermissionsToTestedRole(['access content', 'delete any camelids content']);
64         break;
65     }
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function createEntity() {
72     if (!NodeType::load('camelids')) {
73       // Create a "Camelids" node type.
74       NodeType::create([
75         'name' => 'Camelids',
76         'type' => 'camelids',
77       ])->save();
78     }
79
80     // Create a "Llama" node.
81     $node = Node::create(['type' => 'camelids']);
82     $node->setTitle('Llama')
83       ->setOwnerId(static::$auth ? $this->account->id() : 0)
84       ->setPublished(TRUE)
85       ->setCreatedTime(123456789)
86       ->setChangedTime(123456789)
87       ->setRevisionCreationTime(123456789)
88       ->set('path', '/llama')
89       ->save();
90
91     return $node;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   protected function getExpectedNormalizedEntity() {
98     $author = User::load($this->entity->getOwnerId());
99     return [
100       'nid' => [
101         ['value' => 1],
102       ],
103       'uuid' => [
104         ['value' => $this->entity->uuid()],
105       ],
106       'vid' => [
107         ['value' => 1],
108       ],
109       'langcode' => [
110         [
111           'value' => 'en',
112         ],
113       ],
114       'type' => [
115         [
116           'target_id' => 'camelids',
117           'target_type' => 'node_type',
118           'target_uuid' => NodeType::load('camelids')->uuid(),
119         ],
120       ],
121       'title' => [
122         [
123           'value' => 'Llama',
124         ],
125       ],
126       'status' => [
127         [
128           'value' => TRUE,
129         ],
130       ],
131       'created' => [
132         $this->formatExpectedTimestampItemValues(123456789),
133       ],
134       'changed' => [
135         $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()),
136       ],
137       'promote' => [
138         [
139           'value' => TRUE,
140         ],
141       ],
142       'sticky' => [
143         [
144           'value' => FALSE,
145         ],
146       ],
147       'revision_timestamp' => [
148         $this->formatExpectedTimestampItemValues(123456789),
149       ],
150       'revision_translation_affected' => [
151         [
152           'value' => TRUE,
153         ],
154       ],
155       'default_langcode' => [
156         [
157           'value' => TRUE,
158         ],
159       ],
160       'uid' => [
161         [
162           'target_id' => (int) $author->id(),
163           'target_type' => 'user',
164           'target_uuid' => $author->uuid(),
165           'url' => base_path() . 'user/' . $author->id(),
166         ],
167       ],
168       'revision_uid' => [
169         [
170           'target_id' => (int) $author->id(),
171           'target_type' => 'user',
172           'target_uuid' => $author->uuid(),
173           'url' => base_path() . 'user/' . $author->id(),
174         ],
175       ],
176       'revision_log' => [],
177       'path' => [
178         [
179           'alias' => '/llama',
180           'pid' => 1,
181           'langcode' => 'en',
182         ],
183       ],
184     ];
185   }
186
187   /**
188    * {@inheritdoc}
189    */
190   protected function getNormalizedPostEntity() {
191     return [
192       'type' => [
193         [
194           'target_id' => 'camelids',
195         ],
196       ],
197       'title' => [
198         [
199           'value' => 'Dramallama',
200         ],
201       ],
202     ];
203   }
204
205   /**
206    * {@inheritdoc}
207    */
208   protected function getExpectedUnauthorizedAccessMessage($method) {
209     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
210       return parent::getExpectedUnauthorizedAccessMessage($method);
211     }
212
213     if ($method === 'GET' || $method == 'PATCH' || $method == 'DELETE') {
214       return "The 'access content' permission is required.";
215     }
216     return parent::getExpectedUnauthorizedAccessMessage($method);
217   }
218
219   /**
220    * Tests PATCHing a node's path with and without 'create url aliases'.
221    *
222    * For a positive test, see the similar test coverage for Term.
223    *
224    * @see \Drupal\Tests\rest\Functional\EntityResource\Term\TermResourceTestBase::testPatchPath()
225    */
226   public function testPatchPath() {
227     $this->initAuthentication();
228     $this->provisionEntityResource();
229     $this->setUpAuthorization('GET');
230     $this->setUpAuthorization('PATCH');
231
232     $url = $this->getEntityResourceUrl()->setOption('query', ['_format' => static::$format]);
233
234     // GET node's current normalization.
235     $response = $this->request('GET', $url, $this->getAuthenticationRequestOptions('GET'));
236     $normalization = $this->serializer->decode((string) $response->getBody(), static::$format);
237
238     // Change node's path alias.
239     $normalization['path'][0]['alias'] .= 's-rule-the-world';
240
241     // Create node PATCH request.
242     $request_options = [];
243     $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
244     $request_options = array_merge_recursive($request_options, $this->getAuthenticationRequestOptions('PATCH'));
245     $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
246
247     // PATCH request: 403 when creating URL aliases unauthorized. Before
248     // asserting the 403 response, assert that the stored path alias remains
249     // unchanged.
250     $response = $this->request('PATCH', $url, $request_options);
251     $this->assertSame('/llama', $this->entityStorage->loadUnchanged($this->entity->id())->get('path')->alias);
252     $this->assertResourceErrorResponse(403, "Access denied on updating field 'path'.", $response);
253
254     // Grant permission to create URL aliases.
255     $this->grantPermissionsToTestedRole(['create url aliases']);
256
257     // Repeat PATCH request: 200.
258     $response = $this->request('PATCH', $url, $request_options);
259     $this->assertResourceResponse(200, FALSE, $response);
260     $updated_normalization = $this->serializer->decode((string) $response->getBody(), static::$format);
261     $this->assertSame($normalization['path'], $updated_normalization['path']);
262   }
263
264 }