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