Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENEUROPA-1317: Media entity browser handling for videos. #6

Merged
merged 6 commits into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"require": {
"php": "^7.1",
"drupal/core": "^8.6",
"drupal/entity_browser": "^2.0"
"drupal/entity_browser": "^2.0",
"drupal/inline_entity_form": "^1.0"
},
"require-dev": {
"composer/installers": "~1.5",
Expand Down
11 changes: 11 additions & 0 deletions config/install/entity_browser.browser.media_entity_browser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies:
config:
- views.view.media_entity_browser
module:
- entity_browser_entity_form
- views
name: media_entity_browser
label: 'Media Entity Browser'
Expand All @@ -28,3 +29,13 @@ widgets:
weight: 1
label: View
id: view
ea17ce31-43ba-427e-94ae-88fe55605e7d:
settings:
entity_type: media
bundle: remote_video
form_mode: default
submit_text: 'Save entity'
uuid: ea17ce31-43ba-427e-94ae-88fe55605e7d
weight: 2
label: 'Add Video'
id: entity_form
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ settings:
handler_settings:
target_bundles:
image: image
remote_video: remote_video
sort:
field: _none
auto_create: false
Expand Down
3 changes: 2 additions & 1 deletion oe_media.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ core: 8.x
dependencies:
- drupal:media
- drupal:views
- drupal:entity_browser
- entity_browser:entity_browser
- entity_browser_entity_form:entity_browser_entity_form

108 changes: 88 additions & 20 deletions tests/FunctionalJavascript/MediaEntityBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,78 @@ public function setUp() {
$editor = $this->drupalCreateUser([
'create oe_media_demo content',
'create image media',
'create remote_video media',
'access media_entity_browser entity browser pages',
]);

$this->drupalLogin($editor);
}

/**
* Create a Media Image entity.
*
* @param string $name
* The name of the video.
* @param string $file_source
* The contents of the file.
* Data provider for testMediaBrowserWithRemoteVideo().
*/
public function createMediaImageEntity(string $name, string $file_source): void {
$file = file_save_data(file_get_contents($file_source), 'public://' . $name);
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager */
$entityTypeManager = $this->container->get('entity_type.manager');
$entityTypeManager->getStorage('media')->create([
'bundle' => 'image',
'oe_media_image' => [
'target_id' => $file->id(),
public function providerRemoteVideoMedia() {
return [
'Youtube' => [
'https://www.youtube.com/watch?v=1-g73ty9v04',
'Energy, let\'s save it!',
],
])->save();
'Vimeo' => [
'https://vimeo.com/7073899',
'Drupal Rap Video - Schipulcon09',
],
'Dailymotion' => [
'http://www.dailymotion.com/video/x6pa0tr',
'European Commission Fines Google',
],
];
}

/**
* Test the media entity browser with a remote video.
*
* @param string $video_url
* Video URL.
*
* @dataProvider providerRemoteVideoMedia
*/
public function testMediaBrowserWithRemoteVideo(string $video_url): void {
$this->createRemoteVideoMedia($video_url);
$this->checkMediaBrowserMediaSelection();

// Ensure the iframe exists and that its src attribute contains a coherent
// URL with the query parameters we expect.
$iframe_url = $this->assertSession()->elementExists('css', 'iframe')->getAttribute('src');
$iframe_url = parse_url($iframe_url);
$this->assertStringEndsWith('/media/oembed', $iframe_url['path']);
$this->assertNotEmpty($iframe_url['query']);
$query = [];
parse_str($iframe_url['query'], $query);
$this->assertSame($video_url, $query['url']);
$this->assertNotEmpty($query['hash']);
}

/**
* Test the media entity browser.
* Test the media entity browser with the image.
*/
public function testMediaBrowser(): void {
public function testMediaBrowserWithImage(): void {
$filename = 'example_1.jpeg';
$path = drupal_get_path('module', 'oe_media');
$file_source = $this->root . '/' . $path . '/tests/fixtures/' . $filename;

$this->createMediaImageEntity($filename, $file_source);
$this->createImageMedia($filename, $file_source);
$this->checkMediaBrowserMediaSelection();

$this->assertSession()->elementAttributeContains('css', '.field--name-oe-media-image>img', 'src', $filename);
}

/**
* Generic helper that tests the entity browser media select functionality.
*/
protected function checkMediaBrowserMediaSelection() {
// Select media image though entity browser.
$this->drupalGet('node/add/oe_media_demo');
$this->getSession()->getPage()->fillField("title[0][value]", 'My Node');
$this->getSession()->getPage()->fillField("title[0][value]", $this->randomString());
$this->click('#edit-field-oe-demo-media-browser-wrapper');
$this->getSession()->getPage()->pressButton('Select entities');

Expand All @@ -89,9 +122,44 @@ public function testMediaBrowser(): void {
$this->getSession()->switchToIFrame();
$this->assertSession()->assertWaitOnAjaxRequest();
$this->assertSession()->waitForButton('Remove');
// Save node.
$this->getSession()->getPage()->pressButton('Save');
$this->assertSession()->addressEquals('/node/1');
$this->assertSession()->elementAttributeContains('css', '.field--name-oe-media-image>img', 'src', $filename);
}

/**
* Create a Media Remote video entity.
*
* @param string $video_url
* The URL of the video.
*/
protected function createRemoteVideoMedia(string $video_url): void {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager */
$entityTypeManager = $this->container->get('entity_type.manager');
$entityTypeManager->getStorage('media')->create([
'bundle' => 'remote_video',
'oe_media_oembed_video' => $video_url,
])->save();
}

/**
* Create a Media Image entity.
*
* @param string $name
* The name of the image file.
* @param string $file_source
* The contents of the file.
*/
protected function createImageMedia(string $name, string $file_source): void {
$file = file_save_data(file_get_contents($file_source), 'public://' . $name);
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager */
$entityTypeManager = $this->container->get('entity_type.manager');
$entityTypeManager->getStorage('media')->create([
'bundle' => 'image',
'oe_media_image' => [
'target_id' => $file->id(),
],
])->save();
}

}
68 changes: 66 additions & 2 deletions tests/FunctionalJavascript/MediaRemoteVideoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function setUp() {
$editor = $this->drupalCreateUser([
'create oe_media_demo content',
'create remote_video media',
'access media_entity_browser entity browser pages',
]);

$this->drupalLogin($editor);
Expand All @@ -64,7 +65,7 @@ public function setUp() {
*
* @dataProvider providerRemoteVideoMedia
*/
public function testRemoteVideoMedia(string $video_url, string $video_name): void {
public function testAddVideoDefault(string $video_url, string $video_name): void {
$session = $this->getSession();
$page = $session->getPage();
$assert_session = $this->assertSession();
Expand All @@ -86,9 +87,72 @@ public function testRemoteVideoMedia(string $video_url, string $video_name): voi
$page->pressButton('Save');
$assert_session->addressEquals('/node/1');

$this->checkNodeDetailPageVideo($video_url);
}

/**
* Test the creation of Remote video media via IEF and reuse on the Demo node.
*
* @param string $video_url
* Video URL.
*
* @dataProvider providerRemoteVideoMedia
*/
public function testAddVideoViaEntityBrowser(string $video_url): void {
$this->drupalGet('node/add/oe_media_demo');
$this->getSession()->getPage()->fillField("title[0][value]", $this->randomString());
$this->click('#edit-field-oe-demo-media-browser-wrapper');
$this->getSession()->getPage()->pressButton('Select entities');

// Go to modal window.
$this->assertSession()->assertWaitOnAjaxRequest();
$this->getSession()->switchToIFrame('entity_browser_iframe_media_entity_browser');
$this->assertSession()->assertWaitOnAjaxRequest();
$iframe_page = $this->getSession()->getPage();
$iframe_page->clickLink('Add Video');
$iframe_page->fillField("inline_entity_form[oe_media_oembed_video][0][value]", $video_url);
$iframe_page->pressButton('Save entity');

// Go back to main window.
$this->getSession()->switchToIFrame();
$this->assertSession()->assertWaitOnAjaxRequest();
$this->assertSession()->waitForButton('Remove');
$this->getSession()->getPage()->pressButton('Save');
$this->checkNodeDetailPageVideo($video_url);

// Reuse previously added remote video.
$this->drupalGet('node/add/oe_media_demo');
$this->getSession()->getPage()->fillField("title[0][value]", $this->randomString());
$this->click('#edit-field-oe-demo-media-browser-wrapper');
$this->getSession()->getPage()->pressButton('Select entities');

// Go to modal window with library of media.
$this->assertSession()->assertWaitOnAjaxRequest();
$this->getSession()->switchToIFrame('entity_browser_iframe_media_entity_browser');
$this->assertSession()->assertWaitOnAjaxRequest();
$iframe_page = $this->getSession()->getPage();
$iframe_page->clickLink('View');
$iframe_page->findField('edit-entity-browser-select-media1')->click();
$iframe_page->pressButton('Select entities');

// Go back to main window and save node.
$this->getSession()->switchToIFrame();
$this->assertSession()->assertWaitOnAjaxRequest();
$this->assertSession()->waitForButton('Remove');
$this->getSession()->getPage()->pressButton('Save');
$this->checkNodeDetailPageVideo($video_url);
}

/**
* Check that the video player has been rendered correctly.
*
* @param string $video_url
* Video URL.
*/
protected function checkNodeDetailPageVideo(string $video_url): void {
// Ensure the iframe exists and that its src attribute contains a coherent
// URL with the query parameters we expect.
$iframe_url = $assert_session->elementExists('css', 'iframe')->getAttribute('src');
$iframe_url = $this->assertSession()->elementExists('css', 'iframe')->getAttribute('src');
$iframe_url = parse_url($iframe_url);
$this->assertStringEndsWith('/media/oembed', $iframe_url['path']);
$this->assertNotEmpty($iframe_url['query']);
Expand Down