Skip to content

Commit

Permalink
EWPP-253: Move media creation into trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
drishu committed Sep 17, 2020
1 parent 9216650 commit ec39a1f
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 41 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
59 changes: 20 additions & 39 deletions tests/Behat/MediaContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand 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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
200 changes: 200 additions & 0 deletions tests/Traits/MediaCreationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\oe_media\Traits;

use Drupal\file\FileInterface;
use Drupal\media_avportal\Plugin\media\Source\MediaAvPortalSourceInterface;
use Drupal\media\Entity\Media;

/**
* Helper methods to deal with media creation.
*/
trait MediaCreationTrait {

/**
* Create a file entity from given file path.
*
* @param string $filepath
* Path to the file location.
*
* @return \Drupal\file\FileInterface
* File entity object.
*/
protected function createFile(string $filepath): FileInterface {
$file = file_save_data(file_get_contents($filepath), 'public://' . basename($filepath));
$file->setPermanent();
$file->save();

return $file;
}

/**
* 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' => 'document',
'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' => 'image',
'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;
}

}

0 comments on commit ec39a1f

Please sign in to comment.