Commit ac597399 authored by catch's avatar catch

Issue #3095210 by phenaproxima, dww, bnjmnm, seanB, oknate: Split up MediaLibraryTest

parent 472c813b
......@@ -9,9 +9,6 @@
*
* @coversDefaultClass \Drupal\media_library\Form\SettingsForm
* @group media_library
*
* @todo Roll this test into
* https://www.drupal.org/project/drupal/issues/3087227
*/
class SettingsFormTest extends BrowserTestBase {
......
<?php
namespace Drupal\Tests\media_library\FunctionalJavascript;
/**
* Tests field UI integration for media library widget.
*
* @group media_library
*/
class FieldUiIntegrationTest extends MediaLibraryTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['field_ui'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create a user who can add media fields.
$user = $this->drupalCreateUser([
'access administration pages',
'administer node fields',
'administer node form display',
]);
$this->drupalLogin($user);
$this->drupalCreateContentType(['type' => 'article']);
}
/**
* Tests field UI integration for media library widget.
*/
public function testFieldUiIntegration() {
$page = $this->getSession()->getPage();
$assert_session = $this->assertSession();
$user = $this->drupalCreateUser([
'access administration pages',
'administer node fields',
'administer node form display',
]);
$this->drupalLogin($user);
$this->drupalGet('/admin/structure/types/manage/article/fields/add-field');
$page->selectFieldOption('new_storage_type', 'field_ui:entity_reference:media');
$this->assertNotNull($assert_session->waitForField('label'));
$page->fillField('label', 'Shatner');
$this->waitForText('field_shatner');
$page->pressButton('Save and continue');
$page->pressButton('Save field settings');
$assert_session->pageTextNotContains('Undefined index: target_bundles');
$this->waitForFieldExists('Type One')->check();
$this->assertElementExistsAfterWait('css', '[name="settings[handler_settings][target_bundles][type_one]"][checked="checked"]');
$page->checkField('settings[handler_settings][target_bundles][type_two]');
$this->assertElementExistsAfterWait('css', '[name="settings[handler_settings][target_bundles][type_two]"][checked="checked"]');
$page->checkField('settings[handler_settings][target_bundles][type_three]');
$this->assertElementExistsAfterWait('css', '[name="settings[handler_settings][target_bundles][type_three]"][checked="checked"]');
$page->pressButton('Save settings');
$assert_session->pageTextContains('Saved Shatner configuration.');
}
}
<?php
namespace Drupal\Tests\media_library\FunctionalJavascript;
/**
* Tests the grid-style media overview page.
*
* @group media_library
*/
class MediaOverviewTest extends MediaLibraryTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['block'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create a few example media items for use in selection.
$this->createMediaItems([
'type_one' => [
'Horse',
'Bear',
'Cat',
'Dog',
],
'type_two' => [
'Crocodile',
'Lizard',
'Snake',
'Turtle',
],
]);
$this->drupalPlaceBlock('local_tasks_block');
$this->drupalPlaceBlock('local_actions_block');
$user = $this->drupalCreateUser([
'access media overview',
'create media',
'delete any media',
'update any media',
]);
$this->drupalLogin($user);
}
/**
* Tests that the Media Library's administration page works as expected.
*/
public function testAdministrationPage() {
$session = $this->getSession();
$page = $session->getPage();
$assert_session = $this->assertSession();
// Visit the administration page.
$this->drupalGet('admin/content/media');
// There should be links to both the grid and table displays.
$assert_session->linkExists('Grid');
$assert_session->linkExists('Table');
// We should see the table view and a link to add media.
$assert_session->elementExists('css', '.view-media .views-table');
$assert_session->linkExists('Add media');
// Go to the grid display for the rest of the test.
$page->clickLink('Grid');
$assert_session->addressEquals('admin/content/media-grid');
// Verify that the "Add media" link is present.
$assert_session->linkExists('Add media');
// Verify that media from two separate types is present.
$assert_session->pageTextContains('Dog');
$assert_session->pageTextContains('Turtle');
// Verify that the media name does not contain a link.
$assert_session->elementNotExists('css', '.media-library-item__name a');
// Verify that there are links to edit and delete media items.
$assert_session->linkExists('Edit Dog');
$assert_session->linkExists('Delete Turtle');
// Test that users can filter by type.
$page->selectFieldOption('Media type', 'Type One');
$page->pressButton('Apply filters');
$this->waitForNoText('Turtle');
$assert_session->pageTextContains('Dog');
$page->selectFieldOption('Media type', 'Type Two');
$page->pressButton('Apply filters');
$this->waitForText('Turtle');
$assert_session->pageTextNotContains('Dog');
// Test that selecting elements as a part of bulk operations works.
$page->selectFieldOption('Media type', '- Any -');
$assert_session->elementExists('css', '#views-exposed-form-media-library-page')->submit();
$this->waitForText('Dog');
// This tests that anchor tags clicked inside the preview are suppressed.
$this->getSession()->executeScript('jQuery(".js-click-to-select-trigger a")[4].click()');
$this->submitForm([], 'Apply to selected items');
$assert_session->pageTextContains('Dog');
$assert_session->pageTextNotContains('Cat');
// For reasons that are not clear, deleting media items by pressing the
// "Delete" button can fail (the button is found, but never actually pressed
// by the Mink driver). This workaround allows the delete form to be
// submitted.
$assert_session->elementExists('css', 'form')->submit();
$assert_session->pageTextNotContains('Dog');
$assert_session->pageTextContains('Cat');
// Test the 'Select all media' checkbox and assert that it makes the
// expected announcements.
$select_all = $this->waitForFieldExists('Select all media');
$select_all->check();
$this->waitForText('All 7 items selected');
$select_all->uncheck();
$this->waitForText('Zero items selected');
$select_all->check();
$page->selectFieldOption('Action', 'media_delete_action');
$this->submitForm([], 'Apply to selected items');
// For reasons that are not clear, deleting media items by pressing the
// "Delete" button can fail (the button is found, but never actually pressed
// by the Mink driver). This workaround allows the delete form to be
// submitted.
$assert_session->elementExists('css', 'form')->submit();
$assert_session->pageTextNotContains('Cat');
$assert_session->pageTextNotContains('Turtle');
$assert_session->pageTextNotContains('Snake');
// Test empty text.
$assert_session->pageTextContains('No media available.');
}
}
<?php
namespace Drupal\Tests\media_library\FunctionalJavascript;
/**
* Tests Media Library's integration with Views UI.
*
* @group media_library
*/
class ViewsUiIntegrationTest extends MediaLibraryTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['views_ui'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create a few example media items for use in selection.
$this->createMediaItems([
'type_one' => [
'Horse',
'Bear',
'Cat',
'Dog',
],
'type_two' => [
'Crocodile',
'Lizard',
'Snake',
'Turtle',
],
]);
$account = $this->drupalCreateUser(['administer views']);
$this->drupalLogin($account);
}
/**
* Tests that the integration with Views works correctly.
*/
public function testViewsAdmin() {
$page = $this->getSession()->getPage();
// Assert that the widget can be seen and that there are 8 items.
$this->drupalGet('/admin/structure/views/view/media_library/edit/widget');
$this->waitForElementsCount('css', '.js-media-library-item', 8);
// Assert that filtering works in live preview.
$page->find('css', '.js-media-library-view .view-filters')->fillField('name', 'snake');
$page->find('css', '.js-media-library-view .view-filters')->pressButton('Apply filters');
$this->waitForElementsCount('css', '.js-media-library-item', 1);
// Test the same routine but in the view for the table wiget.
$this->drupalGet('/admin/structure/views/view/media_library/edit/widget_table');
$this->waitForElementsCount('css', '.js-media-library-item', 8);
// Assert that filtering works in live preview.
$page->find('css', '.js-media-library-view .view-filters')->fillField('name', 'snake');
$page->find('css', '.js-media-library-view .view-filters')->pressButton('Apply filters');
$this->waitForElementsCount('css', '.js-media-library-item', 1);
// We cannot test clicking the 'Insert selected' button in either view
// because we expect an AJAX error, which would always throw an exception
// on ::tearDown even if we try to catch it here. If there is an API for
// marking certain elements 'unsuitable for previewing', we could test that
// here.
// @see https://www.drupal.org/project/drupal/issues/3060852
}
}
<?php
namespace Drupal\Tests\media_library\FunctionalJavascript;
use Drupal\media_library\MediaLibraryState;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/**
* Tests the media library UI access.
*
* @group media_library
*/
class WidgetAccessTest extends MediaLibraryTestBase {
/**
* Tests that the widget access works as expected.
*/
public function testWidgetAccess() {
$assert_session = $this->assertSession();
$role = Role::load(RoleInterface::ANONYMOUS_ID);
$role->revokePermission('view media');
$role->save();
// Create a working state.
$allowed_types = ['type_one', 'type_two', 'type_three', 'type_four'];
// The opener parameters are not relevant to the test, but the opener
// expects them to be there or it will deny access.
$state = MediaLibraryState::create('media_library.opener.field_widget', $allowed_types, 'type_three', 2, [
'entity_type_id' => 'node',
'bundle' => 'basic_page',
'field_name' => 'field_unlimited_media',
]);
$url_options = ['query' => $state->all()];
// Verify that unprivileged users can't access the widget view.
$this->drupalGet('admin/content/media-widget', $url_options);
$assert_session->responseContains('Access denied');
$this->drupalGet('admin/content/media-widget-table', $url_options);
$assert_session->responseContains('Access denied');
$this->drupalGet('media-library', $url_options);
$assert_session->responseContains('Access denied');
// Allow users with 'view media' permission to access the media library view
// and controller. Since we are using the node entity type in the state
// object, ensure the user also has permission to work with those.
$this->grantPermissions($role, [
'create basic_page content',
'view media',
]);
$this->drupalGet('admin/content/media-widget', $url_options);
$assert_session->elementExists('css', '.view-media-library');
$this->drupalGet('admin/content/media-widget-table', $url_options);
$assert_session->elementExists('css', '.view-media-library');
$this->drupalGet('media-library', $url_options);
$assert_session->elementExists('css', '.view-media-library');
// Assert the user does not have access to the media add form if the user
// does not have the 'create media' permission.
$assert_session->fieldNotExists('files[upload][]');
// Assert users can not access the widget displays of the media library view
// without a valid media library state.
$this->drupalGet('admin/content/media-widget');
$assert_session->responseContains('Access denied');
$this->drupalGet('admin/content/media-widget-table');
$assert_session->responseContains('Access denied');
$this->drupalGet('media-library');
$assert_session->responseContains('Access denied');
// Assert users with the 'create media' permission can access the media add
// form.
$this->grantPermissions($role, [
'create media',
]);
$this->drupalGet('media-library', $url_options);
$assert_session->elementExists('css', '.view-media-library');
$assert_session->fieldExists('Add files');
// Assert the media library can not be accessed if the required state
// parameters are changed without changing the hash.
$this->drupalGet('media-library', [
'query' => array_merge($url_options['query'], ['media_library_opener_id' => 'fail']),
]);
$assert_session->responseContains('Access denied');
$this->drupalGet('media-library', [
'query' => array_merge($url_options['query'], ['media_library_allowed_types' => ['type_one', 'type_two']]),
]);
$assert_session->responseContains('Access denied');
$this->drupalGet('media-library', [
'query' => array_merge($url_options['query'], ['media_library_selected_type' => 'type_one']),
]);
$assert_session->responseContains('Access denied');
$this->drupalGet('media-library', [
'query' => array_merge($url_options['query'], ['media_library_remaining' => 3]),
]);
$assert_session->responseContains('Access denied');
$this->drupalGet('media-library', [
'query' => array_merge($url_options['query'], ['hash' => 'fail']),
]);
$assert_session->responseContains('Access denied');
}
}
<?php
namespace Drupal\Tests\media_library\FunctionalJavascript;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
/**
* Tests that the widget works as expected for anonymous users.
*
* @group media_library
*/
class WidgetAnonymousTest extends MediaLibraryTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create a few example media items for use in selection.
$this->createMediaItems([
'type_one' => [
'Dog',
],
]);
// Allow the anonymous user to create pages and view media.
$role = Role::load(RoleInterface::ANONYMOUS_ID);
$this->grantPermissions($role, [
'access content',
'create basic_page content',
'view media',
]);
}
/**
* Tests that the widget works as expected for anonymous users.
*/
public function testWidgetAnonymous() {
$assert_session = $this->assertSession();
// Allow the anonymous user to create pages and view media.
$role = Role::load(RoleInterface::ANONYMOUS_ID);
$this->grantPermissions($role, [
'access content',
'create basic_page content',
'view media',
]);
// Ensure the widget works as an anonymous user.
$this->drupalGet('node/add/basic_page');
// Add to the unlimited cardinality field.
$this->openMediaLibraryForField('field_unlimited_media');
// Select the first media item (should be Dog).
$this->selectMediaItem(0);
$this->pressInsertSelected('Added one media item.');
// Ensure that the selection completed successfully.
$this->waitForText('Dog');
// Save the form.
$assert_session->elementExists('css', '.js-media-library-widget-toggle-weight')->click();
$this->submitForm([
'title[0][value]' => 'My page',
'field_unlimited_media[selection][0][weight]' => '0',
], 'Save');
$assert_session->pageTextContains('Basic Page My page has been created');
$assert_session->pageTextContains('Dog');
}
}
<?php
namespace Drupal\Tests\media_library\FunctionalJavascript;
/**
* Tests the views in the media library widget.
*
* @group media_library
*/
class WidgetViewsTest extends MediaLibraryTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Create a few example media items for use in selection.
$this->createMediaItems([
'type_one' => [
'Horse',
'Bear',
'Cat',
'Dog',
'Goat',
'Sheep',
'Pig',
'Cow',
'Chicken',
'Duck',
'Donkey',
'Llama',
'Mouse',
'Goldfish',
'Rabbit',
'Turkey',
'Dove',
'Giraffe',
'Tiger',
'Hamster',
'Parrot',
'Monkey',
'Koala',
'Panda',
'Kangaroo',
],
'type_two' => [
'Crocodile',
'Lizard',
'Snake',
'Turtle',
],
]);
// Create a user who can use the Media library.
$user = $this->drupalCreateUser([
'access content',
'create basic_page content',
'view media',
'create media',
]);
$this->drupalLogin($user);
}
/**
* Tests that the views in the Media library's widget work as expected.
*/
public function testWidgetViews() {
$assert_session = $this->assertSession();
$page = $this->getSession()->getPage();
$this->drupalGet('node/add/basic_page');
$this->openMediaLibraryForField('field_unlimited_media');
// Assert the 'Apply filter' button is not moved to the button pane.
$button_pane = $assert_session->elementExists('css', '.ui-dialog-buttonpane');
$assert_session->buttonExists('Insert selected', $button_pane);
$assert_session->buttonNotExists('Apply filters', $button_pane);
// Assert the pager works as expected.
$assert_session->elementTextContains('css', '.js-media-library-view .pager__item.is-active', 'Page 1');
$this->assertCount(24, $this->getCheckboxes());
$page->clickLink('Next page');
$this->waitForElementTextContains('.js-media-library-view .pager__item.is-active', 'Page 2');
$this->assertCount(1, $this->getCheckboxes());
$page->clickLink('Previous page');
$this->waitForElementTextContains('.js-media-library-view .pager__item.is-active', 'Page 1');
$this->assertCount(24, $this->getCheckboxes());
$this->switchToMediaLibraryTable();
// Assert the 'Apply filter' button is not moved to the button pane.
$assert_session->buttonExists('Insert selected', $button_pane);
$assert_session->buttonNotExists('Apply filters', $button_pane);
$assert_session->pageTextContains('Dog');
$assert_session->pageTextContains('Bear');
$assert_session->pageTextNotContains('Turtle');
// Assert the exposed filters can be applied.
$page->fillField('Name', 'Dog');
$page->pressButton('Apply filters');
$this->waitForText('Dog');
$this->waitForNoText('Bear');
$assert_session->pageTextNotContains('Turtle');
$page->checkField('Select Dog');
$assert_session->linkExists('Table');
$this->switchToMediaLibraryGrid();
// Assert the exposed filters are persisted when changing display.
$this->assertSame('Dog', $page->findField('Name')->getValue());
$assert_session->pageTextContains('Dog');
$assert_session->pageTextNotContains('Bear');
$assert_session->pageTextNotContains('Turtle');
$assert_session->linkExists('Grid');
$this->switchToMediaLibraryTable();
// Select the item.
$this->pressInsertSelected('Added one media item.');
// Ensure that the selection completed successfully.
$assert_session->pageTextContains('Dog');
$assert_session->pageTextNotContains('Bear');
$assert_session->pageTextNotContains('Turtle');
}
}
<?php
namespace Drupal\Tests\media_library\FunctionalJavascript;
use Drupal\Core\Url;
use Drupal\field_ui\FieldUI;
/**
* Tests the media library widget when no media types are available.
*
* @group media_library
*/
class WidgetWithoutTypesTest extends MediaLibraryTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['field_ui'];
/**
* Tests that the widget works as expected when media types are deleted.
*/
public function testWidgetWithoutMediaTypes() {
$assert_session = $this->assertSession();
$user = $this->drupalCreateUser([
'access administration pages',
'access content',
'create basic_page content',
'create media',
'view media',
]);
$this->drupalLogin($user);
$default_message = 'There are no allowed media types configured for this field. Please contact the site administrator.';
$this->drupalGet('node/add/basic_page');
// Assert a properly configured field does not show a message.
$assert_session->elementTextNotContains('css', '.field--name-field-twin-media', 'There are no allowed media types configured for this field.');
$assert_session->elementExists('css', '.js-media-library-open-button[name^="field_twin_media"]');
// Assert that the message is shown when the target_bundles setting for the
// entity reference field is an empty array. No types are allowed in this
// case.
$assert_session->elementTextContains('css', '.field--name-field-empty-types-media', $default_message);
$assert_session->elementNotExists('css', '.js-media-library-open-button[name^="field_empty_types_media"]');
// Assert that the message is not shown when the target_bundles setting for
// the entity reference field is null. All types are allowed in this case.
$assert_session->elementTextNotContains('css', '.field--name-field-null-types-media', 'There are no allowed media types configured for this field.');
$assert_session->elementExists('css', '.js-media-library-open-button[name^="field_null_types_media"]');
// Delete all media and media types.
$entity_type_manager = \Drupal::entityTypeManager();
$media_storage = $entity_type_manager->getStorage('media');
$media_type_storage = $entity_type_manager->getStorage('media_type');
$media_storage->delete($media_storage->loadMultiple());
$media_type_storage->delete($media_type_storage->loadMultiple());
// Visit a node create page.
$this->drupalGet('node/add/basic_page');
// Assert a properly configured field now shows a message.
$assert_session->elementTextContains('css', '.field--name-field-twin-media', $default_message);
$assert_session->elementNotExists('css', '.js-media-library-open-button[name^="field_twin_media"]');
// Assert that the message is shown when the target_bundles setting for the
// entity reference field is an empty array.
$assert_session->elementTextContains('css', '.field--name-field-empty-types-media', $default_message);
$assert_session->elementNotExists('css', '.js-media-library-open-button[name^="field_empty_types_media"]');
// Assert that the message is shown when the target_bundles setting for
// the entity reference field is null.
$assert_session->elementTextContains('css', '.field--name-field-null-types-media', $default_message);
$assert_session->elementNotExists('css', '.js-media-library-open-button[name^="field_null_types_media"]');
// Assert a different message is shown when the user is allowed to
// administer the fields.
$user = $this->drupalCreateUser([
'access administration pages',
'access content',