3 namespace Drupal\Tests\media\Functional\Hal;
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;
15 class MediaHalJsonAnonTest extends MediaResourceTestBase {
17 use HalEntityNormalizationTrait;
18 use AnonResourceTestTrait;
23 public static $modules = ['hal'];
28 protected static $format = 'hal_json';
33 protected static $mimeType = 'application/hal+json';
38 protected function getExpectedNormalizedEntity() {
39 $default_normalization = parent::getExpectedNormalizedEntity();
41 $normalization = $this->applyHalFieldNormalization($default_normalization);
43 $file = File::load(1);
44 $thumbnail = File::load(2);
45 $author = User::load($this->entity->getOwnerId());
46 return $normalization + [
49 'href' => $this->baseUrl . '/media/1?_format=hal_json',
52 'href' => $this->baseUrl . '/rest/type/media/camelids',
54 $this->baseUrl . '/rest/relation/media/camelids/field_media_file' => [
56 'href' => $file->url(),
60 $this->baseUrl . '/rest/relation/media/camelids/revision_user' => [
62 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
65 $this->baseUrl . '/rest/relation/media/camelids/thumbnail' => [
67 'href' => $thumbnail->url(),
71 $this->baseUrl . '/rest/relation/media/camelids/uid' => [
73 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
79 $this->baseUrl . '/rest/relation/media/camelids/field_media_file' => [
83 'href' => $file->url(),
86 'href' => $this->baseUrl . '/rest/type/file/file',
92 'value' => $file->uuid(),
97 $this->baseUrl . '/rest/relation/media/camelids/revision_user' => [
101 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
104 'href' => $this->baseUrl . '/rest/type/user/user',
109 'value' => $author->uuid(),
114 $this->baseUrl . '/rest/relation/media/camelids/thumbnail' => [
118 'href' => $thumbnail->url(),
121 'href' => $this->baseUrl . '/rest/type/file/file',
127 'value' => $thumbnail->uuid(),
132 $this->baseUrl . '/rest/relation/media/camelids/uid' => [
136 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
139 'href' => $this->baseUrl . '/rest/type/user/user',
144 'value' => $author->uuid(),
157 protected function getExpectedNormalizedFileEntity() {
158 $normalization = parent::getExpectedNormalizedFileEntity();
160 $owner = static::$auth ? $this->account : User::load(0);
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.
166 // The HAL normalization adds entity reference fields to '_links' and
168 unset($normalization['uid']);
170 return $normalization + [
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']),
180 'href' => $this->baseUrl . '/rest/type/file/file',
182 $this->baseUrl . '/rest/relation/file/file/uid' => [
183 ['href' => $this->baseUrl . '/user/' . $owner->id() . '?_format=hal_json'],
187 $this->baseUrl . '/rest/relation/file/file/uid' => [
191 'href' => $this->baseUrl . '/user/' . $owner->id() . '?_format=hal_json',
194 'href' => $this->baseUrl . '/rest/type/user/user',
199 'value' => $owner->uuid(),
211 protected function getNormalizedPostEntity() {
212 return parent::getNormalizedPostEntity() + [
215 'href' => $this->baseUrl . '/rest/type/media/camelids',
224 protected function getExpectedCacheTags() {
225 return Cache::mergeTags(parent::getExpectedCacheTags(), ['config:hal.settings']);