2257d6c275e1864e8557ce0d2a0f215806498aed
[yaffs-website] / web / core / modules / rest / tests / src / Functional / EntityResource / EntityTestLabel / EntityTestLabelResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\rest\Functional\EntityResource\EntityTestLabel;
4
5 use Drupal\entity_test\Entity\EntityTestLabel;
6 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
7 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
8 use Drupal\user\Entity\User;
9
10 abstract class EntityTestLabelResourceTestBase extends EntityResourceTestBase {
11
12   use BcTimestampNormalizerUnixTestTrait;
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['entity_test'];
18
19   /**
20    * {@inheritdoc}
21    */
22   protected static $entityTypeId = 'entity_test_label';
23
24   /**
25    * {@inheritdoc}
26    */
27   protected static $patchProtectedFieldNames = [];
28
29   /**
30    * @var \Drupal\entity_test\Entity\EntityTestLabel
31    */
32   protected $entity;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUpAuthorization($method) {
38     switch ($method) {
39       case 'GET':
40         $this->grantPermissionsToTestedRole(['view test entity']);
41         break;
42       case 'POST':
43         $this->grantPermissionsToTestedRole([
44           'administer entity_test content',
45           'administer entity_test_with_bundle content',
46           'create entity_test entity_test_with_bundle entities',
47         ]);
48         break;
49       case 'PATCH':
50       case 'DELETE':
51         $this->grantPermissionsToTestedRole(['administer entity_test content']);
52         break;
53     }
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function createEntity() {
60     $entity_test_label = EntityTestLabel::create([
61       'name' => 'label_llama',
62     ]);
63     $entity_test_label->setOwnerId(0);
64     $entity_test_label->save();
65     return $entity_test_label;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   protected function getExpectedNormalizedEntity() {
72     $author = User::load(0);
73     $normalization = [
74       'uuid' => [
75         [
76           'value' => $this->entity->uuid(),
77         ],
78       ],
79       'id' => [
80         [
81           'value' => (int) $this->entity->id(),
82         ],
83       ],
84       'langcode' => [
85         [
86           'value' => 'en',
87         ],
88       ],
89       'type' => [
90         [
91           'value' => 'entity_test_label',
92         ],
93       ],
94       'name' => [
95         [
96           'value' => 'label_llama',
97         ],
98       ],
99       'created' => [
100         $this->formatExpectedTimestampItemValues((int) $this->entity->get('created')->value),
101       ],
102       'user_id' => [
103         [
104           'target_id' => (int) $author->id(),
105           'target_type' => 'user',
106           'target_uuid' => $author->uuid(),
107           'url' => $author->toUrl()->toString(),
108         ],
109       ],
110     ];
111
112     return $normalization;
113   }
114
115   /**
116    * {@inheritdoc}
117    */
118   protected function getNormalizedPostEntity() {
119     return [
120       'type' => [
121         [
122           'value' => 'entity_test_label',
123         ],
124       ],
125       'name' => [
126         [
127           'value' => 'label_llama',
128         ],
129       ],
130     ];
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   protected function getExpectedCacheContexts() {
137     return ['user.permissions'];
138   }
139
140   /**
141    * {@inheritdoc}
142    */
143   protected function getExpectedUnauthorizedAccessMessage($method) {
144     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
145       return parent::getExpectedUnauthorizedAccessMessage($method);
146     }
147
148     switch ($method) {
149       case 'GET':
150         return "The 'view test entity' permission is required.";
151       case 'POST':
152         return "The following permissions are required: 'administer entity_test content' OR 'administer entity_test_with_bundle content' OR 'create entity_test_label entity_test_with_bundle entities'.";
153       case 'PATCH':
154       case 'DELETE':
155         return "The 'administer entity_test content' permission is required.";
156       default:
157         return parent::getExpectedUnauthorizedAccessMessage($method);
158     }
159   }
160
161 }