3 namespace Drupal\Tests\rest\Functional\EntityResource\Media;
5 use Drupal\file\Entity\File;
6 use Drupal\media\Entity\Media;
7 use Drupal\media\Entity\MediaType;
8 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
9 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
10 use Drupal\user\Entity\User;
12 abstract class MediaResourceTestBase extends EntityResourceTestBase {
14 use BcTimestampNormalizerUnixTestTrait;
19 public static $modules = ['media'];
24 protected static $entityTypeId = 'media';
27 * @var \Drupal\media\MediaInterface
34 protected static $patchProtectedFieldNames = [
41 protected function setUpAuthorization($method) {
44 $this->grantPermissionsToTestedRole(['view media']);
48 $this->grantPermissionsToTestedRole(['create media']);
52 $this->grantPermissionsToTestedRole(['update any media']);
56 $this->grantPermissionsToTestedRole(['delete any media']);
64 protected function createEntity() {
65 if (!MediaType::load('camelids')) {
66 // Create a "Camelids" media type.
67 $media_type = MediaType::create([
70 'description' => 'Camelids are large, strictly herbivorous animals with slender necks and long legs.',
74 // Create the source field.
75 $source_field = $media_type->getSource()->createSourceField($media_type);
76 $source_field->getFieldStorageDefinition()->save();
77 $source_field->save();
79 ->set('source_configuration', [
80 'source_field' => $source_field->getName(),
85 // Create a file to upload.
86 $file = File::create([
87 'uri' => 'public://llama.txt',
89 $file->setPermanent();
92 // Create a "Llama" media item.
93 $media = Media::create([
94 'bundle' => 'camelids',
95 'field_media_file_1' => [
96 'target_id' => $file->id(),
102 ->setCreatedTime(123456789)
103 ->setOwnerId(static::$auth ? $this->account->id() : 0)
104 ->setRevisionUserId(static::$auth ? $this->account->id() : 0)
113 protected function getExpectedNormalizedEntity() {
114 $file = File::load(1);
115 $thumbnail = File::load(2);
116 $author = User::load($this->entity->getOwnerId());
125 'value' => $this->entity->uuid(),
140 'target_id' => 'camelids',
141 'target_type' => 'media_type',
142 'target_uuid' => MediaType::load('camelids')->uuid(),
150 'field_media_file_1' => [
152 'description' => NULL,
154 'target_id' => (int) $file->id(),
155 'target_type' => 'file',
156 'target_uuid' => $file->uuid(),
157 'url' => $file->url(),
162 'alt' => 'Thumbnail',
165 'target_id' => (int) $thumbnail->id(),
166 'target_type' => 'file',
167 'target_uuid' => $thumbnail->uuid(),
169 'url' => $thumbnail->url(),
178 $this->formatExpectedTimestampItemValues(123456789),
181 $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()),
183 'revision_created' => [
184 $this->formatExpectedTimestampItemValues((int) $this->entity->getRevisionCreationTime()),
186 'default_langcode' => [
193 'target_id' => (int) $author->id(),
194 'target_type' => 'user',
195 'target_uuid' => $author->uuid(),
196 'url' => base_path() . 'user/' . $author->id(),
201 'target_id' => (int) $author->id(),
202 'target_type' => 'user',
203 'target_uuid' => $author->uuid(),
204 'url' => base_path() . 'user/' . $author->id(),
207 'revision_log_message' => [],
208 'revision_translation_affected' => [
219 protected function getNormalizedPostEntity() {
223 'target_id' => 'camelids',
228 'value' => 'Dramallama',
237 protected function getExpectedUnauthorizedAccessMessage($method) {
238 if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
239 return parent::getExpectedUnauthorizedAccessMessage($method);
244 return "The 'view media' permission is required and the media item must be published.";
247 return 'You are not authorized to update this media entity of bundle camelids.';
250 return 'You are not authorized to delete this media entity of bundle camelids.';
253 return parent::getExpectedUnauthorizedAccessMessage($method);
260 public function testPost() {
261 $this->markTestSkipped('POSTing File Media items is not supported until https://www.drupal.org/node/1927648 is solved.');