f3036a9de8680885e80e0f63d50cea723a0d1461
[yaffs-website] / web / core / modules / hal / tests / src / Functional / EntityResource / File / FileHalJsonAnonTest.php
1 <?php
2
3 namespace Drupal\Tests\hal\Functional\EntityResource\File;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
7 use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
8 use Drupal\Tests\rest\Functional\EntityResource\File\FileResourceTestBase;
9
10 /**
11  * @group hal
12  */
13 class FileHalJsonAnonTest extends FileResourceTestBase {
14
15   use HalEntityNormalizationTrait;
16   use AnonResourceTestTrait;
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['hal'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected static $format = 'hal_json';
27
28   /**
29    * {@inheritdoc}
30    */
31   protected static $mimeType = 'application/hal+json';
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function getExpectedNormalizedEntity() {
37     $default_normalization = parent::getExpectedNormalizedEntity();
38
39     $normalization = $this->applyHalFieldNormalization($default_normalization);
40
41     $url = file_create_url($this->entity->getFileUri());
42     // @see \Drupal\Tests\hal\Functional\EntityResource\File\FileHalJsonAnonTest::testGetBcUriField()
43     if ($this->config('hal.settings')->get('bc_file_uri_as_url_normalizer')) {
44       $normalization['uri'][0]['value'] = $url;
45     }
46
47     $uid = $this->author->id();
48
49     return $normalization + [
50       '_embedded' => [
51         $this->baseUrl . '/rest/relation/file/file/uid' => [
52           [
53             '_links' => [
54               'self' => [
55                 'href' => $this->baseUrl . "/user/$uid?_format=hal_json",
56               ],
57               'type' => [
58                 'href' => $this->baseUrl . '/rest/type/user/user',
59               ],
60             ],
61             'uuid' => [
62               [
63                 'value' => $this->author->uuid(),
64               ],
65             ],
66           ],
67         ],
68       ],
69       '_links' => [
70         'self' => [
71           'href' => $url,
72         ],
73         'type' => [
74           'href' => $this->baseUrl . '/rest/type/file/file',
75         ],
76         $this->baseUrl . '/rest/relation/file/file/uid' => [
77           [
78             'href' => $this->baseUrl . "/user/$uid?_format=hal_json",
79           ],
80         ],
81       ],
82     ];
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   protected function getNormalizedPostEntity() {
89     return parent::getNormalizedPostEntity() + [
90       '_links' => [
91         'type' => [
92           'href' => $this->baseUrl . '/rest/type/file/file',
93         ],
94       ],
95     ];
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   protected function getExpectedCacheTags() {
102     return Cache::mergeTags(parent::getExpectedCacheTags(), ['config:hal.settings']);
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   protected function getExpectedCacheContexts() {
109     return [
110       'url.site',
111       'user.permissions',
112     ];
113   }
114
115   /**
116    * @see hal_update_8501()
117    */
118   public function testGetBcUriField() {
119     $this->config('hal.settings')->set('bc_file_uri_as_url_normalizer', TRUE)->save(TRUE);
120
121     $this->initAuthentication();
122     $url = $this->getEntityResourceUrl();
123     $url->setOption('query', ['_format' => static::$format]);
124     $request_options = $this->getAuthenticationRequestOptions('GET');
125     $this->provisionEntityResource();
126     $this->setUpAuthorization('GET');
127     $response = $this->request('GET', $url, $request_options);
128     $expected = $this->getExpectedNormalizedEntity();
129     static::recursiveKSort($expected);
130     $actual = $this->serializer->decode((string) $response->getBody(), static::$format);
131     static::recursiveKSort($actual);
132     $this->assertSame($expected, $actual);
133
134     // Explicitly assert that $file->uri->value is an absolute file URL, unlike
135     // the default normalization.
136     $this->assertSame($this->baseUrl . '/' . $this->siteDirectory . '/files/drupal.txt', $actual['uri'][0]['value']);
137   }
138
139   /**
140    * {@inheritdoc}
141    */
142   public function testPatch() {
143     // @todo https://www.drupal.org/node/1927648
144     $this->markTestSkipped();
145   }
146
147 }