View source
<?php
namespace Drupal\Tests\media_library\FunctionalJavascript;
use Drupal\media\Entity\Media;
use Drupal\media_test_oembed\Controller\ResourceController;
use Drupal\Tests\media\Traits\OEmbedTestTrait;
class WidgetOEmbedTest extends MediaLibraryTestBase {
use OEmbedTestTrait;
protected static $modules = [
'media_test_oembed',
];
protected function setUp() : void {
parent::setUp();
$this
->lockHttpClientToFixtures();
$this
->hijackProviderEndpoints();
$user = $this
->drupalCreateUser([
'access content',
'create basic_page content',
'view media',
'create media',
'administer media',
]);
$this
->drupalLogin($user);
}
public function testWidgetOEmbed() {
$this
->hijackProviderEndpoints();
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$youtube_title = "Everyday I'm Drupalin' Drupal Rap (Rick Ross - Hustlin)";
$youtube_url = 'https://www.youtube.com/watch?v=PWjcqE3QKBg';
$vimeo_title = "Drupal Rap Video - Schipulcon09";
$vimeo_url = 'https://vimeo.com/7073899';
ResourceController::setResourceUrl($youtube_url, $this
->getFixturesDirectory() . '/video_youtube.json');
ResourceController::setResourceUrl($vimeo_url, $this
->getFixturesDirectory() . '/video_vimeo.json');
ResourceController::setResource404('https://www.youtube.com/watch?v=PWjcqE3QKBg1');
$this
->drupalGet('node/add/basic_page');
$this
->openMediaLibraryForField('field_unlimited_media');
$assert_session
->fieldNotExists('Add Type Five via URL');
$this
->switchToMediaType('Three');
$assert_session
->fieldNotExists('Add Type Five via URL');
$this
->switchToMediaType('Five');
$page
->fillField('Add Type Five via URL', $youtube_url);
$assert_session
->pageTextContains('Allowed providers: YouTube, Vimeo.');
$page
->pressButton('Add');
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForText('The media item has been created but has not yet been saved.');
$assert_session
->fieldValueEquals('Name', $youtube_title);
$this
->pressSaveButton();
$this
->waitForText('Add Type Five via URL');
$media_items = Media::loadMultiple();
$added_media = array_pop($media_items);
$assert_session
->pageTextContains('Add or select media');
$assert_session
->pageTextContains($youtube_title);
$assert_session
->fieldValueEquals('media_library_select_form[0]', $added_media
->id());
$assert_session
->checkboxChecked('media_library_select_form[0]');
$this
->pressInsertSelected('Added one media item.');
$this
->waitForText($youtube_title);
$this
->openMediaLibraryForField('field_unlimited_media');
$this
->switchToMediaType('Five');
$assert_session
->pageTextContains($youtube_title);
$page
->fillField('Add Type Five via URL', 'https://www.youtube.com/');
$page
->pressButton('Add');
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForText('No matching provider found.');
$page
->fillField('Add Type Five via URL', 'https://www.youtube.com/watch?v=PWjcqE3QKBg1');
$page
->pressButton('Add');
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForText('Could not retrieve the oEmbed resource.');
$checkbox = $page
->findField("Select {$youtube_title}");
$selected_item_id = $checkbox
->getAttribute('value');
$checkbox
->click();
$assert_session
->pageTextContains('1 item selected');
$assert_session
->hiddenFieldValueEquals('current_selection', $selected_item_id);
$page
->fillField('Add Type Five via URL', $youtube_url);
$page
->pressButton('Add');
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForText('The media item has been created but has not yet been saved.');
$page
->fillField('Name', 'Custom video title');
$assert_session
->elementNotExists('css', '.media-library-add-form__selected-media');
$this
->pressSaveButton();
$media_items = Media::loadMultiple();
$added_media = array_pop($media_items);
$assert_session
->pageTextContains('Add or select media');
$assert_session
->pageTextContains('Custom video title');
$assert_session
->fieldValueEquals('media_library_select_form[0]', $added_media
->id());
$assert_session
->checkboxChecked('media_library_select_form[0]');
$assert_session
->pageTextContains('2 items selected');
$assert_session
->checkboxChecked("Select Custom video title");
$assert_session
->checkboxChecked("Select {$youtube_title}");
$assert_session
->hiddenFieldValueEquals('current_selection', implode(',', [
$selected_item_id,
$added_media
->id(),
]));
$selected_checkboxes = [];
foreach ($this
->getCheckboxes() as $checkbox) {
if ($checkbox
->isChecked()) {
$selected_checkboxes[] = $checkbox
->getAttribute('value');
}
}
$this
->assertCount(2, $selected_checkboxes);
$this
->pressInsertSelected('Added 2 media items.');
$this
->waitForText('Custom video title');
$this
->openMediaLibraryForField('field_unlimited_media');
$this
->switchToMediaType('Five');
$page
->fillField('Add Type Five via URL', $vimeo_url);
$page
->pressButton('Add');
$this
->waitForText('The media item has been created but has not yet been saved.');
$this
->pressSaveButton();
$this
->waitForText('Add or select media');
$this
->pressInsertSelected();
$this
->waitForText($vimeo_title);
$this
->openMediaLibraryForField('field_unlimited_media');
$this
->switchToMediaType('Five');
$checkbox = $page
->findField("Select {$vimeo_title}");
$selected_item_id = $checkbox
->getAttribute('value');
$checkbox
->click();
$assert_session
->hiddenFieldValueEquals('current_selection', $selected_item_id);
$page
->fillField('Add Type Five via URL', $youtube_url);
$page
->pressButton('Add');
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForText('The media item has been created but has not yet been saved');
$page
->fillField('Name', 'Another video');
$this
->pressSaveButton();
$page
->uncheckField('media_library_select_form[1]');
$this
->waitForText('1 item selected');
$this
->pressInsertSelected('Added one media item.');
$this
->waitForText('Another video');
$this
->openMediaLibraryForField('field_unlimited_media');
$this
->switchToMediaType('Five');
$page
->fillField('Add Type Five via URL', $youtube_url);
$page
->pressButton('Add');
$this
->assertMediaAdded();
$page
->pressButton('media-0-remove-button');
$this
->waitForText("The media item {$youtube_title} has been removed.");
$this
->assertNoMediaAdded();
}
public function testWidgetOEmbedAdvancedUi() {
$this
->config('media_library.settings')
->set('advanced_ui', TRUE)
->save();
$this
->hijackProviderEndpoints();
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$youtube_title = "Everyday I'm Drupalin' Drupal Rap (Rick Ross - Hustlin)";
$youtube_url = 'https://www.youtube.com/watch?v=PWjcqE3QKBg';
$vimeo_title = "Drupal Rap Video - Schipulcon09";
$vimeo_url = 'https://vimeo.com/7073899';
ResourceController::setResourceUrl($youtube_url, $this
->getFixturesDirectory() . '/video_youtube.json');
ResourceController::setResourceUrl($vimeo_url, $this
->getFixturesDirectory() . '/video_vimeo.json');
ResourceController::setResource404('https://www.youtube.com/watch?v=PWjcqE3QKBg1');
$this
->drupalGet('node/add/basic_page');
$this
->openMediaLibraryForField('field_unlimited_media');
$assert_session
->fieldNotExists('Add Type Five via URL');
$this
->switchToMediaType('Three');
$assert_session
->fieldNotExists('Add Type Five via URL');
$this
->switchToMediaType('Five');
$page
->fillField('Add Type Five via URL', $youtube_url);
$assert_session
->pageTextContains('Allowed providers: YouTube, Vimeo.');
$page
->pressButton('Add');
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForText('The media item has been created but has not yet been saved.');
$assert_session
->fieldValueEquals('Name', $youtube_title);
$this
->saveAnd('select');
$this
->waitForText('Add Type Five via URL');
$media_items = Media::loadMultiple();
$added_media = array_pop($media_items);
$assert_session
->pageTextContains('Add or select media');
$assert_session
->pageTextContains($youtube_title);
$assert_session
->fieldValueEquals('media_library_select_form[0]', $added_media
->id());
$assert_session
->checkboxChecked('media_library_select_form[0]');
$this
->pressInsertSelected('Added one media item.');
$this
->waitForText($youtube_title);
$this
->openMediaLibraryForField('field_unlimited_media');
$this
->switchToMediaType('Five');
$assert_session
->pageTextContains($youtube_title);
$page
->fillField('Add Type Five via URL', 'https://www.youtube.com/');
$page
->pressButton('Add');
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForText('No matching provider found.');
$page
->fillField('Add Type Five via URL', 'https://www.youtube.com/watch?v=PWjcqE3QKBg1');
$page
->pressButton('Add');
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForText('Could not retrieve the oEmbed resource.');
$checkbox = $page
->findField("Select {$youtube_title}");
$selected_item_id = $checkbox
->getAttribute('value');
$checkbox
->click();
$assert_session
->pageTextContains('1 item selected');
$assert_session
->hiddenFieldValueEquals('current_selection', $selected_item_id);
$page
->fillField('Add Type Five via URL', $youtube_url);
$page
->pressButton('Add');
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForText('The media item has been created but has not yet been saved.');
$page
->fillField('Name', 'Custom video title');
$assert_session
->checkboxChecked("Select {$youtube_title}", $this
->getSelectionArea());
$this
->saveAnd('select');
$this
->waitForNoText('Save and select');
$media_items = Media::loadMultiple();
$added_media = array_pop($media_items);
$assert_session
->pageTextContains('Add or select media');
$assert_session
->pageTextContains('Custom video title');
$assert_session
->fieldValueEquals('media_library_select_form[0]', $added_media
->id());
$assert_session
->checkboxChecked('media_library_select_form[0]');
$assert_session
->pageTextContains('2 items selected');
$assert_session
->checkboxChecked("Select Custom video title");
$assert_session
->checkboxChecked("Select {$youtube_title}");
$assert_session
->hiddenFieldValueEquals('current_selection', implode(',', [
$selected_item_id,
$added_media
->id(),
]));
$selected_checkboxes = [];
foreach ($this
->getCheckboxes() as $checkbox) {
if ($checkbox
->isChecked()) {
$selected_checkboxes[] = $checkbox
->getAttribute('value');
}
}
$this
->assertCount(2, $selected_checkboxes);
$this
->pressInsertSelected('Added 2 media items.');
$this
->waitForText('Custom video title');
$this
->openMediaLibraryForField('field_unlimited_media');
$this
->switchToMediaType('Five');
$page
->fillField('Add Type Five via URL', $vimeo_url);
$page
->pressButton('Add');
$this
->waitForText('The media item has been created but has not yet been saved.');
$this
->saveAnd('insert');
$this
->waitForText('Added one media item.');
$this
->waitForNoText('Add or select media');
$this
->waitForText($vimeo_title);
$this
->openMediaLibraryForField('field_unlimited_media');
$this
->switchToMediaType('Five');
$checkbox = $page
->findField("Select {$vimeo_title}");
$selected_item_id = $checkbox
->getAttribute('value');
$checkbox
->click();
$assert_session
->hiddenFieldValueEquals('current_selection', $selected_item_id);
$page
->fillField('Add Type Five via URL', $youtube_url);
$page
->pressButton('Add');
$assert_session
->assertWaitOnAjaxRequest();
$this
->waitForText('The media item has been created but has not yet been saved');
$page
->fillField('Name', 'Another video');
$selection_area = $this
->getSelectionArea();
$assert_session
->checkboxChecked("Select {$vimeo_title}", $selection_area);
$page
->uncheckField("Select {$vimeo_title}");
$assert_session
->hiddenFieldValueEquals('current_selection', '');
$selection_area
->find('css', 'summary')
->click();
$this
->saveAnd('select');
$media_items = Media::loadMultiple();
$added_media = array_pop($media_items);
$this
->waitForText('1 item selected');
$assert_session
->checkboxChecked('Select Another video');
$assert_session
->checkboxNotChecked("Select {$vimeo_title}");
$assert_session
->hiddenFieldValueEquals('current_selection', $added_media
->id());
$this
->pressInsertSelected('Added one media item.');
$this
->waitForText('Another video');
$this
->openMediaLibraryForField('field_unlimited_media');
$this
->switchToMediaType('Five');
$page
->fillField('Add Type Five via URL', $youtube_url);
$page
->pressButton('Add');
$this
->assertMediaAdded();
$page
->pressButton('media-0-remove-button');
$this
->waitForText("The media item {$youtube_title} has been removed.");
$this
->assertNoMediaAdded();
}
}