Upgraded drupal core with security updates
[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\EntityResource\EntityResourceTestBase;
8 use Drupal\user\Entity\User;
9
10 abstract class NodeResourceTestBase extends EntityResourceTestBase {
11
12   /**
13    * {@inheritdoc}
14    */
15   public static $modules = ['node'];
16
17   /**
18    * {@inheritdoc}
19    */
20   protected static $entityTypeId = 'node';
21
22   /**
23    * {@inheritdoc}
24    */
25   protected static $patchProtectedFieldNames = [
26     'uid',
27     'created',
28     'changed',
29     'promote',
30     'sticky',
31     'revision_timestamp',
32     'revision_uid',
33   ];
34
35   /**
36    * @var \Drupal\node\NodeInterface
37    */
38   protected $entity;
39
40   /**
41    * {@inheritdoc}
42    */
43   protected function setUpAuthorization($method) {
44     switch ($method) {
45       case 'GET':
46         $this->grantPermissionsToTestedRole(['access content']);
47         break;
48       case 'POST':
49         $this->grantPermissionsToTestedRole(['access content', 'create camelids content']);
50         break;
51       case 'PATCH':
52         $this->grantPermissionsToTestedRole(['access content', 'edit any camelids content']);
53         break;
54       case 'DELETE':
55         $this->grantPermissionsToTestedRole(['access content', 'delete any camelids content']);
56         break;
57     }
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   protected function createEntity() {
64     if (!NodeType::load('camelids')) {
65       // Create a "Camelids" node type.
66       NodeType::create([
67         'name' => 'Camelids',
68         'type' => 'camelids',
69       ])->save();
70     }
71
72     // Create a "Llama" node.
73     $node = Node::create(['type' => 'camelids']);
74     $node->setTitle('Llama')
75       ->setOwnerId(static::$auth ? $this->account->id() : 0)
76       ->setPublished(TRUE)
77       ->setCreatedTime(123456789)
78       ->setChangedTime(123456789)
79       ->setRevisionCreationTime(123456789)
80       ->save();
81
82     return $node;
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   protected function getExpectedNormalizedEntity() {
89     $author = User::load($this->entity->getOwnerId());
90     return [
91       'nid' => [
92         ['value' => 1],
93       ],
94       'uuid' => [
95         ['value' => $this->entity->uuid()],
96       ],
97       'vid' => [
98         ['value' => 1],
99       ],
100       'langcode' => [
101         [
102           'value' => 'en',
103         ],
104       ],
105       'type' => [
106         [
107           'target_id' => 'camelids',
108           'target_type' => 'node_type',
109           'target_uuid' => NodeType::load('camelids')->uuid(),
110         ],
111       ],
112       'title' => [
113         [
114           'value' => 'Llama',
115         ],
116       ],
117       'status' => [
118         [
119           'value' => TRUE,
120         ],
121       ],
122       'created' => [
123         [
124           'value' => 123456789,
125         ],
126       ],
127       'changed' => [
128         [
129           'value' => $this->entity->getChangedTime(),
130         ],
131       ],
132       'promote' => [
133         [
134           'value' => TRUE,
135         ],
136       ],
137       'sticky' => [
138         [
139           'value' => FALSE,
140         ],
141       ],
142       'revision_timestamp' => [
143         [
144           'value' => 123456789,
145         ],
146       ],
147       'revision_translation_affected' => [
148         [
149           'value' => TRUE,
150         ],
151       ],
152       'default_langcode' => [
153         [
154           'value' => TRUE,
155         ],
156       ],
157       'uid' => [
158         [
159           'target_id' => (int) $author->id(),
160           'target_type' => 'user',
161           'target_uuid' => $author->uuid(),
162           'url' => base_path() . 'user/' . $author->id(),
163         ],
164       ],
165       'revision_uid' => [
166         [
167           'target_id' => (int) $author->id(),
168           'target_type' => 'user',
169           'target_uuid' => $author->uuid(),
170           'url' => base_path() . 'user/' . $author->id(),
171         ],
172       ],
173       'revision_log' => [],
174     ];
175   }
176
177   /**
178    * {@inheritdoc}
179    */
180   protected function getNormalizedPostEntity() {
181     return [
182       'type' => [
183         [
184           'target_id' => 'camelids',
185         ],
186       ],
187       'title' => [
188         [
189           'value' => 'Dramallama',
190         ],
191       ],
192     ];
193   }
194
195   /**
196    * {@inheritdoc}
197    */
198   protected function getExpectedUnauthorizedAccessMessage($method) {
199     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
200       return parent::getExpectedUnauthorizedAccessMessage($method);
201     }
202
203     if ($method === 'GET' || $method == 'PATCH' || $method == 'DELETE') {
204       return "The 'access content' permission is required.";
205     }
206     return parent::getExpectedUnauthorizedAccessMessage($method);
207   }
208
209 }