eae226cfa041d3c1e8a4834dd5e4df0dd0b32833
[yaffs-website] / web / core / modules / system / tests / modules / entity_test / tests / src / Functional / Rest / EntityTestMapFieldResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\entity_test\Functional\Rest;
4
5 use Drupal\entity_test\Entity\EntityTestMapField;
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 EntityTestMapFieldResourceTestBase 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_map_field';
25
26   /**
27    * {@inheritdoc}
28    */
29   protected static $patchProtectedFieldNames = [];
30
31   /**
32    * @var \Drupal\entity_test\Entity\EntityTestMapField
33    */
34   protected $entity;
35
36   /**
37    * The complex nested value to assign to a @FieldType=map field.
38    *
39    * @var array
40    */
41   protected static $mapValue = [
42     'key1' => 'value',
43     'key2' => 'no, val you',
44     'π' => 3.14159,
45     TRUE => 42,
46     'nested' => [
47       'bird' => 'robin',
48       'doll' => 'Russian',
49     ],
50   ];
51
52   /**
53    * {@inheritdoc}
54    */
55   protected function setUpAuthorization($method) {
56     $this->grantPermissionsToTestedRole(['administer entity_test content']);
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   protected function createEntity() {
63     $entity = EntityTestMapField::create([
64       'name' => 'Llama',
65       'type' => 'entity_test_map_field',
66       'data' => [
67         static::$mapValue,
68       ],
69     ]);
70     $entity->setOwnerId(0);
71     $entity->save();
72     return $entity;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   protected function getExpectedNormalizedEntity() {
79     $author = User::load(0);
80     return [
81       'uuid' => [
82         [
83           'value' => $this->entity->uuid(),
84         ],
85       ],
86       'id' => [
87         [
88           'value' => 1,
89         ],
90       ],
91       'name' => [
92         [
93           'value' => 'Llama',
94         ],
95       ],
96       'langcode' => [
97         [
98           'value' => 'en',
99         ],
100       ],
101       'created' => [
102         $this->formatExpectedTimestampItemValues((int) $this->entity->get('created')->value),
103       ],
104       'user_id' => [
105         [
106           'target_id' => (int) $author->id(),
107           'target_type' => 'user',
108           'target_uuid' => $author->uuid(),
109           'url' => $author->toUrl()->toString(),
110         ],
111       ],
112       'data' => [
113         static::$mapValue,
114       ],
115     ];
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   protected function getNormalizedPostEntity() {
122     return [
123       'name' => [
124         [
125           'value' => 'Dramallama',
126         ],
127       ],
128       'data' => [
129         0 => static::$mapValue,
130       ],
131     ];
132   }
133
134   /**
135    * {@inheritdoc}
136    */
137   protected function getExpectedUnauthorizedAccessMessage($method) {
138     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
139       return parent::getExpectedUnauthorizedAccessMessage($method);
140     }
141
142     return "The 'administer entity_test content' permission is required.";
143   }
144
145   /**
146    * {@inheritdoc}
147    */
148   protected function getExpectedCacheContexts() {
149     return ['user.permissions'];
150   }
151
152 }