summaryrefslogtreecommitdiffstats
path: root/core/modules/media/tests/src/Functional
diff options
context:
space:
mode:
Diffstat (limited to 'core/modules/media/tests/src/Functional')
-rw-r--r--core/modules/media/tests/src/Functional/MediaAccessTest.php109
-rw-r--r--core/modules/media/tests/src/Functional/MediaCacheTagsTest.php77
-rw-r--r--core/modules/media/tests/src/Functional/MediaFunctionalTestBase.php29
-rw-r--r--core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php67
-rw-r--r--core/modules/media/tests/src/Functional/MediaFunctionalTestTrait.php74
-rw-r--r--core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php46
-rw-r--r--core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php166
7 files changed, 568 insertions, 0 deletions
diff --git a/core/modules/media/tests/src/Functional/MediaAccessTest.php b/core/modules/media/tests/src/Functional/MediaAccessTest.php
new file mode 100644
index 0000000..090beac
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/MediaAccessTest.php
@@ -0,0 +1,109 @@
+<?php
+
+namespace Drupal\Tests\media\Functional;
+
+use Drupal\media\Entity\Media;
+use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
+use Drupal\user\Entity\Role;
+use Drupal\user\RoleInterface;
+
+/**
+ * Basic access tests for Media.
+ *
+ * @group media
+ */
+class MediaAccessTest extends MediaFunctionalTestBase {
+
+ 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.
+ $this->drupalGet('media/' . $media->id());
+ $this->assertCacheContext('user.permissions');
+ $assert_session->statusCodeEquals(403);
+ $this->grantPermissions($role, ['view media']);
+ $this->drupalGet('media/' . $media->id());
+ $this->assertCacheContext('user.permissions');
+ $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);
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/MediaCacheTagsTest.php b/core/modules/media/tests/src/Functional/MediaCacheTagsTest.php
new file mode 100644
index 0000000..cdbfc29
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/MediaCacheTagsTest.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace Drupal\Tests\media\Functional;
+
+use Drupal\Core\Entity\EntityInterface;
+use Drupal\media\Entity\Media;
+use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase;
+use Drupal\user\Entity\Role;
+use Drupal\user\RoleInterface;
+
+/**
+ * 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 setUp() {
+ parent::setUp();
+
+ // Give anonymous users permission to view media, so that we can
+ // verify the cache tags of cached versions of media items.
+ $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
+ $user_role->grantPermission('view media');
+ $user_role->save();
+ }
+
+ /**
+ * {@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(),
+ ];
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/MediaFunctionalTestBase.php b/core/modules/media/tests/src/Functional/MediaFunctionalTestBase.php
new file mode 100644
index 0000000..017e725
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/MediaFunctionalTestBase.php
@@ -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',
+ ];
+
+}
diff --git a/core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php b/core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php
new file mode 100644
index 0000000..2d60c9b
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/MediaFunctionalTestCreateMediaTypeTrait.php
@@ -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->setLocked(TRUE)->save();
+ $source_field->save();
+
+ $media_type
+ ->set('source_configuration', [
+ 'source_field' => $source_field->getName(),
+ ])
+ ->save();
+ }
+
+ return $media_type;
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/MediaFunctionalTestTrait.php b/core/modules/media/tests/src/Functional/MediaFunctionalTestTrait.php
new file mode 100644
index 0000000..75e0d06
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/MediaFunctionalTestTrait.php
@@ -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\Core\Session\AccountInterface
+ */
+ 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');
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php b/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php
new file mode 100644
index 0000000..e0e5294
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php
@@ -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.');
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
new file mode 100644
index 0000000..883f27e
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/MediaUiFunctionalTest.php
@@ -0,0 +1,166 @@
+<?php
+
+namespace Drupal\Tests\media\Functional;
+
+use Drupal\media\Entity\Media;
+
+/**
+ * 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');
+ }
+
+ /**
+ * 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 and publish');
+ $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->label(), $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 and keep published');
+ /** @var \Drupal\media\MediaInterface $media */
+ $media = $this->container->get('entity_type.manager')
+ ->getStorage('media')
+ ->loadUnchanged($media_id);
+ $this->assertEquals($media->label(), $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 and keep published');
+ $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());
+
+ // 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->label());
+
+ // Go to second media item.
+ $this->drupalGet('media/' . $second_media_item->id());
+ $assert_session->statusCodeEquals(200);
+ $assert_session->pageTextContains($second_media_item->label());
+ }
+
+}