Commit 58d601eb authored by larowlan's avatar larowlan

Issue #2976889 by phenaproxima, xjm, samuel.mortenson, chr.fritsch: Add a...

Issue #2976889 by phenaproxima, xjm, samuel.mortenson, chr.fritsch: Add a trait to create media types in tests and replace MediaFunctionalTestCreateMediaTypeTrait
parent 713294e9
......@@ -102,7 +102,7 @@ public function testDisplayConfiguration() {
$account = $this->drupalCreateUser(['administer media display']);
$this->drupalLogin($account);
$media_type = $this->createMediaType([], 'oembed:video');
$media_type = $this->createMediaType('oembed:video');
$this->drupalGet('/admin/structure/media/manage/' . $media_type->id() . '/display');
$assert = $this->assertSession();
$assert->statusCodeEquals(200);
......@@ -132,7 +132,7 @@ public function testRender($url, $resource_url, array $formatter_settings, array
$account = $this->drupalCreateUser(['view media']);
$this->drupalLogin($account);
$media_type = $this->createMediaType([], 'oembed:video');
$media_type = $this->createMediaType('oembed:video');
$source = $media_type->getSource();
$source_field = $source->getSourceFieldDefinition($media_type);
......
......@@ -39,7 +39,7 @@ protected function setUp() {
public function testMediaAccess() {
$assert_session = $this->assertSession();
$media_type = $this->createMediaType();
$media_type = $this->createMediaType('test');
// Create media.
$media = Media::create([
......
......@@ -39,7 +39,7 @@ class MediaBulkFormTest extends MediaFunctionalTestBase {
protected function setUp() {
parent::setUp();
$this->testMediaType = $this->createMediaType();
$this->testMediaType = $this->createMediaType('test');
// Create some test media items.
$this->mediaItems = [];
......
......@@ -4,6 +4,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\media\Entity\Media;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\system\Functional\Entity\EntityWithUriCacheTagsTestBase;
/**
......@@ -13,7 +14,7 @@
*/
class MediaCacheTagsTest extends EntityWithUriCacheTagsTestBase {
use MediaFunctionalTestCreateMediaTypeTrait;
use MediaTypeCreationTrait;
/**
* {@inheritdoc}
......@@ -28,7 +29,7 @@ class MediaCacheTagsTest extends EntityWithUriCacheTagsTestBase {
*/
protected function createEntity() {
// Create a media type.
$mediaType = $this->createMediaType();
$mediaType = $this->createMediaType('test');
// Create a media item.
$media = Media::create([
......
......@@ -23,7 +23,7 @@ class MediaContextualLinksTest extends MediaFunctionalTestBase {
*/
public function testMediaContextualLinks() {
// Create a media type.
$mediaType = $this->createMediaType();
$mediaType = $this->createMediaType('test');
// Create a media item.
$media = Media::create([
......
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\media\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
/**
* Base class for Media functional tests.
......@@ -10,7 +11,7 @@
abstract class MediaFunctionalTestBase extends BrowserTestBase {
use MediaFunctionalTestTrait;
use MediaFunctionalTestCreateMediaTypeTrait;
use MediaTypeCreationTrait;
/**
* Modules to enable.
......
......@@ -2,13 +2,24 @@
namespace Drupal\Tests\media\Functional;
use Drupal\media\Entity\MediaType;
@trigger_error(__NAMESPACE__ . '\MediaFunctionalTestCreateMediaTypeTrait is deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\media\Traits\MediaTypeCreationTrait instead. See https://www.drupal.org/node/2981614.', E_USER_DEPRECATED);
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
/**
* Trait with helpers for Media functional tests.
*
* @deprecated in Drupal 8.6.0 and will be removed before Drupal 9.0.0. Use
* \Drupal\Tests\media\Traits\MediaTypeCreationTrait instead.
*
* @see https://www.drupal.org/node/2981614
*/
trait MediaFunctionalTestCreateMediaTypeTrait {
use MediaTypeCreationTrait {
createMediaType as traitCreateMediaType;
}
/**
* Creates a media type.
*
......@@ -22,52 +33,7 @@ trait MediaFunctionalTestCreateMediaTypeTrait {
* A newly created media type.
*/
protected function createMediaType(array $values = [], $source = 'test') {
if (empty($values['bundle'])) {
$id = strtolower($this->randomMachineName());
}
else {
$id = $values['bundle'];
}
$values += [
'id' => $id,
'label' => $id,
'source' => $source,
'source_configuration' => [],
'field_map' => [],
'new_revision' => FALSE,
];
$media_type = MediaType::create($values);
$status = $media_type->save();
// @todo Rename to assertSame() when #1945040 is done.
// @see https://www.drupal.org/node/1945040
$this->assertIdentical(SAVED_NEW, $status, 'Media type was created successfully.');
// Ensure that the source field exists.
/** @var \Drupal\media\MediaSourceInterface $source */
$source = $media_type->getSource();
$source_field = $source->getSourceFieldDefinition($media_type);
if (!$source_field) {
$source_field = $source->createSourceField($media_type);
/** @var \Drupal\field\FieldStorageConfigInterface $storage */
$storage = $source_field->getFieldStorageDefinition();
$storage->save();
$source_field->save();
$media_type
->set('source_configuration', [
'source_field' => $source_field->getName(),
])
->save();
}
// Move source field in form display.
entity_get_form_display('media', $id, 'default')
->setComponent($source_field->getName())
->save();
return $media_type;
return $this->traitCreateMediaType($source, $values);
}
}
......@@ -57,8 +57,8 @@ public function testMediaOverviewPage() {
$assert_session->pageTextContains('No content available.');
// Create some content for the view.
$media_type1 = $this->createMediaType();
$media_type2 = $this->createMediaType();
$media_type1 = $this->createMediaType('test');
$media_type2 = $this->createMediaType('test');
$media1 = Media::create([
'bundle' => $media_type1->id(),
'name' => 'Media 1',
......
......@@ -25,7 +25,7 @@ public function testRevisions() {
$media_storage = $this->container->get('entity.manager')->getStorage('media');
// Create a media type and media item.
$media_type = $this->createMediaType();
$media_type = $this->createMediaType('test');
$media = $media_storage->create([
'bundle' => $media_type->id(),
'name' => 'Unnamed',
......@@ -80,7 +80,7 @@ public function testFileMediaRevision() {
$uri = 'temporary://foo.txt';
file_put_contents($uri, $this->randomString(128));
$this->createMediaType(['bundle' => 'file', 'new_revision' => TRUE], 'file');
$this->createMediaType('file', ['bundle' => 'file', 'new_revision' => TRUE]);
// Create a media item.
$this->drupalGet('/media/add/file');
......@@ -119,7 +119,7 @@ public function testFileMediaRevision() {
public function testImageMediaRevision() {
$assert = $this->assertSession();
$this->createMediaType(['bundle' => 'image', 'new_revision' => TRUE], 'image');
$this->createMediaType('image', ['bundle' => 'image', 'new_revision' => TRUE]);
/** @var \Drupal\field\FieldConfigInterface $field */
// Disable the alt text field, because this is not a JavaScript test and
......
......@@ -26,7 +26,7 @@ public function testStatusPage() {
$this->drupalGet('admin/reports/status');
$assert_session->pageTextNotContains('It is potentially insecure to display oEmbed content in a frame');
$this->createMediaType([], 'oembed:video');
$this->createMediaType('oembed:video');
$this->drupalGet('admin/reports/status');
$assert_session->pageTextContains('It is potentially insecure to display oEmbed content in a frame');
......
......@@ -19,7 +19,7 @@ public function testSourceFieldSettingsEditing() {
$page = $session->getPage();
$assert_session = $this->assertSession();
$media_type = $this->createMediaType([], 'file');
$media_type = $this->createMediaType('file');
$media_type_id = $media_type->id();
$this->assertSame('txt doc docx pdf', FieldConfig::load("media.$media_type_id.field_media_file")->get('settings')['file_extensions']);
......@@ -43,7 +43,7 @@ public function testSourceFieldSettingsEditing() {
* Ensure source field deletion is not possible.
*/
public function testPreventSourceFieldDeletion() {
$media_type = $this->createMediaType([], 'file');
$media_type = $this->createMediaType('file');
$media_type_id = $media_type->id();
$this->drupalGet("admin/structure/media/manage/$media_type_id/fields/media.$media_type_id.field_media_file/delete");
......
......@@ -22,7 +22,7 @@ class MediaTemplateSuggestionsTest extends MediaFunctionalTestBase {
* Tests template suggestions from media_theme_suggestions_media().
*/
public function testMediaThemeHookSuggestions() {
$media_type = $this->createMediaType([
$media_type = $this->createMediaType('test', [
'new_revision' => FALSE,
'queue_thumbnail_downloads' => FALSE,
]);
......
......@@ -43,7 +43,7 @@ public function testMediaWithOnlyOneMediaType() {
$page = $session->getPage();
$assert_session = $this->assertSession();
$media_type = $this->createMediaType([
$media_type = $this->createMediaType('test', [
'new_revision' => FALSE,
'queue_thumbnail_downloads' => FALSE,
]);
......@@ -151,10 +151,10 @@ public function testMediaWithMultipleMediaTypes() {
$assert_session = $this->assertSession();
// Tests and creates the first media type.
$first_media_type = $this->createMediaType(['description' => $this->randomMachineName(32)]);
$first_media_type = $this->createMediaType('test', ['description' => $this->randomMachineName(32)]);
// Test and create a second media type.
$second_media_type = $this->createMediaType(['description' => $this->randomMachineName(32)]);
$second_media_type = $this->createMediaType('test', ['description' => $this->randomMachineName(32)]);
// Test if media/add displays two media type options.
$this->drupalGet('media/add');
......@@ -284,7 +284,10 @@ public function testMediaReferenceWidget($cardinality, array $media_type_create_
$create_media_types[] = "media_type_$id";
$permissions[] = "create media_type_$id media";
}
$this->createMediaType(['bundle' => "media_type_$id"]);
$this->createMediaType('test', [
'bundle' => "media_type_$id",
'label' => "media_type_$id",
]);
$media_types["media_type_$id"] = "media_type_$id";
}
......@@ -513,7 +516,7 @@ public function testMediaCollectionRoute() {
$this->container->get('module_installer')->uninstall(['views']);
// Create a media type and media item.
$media_type = $this->createMediaType();
$media_type = $this->createMediaType('test');
$media = $media_storage->create([
'bundle' => $media_type->id(),
'name' => 'Unnamed',
......
......@@ -39,7 +39,7 @@ public function testMediaDisplay() {
$assert_session = $this->assertSession();
$page = $this->getSession()->getPage();
$media_type = $this->createMediaType();
$media_type = $this->createMediaType('test');
// Create a media item.
$media = Media::create([
......
......@@ -3,8 +3,8 @@
namespace Drupal\Tests\media\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
use Drupal\Tests\media\Functional\MediaFunctionalTestCreateMediaTypeTrait;
use Drupal\Tests\media\Functional\MediaFunctionalTestTrait;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
/**
* Base class for Media functional JavaScript tests.
......@@ -12,7 +12,7 @@
abstract class MediaJavascriptTestBase extends JavascriptTestBase {
use MediaFunctionalTestTrait;
use MediaFunctionalTestCreateMediaTypeTrait;
use MediaTypeCreationTrait;
/**
* Modules to enable.
......
......@@ -191,7 +191,7 @@ public function testMediaTypes() {
// Test that the system for preventing the deletion of media types works
// (they cannot be deleted if there is media content of that type/bundle).
$media_type2 = $this->createMediaType();
$media_type2 = $this->createMediaType('test');
$label2 = $media_type2->label();
$media = Media::create(['name' => 'lorem ipsum', 'bundle' => $media_type2->id()]);
$media->save();
......
......@@ -22,7 +22,7 @@ public function testMediaWizard() {
$page = $session->getPage();
$assert_session = $this->assertSession();
$this->createMediaType();
$this->createMediaType('test');
$view_id = strtolower($this->randomMachineName(16));
$this->drupalGet('admin/structure/views/add');
......
......@@ -5,8 +5,8 @@
use Drupal\file\Entity\File;
use Drupal\KernelTests\KernelTestBase;
use Drupal\media\Entity\Media;
use Drupal\media\Entity\MediaType;
use Drupal\media\MediaTypeInterface;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\user\Entity\User;
use org\bovigo\vfs\vfsStream;
......@@ -15,6 +15,8 @@
*/
abstract class MediaKernelTestBase extends KernelTestBase {
use MediaTypeCreationTrait;
/**
* Modules to install.
*
......@@ -77,37 +79,6 @@ protected function setUp() {
$this->container->get('current_user')->setAccount($this->user);
}
/**
* Create a media type for a source plugin.
*
* @param string $media_source_name
* The name of the media source.
*
* @return \Drupal\media\MediaTypeInterface
* A media type.
*/
protected function createMediaType($media_source_name) {
$id = strtolower($this->randomMachineName());
$media_type = MediaType::create([
'id' => $id,
'label' => $id,
'source' => $media_source_name,
'new_revision' => FALSE,
]);
$media_type->save();
$source_field = $media_type->getSource()->createSourceField($media_type);
// The media type form creates a source field if it does not exist yet. The
// same must be done in a kernel test, since it does not use that form.
// @see \Drupal\media\MediaTypeForm::save()
$source_field->getFieldStorageDefinition()->save();
// The source field storage has been created, now the field can be saved.
$source_field->save();
$media_type->set('source_configuration', [
'source_field' => $source_field->getName(),
])->save();
return $media_type;
}
/**
* Helper to generate a media item.
*
......
<?php
namespace Drupal\Tests\media\Traits;
use Drupal\media\Entity\MediaType;
/**
* Provides methods to create a media type from given values.
*
* This trait is meant to be used only by test classes.
*/
trait MediaTypeCreationTrait {
/**
* Create a media type for a source plugin.
*
* @param string $source_plugin_id
* The media source plugin ID.
* @param mixed[] $values
* (optional) Additional values for the media type entity:
* - id: The ID of the media type. If none is provided, a random value will
* be used.
* - label: The human-readable label of the media type. If none is provided,
* a random value will be used.
* - new_revision: Whether media items of this type should create new
* revisions on save by default. Defaults to FALSE.
* - field_map: Array containing the field map configuration. The keys are
* the names of metadata attributes provided by the source plugin, and the
* values are the names of entity fields to which those attributes should be
* copied. Empty by default.
* - source_configuration: Additional configuration options for the source
* plugin. Empty by default.
*
* @return \Drupal\media\MediaTypeInterface
* A media type.
*/
protected function createMediaType($source_plugin_id, array $values = []) {
if (isset($values['bundle'])) {
$values['id'] = $values['bundle'];
unset($values['bundle']);
}
$values += [
'id' => $this->randomMachineName(),
'label' => $this->randomMachineName(),
'source' => $source_plugin_id,
'source_configuration' => [],
'field_map' => [],
'new_revision' => FALSE,
];
/** @var \Drupal\media\MediaTypeInterface $media_type */
$media_type = MediaType::create($values);
$this->assertSame(SAVED_NEW, $media_type->save());
$source = $media_type->getSource();
$source_field = $source->createSourceField($media_type);
// The media type form creates a source field if it does not exist yet. The
// same must be done in a kernel test, since it does not use that form.
// @see \Drupal\media\MediaTypeForm::save()
$source_field->getFieldStorageDefinition()->save();
// The source field storage has been created, now the field can be saved.
$source_field->save();
$source_configuration = $source->getConfiguration();
$source_configuration['source_field'] = $source_field->getName();
$source->setConfiguration($source_configuration);
$this->assertSame(SAVED_UPDATED, $media_type->save());
// Add the source field to the form display for the media type.
$form_display = entity_get_form_display('media', $media_type->id(), 'default');
$source->prepareFormDisplay($media_type, $form_display);
$form_display->save();
return $media_type;
}
}
......@@ -16,7 +16,7 @@
use Drupal\node\Entity\NodeType;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\media\Functional\MediaFunctionalTestCreateMediaTypeTrait;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\user\Entity\User;
/**
......@@ -27,7 +27,7 @@
class ContentEntityTest extends KernelTestBase {
use EntityReferenceTestTrait;
use MediaFunctionalTestCreateMediaTypeTrait;
use MediaTypeCreationTrait;
/**
* {@inheritdoc}
......@@ -344,7 +344,7 @@ public function testMediaSource() {
'source' => 'test',
'new_revision' => FALSE,
];
$media_type = $this->createMediaType($values);
$media_type = $this->createMediaType('test', $values);
$media = Media::create([
'name' => 'Foo media',
'uid' => $this->user->id(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment