View source
<?php
namespace Drupal\Tests\lightning_media\FunctionalJavascript;
use Drupal\Component\Serialization\Json;
use Drupal\file\Entity\File;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\media\Entity\Media;
use Drupal\Tests\lightning_media\Traits\EntityBrowserTrait;
use Drupal\Tests\lightning_media\Traits\EntityEmbedTrait;
class EmbeddedMediaTest extends WebDriverTestBase {
use EntityBrowserTrait;
use EntityEmbedTrait;
protected $defaultTheme = 'stark';
protected static $modules = [
'lightning_media_image',
'lightning_media_instagram',
'lightning_media_twitter',
'lightning_media_video',
'lightning_roles',
];
public function providerEmbedCodeBasedEmbeddedMedia() {
return [
[
'video',
'https://www.youtube.com/watch?v=N2_HkWs7OM0',
],
[
'video',
'https://vimeo.com/25585320',
],
[
'tweet',
'https://twitter.com/djphenaproxima/status/879739227617079296',
],
[
'instagram',
'https://www.instagram.com/p/lV3WqOoNDD',
],
];
}
protected function setUp() {
parent::setUp();
$this
->addMediaLibraryToEntityBrowsers();
$this
->drupalCreateContentType([
'type' => 'page',
]);
$account = $this
->drupalCreateUser([
'create page content',
'use text format rich_text',
]);
$account
->addRole('media_creator');
$account
->save();
$this
->drupalLogin($account);
}
public function testEmbedCodeBasedEmbeddedMedia($media_type, $embed_code) {
$media = Media::create([
'bundle' => $media_type,
'name' => $this
->randomString(),
'embed_code' => (string) $embed_code,
'field_media_in_library' => TRUE,
]);
$media
->setPublished()
->save();
$this
->drupalGet('/node/add/page');
$this
->open();
$items = $this
->waitForItems();
$this
->selectItem($items[0]);
$this
->getSession()
->getPage()
->pressButton('Place');
$this
->waitForEntityBrowserToClose();
$this
->waitForStandardEmbedForm();
}
public function testEmbedImageMedia() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$random = $this
->getRandomGenerator();
$uri = $random
->image(uniqid('public://random_') . '.png', '240x240', '640x480');
$file = File::create([
'uri' => $uri,
]);
$file
->setMimeType('image/png');
$file
->setTemporary();
$file
->save();
$media = Media::create([
'bundle' => 'image',
'name' => $random
->name(32),
'image' => $file
->id(),
'field_media_in_library' => TRUE,
]);
$media
->setPublished()
->save();
$this
->drupalGet('/node/add/page');
$page
->fillField('Title', 'Foobar');
$this
->open();
$items = $this
->waitForItems();
$this
->selectItem($items[0]);
$page
->pressButton('Place');
$embed_form = $this
->waitForImageEmbedForm();
$embed_form
->selectFieldOption('Image style', 'medium');
$embed_form
->fillField('Alternate text', 'Behold my image of randomness');
$embed_form
->fillField('Title', 'Ye gods!');
$this
->submitEmbedForm();
$page
->pressButton('Save');
$assert_session
->responseContains('Behold my image of randomness');
$assert_session
->responseContains('Ye gods!');
}
private function executeEditorCommand($command, $id = NULL, $data = NULL) {
$js = $this
->assertEditor($id);
$result = $this
->getSession()
->evaluateScript("{$js}.execCommand('{$command}', " . Json::encode($data) . ')');
$result = Json::decode($result);
$this
->assertNotEmpty($result);
}
private function assertEditor($id = NULL) {
$id = $id ?: $this
->getDefaultEditor();
$js = "CKEDITOR.instances['{$id}']";
$this
->assertJsCondition("{$js}.status === 'ready'");
return $js;
}
private function getDefaultEditor() {
$keys = $this
->getEditors();
return reset($keys);
}
private function getEditors() {
$this
->assertJsCondition("typeof CKEDITOR.instances === 'object'");
$keys = $this
->getSession()
->evaluateScript('Object.keys(CKEDITOR.instances).join(",")');
return explode(',', $keys);
}
private function open() {
$this
->executeEditorCommand('editdrupalentity', NULL, [
'id' => 'media_browser',
]);
$this
->waitForEntityBrowser('ckeditor_media_browser');
}
}