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