Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / media / tests / src / Functional / Hal / MediaHalJsonAnonTest.php
1 <?php
2
3 namespace Drupal\Tests\media\Functional\Hal;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\file\Entity\File;
7 use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
8 use Drupal\Tests\media\Functional\Rest\MediaResourceTestBase;
9 use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
10 use Drupal\user\Entity\User;
11
12 /**
13  * @group hal
14  */
15 class MediaHalJsonAnonTest extends MediaResourceTestBase {
16
17   use HalEntityNormalizationTrait;
18   use AnonResourceTestTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = ['hal'];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected static $format = 'hal_json';
29
30   /**
31    * {@inheritdoc}
32    */
33   protected static $mimeType = 'application/hal+json';
34
35   /**
36    * {@inheritdoc}
37    */
38   protected function getExpectedNormalizedEntity() {
39     $default_normalization = parent::getExpectedNormalizedEntity();
40
41     $normalization = $this->applyHalFieldNormalization($default_normalization);
42
43     $file = File::load(1);
44     $thumbnail = File::load(2);
45     $author = User::load($this->entity->getOwnerId());
46     return $normalization + [
47       '_links' => [
48         'self' => [
49           'href' => $this->baseUrl . '/media/1?_format=hal_json',
50         ],
51         'type' => [
52           'href' => $this->baseUrl . '/rest/type/media/camelids',
53         ],
54         $this->baseUrl . '/rest/relation/media/camelids/field_media_file' => [
55           [
56             'href' => $file->url(),
57             'lang' => 'en',
58           ],
59         ],
60         $this->baseUrl . '/rest/relation/media/camelids/revision_user' => [
61           [
62             'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
63           ],
64         ],
65         $this->baseUrl . '/rest/relation/media/camelids/thumbnail' => [
66           [
67             'href' => $thumbnail->url(),
68             'lang' => 'en',
69           ],
70         ],
71         $this->baseUrl . '/rest/relation/media/camelids/uid' => [
72           [
73             'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
74             'lang' => 'en',
75           ],
76         ],
77       ],
78       '_embedded' => [
79         $this->baseUrl . '/rest/relation/media/camelids/field_media_file' => [
80           [
81             '_links' => [
82               'self' => [
83                 'href' => $file->url(),
84               ],
85               'type' => [
86                 'href' => $this->baseUrl . '/rest/type/file/file',
87               ],
88             ],
89             'lang' => 'en',
90             'uuid' => [
91               [
92                 'value' => $file->uuid(),
93               ],
94             ],
95           ],
96         ],
97         $this->baseUrl . '/rest/relation/media/camelids/revision_user' => [
98           [
99             '_links' => [
100               'self' => [
101                 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
102               ],
103               'type' => [
104                 'href' => $this->baseUrl . '/rest/type/user/user',
105               ],
106             ],
107             'uuid' => [
108               [
109                 'value' => $author->uuid(),
110               ],
111             ],
112           ],
113         ],
114         $this->baseUrl . '/rest/relation/media/camelids/thumbnail' => [
115           [
116             '_links' => [
117               'self' => [
118                 'href' => $thumbnail->url(),
119               ],
120               'type' => [
121                 'href' => $this->baseUrl . '/rest/type/file/file',
122               ],
123             ],
124             'lang' => 'en',
125             'uuid' => [
126               [
127                 'value' => $thumbnail->uuid(),
128               ],
129             ],
130           ],
131         ],
132         $this->baseUrl . '/rest/relation/media/camelids/uid' => [
133           [
134             '_links' => [
135               'self' => [
136                 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
137               ],
138               'type' => [
139                 'href' => $this->baseUrl . '/rest/type/user/user',
140               ],
141             ],
142             'uuid' => [
143               [
144                 'value' => $author->uuid(),
145               ],
146             ],
147             'lang' => 'en',
148           ],
149         ],
150       ],
151     ];
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   protected function getExpectedNormalizedFileEntity() {
158     $normalization = parent::getExpectedNormalizedFileEntity();
159
160     $owner = static::$auth ? $this->account : User::load(0);
161
162     // Cannot use applyHalFieldNormalization() as it uses the $entity property
163     // from the test class, which in the case of file upload tests, is the
164     // parent entity test entity for the file that's created.
165
166     // The HAL normalization adds entity reference fields to '_links' and
167     // '_embedded'.
168     unset($normalization['uid']);
169
170     return $normalization + [
171       '_links' => [
172         'self' => [
173           // @todo This can use a proper link once
174           // https://www.drupal.org/project/drupal/issues/2907402 is complete.
175           // This link matches what is generated from from File::url(), a
176           // resource URL is currently not available.
177           'href' => file_create_url($normalization['uri'][0]['value']),
178         ],
179         'type' => [
180           'href' => $this->baseUrl . '/rest/type/file/file',
181         ],
182         $this->baseUrl . '/rest/relation/file/file/uid' => [
183           ['href' => $this->baseUrl . '/user/' . $owner->id() . '?_format=hal_json'],
184         ],
185       ],
186       '_embedded' => [
187         $this->baseUrl . '/rest/relation/file/file/uid' => [
188           [
189             '_links' => [
190               'self' => [
191                 'href' => $this->baseUrl . '/user/' . $owner->id() . '?_format=hal_json',
192               ],
193               'type' => [
194                 'href' => $this->baseUrl . '/rest/type/user/user',
195               ],
196             ],
197             'uuid' => [
198               [
199                 'value' => $owner->uuid(),
200               ],
201             ],
202           ],
203         ],
204       ],
205     ];
206   }
207
208   /**
209    * {@inheritdoc}
210    */
211   protected function getNormalizedPostEntity() {
212     return parent::getNormalizedPostEntity() + [
213       '_links' => [
214         'type' => [
215           'href' => $this->baseUrl . '/rest/type/media/camelids',
216         ],
217       ],
218     ];
219   }
220
221   /**
222    * {@inheritdoc}
223    */
224   protected function getExpectedCacheTags() {
225     return Cache::mergeTags(parent::getExpectedCacheTags(), ['config:hal.settings']);
226   }
227
228 }