From 74bc4670bd568add7917ed7d735367f8aaac8049 Mon Sep 17 00:00:00 2001 From: drishu Date: Wed, 16 Sep 2020 12:33:56 +0000 Subject: [PATCH 1/3] EWPP-253: Move media creation into trait. --- composer.json | 2 +- .../EntityBrowser/Widget/AVPortalSearch.php | 2 +- tests/Behat/MediaContext.php | 59 ++--- tests/Traits/MediaCreationTrait.php | 214 ++++++++++++++++++ 4 files changed, 236 insertions(+), 41 deletions(-) create mode 100644 tests/Traits/MediaCreationTrait.php diff --git a/composer.json b/composer.json index 5202e60c..751329ec 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "drupal-composer/drupal-scaffold": "~2.5.2", "guzzlehttp/guzzle": "~6.3", "openeuropa/behat-transformation-context": "~0.1", - "openeuropa/code-review": "~1.0.0-beta2", + "openeuropa/code-review": "~1.5", "openeuropa/drupal-core-require-dev": "^8.7", "openeuropa/task-runner": "~1.0@beta", "openeuropa/oe_link_lists": "dev-master", diff --git a/modules/oe_media_avportal/src/Plugin/EntityBrowser/Widget/AVPortalSearch.php b/modules/oe_media_avportal/src/Plugin/EntityBrowser/Widget/AVPortalSearch.php index 2b48adcf..50afe273 100644 --- a/modules/oe_media_avportal/src/Plugin/EntityBrowser/Widget/AVPortalSearch.php +++ b/modules/oe_media_avportal/src/Plugin/EntityBrowser/Widget/AVPortalSearch.php @@ -56,7 +56,7 @@ protected function prepareEntities(array $form, FormStateInterface $form_state) * @return \Drupal\media\MediaInterface|null * The media entity. */ - protected function getMediaEntityFromRef(string $ref): MediaInterface { + protected function getMediaEntityFromRef(string $ref): ?MediaInterface { $bundle = NULL; $field = NULL; diff --git a/tests/Behat/MediaContext.php b/tests/Behat/MediaContext.php index 04759704..24f57fde 100644 --- a/tests/Behat/MediaContext.php +++ b/tests/Behat/MediaContext.php @@ -11,12 +11,15 @@ use Drupal\DrupalExtension\Context\RawDrupalContext; use Drupal\file\FileInterface; use Drupal\media\MediaInterface; +use Drupal\Tests\oe_media\Traits\MediaCreationTrait; /** * Context to related to media testing. */ class MediaContext extends RawDrupalContext { + use MediaCreationTrait; + /** * Keep track of medias so they can be cleaned up. * @@ -120,16 +123,10 @@ public function createMediaDocuments(TableNode $table): void { $files = $table->getColumnsHash(); foreach ($files as $properties) { $file = $this->createFileEntity($properties['file']); - $media = \Drupal::entityTypeManager() - ->getStorage('media')->create([ - 'bundle' => 'document', - 'name' => $properties['name'], - 'oe_media_file' => [ - 'target_id' => (int) $file->id(), - ], - 'status' => 1, - ]); - $media->save(); + $media = $this->createMediaDocument($file, [ + 'name' => $properties['name'], + 'file_id' => $file->id(), + ]); // Store for cleanup. $this->media[] = $media; @@ -156,18 +153,11 @@ public function createMediaImages(TableNode $table): void { $files = $table->getColumnsHash(); foreach ($files as $properties) { $file = $this->createFileEntity($properties['file']); - $media = \Drupal::entityTypeManager() - ->getStorage('media')->create([ - 'bundle' => 'image', - 'name' => $properties['name'], - 'oe_media_image' => [ - 'target_id' => (int) $file->id(), - 'alt' => $properties['alt'] ?? $properties['name'], - 'title' => $properties['title'] ?? $properties['name'], - ], - 'status' => 1, - ]); - $media->save(); + $media = $this->createMediaImage($file, [ + 'name' => $properties['name'], + 'alt' => $properties['alt'] ?? $properties['name'], + 'title' => $properties['title'] ?? $properties['name'], + ]); // Store for cleanup. $this->media[] = $media; @@ -195,13 +185,9 @@ public function createMediaAvPortalPhotos(TableNode $table): void { // Retrieve the url table from the test scenario. foreach ($table->getColumnsHash() as $hash) { - $media = \Drupal::entityTypeManager() - ->getStorage('media')->create([ - 'bundle' => 'av_portal_photo', - 'oe_media_avportal_photo' => $media_source->transformUrlToReference($hash['url']), - 'status' => 1, - ]); - $media->save(); + $media = $this->createMediaAvPortalPhoto($media_source, [ + 'url' => $hash['url'], + ]); // Store for cleanup. $this->media[] = $media; @@ -222,13 +208,9 @@ public function createMediaAvPortalPhotos(TableNode $table): void { */ public function createMediaRemoteVideo(TableNode $table): void { foreach ($table->getColumnsHash() as $hash) { - $media = \Drupal::entityTypeManager() - ->getStorage('media')->create([ - 'bundle' => 'remote_video', - 'oe_media_oembed_video' => $hash['url'], - 'status' => 1, - ]); - $media->save(); + $media = $this->createMediaRemoteVideo([ + 'url' => $hash['url'], + ]); // Store for cleanup. $this->media[] = $media; @@ -296,9 +278,8 @@ protected function getConfigContext(): ConfigContext { * File entity object. */ protected function createFileEntity(string $file_name): FileInterface { - $file = file_save_data(file_get_contents($this->getMinkParameter('files_path') . $file_name), 'public://' . basename($file_name)); - $file->setPermanent(); - $file->save(); + $filepah = $this->getMinkParameter('files_path') . $file_name; + $file = $this->createFile($filepah); // Store for cleanup. $this->files[] = $file; diff --git a/tests/Traits/MediaCreationTrait.php b/tests/Traits/MediaCreationTrait.php new file mode 100644 index 00000000..137015e9 --- /dev/null +++ b/tests/Traits/MediaCreationTrait.php @@ -0,0 +1,214 @@ +setPermanent(); + $file->save(); + + return $file; + } + + /** + * Suggest a safe file name using the file uri. + * + * @param \Drupal\file\FileInterface $file + * The file entity. + * + * @return string + * The suggested name for the file entity. + */ + protected function getFileNameSuggestion(FileInterface $file): string { + return Html::cleanCssIdentifier($file->getFileUri()); + } + + /** + * Create a media entity of image bundle. + * + * @param \Drupal\file\FileInterface $file + * The file entity. + * @param array $settings + * An associative array of settings for the media entity. + * + * @return \Drupal\media\Entity\Media + * The media object. + */ + protected function createMediaDocument(FileInterface $file, array $settings = []): Media { + $settings += [ + 'name' => $this->getFileNameSuggestion($file), + 'status' => 1, + 'uid' => 0, + ]; + + $values = [ + 'bundle' => 'document', + 'name' => $settings['name'], + 'oe_media_file' => [ + 'target_id' => (int) $file->id(), + ], + 'status' => $settings['status'], + 'uid' => $settings['uid'], + ]; + + foreach (['name', 'status', 'uid'] as $key) { + if (isset($settings[$key])) { + // Remove already used values. + unset($settings[$key]); + } + } + + $values += $settings; + $media = \Drupal::entityTypeManager() + ->getStorage('media')->create($values); + $media->save(); + + return $media; + } + + /** + * Create a media entity of image bundle. + * + * @param \Drupal\file\FileInterface $file + * The file entity. + * @param array $settings + * An associative array of settings for the media entity. + * + * @return \Drupal\media\Entity\Media + * The media object. + */ + protected function createMediaImage(FileInterface $file, array $settings = []): Media { + $settings += [ + 'name' => $this->getFileNameSuggestion($file), + 'file_id' => FALSE, + 'alt' => 'image', + 'title' => 'image', + 'status' => 1, + 'uid' => 0, + ]; + + $values = [ + 'bundle' => 'image', + 'name' => $settings['name'], + 'oe_media_image' => [ + 'target_id' => (int) $file->id(), + 'alt' => $settings['alt'], + 'title' => $settings['title'], + ], + 'status' => $settings['status'], + 'uid' => $settings['uid'], + ]; + + foreach (['name', 'alt', 'title', 'status', 'uid'] as $key) { + if (isset($settings[$key])) { + // Remove already used values. + unset($settings[$key]); + } + } + + $values += $settings; + $media = \Drupal::entityTypeManager() + ->getStorage('media')->create($values); + $media->save(); + + return $media; + } + + /** + * Create a media entity of av_portal_photo bundle. + * + * @param Drupal\media_avportal\Plugin\media\Source\MediaAvPortalSourceInterface $media_source + * The av_portal_photo media source. + * @param array $settings + * An associative array of settings for the media entity. + * + * @return \Drupal\media\Entity\Media + * The media object. + */ + protected function createMediaAvPortalPhoto(MediaAvPortalSourceInterface $media_source, array $settings = []): Media { + $settings += [ + 'status' => 1, + 'uid' => 0, + ]; + + $values = [ + 'bundle' => 'av_portal_photo', + 'oe_media_avportal_photo' => $media_source->transformUrlToReference($settings['url']), + 'status' => $settings['status'], + 'uid' => $settings['uid'], + ]; + + foreach (['url', 'status', 'uid'] as $key) { + if (isset($settings[$key])) { + // Remove already used values. + unset($settings[$key]); + } + } + + $values += $settings; + $media = \Drupal::entityTypeManager() + ->getStorage('media')->create($values); + $media->save(); + + return $media; + } + + /** + * Create a media entity of remote_video bundle. + * + * @param array $settings + * An associative array of settings for the media entity. + * + * @return \Drupal\media\Entity\Media + * The media object. + */ + protected function createMediaRemoteVideo(array $settings = []): Media { + $settings += [ + 'status' => 1, + 'uid' => 0, + ]; + + $values = [ + 'bundle' => 'remote_video', + 'oe_media_oembed_video' => $settings['url'], + 'status' => $settings['status'], + 'uid' => $settings['uid'], + ]; + + foreach (['url', 'status', 'uid'] as $key) { + if (isset($settings[$key])) { + // Remove already used values. + unset($settings[$key]); + } + } + + $values += $settings; + $media = \Drupal::entityTypeManager() + ->getStorage('media')->create($values); + $media->save(); + + return $media; + } + +} From fac45b1814494b9ec6178be4ccb7b7e2ea94b768 Mon Sep 17 00:00:00 2001 From: drishu Date: Fri, 18 Sep 2020 07:18:17 +0000 Subject: [PATCH 2/3] EWPP-253: Add filename suggestion to image alt and title. --- tests/Traits/MediaCreationTrait.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Traits/MediaCreationTrait.php b/tests/Traits/MediaCreationTrait.php index 137015e9..0a9d6b64 100644 --- a/tests/Traits/MediaCreationTrait.php +++ b/tests/Traits/MediaCreationTrait.php @@ -99,11 +99,12 @@ protected function createMediaDocument(FileInterface $file, array $settings = [] * The media object. */ protected function createMediaImage(FileInterface $file, array $settings = []): Media { + $filename_suggestion = $this->getFileNameSuggestion($file); $settings += [ - 'name' => $this->getFileNameSuggestion($file), + 'name' => $filename_suggestion, 'file_id' => FALSE, - 'alt' => 'image', - 'title' => 'image', + 'alt' => $filename_suggestion, + 'title' => $filename_suggestion, 'status' => 1, 'uid' => 0, ]; From 29cfe2523423cd11b1ccfee7d01108139af6b212 Mon Sep 17 00:00:00 2001 From: drishu Date: Fri, 18 Sep 2020 14:50:17 +0000 Subject: [PATCH 3/3] EWPP-253: Change return type hint to interface. --- tests/Traits/MediaCreationTrait.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/Traits/MediaCreationTrait.php b/tests/Traits/MediaCreationTrait.php index 0a9d6b64..fdb7447b 100644 --- a/tests/Traits/MediaCreationTrait.php +++ b/tests/Traits/MediaCreationTrait.php @@ -6,7 +6,7 @@ use Drupal\file\FileInterface; use Drupal\media_avportal\Plugin\media\Source\MediaAvPortalSourceInterface; -use Drupal\media\Entity\Media; +use Drupal\media\MediaInterface; use Drupal\Component\Utility\Html; /** @@ -15,7 +15,7 @@ trait MediaCreationTrait { /** - * Create a file entity from given file path. + * Create a file entity from the given file path. * * @param string $filepath * Path to the file location. @@ -52,10 +52,10 @@ protected function getFileNameSuggestion(FileInterface $file): string { * @param array $settings * An associative array of settings for the media entity. * - * @return \Drupal\media\Entity\Media + * @return \Drupal\media\MediaInterface * The media object. */ - protected function createMediaDocument(FileInterface $file, array $settings = []): Media { + protected function createMediaDocument(FileInterface $file, array $settings = []): MediaInterface { $settings += [ 'name' => $this->getFileNameSuggestion($file), 'status' => 1, @@ -95,10 +95,10 @@ protected function createMediaDocument(FileInterface $file, array $settings = [] * @param array $settings * An associative array of settings for the media entity. * - * @return \Drupal\media\Entity\Media + * @return \Drupal\media\MediaInterface * The media object. */ - protected function createMediaImage(FileInterface $file, array $settings = []): Media { + protected function createMediaImage(FileInterface $file, array $settings = []): MediaInterface { $filename_suggestion = $this->getFileNameSuggestion($file); $settings += [ 'name' => $filename_suggestion, @@ -144,10 +144,10 @@ protected function createMediaImage(FileInterface $file, array $settings = []): * @param array $settings * An associative array of settings for the media entity. * - * @return \Drupal\media\Entity\Media + * @return \Drupal\media\MediaInterface * The media object. */ - protected function createMediaAvPortalPhoto(MediaAvPortalSourceInterface $media_source, array $settings = []): Media { + protected function createMediaAvPortalPhoto(MediaAvPortalSourceInterface $media_source, array $settings = []): MediaInterface { $settings += [ 'status' => 1, 'uid' => 0, @@ -181,10 +181,10 @@ protected function createMediaAvPortalPhoto(MediaAvPortalSourceInterface $media_ * @param array $settings * An associative array of settings for the media entity. * - * @return \Drupal\media\Entity\Media + * @return \Drupal\media\MediaInterface * The media object. */ - protected function createMediaRemoteVideo(array $settings = []): Media { + protected function createMediaRemoteVideo(array $settings = []): MediaInterface { $settings += [ 'status' => 1, 'uid' => 0,