upgrades core to 8.4.2
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 8.7 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 8.7 KiB |
+15
@@ -0,0 +1,15 @@
|
||||
media.source.test:
|
||||
type: media.source.field_aware
|
||||
label: 'Test media source configuration'
|
||||
mapping:
|
||||
test_config_value:
|
||||
type: string
|
||||
label: 'Test config value'
|
||||
|
||||
media.source.test_translation:
|
||||
type: media.source.test
|
||||
label: 'Test media source with translations'
|
||||
|
||||
media.source.test_constraints:
|
||||
type: media.source.test
|
||||
label: 'Test media source with constraints configuration'
|
||||
@@ -0,0 +1,12 @@
|
||||
name: 'Test media source'
|
||||
type: module
|
||||
description: 'Provides test media source to test configuration forms.'
|
||||
# core: 8.x
|
||||
package: Testing
|
||||
# version: VERSION
|
||||
|
||||
# Information added by Drupal.org packaging script on 2017-11-03
|
||||
version: '8.4.2'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1509719929
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\media_test_source\Plugin\Validation\Constraint;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* A media test constraint.
|
||||
*
|
||||
* @Constraint(
|
||||
* id = "MediaTestConstraint",
|
||||
* label = @Translation("Media constraint for test purposes.", context = "Validation"),
|
||||
* type = { "entity", "string" }
|
||||
* )
|
||||
*/
|
||||
class MediaTestConstraint extends Constraint {
|
||||
|
||||
/**
|
||||
* The default violation message.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $message = 'Inappropriate text.';
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\media_test_source\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
/**
|
||||
* Validates the MediaTestConstraint.
|
||||
*/
|
||||
class MediaTestConstraintValidator extends ConstraintValidator {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($value, Constraint $constraint) {
|
||||
if ($value instanceof EntityInterface) {
|
||||
$string_to_test = $value->label();
|
||||
}
|
||||
elseif ($value instanceof FieldItemListInterface) {
|
||||
$string_to_test = $value->value;
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (strpos($string_to_test, 'love Drupal') === FALSE) {
|
||||
$this->context->addViolation($constraint->message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\media_test_source\Plugin\media\Source;
|
||||
|
||||
use Drupal\Component\Utility\NestedArray;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\media\MediaInterface;
|
||||
use Drupal\media\MediaSourceBase;
|
||||
|
||||
/**
|
||||
* Provides test media source.
|
||||
*
|
||||
* @MediaSource(
|
||||
* id = "test",
|
||||
* label = @Translation("Test source"),
|
||||
* description = @Translation("Test media source."),
|
||||
* allowed_field_types = {"string"},
|
||||
* )
|
||||
*/
|
||||
class Test extends MediaSourceBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMetadataAttributes() {
|
||||
// The metadata attributes are kept in state storage. This allows tests to
|
||||
// change the metadata attributes and makes it easier to test different
|
||||
// variations.
|
||||
$attributes = \Drupal::state()->get('media_source_test_attributes', [
|
||||
'attribute_1' => ['label' => $this->t('Attribute 1'), 'value' => 'Value 1'],
|
||||
'attribute_2' => ['label' => $this->t('Attribute 2'), 'value' => 'Value 1'],
|
||||
]);
|
||||
return array_map(function ($item) {
|
||||
return $item['label'];
|
||||
}, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMetadata(MediaInterface $media, $attribute_name) {
|
||||
$attributes = \Drupal::state()->get('media_source_test_attributes', [
|
||||
'attribute_1' => ['label' => $this->t('Attribute 1'), 'value' => 'Value 1'],
|
||||
'attribute_2' => ['label' => $this->t('Attribute 2'), 'value' => 'Value 1'],
|
||||
]);
|
||||
|
||||
if (in_array($attribute_name, array_keys($attributes))) {
|
||||
return $attributes[$attribute_name]['value'];
|
||||
}
|
||||
|
||||
return parent::getMetadata($media, $attribute_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPluginDefinition() {
|
||||
return NestedArray::mergeDeep(
|
||||
parent::getPluginDefinition(),
|
||||
\Drupal::state()->get('media_source_test_definition', [])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return parent::defaultConfiguration() + [
|
||||
'test_config_value' => 'This is default value.',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::buildConfigurationForm($form, $form_state);
|
||||
|
||||
$form['test_config_value'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Test config value'),
|
||||
'#default_value' => $this->configuration['test_config_value'],
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\media_test_source\Plugin\media\Source;
|
||||
|
||||
use Drupal\media\MediaInterface;
|
||||
|
||||
/**
|
||||
* Provides test media source.
|
||||
*
|
||||
* @MediaSource(
|
||||
* id = "test_translation",
|
||||
* label = @Translation("Test source with translations"),
|
||||
* description = @Translation("Test media source with translations."),
|
||||
* allowed_field_types = {"string"},
|
||||
* thumbnail_alt_metadata_attribute = "test_thumbnail_alt",
|
||||
* )
|
||||
*/
|
||||
class TestTranslation extends Test {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMetadata(MediaInterface $media, $attribute_name) {
|
||||
if ($attribute_name == 'thumbnail_uri') {
|
||||
return 'public://' . $media->language()->getId() . '.png';
|
||||
}
|
||||
|
||||
if ($attribute_name == 'test_thumbnail_alt') {
|
||||
$langcode = $media->language()->getId();
|
||||
return $this->t('Test Thumbnail @language', ['@language' => $langcode], ['langcode' => $langcode]);
|
||||
}
|
||||
|
||||
return parent::getMetadata($media, $attribute_name);
|
||||
}
|
||||
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\media_test_source\Plugin\media\Source;
|
||||
|
||||
use Drupal\media\MediaSourceEntityConstraintsInterface;
|
||||
use Drupal\media\MediaSourceFieldConstraintsInterface;
|
||||
|
||||
/**
|
||||
* Provides generic media type.
|
||||
*
|
||||
* @MediaSource(
|
||||
* id = "test_constraints",
|
||||
* label = @Translation("Test source with constraints"),
|
||||
* description = @Translation("Test media source that provides constraints."),
|
||||
* allowed_field_types = {"string_long"},
|
||||
* )
|
||||
*/
|
||||
class TestWithConstraints extends Test implements MediaSourceEntityConstraintsInterface, MediaSourceFieldConstraintsInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getEntityConstraints() {
|
||||
return \Drupal::state()->get('media_source_test_entity_constraints', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSourceFieldConstraints() {
|
||||
return \Drupal::state()->get('media_source_test_field_constraints', []);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
id: test
|
||||
label: 'Test type'
|
||||
description: 'Test type.'
|
||||
source: test
|
||||
source_configuration:
|
||||
test_config_value: 'Kakec'
|
||||
status: true
|
||||
langcode: en
|
||||
dependencies: { }
|
||||
field_map:
|
||||
metadata_attribute: 'field_attribute_config_test'
|
||||
@@ -0,0 +1,14 @@
|
||||
name: 'Media test type'
|
||||
type: module
|
||||
description: 'Provides test type for a media item.'
|
||||
# core: 8.x
|
||||
package: Testing
|
||||
# version: VERSION
|
||||
dependencies:
|
||||
- media_test_source
|
||||
|
||||
# Information added by Drupal.org packaging script on 2017-11-03
|
||||
version: '8.4.2'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1509719929
|
||||
@@ -0,0 +1,15 @@
|
||||
name: 'Media test views'
|
||||
type: module
|
||||
description: 'Provides default views for views media tests.'
|
||||
package: Testing
|
||||
# version: VERSION
|
||||
# core: 8.x
|
||||
dependencies:
|
||||
- media
|
||||
- views
|
||||
|
||||
# Information added by Drupal.org packaging script on 2017-11-03
|
||||
version: '8.4.2'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1509719929
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Functional;
|
||||
|
||||
use Drupal\media\Entity\Media;
|
||||
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Basic access tests for Media.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaAccessTest extends MediaUiFunctionalTest {
|
||||
|
||||
use AssertPageCacheContextsAndTagsTrait;
|
||||
|
||||
/**
|
||||
* Test some access control functionality.
|
||||
*/
|
||||
public function testMediaAccess() {
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$media_type = $this->createMediaType();
|
||||
|
||||
// Create media.
|
||||
$media = Media::create([
|
||||
'bundle' => $media_type->id(),
|
||||
'name' => 'Unnamed',
|
||||
]);
|
||||
$media->save();
|
||||
$user_media = Media::create([
|
||||
'bundle' => $media_type->id(),
|
||||
'name' => 'Unnamed',
|
||||
'uid' => $this->nonAdminUser->id(),
|
||||
]);
|
||||
$user_media->save();
|
||||
|
||||
// We are logged in as admin, so test 'administer media' permission.
|
||||
$this->drupalGet('media/add/' . $media_type->id());
|
||||
$this->assertCacheContext('user.permissions');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
$this->drupalGet('media/' . $user_media->id());
|
||||
$this->assertCacheContext('user.permissions');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
$this->drupalGet('media/' . $user_media->id() . '/edit');
|
||||
$this->assertCacheContext('user.permissions');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
$this->drupalGet('media/' . $user_media->id() . '/delete');
|
||||
$this->assertCacheContext('user.permissions');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
|
||||
$this->drupalLogin($this->nonAdminUser);
|
||||
/** @var \Drupal\user\RoleInterface $role */
|
||||
$role = Role::load(RoleInterface::AUTHENTICATED_ID);
|
||||
|
||||
// Test 'view media' permission.
|
||||
user_role_revoke_permissions($role->id(), ['view media']);
|
||||
$this->drupalGet('media/' . $media->id());
|
||||
$this->assertCacheContext('user.permissions');
|
||||
$assert_session->statusCodeEquals(403);
|
||||
$access_result = $media->access('view', NULL, TRUE);
|
||||
$this->assertSame("The 'view media' permission is required and the media item must be published.", $access_result->getReason());
|
||||
$this->grantPermissions($role, ['view media']);
|
||||
$this->drupalGet('media/' . $media->id());
|
||||
$this->assertCacheContext('user');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
|
||||
// Test 'create media' permission.
|
||||
$this->drupalGet('media/add/' . $media_type->id());
|
||||
$this->assertCacheContext('user.permissions');
|
||||
$assert_session->statusCodeEquals(403);
|
||||
$this->grantPermissions($role, ['create media']);
|
||||
$this->drupalGet('media/add/' . $media_type->id());
|
||||
$this->assertCacheContext('user.permissions');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
|
||||
// Test 'update media' and 'delete media' permissions.
|
||||
$this->drupalGet('media/' . $user_media->id() . '/edit');
|
||||
$this->assertCacheContext('user');
|
||||
$assert_session->statusCodeEquals(403);
|
||||
$this->drupalGet('media/' . $user_media->id() . '/delete');
|
||||
$this->assertCacheContext('user');
|
||||
$assert_session->statusCodeEquals(403);
|
||||
$this->grantPermissions($role, ['update media']);
|
||||
$this->grantPermissions($role, ['delete media']);
|
||||
$this->drupalGet('media/' . $user_media->id() . '/edit');
|
||||
$this->assertCacheContext('user');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
$this->drupalGet('media/' . $user_media->id() . '/delete');
|
||||
$this->assertCacheContext('user');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
|
||||
// Test 'update any media' and 'delete any media' permissions.
|
||||
$this->drupalGet('media/' . $media->id() . '/edit');
|
||||
$this->assertCacheContext('user');
|
||||
$assert_session->statusCodeEquals(403);
|
||||
$this->drupalGet('media/' . $media->id() . '/delete');
|
||||
$this->assertCacheContext('user');
|
||||
$assert_session->statusCodeEquals(403);
|
||||
$this->grantPermissions($role, ['update any media']);
|
||||
$this->grantPermissions($role, ['delete any media']);
|
||||
$this->drupalGet('media/' . $media->id() . '/edit');
|
||||
$this->assertCacheContext('user.permissions');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
$this->drupalGet('media/' . $media->id() . '/delete');
|
||||
$this->assertCacheContext('user.permissions');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
|
||||
// Test the 'access media overview' permission.
|
||||
$this->grantPermissions($role, ['access content overview']);
|
||||
$this->drupalGet('admin/content');
|
||||
$assert_session->linkByHrefNotExists('/admin/content/media');
|
||||
$this->assertCacheContext('user');
|
||||
|
||||
// Create a new role, which implicitly checks if the permission exists.
|
||||
$mediaOverviewRole = $this->createRole(['access content overview', 'access media overview']);
|
||||
$this->nonAdminUser->addRole($mediaOverviewRole);
|
||||
$this->nonAdminUser->save();
|
||||
|
||||
$this->drupalGet('admin/content');
|
||||
$assert_session->linkByHrefExists('/admin/content/media');
|
||||
$this->clickLink('Media');
|
||||
$this->assertCacheContext('user.permissions');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
$assert_session->elementExists('css', '.view-media');
|
||||
$assert_session->pageTextContains($this->loggedInUser->getDisplayName());
|
||||
$assert_session->pageTextContains($this->nonAdminUser->getDisplayName());
|
||||
$assert_session->linkByHrefExists('/media/' . $media->id());
|
||||
$assert_session->linkByHrefExists('/media/' . $user_media->id());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Functional;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\media\Entity\Media;
|
||||
use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase;
|
||||
|
||||
/**
|
||||
* Tests the media entity's cache tags.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaCacheTagsTest extends EntityWithUriCacheTagsTestBase {
|
||||
|
||||
use MediaFunctionalTestCreateMediaTypeTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = [
|
||||
'media',
|
||||
'media_test_source',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createEntity() {
|
||||
// Create a media type.
|
||||
$mediaType = $this->createMediaType();
|
||||
|
||||
// Create a media item.
|
||||
$media = Media::create([
|
||||
'bundle' => $mediaType->id(),
|
||||
'name' => 'Unnamed',
|
||||
]);
|
||||
$media->save();
|
||||
|
||||
return $media;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getAdditionalCacheContextsForEntity(EntityInterface $media) {
|
||||
return ['timezone'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getAdditionalCacheTagsForEntity(EntityInterface $media) {
|
||||
// Each media item must have an author and a thumbnail.
|
||||
return [
|
||||
'user:' . $media->getOwnerId(),
|
||||
'config:image.style.thumbnail',
|
||||
'file:' . $media->get('thumbnail')->entity->id(),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Base class for Media functional tests.
|
||||
*/
|
||||
abstract class MediaFunctionalTestBase extends BrowserTestBase {
|
||||
|
||||
use MediaFunctionalTestTrait;
|
||||
use MediaFunctionalTestCreateMediaTypeTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = [
|
||||
'system',
|
||||
'node',
|
||||
'field_ui',
|
||||
'views_ui',
|
||||
'media',
|
||||
'media_test_source',
|
||||
];
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Functional;
|
||||
|
||||
use Drupal\media\Entity\MediaType;
|
||||
|
||||
/**
|
||||
* Trait with helpers for Media functional tests.
|
||||
*/
|
||||
trait MediaFunctionalTestCreateMediaTypeTrait {
|
||||
|
||||
/**
|
||||
* Creates a media type.
|
||||
*
|
||||
* @param array $values
|
||||
* The media type values.
|
||||
* @param string $source
|
||||
* (optional) The media source plugin that is responsible for additional
|
||||
* logic related to this media type. Defaults to 'test'.
|
||||
*
|
||||
* @return \Drupal\media\MediaTypeInterface
|
||||
* 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.
|
||||
$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();
|
||||
}
|
||||
|
||||
return $media_type;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Functional;
|
||||
|
||||
/**
|
||||
* Trait with helpers for Media functional tests.
|
||||
*/
|
||||
trait MediaFunctionalTestTrait {
|
||||
|
||||
/**
|
||||
* Permissions for the admin user that will be logged-in for test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $adminUserPermissions = [
|
||||
// Media entity permissions.
|
||||
'administer media',
|
||||
'administer media fields',
|
||||
'administer media form display',
|
||||
'administer media display',
|
||||
'administer media types',
|
||||
'view media',
|
||||
'create media',
|
||||
'update media',
|
||||
'update any media',
|
||||
'delete media',
|
||||
'delete any media',
|
||||
// Other permissions.
|
||||
'administer views',
|
||||
'access content overview',
|
||||
'view all revisions',
|
||||
'administer content types',
|
||||
'administer node fields',
|
||||
'administer node form display',
|
||||
'bypass node access',
|
||||
];
|
||||
|
||||
/**
|
||||
* An admin test user account.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* A non-admin test user account.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $nonAdminUser;
|
||||
|
||||
/**
|
||||
* The storage service.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Have two users ready to be used in tests.
|
||||
$this->adminUser = $this->drupalCreateUser(static::$adminUserPermissions);
|
||||
$this->nonAdminUser = $this->drupalCreateUser([]);
|
||||
// Start off logged in as admin.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
$this->storage = $this->container->get('entity_type.manager')->getStorage('media');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Functional;
|
||||
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Tests media Install / Uninstall logic.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaInstallTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['media'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->drupalLogin($this->drupalCreateUser(['administer modules']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests reinstalling after being uninstalled.
|
||||
*/
|
||||
public function testReinstallAfterUninstall() {
|
||||
$page = $this->getSession()->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
// Uninstall the media module.
|
||||
$this->container->get('module_installer')->uninstall(['media'], FALSE);
|
||||
|
||||
// Install the media module again, through a test module that depends on it.
|
||||
// Note: We use a test module because in 8.4 the media module is hidden.
|
||||
// @todo Simplify this in https://www.drupal.org/node/2897028 once it's
|
||||
// shown again.
|
||||
$this->drupalGet('/admin/modules');
|
||||
$page->checkField('modules[media_test_views][enable]');
|
||||
$page->pressButton('Install');
|
||||
$assert_session->pageTextContains('Some required modules must be enabled');
|
||||
$page->pressButton('Continue');
|
||||
$this->assertSession()->pageTextNotContains('could not be moved/copied because a file by that name already exists in the destination directory');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Functional;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\media\MediaInterface;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests the revisionability of media entities.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaRevisionTest extends MediaFunctionalTestBase {
|
||||
|
||||
/**
|
||||
* Checks media revision operations.
|
||||
*/
|
||||
public function testRevisions() {
|
||||
$assert = $this->assertSession();
|
||||
|
||||
/** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $media_storage */
|
||||
$media_storage = $this->container->get('entity.manager')->getStorage('media');
|
||||
|
||||
// Create a media type and media item.
|
||||
$media_type = $this->createMediaType();
|
||||
$media = $media_storage->create([
|
||||
'bundle' => $media_type->id(),
|
||||
'name' => 'Unnamed',
|
||||
]);
|
||||
$media->save();
|
||||
|
||||
// You can't access the revision page when there is only 1 revision.
|
||||
$this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view');
|
||||
$assert->statusCodeEquals(403);
|
||||
|
||||
// Create some revisions.
|
||||
$media_revisions = [];
|
||||
$media_revisions[] = clone $media;
|
||||
$revision_count = 3;
|
||||
for ($i = 0; $i < $revision_count; $i++) {
|
||||
$media->revision_log = $this->randomMachineName(32);
|
||||
$media = $this->createMediaRevision($media);
|
||||
$media_revisions[] = clone $media;
|
||||
}
|
||||
|
||||
// Get the last revision for simple checks.
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = end($media_revisions);
|
||||
|
||||
// Test permissions.
|
||||
$this->drupalLogin($this->nonAdminUser);
|
||||
/** @var \Drupal\user\RoleInterface $role */
|
||||
$role = Role::load(RoleInterface::AUTHENTICATED_ID);
|
||||
|
||||
// Test 'view all media revisions' permission ('view media' permission is
|
||||
// needed as well).
|
||||
user_role_revoke_permissions($role->id(), ['view media', 'view all media revisions']);
|
||||
$this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view');
|
||||
$assert->statusCodeEquals(403);
|
||||
$this->grantPermissions($role, ['view media', 'view all media revisions']);
|
||||
$this->drupalGet('media/' . $media->id() . '/revisions/' . $media->getRevisionId() . '/view');
|
||||
$assert->statusCodeEquals(200);
|
||||
|
||||
// Confirm the revision page shows the correct title.
|
||||
$assert->pageTextContains($media->getName());
|
||||
|
||||
// Confirm that the last revision is the default revision.
|
||||
$this->assertTrue($media->isDefaultRevision(), 'Last revision is the default.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating revisions of a File media item.
|
||||
*/
|
||||
public function testFileMediaRevision() {
|
||||
$assert = $this->assertSession();
|
||||
|
||||
$uri = 'temporary://foo.txt';
|
||||
file_put_contents($uri, $this->randomString(128));
|
||||
|
||||
// Create a media item.
|
||||
$this->drupalGet('/media/add/file');
|
||||
$page = $this->getSession()->getPage();
|
||||
$page->fillField('Name', 'Foobar');
|
||||
$page->attachFileToField('File', $this->container->get('file_system')->realpath($uri));
|
||||
$page->pressButton('Save');
|
||||
$assert->addressMatches('/^\/media\/[0-9]+$/');
|
||||
|
||||
// The media item was just created, so it should only have one revision.
|
||||
$media = $this->container
|
||||
->get('entity_type.manager')
|
||||
->getStorage('media')
|
||||
->load(1);
|
||||
$this->assertRevisionCount($media, 1);
|
||||
|
||||
// If we edit the item, we should get a new revision.
|
||||
$this->drupalGet('/media/1/edit');
|
||||
$assert->checkboxChecked('Create new revision');
|
||||
$page = $this->getSession()->getPage();
|
||||
$page->fillField('Name', 'Foobaz');
|
||||
$page->pressButton('Save');
|
||||
$this->assertRevisionCount($media, 2);
|
||||
|
||||
// Confirm the correct revision title appears on "view revisions" page.
|
||||
$media = $this->container->get('entity_type.manager')
|
||||
->getStorage('media')
|
||||
->loadUnchanged(1);
|
||||
$this->drupalGet("media/" . $media->id() . "/revisions/" . $media->getRevisionId() . "/view");
|
||||
$assert->pageTextContains('Foobaz');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating revisions of a Image media item.
|
||||
*/
|
||||
public function testImageMediaRevision() {
|
||||
$assert = $this->assertSession();
|
||||
|
||||
/** @var \Drupal\field\FieldConfigInterface $field */
|
||||
// Disable the alt text field, because this is not a JavaScript test and
|
||||
// the alt text field will therefore not appear without a full page refresh.
|
||||
$field = FieldConfig::load('media.image.field_media_image');
|
||||
$settings = $field->getSettings();
|
||||
$settings['alt_field'] = FALSE;
|
||||
$settings['alt_field_required'] = FALSE;
|
||||
$field->set('settings', $settings);
|
||||
$field->save();
|
||||
|
||||
// Create a media item.
|
||||
$this->drupalGet('/media/add/image');
|
||||
$page = $this->getSession()->getPage();
|
||||
$page->fillField('Name', 'Foobar');
|
||||
$page->attachFileToField('Image', \Drupal::root() . '/core/modules/media/tests/fixtures/example_1.jpeg');
|
||||
$page->pressButton('Save');
|
||||
$assert->addressMatches('/^\/media\/[0-9]+$/');
|
||||
|
||||
// The media item was just created, so it should only have one revision.
|
||||
$media = $this->container
|
||||
->get('entity_type.manager')
|
||||
->getStorage('media')
|
||||
->load(1);
|
||||
$this->assertRevisionCount($media, 1);
|
||||
|
||||
// If we edit the item, we should get a new revision.
|
||||
$this->drupalGet('/media/1/edit');
|
||||
$assert->checkboxChecked('Create new revision');
|
||||
$page = $this->getSession()->getPage();
|
||||
$page->fillField('Name', 'Foobaz');
|
||||
$page->pressButton('Save');
|
||||
$this->assertRevisionCount($media, 2);
|
||||
|
||||
// Confirm the correct revision title appears on "view revisions" page.
|
||||
$media = $this->container->get('entity_type.manager')
|
||||
->getStorage('media')
|
||||
->loadUnchanged(1);
|
||||
$this->drupalGet("media/" . $media->id() . "/revisions/" . $media->getRevisionId() . "/view");
|
||||
$assert->pageTextContains('Foobaz');
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new revision for a given media item.
|
||||
*
|
||||
* @param \Drupal\media\MediaInterface $media
|
||||
* A media object.
|
||||
*
|
||||
* @return \Drupal\media\MediaInterface
|
||||
* A media object with up to date revision information.
|
||||
*/
|
||||
protected function createMediaRevision(MediaInterface $media) {
|
||||
$media->setName($this->randomMachineName());
|
||||
$media->setNewRevision();
|
||||
$media->save();
|
||||
return $media;
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that an entity has a certain number of revisions.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The entity in question.
|
||||
* @param int $expected_revisions
|
||||
* The expected number of revisions.
|
||||
*/
|
||||
protected function assertRevisionCount(EntityInterface $entity, $expected_revisions) {
|
||||
$entity_type = $entity->getEntityType();
|
||||
|
||||
$count = $this->container
|
||||
->get('entity.query')
|
||||
->get($entity_type->id())
|
||||
->count()
|
||||
->allRevisions()
|
||||
->condition($entity_type->getKey('id'), $entity->id())
|
||||
->execute();
|
||||
|
||||
$this->assertSame($expected_revisions, (int) $count);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Functional;
|
||||
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\media\Entity\MediaType;
|
||||
|
||||
/**
|
||||
* Tests the file media source.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaSourceFileTest extends MediaFunctionalTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// We need to test without any default configuration in place.
|
||||
// @TODO: Remove this as part of https://www.drupal.org/node/2883813.
|
||||
MediaType::load('file')->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that it's possible to change the allowed file extensions.
|
||||
*/
|
||||
public function testSourceFieldSettingsEditing() {
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$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']);
|
||||
|
||||
$this->drupalGet("admin/structure/media/manage/$media_type_id/fields/media.$media_type_id.field_media_file");
|
||||
|
||||
// File extension field exists.
|
||||
$assert_session->fieldExists('Allowed file extensions');
|
||||
|
||||
// Add another extension.
|
||||
$page->fillField('settings[file_extensions]', 'txt, doc, docx, pdf, odt');
|
||||
|
||||
$page->pressButton('Save settings');
|
||||
$this->drupalGet("admin/structure/media/manage/$media_type_id/fields/media.$media_type_id.field_media_file");
|
||||
|
||||
// Verify that new extension is present.
|
||||
$assert_session->fieldValueEquals('settings[file_extensions]', 'txt, doc, docx, pdf, odt');
|
||||
$this->assertSame('txt doc docx pdf odt', FieldConfig::load("media.$media_type_id.field_media_file")->get('settings')['file_extensions']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure source field deletion is not possible.
|
||||
*/
|
||||
public function testPreventSourceFieldDeletion() {
|
||||
$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");
|
||||
$this->assertSession()->statusCodeEquals(403);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Functional;
|
||||
|
||||
use Drupal\media\Entity\Media;
|
||||
|
||||
/**
|
||||
* Tests media template suggestions.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaTemplateSuggestionsTest extends MediaFunctionalTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['media'];
|
||||
|
||||
/**
|
||||
* Tests template suggestions from media_theme_suggestions_media().
|
||||
*/
|
||||
public function testMediaThemeHookSuggestions() {
|
||||
$media_type = $this->createMediaType([
|
||||
'new_revision' => FALSE,
|
||||
'queue_thumbnail_downloads' => FALSE,
|
||||
]);
|
||||
|
||||
// Create media item to be rendered.
|
||||
$media = Media::create([
|
||||
'bundle' => $media_type->id(),
|
||||
'name' => 'Unnamed',
|
||||
]);
|
||||
$media->save();
|
||||
$view_mode = 'full';
|
||||
|
||||
// Simulate theming of the media item.
|
||||
$build = \Drupal::entityTypeManager()->getViewBuilder('media')->view($media, $view_mode);
|
||||
|
||||
$variables['elements'] = $build;
|
||||
$suggestions = \Drupal::moduleHandler()->invokeAll('theme_suggestions_media', [$variables]);
|
||||
$this->assertEquals($suggestions, ['media__full', 'media__' . $media_type->id(), 'media__' . $media_type->id() . '__full'], 'Found expected media suggestions.');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Functional;
|
||||
|
||||
use Drupal\media\Entity\Media;
|
||||
use Drupal\media\Entity\MediaType;
|
||||
|
||||
/**
|
||||
* Ensures that media UI works correctly.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaUiFunctionalTest extends MediaFunctionalTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = [
|
||||
'block',
|
||||
'media_test_source',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->drupalPlaceBlock('local_actions_block');
|
||||
$this->drupalPlaceBlock('local_tasks_block');
|
||||
|
||||
// We need to test without any default configuration in place.
|
||||
// @TODO: Remove this when https://www.drupal.org/node/2883813 lands.
|
||||
MediaType::load('file')->delete();
|
||||
MediaType::load('image')->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the media actions (add/edit/delete).
|
||||
*/
|
||||
public function testMediaWithOnlyOneMediaType() {
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$media_type = $this->createMediaType([
|
||||
'new_revision' => FALSE,
|
||||
'queue_thumbnail_downloads' => FALSE,
|
||||
]);
|
||||
|
||||
$this->drupalGet('media/add');
|
||||
$assert_session->statusCodeEquals(200);
|
||||
$assert_session->addressEquals('media/add/' . $media_type->id());
|
||||
$assert_session->elementNotExists('css', '#edit-revision');
|
||||
|
||||
// Tests media add form.
|
||||
$media_name = $this->randomMachineName();
|
||||
$page->fillField('name[0][value]', $media_name);
|
||||
$revision_log_message = $this->randomString();
|
||||
$page->fillField('revision_log_message[0][value]', $revision_log_message);
|
||||
$page->pressButton('Save');
|
||||
$media_id = $this->container->get('entity.query')->get('media')->execute();
|
||||
$media_id = reset($media_id);
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = $this->container->get('entity_type.manager')
|
||||
->getStorage('media')
|
||||
->loadUnchanged($media_id);
|
||||
$this->assertEquals($media->getRevisionLogMessage(), $revision_log_message);
|
||||
$this->assertEquals($media->getName(), $media_name);
|
||||
$assert_session->titleEquals($media_name . ' | Drupal');
|
||||
|
||||
// Tests media edit form.
|
||||
$media_type->setNewRevision(FALSE);
|
||||
$media_type->save();
|
||||
$media_name2 = $this->randomMachineName();
|
||||
$this->drupalGet('media/' . $media_id . '/edit');
|
||||
$assert_session->checkboxNotChecked('edit-revision');
|
||||
$media_name = $this->randomMachineName();
|
||||
$page->fillField('name[0][value]', $media_name2);
|
||||
$page->pressButton('Save');
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = $this->container->get('entity_type.manager')
|
||||
->getStorage('media')
|
||||
->loadUnchanged($media_id);
|
||||
$this->assertEquals($media->getName(), $media_name2);
|
||||
$assert_session->titleEquals($media_name2 . ' | Drupal');
|
||||
|
||||
// Test that there is no empty vertical tabs element, if the container is
|
||||
// empty (see #2750697).
|
||||
// Make the "Publisher ID" and "Created" fields hidden.
|
||||
$this->drupalGet('/admin/structure/media/manage/' . $media_type->id() . '/form-display');
|
||||
$page->selectFieldOption('fields[created][parent]', 'hidden');
|
||||
$page->selectFieldOption('fields[uid][parent]', 'hidden');
|
||||
$page->pressButton('Save');
|
||||
// Assure we are testing with a user without permission to manage revisions.
|
||||
$this->drupalLogin($this->nonAdminUser);
|
||||
// Check the container is not present.
|
||||
$this->drupalGet('media/' . $media_id . '/edit');
|
||||
$assert_session->elementNotExists('css', 'input.vertical-tabs__active-tab');
|
||||
// Continue testing as admin.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Enable revisions by default.
|
||||
$previous_revision_id = $media->getRevisionId();
|
||||
$media_type->setNewRevision(TRUE);
|
||||
$media_type->save();
|
||||
$this->drupalGet('media/' . $media_id . '/edit');
|
||||
$assert_session->checkboxChecked('edit-revision');
|
||||
$page->fillField('name[0][value]', $media_name);
|
||||
$page->fillField('revision_log_message[0][value]', $revision_log_message);
|
||||
$page->pressButton('Save');
|
||||
$assert_session->titleEquals($media_name . ' | Drupal');
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = $this->container->get('entity_type.manager')
|
||||
->getStorage('media')
|
||||
->loadUnchanged($media_id);
|
||||
$this->assertEquals($media->getRevisionLogMessage(), $revision_log_message);
|
||||
$this->assertNotEquals($previous_revision_id, $media->getRevisionId());
|
||||
|
||||
// Test the status checkbox.
|
||||
$this->drupalGet('media/' . $media_id . '/edit');
|
||||
$page->uncheckField('status[value]');
|
||||
$page->pressButton('Save');
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = $this->container->get('entity_type.manager')
|
||||
->getStorage('media')
|
||||
->loadUnchanged($media_id);
|
||||
$this->assertFalse($media->isPublished());
|
||||
|
||||
// Tests media delete form.
|
||||
$this->drupalGet('media/' . $media_id . '/edit');
|
||||
$page->clickLink('Delete');
|
||||
$assert_session->pageTextContains('This action cannot be undone');
|
||||
$page->pressButton('Delete');
|
||||
$media_id = \Drupal::entityQuery('media')->execute();
|
||||
$this->assertFalse($media_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the "media/add" and "media/mid" pages.
|
||||
*
|
||||
* Tests if the "media/add" page gives you a selecting option if there are
|
||||
* multiple media types available.
|
||||
*/
|
||||
public function testMediaWithMultipleMediaTypes() {
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
// Tests and creates the first media type.
|
||||
$first_media_type = $this->createMediaType(['description' => $this->randomMachineName(32)]);
|
||||
|
||||
// Test and create a second media type.
|
||||
$second_media_type = $this->createMediaType(['description' => $this->randomMachineName(32)]);
|
||||
|
||||
// Test if media/add displays two media type options.
|
||||
$this->drupalGet('media/add');
|
||||
|
||||
// Checks for the first media type.
|
||||
$assert_session->pageTextContains($first_media_type->label());
|
||||
$assert_session->pageTextContains($first_media_type->getDescription());
|
||||
// Checks for the second media type.
|
||||
$assert_session->pageTextContains($second_media_type->label());
|
||||
$assert_session->pageTextContains($second_media_type->getDescription());
|
||||
|
||||
// Continue testing media type filter.
|
||||
$first_media_item = Media::create(['bundle' => $first_media_type->id()]);
|
||||
$first_media_item->save();
|
||||
$second_media_item = Media::create(['bundle' => $second_media_type->id()]);
|
||||
$second_media_item->save();
|
||||
|
||||
// Go to first media item.
|
||||
$this->drupalGet('media/' . $first_media_item->id());
|
||||
$assert_session->statusCodeEquals(200);
|
||||
$assert_session->pageTextContains($first_media_item->getName());
|
||||
|
||||
// Go to second media item.
|
||||
$this->drupalGet('media/' . $second_media_item->id());
|
||||
$assert_session->statusCodeEquals(200);
|
||||
$assert_session->pageTextContains($second_media_item->getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\FunctionalJavascript;
|
||||
|
||||
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
|
||||
use Drupal\Tests\media\Functional\MediaFunctionalTestCreateMediaTypeTrait;
|
||||
use Drupal\Tests\media\Functional\MediaFunctionalTestTrait;
|
||||
|
||||
/**
|
||||
* Base class for Media functional JavaScript tests.
|
||||
*/
|
||||
abstract class MediaJavascriptTestBase extends JavascriptTestBase {
|
||||
|
||||
use MediaFunctionalTestTrait;
|
||||
use MediaFunctionalTestCreateMediaTypeTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = [
|
||||
'system',
|
||||
'node',
|
||||
'field_ui',
|
||||
'views_ui',
|
||||
'media',
|
||||
'media_test_source',
|
||||
];
|
||||
|
||||
/**
|
||||
* Waits and asserts that a given element is visible.
|
||||
*
|
||||
* @param string $selector
|
||||
* The CSS selector.
|
||||
* @param int $timeout
|
||||
* (Optional) Timeout in milliseconds, defaults to 1000.
|
||||
* @param string $message
|
||||
* (Optional) Message to pass to assertJsCondition().
|
||||
*/
|
||||
protected function waitUntilVisible($selector, $timeout = 1000, $message = '') {
|
||||
$condition = "jQuery('" . $selector . ":visible').length > 0";
|
||||
$this->assertJsCondition($condition, $timeout, $message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\FunctionalJavascript;
|
||||
|
||||
use Drupal\media\Entity\Media;
|
||||
use Drupal\media\Entity\MediaType;
|
||||
|
||||
/**
|
||||
* Tests the file media source.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaSourceFileTest extends MediaSourceTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// We need to test without any default configuration in place.
|
||||
// @TODO: Remove this as part of https://www.drupal.org/node/2883813.
|
||||
MediaType::load('file')->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the file media source.
|
||||
*/
|
||||
public function testMediaFileSource() {
|
||||
$media_type_id = 'test_media_file_type';
|
||||
$source_field_id = 'field_media_file';
|
||||
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$this->doTestCreateMediaType($media_type_id, 'file');
|
||||
|
||||
// Hide the name field widget to test default name generation.
|
||||
$this->hideMediaTypeFieldWidget('name', $media_type_id);
|
||||
|
||||
$test_filename = $this->randomMachineName() . '.txt';
|
||||
$test_filepath = 'public://' . $test_filename;
|
||||
file_put_contents($test_filepath, $this->randomMachineName());
|
||||
|
||||
// Create a media item.
|
||||
$this->drupalGet("media/add/{$media_type_id}");
|
||||
$page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath));
|
||||
$result = $assert_session->waitForButton('Remove');
|
||||
$this->assertNotEmpty($result);
|
||||
$page->pressButton('Save');
|
||||
|
||||
$assert_session->addressEquals('media/1');
|
||||
|
||||
// Make sure the thumbnail is displayed.
|
||||
$assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'generic.png');
|
||||
|
||||
// Make sure checkbox changes the visibility of log message field.
|
||||
$this->drupalGet("media/1/edit");
|
||||
$page->uncheckField('revision');
|
||||
$assert_session->elementAttributeContains('css', '.field--name-revision-log-message', 'style', 'display: none');
|
||||
$page->checkField('revision');
|
||||
$assert_session->elementAttributeNotContains('css', '.field--name-revision-log-message', 'style', 'display');
|
||||
|
||||
// Load the media and check if the label was properly populated.
|
||||
$media = Media::load(1);
|
||||
$this->assertEquals($test_filename, $media->getName());
|
||||
|
||||
// Test the MIME type icon.
|
||||
$icon_base = \Drupal::config('media.settings')->get('icon_base_uri');
|
||||
file_unmanaged_copy($icon_base . '/generic.png', $icon_base . '/text--plain.png');
|
||||
$this->drupalGet("media/add/{$media_type_id}");
|
||||
$page->attachFileToField("files[{$source_field_id}_0]", \Drupal::service('file_system')->realpath($test_filepath));
|
||||
$result = $assert_session->waitForButton('Remove');
|
||||
$this->assertNotEmpty($result);
|
||||
$page->pressButton('Save');
|
||||
$assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'text--plain.png');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\FunctionalJavascript;
|
||||
|
||||
use Drupal\media\Entity\Media;
|
||||
use Drupal\media\Entity\MediaType;
|
||||
use Drupal\media\Plugin\media\Source\Image;
|
||||
|
||||
/**
|
||||
* Tests the image media source.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaSourceImageTest extends MediaSourceTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// We need to test without any default configuration in place.
|
||||
// @TODO: Remove this as part of https://www.drupal.org/node/2883813.
|
||||
MediaType::load('file')->delete();
|
||||
MediaType::load('image')->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the image media source.
|
||||
*/
|
||||
public function testMediaImageSource() {
|
||||
$media_type_id = 'test_media_image_type';
|
||||
$source_field_id = 'field_media_image';
|
||||
$provided_fields = [
|
||||
Image::METADATA_ATTRIBUTE_WIDTH,
|
||||
Image::METADATA_ATTRIBUTE_HEIGHT,
|
||||
];
|
||||
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$this->doTestCreateMediaType($media_type_id, 'image', $provided_fields);
|
||||
|
||||
// Create custom fields for the media type to store metadata attributes.
|
||||
$fields = [
|
||||
'field_string_width' => 'string',
|
||||
'field_string_height' => 'string',
|
||||
];
|
||||
$this->createMediaTypeFields($fields, $media_type_id);
|
||||
|
||||
// Hide the name field widget to test default name generation.
|
||||
$this->hideMediaTypeFieldWidget('name', $media_type_id);
|
||||
|
||||
$this->drupalGet("admin/structure/media/manage/{$media_type_id}");
|
||||
$page->selectFieldOption("field_map[" . Image::METADATA_ATTRIBUTE_WIDTH . "]", 'field_string_width');
|
||||
$page->selectFieldOption("field_map[" . Image::METADATA_ATTRIBUTE_HEIGHT . "]", 'field_string_height');
|
||||
$page->pressButton('Save');
|
||||
|
||||
// Create a media item.
|
||||
$this->drupalGet("media/add/{$media_type_id}");
|
||||
$page->attachFileToField("files[{$source_field_id}_0]", \Drupal::root() . '/core/modules/media/tests/fixtures/example_1.jpeg');
|
||||
$result = $assert_session->waitForButton('Remove');
|
||||
$this->assertNotEmpty($result);
|
||||
$page->fillField("{$source_field_id}[0][alt]", 'Image Alt Text 1');
|
||||
$page->pressButton('Save');
|
||||
|
||||
$assert_session->addressEquals('media/1');
|
||||
|
||||
// Make sure the thumbnail is displayed from uploaded image.
|
||||
$assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'example_1.jpeg');
|
||||
|
||||
// Load the media and check that all fields are properly populated.
|
||||
$media = Media::load(1);
|
||||
$this->assertEquals('example_1.jpeg', $media->getName());
|
||||
$this->assertEquals('200', $media->get('field_string_width')->value);
|
||||
$this->assertEquals('89', $media->get('field_string_height')->value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\FunctionalJavascript;
|
||||
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\media\Entity\MediaType;
|
||||
|
||||
/**
|
||||
* Base class for media source tests.
|
||||
*/
|
||||
abstract class MediaSourceTestBase extends MediaJavascriptTestBase {
|
||||
|
||||
/**
|
||||
* Creates storage and field instance, attached to a given media type.
|
||||
*
|
||||
* @param string $field_name
|
||||
* The field name.
|
||||
* @param string $field_type
|
||||
* The field type.
|
||||
* @param string $media_type_id
|
||||
* The media type config entity ID.
|
||||
*/
|
||||
protected function createMediaTypeField($field_name, $field_type, $media_type_id) {
|
||||
$storage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'media',
|
||||
'type' => $field_type,
|
||||
]);
|
||||
$storage->save();
|
||||
|
||||
FieldConfig::create([
|
||||
'field_storage' => $storage,
|
||||
'bundle' => $media_type_id,
|
||||
])->save();
|
||||
|
||||
// Make the field widget visible in the form display.
|
||||
$component = \Drupal::service('plugin.manager.field.widget')
|
||||
->prepareConfiguration($field_type, []);
|
||||
|
||||
// @todo Replace entity_get_form_display() when #2367933 is done.
|
||||
// https://www.drupal.org/node/2872159.
|
||||
$entity_form_display = entity_get_form_display('media', $media_type_id, 'default');
|
||||
$entity_form_display->setComponent($field_name, $component)
|
||||
->save();
|
||||
|
||||
// Use the default formatter and settings.
|
||||
$component = \Drupal::service('plugin.manager.field.formatter')
|
||||
->prepareConfiguration($field_type, []);
|
||||
|
||||
// @todo Replace entity_get_display() when #2367933 is done.
|
||||
// https://www.drupal.org/node/2872159.
|
||||
$entity_display = entity_get_display('media', $media_type_id, 'default');
|
||||
$entity_display->setComponent($field_name, $component)
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a set of fields in a media type.
|
||||
*
|
||||
* @param array $fields
|
||||
* An associative array where keys are field names and values field types.
|
||||
* @param string $media_type_id
|
||||
* The media type config entity ID.
|
||||
*/
|
||||
protected function createMediaTypeFields(array $fields, $media_type_id) {
|
||||
foreach ($fields as $field_name => $field_type) {
|
||||
$this->createMediaTypeField($field_name, $field_type, $media_type_id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hides a widget in the default form display config.
|
||||
*
|
||||
* @param string $field_name
|
||||
* The field name.
|
||||
* @param string $media_type_id
|
||||
* The media type config entity ID.
|
||||
*/
|
||||
protected function hideMediaTypeFieldWidget($field_name, $media_type_id) {
|
||||
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */
|
||||
$entity_form_display = entity_get_form_display('media', $media_type_id, 'default');
|
||||
if ($entity_form_display->getComponent($field_name)) {
|
||||
$entity_form_display->removeComponent($field_name)->save();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test generic media type creation.
|
||||
*
|
||||
* @param string $media_type_id
|
||||
* The media type config entity ID.
|
||||
* @param string $source_id
|
||||
* The media source ID.
|
||||
* @param array $provided_fields
|
||||
* (optional) An array of field machine names this type provides.
|
||||
*
|
||||
* @return \Drupal\media\MediaTypeInterface
|
||||
* The created media type.
|
||||
*/
|
||||
public function doTestCreateMediaType($media_type_id, $source_id, array $provided_fields = []) {
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$this->drupalGet('admin/structure/media/add');
|
||||
$page->fillField('label', $media_type_id);
|
||||
$this->getSession()
|
||||
->wait(5000, "jQuery('.machine-name-value').text() === '{$media_type_id}'");
|
||||
|
||||
// Make sure the source is available.
|
||||
$assert_session->fieldExists('Media source');
|
||||
$assert_session->optionExists('Media source', $source_id);
|
||||
$page->selectFieldOption('Media source', $source_id);
|
||||
$result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
|
||||
$this->assertNotEmpty($result);
|
||||
|
||||
// Make sure the provided fields are visible on the form.
|
||||
foreach ($provided_fields as $provided_field) {
|
||||
$result = $assert_session->waitForElementVisible('css', 'select[name="field_map[' . $provided_field . ']"]');
|
||||
$this->assertNotEmpty($result);
|
||||
}
|
||||
|
||||
// Save the form to create the type.
|
||||
$page->pressButton('Save');
|
||||
$assert_session->pageTextContains('The media type ' . $media_type_id . ' has been added.');
|
||||
$this->drupalGet('admin/structure/media');
|
||||
$assert_session->pageTextContains($media_type_id);
|
||||
|
||||
// Bundle definitions are statically cached in the context of the test, we
|
||||
// need to make sure we have updated information before proceeding with the
|
||||
// actions on the UI.
|
||||
\Drupal::service('entity_type.bundle.info')->clearCachedBundles();
|
||||
|
||||
return MediaType::load($media_type_id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\FunctionalJavascript;
|
||||
|
||||
/**
|
||||
* Tests the media type creation.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaTypeCreationTest extends MediaJavascriptTestBase {
|
||||
|
||||
/**
|
||||
* Tests the media type creation form.
|
||||
*/
|
||||
public function testMediaTypeCreationFormWithDefaultField() {
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$label = 'Type with Default Field';
|
||||
$mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
|
||||
|
||||
$this->drupalGet('admin/structure/media/add');
|
||||
|
||||
// Fill in a label to the media type.
|
||||
$page->fillField('label', $label);
|
||||
// Wait for machine name generation. Default: waitUntilVisible(), does not
|
||||
// work properly.
|
||||
$session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
|
||||
|
||||
// Select the media source used by our media type.
|
||||
$assert_session->fieldExists('Media source');
|
||||
$assert_session->optionExists('Media source', 'test');
|
||||
$page->selectFieldOption('Media source', 'test');
|
||||
$result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
|
||||
$this->assertNotEmpty($result);
|
||||
|
||||
$page->pressButton('Save');
|
||||
|
||||
// Check whether the source field was correctly created.
|
||||
$this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
|
||||
|
||||
// Check 2nd column of first data row, to be machine name for field name.
|
||||
$assert_session->elementContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[2]', 'field_media_test');
|
||||
// Check 3rd column of first data row, to be correct field type.
|
||||
$assert_session->elementTextContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[3]', 'Text (plain)');
|
||||
|
||||
// Check that the source field is correctly assigned to media type.
|
||||
$this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");
|
||||
|
||||
$assert_session->pageTextContains('Test source field is used to store the essential information about the media item.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test creation of media type, reusing an existing source field.
|
||||
*/
|
||||
public function testMediaTypeCreationReuseSourceField() {
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
// Create a new media type, which should create a new field we can reuse.
|
||||
$this->drupalGet('/admin/structure/media/add');
|
||||
$page->fillField('label', 'Pastafazoul');
|
||||
$session->wait(5000, "jQuery('.machine-name-value').text() === 'pastafazoul'");
|
||||
$page->selectFieldOption('Media source', 'test');
|
||||
$result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
|
||||
$this->assertNotEmpty($result);
|
||||
$page->pressButton('Save');
|
||||
|
||||
$label = 'Type reusing Default Field';
|
||||
$mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
|
||||
|
||||
$this->drupalGet('admin/structure/media/add');
|
||||
|
||||
// Fill in a label to the media type.
|
||||
$page->fillField('label', $label);
|
||||
|
||||
// Wait for machine name generation. Default: waitUntilVisible(), does not
|
||||
// work properly.
|
||||
$session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
|
||||
|
||||
// Select the media source used by our media type.
|
||||
$assert_session->fieldExists('Media source');
|
||||
$assert_session->optionExists('Media source', 'test');
|
||||
$page->selectFieldOption('Media source', 'test');
|
||||
$result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
|
||||
$this->assertNotEmpty($result);
|
||||
// Select the existing field for re-use.
|
||||
$page->selectFieldOption('source_configuration[source_field]', 'field_media_test');
|
||||
$page->pressButton('Save');
|
||||
|
||||
// Check that no new fields were created.
|
||||
$this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
|
||||
// The reused field should be present...
|
||||
$assert_session->pageTextContains('field_media_test');
|
||||
// ...not a new, unique one.
|
||||
$assert_session->pageTextNotContains('field_media_test_1');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\FunctionalJavascript;
|
||||
|
||||
use Drupal\field\FieldConfigInterface;
|
||||
use Drupal\media\Entity\Media;
|
||||
use Drupal\media\Entity\MediaType;
|
||||
use Drupal\media\MediaSourceInterface;
|
||||
|
||||
/**
|
||||
* Ensures that media UI works correctly.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaUiJavascriptTest extends MediaJavascriptTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = [
|
||||
'block',
|
||||
'media_test_source',
|
||||
];
|
||||
|
||||
/**
|
||||
* The test media type.
|
||||
*
|
||||
* @var \Drupal\media\MediaTypeInterface
|
||||
*/
|
||||
protected $testMediaType;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->drupalPlaceBlock('local_actions_block');
|
||||
$this->drupalPlaceBlock('local_tasks_block');
|
||||
|
||||
// We need to test without any default configuration in place.
|
||||
// @TODO: Remove this as part of https://www.drupal.org/node/2883813.
|
||||
MediaType::load('file')->delete();
|
||||
MediaType::load('image')->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests a media type administration.
|
||||
*/
|
||||
public function testMediaTypes() {
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$this->drupalGet('admin/structure/media');
|
||||
$assert_session->pageTextContains('No media types available. Add media type.');
|
||||
$assert_session->linkExists('Add media type');
|
||||
|
||||
// Test the creation of a media type using the UI.
|
||||
$name = $this->randomMachineName();
|
||||
$description = $this->randomMachineName();
|
||||
$this->drupalGet('admin/structure/media/add');
|
||||
$page->fillField('label', $name);
|
||||
$machine_name = strtolower($name);
|
||||
$this->assertJsCondition("jQuery('.machine-name-value').html() == '$machine_name'");
|
||||
$page->selectFieldOption('source', 'test');
|
||||
$this->assertJsCondition("jQuery('.form-item-source-configuration-test-config-value').length > 0;");
|
||||
$page->fillField('description', $description);
|
||||
$page->pressButton('Save');
|
||||
$assert_session->pageTextContains('The media type ' . $name . ' has been added.');
|
||||
$this->drupalGet('admin/structure/media');
|
||||
$assert_session->pageTextContains($name);
|
||||
$assert_session->pageTextContains($description);
|
||||
|
||||
// We need to clear the statically cached field definitions to account for
|
||||
// fields that have been created by API calls in this test, since they exist
|
||||
// in a separate memory space from the web server.
|
||||
$this->container->get('entity_field.manager')->clearCachedFieldDefinitions();
|
||||
// Assert that the field and field storage were created.
|
||||
$media_type = MediaType::load($machine_name);
|
||||
$source = $media_type->getSource();
|
||||
/** @var \Drupal\field\FieldConfigInterface $source_field */
|
||||
$source_field = $source->getSourceFieldDefinition($media_type);
|
||||
$this->assertInstanceOf(FieldConfigInterface::class, $source_field, 'Source field exists.');
|
||||
$this->assertFalse($source_field->isNew(), 'Source field was saved.');
|
||||
/** @var \Drupal\field\FieldStorageConfigInterface $storage */
|
||||
$storage = $source_field->getFieldStorageDefinition();
|
||||
$this->assertFalse($storage->isNew(), 'Source field storage definition was saved.');
|
||||
$this->assertFalse($storage->isLocked(), 'Source field storage definition was not locked.');
|
||||
|
||||
/** @var \Drupal\media\MediaTypeInterface $media_type_storage */
|
||||
$media_type_storage = $this->container->get('entity_type.manager')->getStorage('media_type');
|
||||
$this->testMediaType = $media_type_storage->load(strtolower($name));
|
||||
|
||||
// Check if all action links exist.
|
||||
$assert_session->linkByHrefExists('admin/structure/media/add');
|
||||
$assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id());
|
||||
$assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/fields');
|
||||
$assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/form-display');
|
||||
$assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/display');
|
||||
|
||||
// Assert that fields have expected values before editing.
|
||||
$page->clickLink('Edit');
|
||||
$assert_session->fieldValueEquals('label', $name);
|
||||
$assert_session->fieldValueEquals('description', $description);
|
||||
$assert_session->fieldValueEquals('source', 'test');
|
||||
$assert_session->fieldValueEquals('label', $name);
|
||||
$assert_session->checkboxNotChecked('edit-options-new-revision');
|
||||
$assert_session->checkboxChecked('edit-options-status');
|
||||
$assert_session->checkboxNotChecked('edit-options-queue-thumbnail-downloads');
|
||||
$assert_session->pageTextContains('Create new revision');
|
||||
$assert_session->pageTextContains('Automatically create new revisions. Users with the "Administer media" permission will be able to override this option.');
|
||||
$assert_session->pageTextContains('Download thumbnails via a queue.');
|
||||
$assert_session->pageTextContains('Media will be automatically published when created.');
|
||||
$assert_session->pageTextContains('Media sources can provide metadata fields such as title, caption, size information, credits, etc. Media can automatically save this metadata information to entity fields, which can be configured below. Information will only be mapped if the entity field is empty.');
|
||||
|
||||
// Try to change media type and check if new configuration sub-form appears.
|
||||
$page->selectFieldOption('source', 'test');
|
||||
$result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
|
||||
$this->assertNotEmpty($result);
|
||||
$assert_session->fieldExists('Test config value');
|
||||
$assert_session->fieldValueEquals('Test config value', 'This is default value.');
|
||||
$assert_session->fieldExists('Attribute 1');
|
||||
$assert_session->fieldExists('Attribute 2');
|
||||
|
||||
// Test if the edit machine name is not editable.
|
||||
$assert_session->fieldDisabled('Machine-readable name');
|
||||
|
||||
// Edit and save media type form fields with new values.
|
||||
$new_name = $this->randomMachineName();
|
||||
$new_description = $this->randomMachineName();
|
||||
$page->fillField('label', $new_name);
|
||||
$page->fillField('description', $new_description);
|
||||
$page->selectFieldOption('source', 'test');
|
||||
$page->fillField('Test config value', 'This is new config value.');
|
||||
$page->selectFieldOption('field_map[attribute_1]', 'name');
|
||||
$page->checkField('options[new_revision]');
|
||||
$page->uncheckField('options[status]');
|
||||
$page->checkField('options[queue_thumbnail_downloads]');
|
||||
$page->pressButton('Save');
|
||||
$assert_session->pageTextContains("The media type $new_name has been updated.");
|
||||
|
||||
// Test if edit worked and if new field values have been saved as expected.
|
||||
$this->drupalGet('admin/structure/media/manage/' . $this->testMediaType->id());
|
||||
$assert_session->fieldValueEquals('label', $new_name);
|
||||
$assert_session->fieldValueEquals('description', $new_description);
|
||||
$assert_session->fieldValueEquals('source', 'test');
|
||||
$assert_session->checkboxChecked('options[new_revision]');
|
||||
$assert_session->checkboxNotChecked('options[status]');
|
||||
$assert_session->checkboxChecked('options[queue_thumbnail_downloads]');
|
||||
$assert_session->fieldValueEquals('Test config value', 'This is new config value.');
|
||||
$assert_session->fieldValueEquals('Attribute 1', 'name');
|
||||
$assert_session->fieldValueEquals('Attribute 2', MediaSourceInterface::METADATA_FIELD_EMPTY);
|
||||
|
||||
/** @var \Drupal\media\MediaTypeInterface $loaded_media_type */
|
||||
$loaded_media_type = $this->container->get('entity_type.manager')
|
||||
->getStorage('media_type')
|
||||
->load($this->testMediaType->id());
|
||||
$this->assertEquals($loaded_media_type->id(), $this->testMediaType->id());
|
||||
$this->assertEquals($loaded_media_type->label(), $new_name);
|
||||
$this->assertEquals($loaded_media_type->getDescription(), $new_description);
|
||||
$this->assertEquals($loaded_media_type->getSource()->getPluginId(), 'test');
|
||||
$this->assertEquals($loaded_media_type->getSource()->getConfiguration()['test_config_value'], 'This is new config value.');
|
||||
$this->assertTrue($loaded_media_type->shouldCreateNewRevision());
|
||||
$this->assertTrue($loaded_media_type->thumbnailDownloadsAreQueued());
|
||||
$this->assertFalse($loaded_media_type->getStatus());
|
||||
$this->assertEquals($loaded_media_type->getFieldMap(), ['attribute_1' => 'name']);
|
||||
|
||||
// We need to clear the statically cached field definitions to account for
|
||||
// fields that have been created by API calls in this test, since they exist
|
||||
// in a separate memory space from the web server.
|
||||
$this->container->get('entity_field.manager')->clearCachedFieldDefinitions();
|
||||
|
||||
// Test that a media item being created with default status to "FALSE",
|
||||
// will be created unpublished.
|
||||
/** @var \Drupal\media\MediaInterface $unpublished_media */
|
||||
$unpublished_media = Media::create(['name' => 'unpublished test media', 'bundle' => $loaded_media_type->id()]);
|
||||
$this->assertFalse($unpublished_media->isPublished());
|
||||
$unpublished_media->delete();
|
||||
|
||||
// Tests media type delete form.
|
||||
$page->clickLink('Delete');
|
||||
$assert_session->addressEquals('admin/structure/media/manage/' . $this->testMediaType->id() . '/delete');
|
||||
$page->pressButton('Delete');
|
||||
$assert_session->addressEquals('admin/structure/media');
|
||||
$assert_session->pageTextContains('The media type ' . $new_name . ' has been deleted.');
|
||||
|
||||
// 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();
|
||||
$label2 = $media_type2->label();
|
||||
$media = Media::create(['name' => 'lorem ipsum', 'bundle' => $media_type2->id()]);
|
||||
$media->save();
|
||||
$this->drupalGet('admin/structure/media/manage/' . $media_type2->id());
|
||||
$page->clickLink('Delete');
|
||||
$assert_session->addressEquals('admin/structure/media/manage/' . $media_type2->id() . '/delete');
|
||||
$assert_session->buttonNotExists('edit-submit');
|
||||
$assert_session->pageTextContains("$label2 is used by 1 media item on your site. You can not remove this media type until you have removed all of the $label2 media items.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\FunctionalJavascript;
|
||||
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
* Tests the media entity type integration into the wizard.
|
||||
*
|
||||
* @group media
|
||||
*
|
||||
* @see \Drupal\media\Plugin\views\wizard\Media
|
||||
* @see \Drupal\media\Plugin\views\wizard\MediaRevision
|
||||
*/
|
||||
class MediaViewsWizardTest extends MediaJavascriptTestBase {
|
||||
|
||||
/**
|
||||
* Tests adding a view of media.
|
||||
*/
|
||||
public function testMediaWizard() {
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$view_id = strtolower($this->randomMachineName(16));
|
||||
$this->drupalGet('admin/structure/views/add');
|
||||
$page->fillField('label', $view_id);
|
||||
$this->waitUntilVisible('.machine-name-value');
|
||||
$page->selectFieldOption('show[wizard_key]', 'media');
|
||||
$result = $assert_session->waitForElementVisible('css', 'select[data-drupal-selector="edit-show-type"]');
|
||||
$this->assertNotEmpty($result);
|
||||
$page->checkField('page[create]');
|
||||
$page->fillField('page[path]', $this->randomMachineName(16));
|
||||
$page->pressButton('Save and edit');
|
||||
$this->assertEquals($session->getCurrentUrl(), $this->baseUrl . '/admin/structure/views/view/' . $view_id);
|
||||
|
||||
$view = Views::getView($view_id);
|
||||
$view->initHandlers();
|
||||
$row = $view->display_handler->getOption('row');
|
||||
$this->assertEquals($row['type'], 'fields');
|
||||
// Check for the default filters.
|
||||
$this->assertEquals($view->filter['status']->table, 'media_field_data');
|
||||
$this->assertEquals($view->filter['status']->field, 'status');
|
||||
$this->assertTrue($view->filter['status']->value);
|
||||
// Check for the default fields.
|
||||
$this->assertEquals($view->field['name']->table, 'media_field_data');
|
||||
$this->assertEquals($view->field['name']->field, 'name');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests adding a view of media revisions.
|
||||
*/
|
||||
public function testMediaRevisionWizard() {
|
||||
$session = $this->getSession();
|
||||
$page = $session->getPage();
|
||||
$assert_session = $this->assertSession();
|
||||
|
||||
$view_id = strtolower($this->randomMachineName(16));
|
||||
$this->drupalGet('admin/structure/views/add');
|
||||
$page->fillField('label', $view_id);
|
||||
$this->waitUntilVisible('.machine-name-value');
|
||||
$page->selectFieldOption('show[wizard_key]', 'media_revision');
|
||||
$assert_session->assertWaitOnAjaxRequest();
|
||||
$page->checkField('page[create]');
|
||||
$page->fillField('page[path]', $this->randomMachineName(16));
|
||||
$page->pressButton('Save and edit');
|
||||
$this->assertEquals($session->getCurrentUrl(), $this->baseUrl . '/admin/structure/views/view/' . $view_id);
|
||||
|
||||
$view = Views::getView($view_id);
|
||||
$view->initHandlers();
|
||||
$row = $view->display_handler->getOption('row');
|
||||
$this->assertEquals($row['type'], 'fields');
|
||||
|
||||
// Check for the default filters.
|
||||
$this->assertEquals($view->filter['status']->table, 'media_field_revision');
|
||||
$this->assertEquals($view->filter['status']->field, 'status');
|
||||
$this->assertTrue($view->filter['status']->value);
|
||||
|
||||
// Check for the default fields.
|
||||
$this->assertEquals($view->field['name']->table, 'media_field_revision');
|
||||
$this->assertEquals($view->field['name']->field, 'name');
|
||||
$this->assertEquals($view->field['changed']->table, 'media_field_revision');
|
||||
$this->assertEquals($view->field['changed']->field, 'changed');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Kernel;
|
||||
|
||||
use Drupal\media\Entity\Media;
|
||||
use Drupal\media\Entity\MediaType;
|
||||
use Drupal\media\MediaInterface;
|
||||
use Drupal\media\MediaTypeInterface;
|
||||
|
||||
/**
|
||||
* Tests creation of media types and media items.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaCreationTest extends MediaKernelTestBase {
|
||||
|
||||
/**
|
||||
* Tests creating a media type programmatically.
|
||||
*/
|
||||
public function testMediaTypeCreation() {
|
||||
$media_type_storage = $this->container->get('entity_type.manager')->getStorage('media_type');
|
||||
|
||||
$this->assertInstanceOf(MediaTypeInterface::class, MediaType::load($this->testMediaType->id()), 'The new media type has not been correctly created in the database.');
|
||||
|
||||
// Test a media type created from default configuration.
|
||||
$this->container->get('module_installer')->install(['media_test_type']);
|
||||
$test_media_type = $media_type_storage->load('test');
|
||||
$this->assertInstanceOf(MediaTypeInterface::class, $test_media_type, 'The media type from default configuration has not been created in the database.');
|
||||
$this->assertEquals('Test type', $test_media_type->get('label'), 'Could not assure the correct type name.');
|
||||
$this->assertEquals('Test type.', $test_media_type->get('description'), 'Could not assure the correct type description.');
|
||||
$this->assertEquals('test', $test_media_type->get('source'), 'Could not assure the correct media source.');
|
||||
// Source field is not set on the media source, but it should never
|
||||
// be created automatically when a config is being imported.
|
||||
$this->assertEquals(['source_field' => '', 'test_config_value' => 'Kakec'], $test_media_type->get('source_configuration'), 'Could not assure the correct media source configuration.');
|
||||
$this->assertEquals(['metadata_attribute' => 'field_attribute_config_test'], $test_media_type->get('field_map'), 'Could not assure the correct field map.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a media item programmatically.
|
||||
*/
|
||||
public function testMediaEntityCreation() {
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testMediaType->id(),
|
||||
'name' => 'Unnamed',
|
||||
'field_media_test' => 'Nation of sheep, ruled by wolves, owned by pigs.',
|
||||
]);
|
||||
$media->save();
|
||||
|
||||
$this->assertNotInstanceOf(MediaInterface::class, Media::load(rand(1000, 9999)), 'Failed asserting a non-existent media.');
|
||||
|
||||
$this->assertInstanceOf(MediaInterface::class, Media::load($media->id()), 'The new media item has not been created in the database.');
|
||||
$this->assertEquals($this->testMediaType->id(), $media->bundle(), 'The media item was not created with the correct type.');
|
||||
$this->assertEquals('Unnamed', $media->getName(), 'The media item was not created with the correct name.');
|
||||
$source_field_name = $media->bundle->entity->getSource()->getSourceFieldDefinition($media->bundle->entity)->getName();
|
||||
$this->assertEquals('Nation of sheep, ruled by wolves, owned by pigs.', $media->get($source_field_name)->value, 'Source returns incorrect source field value.');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Kernel;
|
||||
|
||||
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\user\Entity\User;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
|
||||
/**
|
||||
* Base class for Media kernel tests.
|
||||
*/
|
||||
abstract class MediaKernelTestBase extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = [
|
||||
'media',
|
||||
'media_test_source',
|
||||
'image',
|
||||
'user',
|
||||
'field',
|
||||
'system',
|
||||
'file',
|
||||
];
|
||||
|
||||
/**
|
||||
* The test media type.
|
||||
*
|
||||
* @var \Drupal\media\MediaTypeInterface
|
||||
*/
|
||||
protected $testMediaType;
|
||||
|
||||
/**
|
||||
* The test media type with constraints.
|
||||
*
|
||||
* @var \Drupal\media\MediaTypeInterface
|
||||
*/
|
||||
protected $testConstraintsMediaType;
|
||||
|
||||
/**
|
||||
* A user.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('file');
|
||||
$this->installSchema('file', 'file_usage');
|
||||
$this->installSchema('system', 'sequences');
|
||||
$this->installEntitySchema('media');
|
||||
$this->installConfig(['field', 'system', 'image', 'file', 'media']);
|
||||
|
||||
// Create a test media type.
|
||||
$this->testMediaType = $this->createMediaType('test');
|
||||
// Create a test media type with constraints.
|
||||
$this->testConstraintsMediaType = $this->createMediaType('test_constraints');
|
||||
|
||||
$this->user = User::create([
|
||||
'name' => 'username',
|
||||
'status' => 1,
|
||||
]);
|
||||
$this->user->save();
|
||||
$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 media entity.
|
||||
*
|
||||
* @param string $filename
|
||||
* String filename with extension.
|
||||
* @param \Drupal\media\MediaTypeInterface $media_type
|
||||
* The the media type.
|
||||
*
|
||||
* @return \Drupal\media\Entity\Media
|
||||
* A media entity.
|
||||
*/
|
||||
protected function generateMedia($filename, MediaTypeInterface $media_type) {
|
||||
vfsStream::setup('drupal_root');
|
||||
vfsStream::create([
|
||||
'sites' => [
|
||||
'default' => [
|
||||
'files' => [
|
||||
$filename => str_repeat('a', 3000),
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$file = File::create([
|
||||
'uri' => 'vfs://drupal_root/sites/default/files/' . $filename,
|
||||
'uid' => $this->user->id(),
|
||||
]);
|
||||
$file->setPermanent();
|
||||
$file->save();
|
||||
|
||||
return Media::create([
|
||||
'bundle' => $media_type->id(),
|
||||
'name' => 'Mr. Jones',
|
||||
'field_media_file' => [
|
||||
'target_id' => $file->id(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Kernel;
|
||||
|
||||
use Drupal\media\Entity\Media;
|
||||
|
||||
/**
|
||||
* Tests link relationships for Media entity.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaLinkRelationsTest extends MediaKernelTestBase {
|
||||
|
||||
/**
|
||||
* Tests that all link relationships for Media exist.
|
||||
*/
|
||||
public function testExistLinkRelationships() {
|
||||
/** @var \Drupal\Core\Http\LinkRelationTypeManager $link_relation_type_manager */
|
||||
$link_relation_type_manager = $this->container->get('plugin.manager.link_relation_type');
|
||||
$media = Media::create(['bundle' => $this->testMediaType->id()]);
|
||||
$media->save();
|
||||
foreach ($media->uriRelationships() as $relation_name) {
|
||||
$this->assertTrue($link_relation_type_manager->hasDefinition($relation_name), "Link relationship '{$relation_name}' for Media entity");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Kernel;
|
||||
|
||||
use Drupal\media\Entity\MediaType;
|
||||
|
||||
/**
|
||||
* Tests the file media source.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaSourceFileTest extends MediaKernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// We need to test without any default configuration in place.
|
||||
// @TODO: Remove this as part of https://www.drupal.org/node/2883813.
|
||||
MediaType::load('file')->delete();
|
||||
MediaType::load('image')->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the file extension constraint.
|
||||
*/
|
||||
public function testFileExtensionConstraint() {
|
||||
$mediaType = $this->createMediaType('file');
|
||||
// Create a random file that should fail.
|
||||
$media = $this->generateMedia('test.patch', $mediaType);
|
||||
$result = $media->validate();
|
||||
$this->assertCount(1, $result);
|
||||
$this->assertEquals('field_media_file.0', $result->get(0)->getPropertyPath());
|
||||
$this->assertContains('Only files with the following extensions are allowed:', (string) $result->get(0)->getMessage());
|
||||
|
||||
// Create a random file that should pass.
|
||||
$media = $this->generateMedia('test.txt', $mediaType);
|
||||
$result = $media->validate();
|
||||
$this->assertCount(0, $result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,445 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Kernel;
|
||||
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\media\Entity\Media;
|
||||
use Drupal\media\Entity\MediaType;
|
||||
|
||||
/**
|
||||
* Tests media source plugins related logic.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaSourceTest extends MediaKernelTestBase {
|
||||
|
||||
/**
|
||||
* Tests default media name functionality.
|
||||
*/
|
||||
public function testDefaultName() {
|
||||
// Make sure that the default name is set if not provided by the user.
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = Media::create(['bundle' => $this->testMediaType->id()]);
|
||||
$media_source = $media->getSource();
|
||||
$this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
|
||||
$this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
|
||||
$this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->getName(), 'Default name was not used correctly by getName().');
|
||||
$this->assertEquals($media->getName(), $media->label(), 'Default name and label are not the same.');
|
||||
$media->save();
|
||||
$this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->getName(), 'Default name was not saved correctly.');
|
||||
$this->assertEquals($media->getName(), $media->label(), 'The label changed during save.');
|
||||
|
||||
// Make sure that the user-supplied name is used.
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$name = 'User-supplied name';
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testMediaType->id(),
|
||||
'name' => $name,
|
||||
]);
|
||||
$media_source = $media->getSource();
|
||||
$this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
|
||||
$this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
|
||||
$media->save();
|
||||
$this->assertEquals($name, $media->getName(), 'User-supplied name was not set correctly.');
|
||||
$this->assertEquals($media->getName(), $media->label(), 'The user-supplied name does not match the label.');
|
||||
|
||||
// Change the default name attribute and see if it is used to set the name.
|
||||
$name = 'Old Major';
|
||||
\Drupal::state()->set('media_source_test_attributes', ['alternative_name' => ['title' => 'Alternative name', 'value' => $name]]);
|
||||
\Drupal::state()->set('media_source_test_definition', ['default_name_metadata_attribute' => 'alternative_name']);
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = Media::create(['bundle' => $this->testMediaType->id()]);
|
||||
$media_source = $media->getSource();
|
||||
$this->assertEquals('alternative_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Correct metadata attribute is not used for the default name.');
|
||||
$this->assertEquals($name, $media_source->getMetadata($media, 'alternative_name'), 'Value of the default name metadata attribute does not look correct.');
|
||||
$media->save();
|
||||
$this->assertEquals($name, $media->getName(), 'Default name was not set correctly.');
|
||||
$this->assertEquals($media->getName(), $media->label(), 'The default name does not match the label.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests metadata mapping functionality.
|
||||
*/
|
||||
public function testMetadataMapping() {
|
||||
$field_name = 'field_to_map_to';
|
||||
$attribute_name = 'attribute_to_map';
|
||||
$storage = FieldStorageConfig::create([
|
||||
'entity_type' => 'media',
|
||||
'field_name' => $field_name,
|
||||
'type' => 'string',
|
||||
]);
|
||||
$storage->save();
|
||||
|
||||
FieldConfig::create([
|
||||
'field_storage' => $storage,
|
||||
'bundle' => $this->testMediaType->id(),
|
||||
'label' => 'Field to map to',
|
||||
])->save();
|
||||
|
||||
// Save the entity without defining the metadata mapping and check that the
|
||||
// field stays empty.
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testMediaType->id(),
|
||||
'field_media_test' => 'some_value',
|
||||
]);
|
||||
$media->save();
|
||||
$this->assertEmpty($media->get($field_name)->value, 'Field stayed empty.');
|
||||
|
||||
// Make sure that source plugin returns NULL for non-existing fields.
|
||||
$this->testMediaType->setFieldMap(['not_here_at_all' => $field_name])->save();
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testMediaType->id(),
|
||||
'field_media_test' => 'some_value',
|
||||
]);
|
||||
$media_source = $media->getSource();
|
||||
$this->assertNull($media_source->getMetadata($media, 'not_here_at_all'), 'NULL is not returned if asking for a value of non-existing metadata.');
|
||||
$media->save();
|
||||
$this->assertTrue($media->get($field_name)->isEmpty(), 'Non-existing metadata attribute was wrongly mapped to the field.');
|
||||
|
||||
// Define mapping and make sure that the value was stored in the field.
|
||||
\Drupal::state()->set('media_source_test_attributes', [
|
||||
$attribute_name => ['title' => 'Attribute to map', 'value' => 'Snowball'],
|
||||
]);
|
||||
$this->testMediaType->setFieldMap([$attribute_name => $field_name])->save();
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testMediaType->id(),
|
||||
'field_media_test' => 'some_value',
|
||||
]);
|
||||
$media_source = $media->getSource();
|
||||
$this->assertEquals('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
|
||||
$media->save();
|
||||
$this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
|
||||
|
||||
// Change the metadata attribute value and re-save the entity. Field value
|
||||
// should stay the same.
|
||||
\Drupal::state()->set('media_source_test_attributes', [
|
||||
$attribute_name => ['title' => 'Attribute to map', 'value' => 'Pinkeye'],
|
||||
]);
|
||||
$this->assertEquals('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
|
||||
$media->save();
|
||||
$this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
|
||||
|
||||
// Now change the value of the source field and make sure that the mapped
|
||||
// values update too.
|
||||
$this->assertEquals('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
|
||||
$media->set('field_media_test', 'some_new_value');
|
||||
$media->save();
|
||||
$this->assertEquals('Pinkeye', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
|
||||
|
||||
// Remove the value of the mapped field and make sure that it is re-mapped
|
||||
// on save.
|
||||
\Drupal::state()->set('media_source_test_attributes', [
|
||||
$attribute_name => ['title' => 'Attribute to map', 'value' => 'Snowball'],
|
||||
]);
|
||||
$media->{$field_name}->value = NULL;
|
||||
$this->assertEquals('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
|
||||
$media->save();
|
||||
$this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the thumbnail functionality.
|
||||
*/
|
||||
public function testThumbnail() {
|
||||
file_put_contents('public://thumbnail1.jpg', '');
|
||||
file_put_contents('public://thumbnail2.jpg', '');
|
||||
|
||||
// Save a media entity and make sure thumbnail was added.
|
||||
\Drupal::state()->set('media_source_test_attributes', [
|
||||
'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'],
|
||||
]);
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testMediaType->id(),
|
||||
'name' => 'Mr. Jones',
|
||||
'field_media_test' => 'some_value',
|
||||
]);
|
||||
$media_source = $media->getSource();
|
||||
$this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
|
||||
$media->save();
|
||||
$this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not added to the media entity.');
|
||||
$this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
|
||||
$this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
|
||||
|
||||
// Now change the metadata attribute and make sure that the thumbnail stays
|
||||
// the same.
|
||||
\Drupal::state()->set('media_source_test_attributes', [
|
||||
'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail2.jpg'],
|
||||
]);
|
||||
$this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
|
||||
$media->save();
|
||||
$this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not preserved.');
|
||||
$this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
|
||||
$this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
|
||||
|
||||
// Remove the thumbnail and make sure that it is auto-updated on save.
|
||||
$media->thumbnail->target_id = NULL;
|
||||
$this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
|
||||
$media->save();
|
||||
$this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media entity.');
|
||||
$this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
|
||||
$this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
|
||||
|
||||
// Change the metadata attribute again, change the source field value too
|
||||
// and make sure that the thumbnail updates.
|
||||
\Drupal::state()->set('media_source_test_attributes', [
|
||||
'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'],
|
||||
]);
|
||||
$media->field_media_test->value = 'some_new_value';
|
||||
$this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
|
||||
$media->save();
|
||||
$this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media entity.');
|
||||
$this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
|
||||
$this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
|
||||
|
||||
// Change the thumbnail metadata attribute and make sure that the thumbnail
|
||||
// is set correctly.
|
||||
\Drupal::state()->set('media_source_test_attributes', [
|
||||
'thumbnail_uri' => ['title' => 'Should not be used', 'value' => 'public://thumbnail1.jpg'],
|
||||
'alternative_thumbnail_uri' => ['title' => 'Should be used', 'value' => 'public://thumbnail2.jpg'],
|
||||
]);
|
||||
\Drupal::state()->set('media_source_test_definition', ['thumbnail_uri_metadata_attribute' => 'alternative_thumbnail_uri']);
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testMediaType->id(),
|
||||
'name' => 'Mr. Jones',
|
||||
'field_media_test' => 'some_value',
|
||||
]);
|
||||
$media_source = $media->getSource();
|
||||
$this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
|
||||
$this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'alternative_thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
|
||||
$media->save();
|
||||
$this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'Correct metadata attribute was not used for the thumbnail.');
|
||||
$this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
|
||||
$this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
|
||||
|
||||
// Enable queued thumbnails and make sure that the entity gets the default
|
||||
// thumbnail initially.
|
||||
\Drupal::state()->set('media_source_test_definition', []);
|
||||
\Drupal::state()->set('media_source_test_attributes', [
|
||||
'thumbnail_uri' => ['title' => 'Should not be used', 'value' => 'public://thumbnail1.jpg'],
|
||||
]);
|
||||
$this->testMediaType->setQueueThumbnailDownloadsStatus(TRUE)->save();
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testMediaType->id(),
|
||||
'name' => 'Mr. Jones',
|
||||
'field_media_test' => 'some_value',
|
||||
]);
|
||||
$this->assertEquals('public://thumbnail1.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
|
||||
$media->save();
|
||||
$this->assertEquals('public://media-icons/generic/generic.png', $media->thumbnail->entity->getFileUri(), 'Default thumbnail was not set initially.');
|
||||
$this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
|
||||
$this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
|
||||
|
||||
// Process the queue item and make sure that the thumbnail was updated too.
|
||||
$queue_name = 'media_entity_thumbnail';
|
||||
/** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */
|
||||
$queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance($queue_name);
|
||||
$queue = \Drupal::queue($queue_name);
|
||||
$this->assertEquals(1, $queue->numberOfItems(), 'Item was not added to the queue.');
|
||||
|
||||
$item = $queue->claimItem();
|
||||
$this->assertEquals($media->id(), $item->data['id'], 'Queue item that was created does not belong to the correct entity.');
|
||||
|
||||
$queue_worker->processItem($item->data);
|
||||
$queue->deleteItem($item);
|
||||
$this->assertEquals(0, $queue->numberOfItems(), 'Item was not removed from the queue.');
|
||||
|
||||
$media = Media::load($media->id());
|
||||
$this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not updated by the queue.');
|
||||
$this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
|
||||
$this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
|
||||
|
||||
// Set alt and title metadata attributes and make sure they are used for the
|
||||
// thumbnail.
|
||||
\Drupal::state()->set('media_source_test_definition', [
|
||||
'thumbnail_alt_metadata_attribute' => 'alt',
|
||||
'thumbnail_title_metadata_attribute' => 'title',
|
||||
]);
|
||||
\Drupal::state()->set('media_source_test_attributes', [
|
||||
'alt' => ['title' => 'Alt text', 'value' => 'This will be alt.'],
|
||||
'title' => ['title' => 'Title text', 'value' => 'This will be title.'],
|
||||
]);
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testMediaType->id(),
|
||||
'name' => 'Boxer',
|
||||
'field_media_test' => 'some_value',
|
||||
]);
|
||||
$media->save();
|
||||
$this->assertEquals('Boxer', $media->getName(), 'Correct name was not set on the media entity.');
|
||||
$this->assertEquals('This will be title.', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
|
||||
$this->assertEquals('This will be alt.', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the media entity constraints functionality.
|
||||
*/
|
||||
public function testConstraints() {
|
||||
// Test entity constraints.
|
||||
\Drupal::state()->set('media_source_test_entity_constraints', [
|
||||
'MediaTestConstraint' => [],
|
||||
]);
|
||||
|
||||
// Create a media item media that uses a source plugin with constraints and
|
||||
// make sure the constraints works as expected when validating.
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testConstraintsMediaType->id(),
|
||||
'name' => 'I do not like Drupal',
|
||||
'field_media_test_constraints' => 'Not checked',
|
||||
]);
|
||||
|
||||
// Validate the entity and make sure violation is reported.
|
||||
/** @var \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations */
|
||||
$violations = $media->validate();
|
||||
$this->assertCount(1, $violations, 'Expected number of validations not found.');
|
||||
$this->assertEquals('Inappropriate text.', $violations->get(0)->getMessage(), 'Incorrect constraint validation message found.');
|
||||
|
||||
// Fix the violation and make sure it is not reported anymore.
|
||||
$media->setName('I love Drupal!');
|
||||
$violations = $media->validate();
|
||||
$this->assertCount(0, $violations, 'Expected number of validations not found.');
|
||||
|
||||
// Save and make sure it succeeded.
|
||||
$this->assertEmpty($media->id(), 'Entity ID was found.');
|
||||
$media->save();
|
||||
$this->assertNotEmpty($media->id(), 'Entity ID was not found.');
|
||||
$this->assertSame($media->getName(), 'I love Drupal!');
|
||||
|
||||
// Test source field constraints.
|
||||
\Drupal::state()->set('media_source_test_field_constraints', [
|
||||
'MediaTestConstraint' => [],
|
||||
]);
|
||||
\Drupal::state()->set('media_source_test_entity_constraints', []);
|
||||
|
||||
// Create media that uses source with constraints and make sure it can't
|
||||
// be saved without validating them.
|
||||
/** @var \Drupal\media\MediaInterface $media */
|
||||
$media = Media::create([
|
||||
'bundle' => $this->testConstraintsMediaType->id(),
|
||||
'name' => 'Not checked',
|
||||
'field_media_test_constraints' => 'I do not like Drupal',
|
||||
]);
|
||||
|
||||
// Validate the entity and make sure violation is reported.
|
||||
/** @var \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations */
|
||||
$violations = $media->validate();
|
||||
$this->assertCount(1, $violations, 'Expected number of validations not found.');
|
||||
$this->assertEquals('Inappropriate text.', $violations->get(0)->getMessage(), 'Incorrect constraint validation message found.');
|
||||
|
||||
// Fix the violation and make sure it is not reported anymore.
|
||||
$media->set('field_media_test_constraints', 'I love Drupal!');
|
||||
$violations = $media->validate();
|
||||
$this->assertCount(0, $violations, 'Expected number of validations not found.');
|
||||
|
||||
// Save and make sure it succeeded.
|
||||
$this->assertEmpty($media->id(), 'Entity ID was found.');
|
||||
$media->save();
|
||||
$this->assertNotEmpty($media->id(), 'Entity ID was not found.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests logic related to the automated source field creation.
|
||||
*/
|
||||
public function testSourceFieldCreation() {
|
||||
/** @var \Drupal\media\MediaTypeInterface $type */
|
||||
$type = MediaType::create([
|
||||
'id' => 'test_type',
|
||||
'label' => 'Test type',
|
||||
'source' => 'test',
|
||||
]);
|
||||
|
||||
/** @var \Drupal\field\Entity\FieldConfig $field */
|
||||
$field = $type->getSource()->createSourceField($type);
|
||||
/** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
|
||||
$field_storage = $field->getFieldStorageDefinition();
|
||||
|
||||
// Test field storage.
|
||||
$this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.');
|
||||
$this->assertFalse($field_storage->isLocked(), 'Field storage is not locked.');
|
||||
$this->assertEquals('string', $field_storage->getType(), 'Field is not of correct type.');
|
||||
$this->assertEquals('field_media_test_1', $field_storage->getName(), 'Incorrect field name is used.');
|
||||
$this->assertEquals('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.');
|
||||
|
||||
// Test field.
|
||||
$this->assertTrue($field->isNew(), 'Field is saved automatically.');
|
||||
$this->assertEquals('field_media_test_1', $field->getName(), 'Incorrect field name is used.');
|
||||
$this->assertEquals('string', $field->getType(), 'Field is of incorrect type.');
|
||||
$this->assertTrue($field->isRequired(), 'Field is not required.');
|
||||
$this->assertEquals('Test source', $field->label(), 'Incorrect label is used.');
|
||||
$this->assertEquals('test_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.');
|
||||
|
||||
// Fields should be automatically saved only when creating the media type
|
||||
// using the media type creation form. Make sure that they are not saved
|
||||
// when creating a media type programmatically.
|
||||
// Drupal\Tests\media\FunctionalJavascript\MediaTypeCreationTest is testing
|
||||
// form part of the functionality.
|
||||
$type->save();
|
||||
$storage = FieldStorageConfig::load('media.field_media_test_1');
|
||||
$this->assertNull($storage, 'Field storage was not saved.');
|
||||
$field = FieldConfig::load('media.test_type.field_media_test_1');
|
||||
$this->assertNull($field, 'Field storage was not saved.');
|
||||
|
||||
// Test the plugin with a different default source field type.
|
||||
$type = MediaType::create([
|
||||
'id' => 'test_constraints_type',
|
||||
'label' => 'Test type with constraints',
|
||||
'source' => 'test_constraints',
|
||||
]);
|
||||
$field = $type->getSource()->createSourceField($type);
|
||||
$field_storage = $field->getFieldStorageDefinition();
|
||||
|
||||
// Test field storage.
|
||||
$this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.');
|
||||
$this->assertFalse($field_storage->isLocked(), 'Field storage is not locked.');
|
||||
$this->assertEquals('string_long', $field_storage->getType(), 'Field is of incorrect type.');
|
||||
$this->assertEquals('field_media_test_constraints_1', $field_storage->getName(), 'Incorrect field name is used.');
|
||||
$this->assertEquals('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.');
|
||||
|
||||
// Test field.
|
||||
$this->assertTrue($field->isNew(), 'Field is saved automatically.');
|
||||
$this->assertEquals('field_media_test_constraints_1', $field->getName(), 'Incorrect field name is used.');
|
||||
$this->assertEquals('string_long', $field->getType(), 'Field is of incorrect type.');
|
||||
$this->assertTrue($field->isRequired(), 'Field is not required.');
|
||||
$this->assertEquals('Test source with constraints', $field->label(), 'Incorrect label is used.');
|
||||
$this->assertEquals('test_constraints_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests configuration form submit handler on the base media source plugin.
|
||||
*/
|
||||
public function testSourceConfigurationSubmit() {
|
||||
/** @var \Drupal\media\MediaSourceManager $manager */
|
||||
$manager = $this->container->get('plugin.manager.media.source');
|
||||
$form = [];
|
||||
$form_state = new FormState();
|
||||
$form_state->setValues(['test_config_value' => 'Somewhere over the rainbow.']);
|
||||
|
||||
/** @var \Drupal\media\MediaSourceInterface $source */
|
||||
$source = $manager->createInstance('test', []);
|
||||
$source->submitConfigurationForm($form, $form_state);
|
||||
$expected = ['test_config_value' => 'Somewhere over the rainbow.', 'source_field' => 'field_media_test_1'];
|
||||
$this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
|
||||
|
||||
// Try to save a NULL value.
|
||||
$form_state->setValue('test_config_value', NULL);
|
||||
$source->submitConfigurationForm($form, $form_state);
|
||||
$expected['test_config_value'] = NULL;
|
||||
$this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
|
||||
|
||||
// Make sure that the config keys are determined correctly even if the
|
||||
// existing value is NULL.
|
||||
$form_state->setValue('test_config_value', 'Somewhere over the rainbow.');
|
||||
$source->submitConfigurationForm($form, $form_state);
|
||||
$expected['test_config_value'] = 'Somewhere over the rainbow.';
|
||||
$this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
|
||||
|
||||
// Make sure that a non-relevant value will be skipped.
|
||||
$form_state->setValue('not_relevant', 'Should not be saved in the plugin.');
|
||||
$source->submitConfigurationForm($form, $form_state);
|
||||
$this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Kernel;
|
||||
|
||||
use Drupal\media\Entity\Media;
|
||||
|
||||
/**
|
||||
* Tests Media.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaTest extends MediaKernelTestBase {
|
||||
|
||||
/**
|
||||
* Tests various aspects of a Media entity.
|
||||
*/
|
||||
public function testEntity() {
|
||||
$media = Media::create(['bundle' => $this->testMediaType->id()]);
|
||||
|
||||
$this->assertSame($media, $media->setOwnerId($this->user->id()), 'setOwnerId() method returns its own entity.');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\media\Kernel;
|
||||
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
|
||||
/**
|
||||
* Tests multilanguage fields logic.
|
||||
*
|
||||
* @group media
|
||||
*/
|
||||
class MediaTranslationTest extends MediaKernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['language'];
|
||||
|
||||
/**
|
||||
* The test media translation type.
|
||||
*
|
||||
* @var \Drupal\media\MediaTypeInterface
|
||||
*/
|
||||
protected $testTranslationMediaType;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installConfig(['language']);
|
||||
|
||||
// Create a test media type for translations.
|
||||
$this->testTranslationMediaType = $this->createMediaType('test_translation');
|
||||
|
||||
for ($i = 0; $i < 3; ++$i) {
|
||||
$language_id = 'l' . $i;
|
||||
ConfigurableLanguage::create([
|
||||
'id' => $language_id,
|
||||
'label' => $this->randomString(),
|
||||
])->save();
|
||||
file_put_contents('public://' . $language_id . '.png', '');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test translatable fields storage/retrieval.
|
||||
*/
|
||||
public function testTranslatableFieldSaveLoad() {
|
||||
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
|
||||
$entity_type = $this->container->get('entity_type.manager')->getDefinition('media');
|
||||
$this->assertTrue($entity_type->isTranslatable(), 'Media is translatable.');
|
||||
|
||||
// Prepare the field translations.
|
||||
$source_field_definition = $this->testTranslationMediaType->getSource()->getSourceFieldDefinition($this->testTranslationMediaType);
|
||||
$source_field_storage = $source_field_definition->getFieldStorageDefinition();
|
||||
/** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $media_storage */
|
||||
$media_storage = $this->container->get('entity_type.manager')->getStorage('media');
|
||||
/** @var \Drupal\media\Entity\Media $media */
|
||||
$media = $media_storage->create([
|
||||
'bundle' => $this->testTranslationMediaType->id(),
|
||||
'name' => 'Unnamed',
|
||||
]);
|
||||
|
||||
$field_translations = [];
|
||||
$available_langcodes = array_keys($this->container->get('language_manager')->getLanguages());
|
||||
$media->set('langcode', reset($available_langcodes));
|
||||
foreach ($available_langcodes as $langcode) {
|
||||
$values = [];
|
||||
for ($i = 0; $i < $source_field_storage->getCardinality(); $i++) {
|
||||
$values[$i]['value'] = $this->randomString();
|
||||
}
|
||||
$field_translations[$langcode] = $values;
|
||||
$translation = $media->hasTranslation($langcode) ? $media->getTranslation($langcode) : $media->addTranslation($langcode);
|
||||
$translation->{$source_field_definition->getName()}->setValue($field_translations[$langcode]);
|
||||
}
|
||||
|
||||
// Save and reload the field translations.
|
||||
$media->save();
|
||||
$media_storage->resetCache();
|
||||
$media = $media_storage->load($media->id());
|
||||
|
||||
// Check if the correct source field values were saved/loaded.
|
||||
foreach ($field_translations as $langcode => $items) {
|
||||
/** @var \Drupal\media\MediaInterface $media_translation */
|
||||
$media_translation = $media->getTranslation($langcode);
|
||||
$result = TRUE;
|
||||
foreach ($items as $delta => $item) {
|
||||
$result = $result && $item['value'] == $media_translation->{$source_field_definition->getName()}[$delta]->value;
|
||||
}
|
||||
$this->assertTrue($result, new FormattableMarkup('%language translation field value not correct.', ['%language' => $langcode]));
|
||||
$this->assertEquals('public://' . $langcode . '.png', $media_translation->getSource()->getMetadata($media_translation, 'thumbnail_uri'), new FormattableMarkup('%language translation thumbnail metadata attribute is not correct.', ['%language' => $langcode]));
|
||||
$this->assertEquals('public://' . $langcode . '.png', $media_translation->get('thumbnail')->entity->getFileUri(), new FormattableMarkup('%language translation thumbnail value is not correct.', ['%language' => $langcode]));
|
||||
$this->assertEquals('Test Thumbnail ' . $langcode, $media_translation->getSource()->getMetadata($media_translation, 'test_thumbnail_alt'), new FormattableMarkup('%language translation thumbnail alt metadata attribute is not correct.', ['%language' => $langcode]));
|
||||
$this->assertEquals('Test Thumbnail ' . $langcode, $media_translation->get('thumbnail')->alt, new FormattableMarkup('%language translation thumbnail alt value is not correct.', ['%language' => $langcode]));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user