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