79f24da5a8eae4f4d0c9b0353baf7ebae979fb21
[yaffs-website] / web / core / modules / workspaces / tests / src / Functional / EntityResource / WorkspaceResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\workspaces\Functional\EntityResource;
4
5 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
6 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
7 use Drupal\user\Entity\User;
8 use Drupal\workspaces\Entity\Workspace;
9
10 /**
11  * Base class for workspace EntityResource tests.
12  */
13 abstract class WorkspaceResourceTestBase extends EntityResourceTestBase {
14
15   use BcTimestampNormalizerUnixTestTrait;
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['workspaces'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected static $entityTypeId = 'workspace';
26
27   /**
28    * {@inheritdoc}
29    */
30   protected static $patchProtectedFieldNames = [
31     'changed' => NULL,
32   ];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected static $firstCreatedEntityId = 'running_on_faith';
38
39   /**
40    * {@inheritdoc}
41    */
42   protected static $secondCreatedEntityId = 'running_on_faith_2';
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function setUpAuthorization($method) {
48     switch ($method) {
49       case 'GET':
50         $this->grantPermissionsToTestedRole(['view any workspace']);
51         break;
52       case 'POST':
53         $this->grantPermissionsToTestedRole(['create workspace']);
54         break;
55       case 'PATCH':
56         $this->grantPermissionsToTestedRole(['edit any workspace']);
57         break;
58       case 'DELETE':
59         $this->grantPermissionsToTestedRole(['delete any workspace']);
60         break;
61     }
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   protected function createEntity() {
68     $workspace = Workspace::create([
69       'id' => 'layla',
70       'label' => 'Layla',
71     ]);
72     $workspace->save();
73     return $workspace;
74   }
75
76   /**
77    * {@inheritdoc}
78    */
79   protected function createAnotherEntity() {
80     $workspace = $this->entity->createDuplicate();
81     $workspace->id = 'layla_dupe';
82     $workspace->label = 'Layla_dupe';
83     $workspace->save();
84     return $workspace;
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   protected function getExpectedNormalizedEntity() {
91     $author = User::load($this->entity->getOwnerId());
92     return [
93       'created' => [
94         $this->formatExpectedTimestampItemValues((int) $this->entity->getCreatedTime()),
95       ],
96       'changed' => [
97         $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()),
98       ],
99       'id' => [
100         [
101           'value' => 'layla',
102         ],
103       ],
104       'label' => [
105         [
106           'value' => 'Layla',
107         ],
108       ],
109       'revision_id' => [
110         [
111           'value' => 3,
112         ],
113       ],
114       'uid' => [
115         [
116           'target_id' => (int) $author->id(),
117           'target_type' => 'user',
118           'target_uuid' => $author->uuid(),
119           'url' => base_path() . 'user/' . $author->id(),
120         ],
121       ],
122       'uuid' => [
123         [
124           'value' => $this->entity->uuid(),
125         ],
126       ],
127     ];
128   }
129
130   /**
131    * {@inheritdoc}
132    */
133   protected function getNormalizedPostEntity() {
134     return [
135       'id' => [
136         [
137           'value' => static::$firstCreatedEntityId,
138         ],
139       ],
140       'label' => [
141         [
142           'value' => 'Running on faith',
143         ],
144       ],
145     ];
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   protected function getSecondNormalizedPostEntity() {
152     $normalized_post_entity = $this->getNormalizedPostEntity();
153     $normalized_post_entity['id'][0]['value'] = static::$secondCreatedEntityId;
154
155     return $normalized_post_entity;
156   }
157
158   /**
159    * {@inheritdoc}
160    */
161   protected function getNormalizedPatchEntity() {
162     return [
163       'label' => [
164         [
165           'value' => 'Running on faith',
166         ],
167       ],
168     ];
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   protected function getExpectedUnauthorizedAccessMessage($method) {
175     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
176       return parent::getExpectedUnauthorizedAccessMessage($method);
177     }
178
179     switch ($method) {
180       case 'GET':
181         return "The 'view any workspace' permission is required.";
182       break;
183       case 'POST':
184         return "The 'create workspace' permission is required.";
185       break;
186       case 'PATCH':
187         return "The 'edit any workspace' permission is required.";
188       break;
189       case 'DELETE':
190         return "The 'delete any workspace' permission is required.";
191       break;
192     }
193     return parent::getExpectedUnauthorizedAccessMessage($method);
194   }
195
196 }