3 namespace Drupal\Tests\file\Functional\Hal;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\file\Functional\Rest\FileResourceTestBase;
7 use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
8 use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
13 class FileHalJsonAnonTest extends FileResourceTestBase {
15 use HalEntityNormalizationTrait;
16 use AnonResourceTestTrait;
21 public static $modules = ['hal'];
26 protected static $format = 'hal_json';
31 protected static $mimeType = 'application/hal+json';
36 protected function getExpectedNormalizedEntity() {
37 $default_normalization = parent::getExpectedNormalizedEntity();
39 $normalization = $this->applyHalFieldNormalization($default_normalization);
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;
47 $uid = $this->author->id();
49 return $normalization + [
51 $this->baseUrl . '/rest/relation/file/file/uid' => [
55 'href' => $this->baseUrl . "/user/$uid?_format=hal_json",
58 'href' => $this->baseUrl . '/rest/type/user/user',
63 'value' => $this->author->uuid(),
74 'href' => $this->baseUrl . '/rest/type/file/file',
76 $this->baseUrl . '/rest/relation/file/file/uid' => [
78 'href' => $this->baseUrl . "/user/$uid?_format=hal_json",
88 protected function getNormalizedPostEntity() {
89 return parent::getNormalizedPostEntity() + [
92 'href' => $this->baseUrl . '/rest/type/file/file',
101 protected function getExpectedCacheTags() {
102 return Cache::mergeTags(parent::getExpectedCacheTags(), ['config:hal.settings']);
108 protected function getExpectedCacheContexts() {
116 * @see hal_update_8501()
118 public function testGetBcUriField() {
119 $this->config('hal.settings')->set('bc_file_uri_as_url_normalizer', TRUE)->save(TRUE);
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);
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']);