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