View source
<?php
namespace Drupal\Tests\entity_embed\FunctionalJavascript;
use Drupal\Component\Utility\Html;
use Drupal\editor\Entity\Editor;
use Drupal\entity_embed\Plugin\entity_embed\EntityEmbedDisplay\MediaImageDecorator;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
class MediaImageTest extends EntityEmbedTestBase {
use ContentTypeCreationTrait;
use TestFileCreationTrait;
protected $adminUser;
protected $media;
protected $host;
protected $defaultTheme = 'stable';
public static $modules = [
'entity_embed_test',
];
protected function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'use text format full_html',
'bypass node access',
]);
$this
->createNode([
'type' => 'article',
'title' => 'Red-lipped batfish',
]);
File::create([
'uri' => $this
->getTestFiles('image')[0]->uri,
])
->save();
$this->media = Media::create([
'bundle' => 'image',
'name' => 'Screaming hairy armadillo',
'field_media_image' => [
[
'target_id' => 1,
'alt' => 'default alt',
'title' => 'default title',
],
],
]);
$this->media
->save();
$this
->drupalCreateContentType([
'type' => 'blog',
]);
$this->host = $this
->createNode([
'type' => 'blog',
'title' => 'Animals with strange names',
'body' => [
'value' => '',
'format' => 'full_html',
],
]);
$this->host
->save();
$this
->drupalLogin($this->adminUser);
}
public function testAltAndTitle() {
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assignNameToCkeditorIframe();
$this
->pressEditorButton('test_node');
$this
->assertSession()
->waitForId('drupal-modal');
$this
->assertSession()
->fieldExists('entity_id')
->setValue('Red-lipped batfish (1)');
$this
->assertSession()
->elementExists('css', 'button.js-button-next')
->click();
$form = $this
->assertSession()
->waitForElementVisible('css', 'form.entity-embed-dialog-step--embed');
$text = $form
->getText();
$this
->assertStringContainsString('Red-lipped batfish', $text);
$select = $this
->assertSession()
->selectExists('attributes[data-entity-embed-display]');
$select
->setValue('view_mode:node.full');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][alt]');
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][title]');
$select = $this
->assertSession()
->selectExists('attributes[data-entity-embed-display]');
$select
->setValue('view_mode:node.teaser');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][alt]');
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][title]');
$this
->assertSession()
->elementExists('css', '.ui-dialog-titlebar-close')
->press();
$this
->pressEditorButton('test_media_entity_embed');
$this
->assertSession()
->waitForId('drupal-modal');
$this
->assertSession()
->fieldExists('entity_id')
->setValue('Screaming hairy armadillo (1)');
$this
->assertSession()
->elementExists('css', 'button.js-button-next')
->click();
$form = $this
->assertSession()
->waitForElementVisible('css', 'form.entity-embed-dialog-step--embed');
$text = $form
->getText();
$this
->assertStringContainsString('Screaming hairy armadillo', $text);
$select = $this
->assertSession()
->selectExists('attributes[data-entity-embed-display]');
$select
->setValue('entity_reference:entity_reference_entity_id');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][alt]');
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][title]');
$select
->setValue('entity_reference:entity_reference_label');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][alt]');
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][title]');
$select
->setValue('entity_reference:media_thumbnail');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->selectExists('attributes[data-entity-embed-display]')
->setValue('view_mode:media.embed');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$alt = $this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][alt]');
$this
->assertEquals($this->media->field_media_image->alt, $alt
->getAttribute('placeholder'));
$title = $this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][title]');
$this
->assertEquals($this->media->field_media_image->title, $title
->getAttribute('placeholder'));
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEquals("default alt", $img
->getAttribute('alt'));
$this
->assertEquals("default title", $img
->getAttribute('title'));
$this
->reopenDialog();
$this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][alt]')
->setValue('Satanic leaf-tailed gecko alt');
$this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][title]')
->setValue('Satanic leaf-tailed gecko title');
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEquals("Satanic leaf-tailed gecko alt", $img
->getAttribute('alt'));
$this
->assertEquals("Satanic leaf-tailed gecko title", $img
->getAttribute('title'));
$this
->reopenDialog();
$select
->setValue('view_mode:media.thumb');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->selectExists('attributes[data-entity-embed-display]')
->setValue('view_mode:media.embed');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$alt = $this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][alt]');
$this
->assertEquals('Satanic leaf-tailed gecko alt', $alt
->getValue());
$title = $this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][title]');
$this
->assertEquals('Satanic leaf-tailed gecko title', $title
->getValue());
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEquals('Satanic leaf-tailed gecko alt', $img
->getAttribute('alt'));
$this
->assertEquals('Satanic leaf-tailed gecko title', $img
->getAttribute('title'));
$this
->reopenDialog();
$this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][alt]')
->setValue('Goblin shark alt');
$this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][title]')
->setValue('Goblin shark title');
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEquals("Goblin shark alt", $img
->getAttribute('alt'));
$this
->assertEquals("Goblin shark title", $img
->getAttribute('title'));
$this
->reopenDialog();
$select
->setValue('view_mode:media.embed');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->selectExists('attributes[data-entity-embed-display]')
->setValue('view_mode:media.embed');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$alt = $this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][alt]');
$this
->assertEquals("Goblin shark alt", $alt
->getValue());
$title = $this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][title]');
$this
->assertEquals("Goblin shark title", $title
->getValue());
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEquals("Goblin shark alt", $img
->getAttribute('alt'));
$this
->assertEquals("Goblin shark title", $img
->getAttribute('title'));
$this
->reopenDialog();
$this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][alt]')
->setValue('Satanic leaf-tailed gecko alt');
$this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][title]')
->setValue('Satanic leaf-tailed gecko title');
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEquals('Satanic leaf-tailed gecko alt', $img
->getAttribute('alt'));
$this
->assertEquals('Satanic leaf-tailed gecko title', $img
->getAttribute('title'));
$this
->config('field.field.media.image.field_media_image')
->set('settings.alt_field', FALSE)
->set('settings.title_field', FALSE)
->save();
drupal_flush_all_caches();
$this
->reopenDialog();
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][alt]');
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][title]');
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEquals('default alt', $img
->getAttribute('alt'));
$this
->assertEquals('default title', $img
->getAttribute('title'));
$field = FieldConfig::load('media.image.field_media_image');
$settings = $field
->getSettings();
$settings['alt_field'] = TRUE;
$field
->set('settings', $settings);
$field
->save();
drupal_flush_all_caches();
$this
->reopenDialog();
$this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][alt]')
->setValue('Satanic leaf-tailed gecko alt');
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][title]');
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEquals('Satanic leaf-tailed gecko alt', $img
->getAttribute('alt'));
$this
->assertEquals('default title', $img
->getAttribute('title'));
$field = FieldConfig::load('media.image.field_media_image');
$settings = $field
->getSettings();
$settings['alt_field'] = FALSE;
$settings['title_field'] = TRUE;
$field
->set('settings', $settings);
$field
->save();
drupal_flush_all_caches();
$this
->reopenDialog();
$this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][title]')
->setValue('Satanic leaf-tailed gecko title');
$this
->assertSession()
->fieldNotExists('attributes[data-entity-embed-display-settings][alt]');
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEquals('Satanic leaf-tailed gecko title', $img
->getAttribute('title'));
$this
->assertEquals('default alt', $img
->getAttribute('alt'));
$field = FieldConfig::load('media.image.field_media_image');
$settings = $field
->getSettings();
$settings['alt_field'] = TRUE;
$settings['title_field'] = TRUE;
$field
->set('settings', $settings);
$field
->save();
drupal_flush_all_caches();
$this
->reopenDialog();
$alt
->setValue(MediaImageDecorator::EMPTY_STRING);
$title
->setValue(MediaImageDecorator::EMPTY_STRING);
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEmpty($img
->getAttribute('alt'));
$this
->assertEmpty($img
->getAttribute('title'));
$this
->reopenDialog();
$alt
->setValue('');
$title
->setValue('');
$this
->submitDialog();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertEquals('default alt', $img
->getAttribute('alt'));
$this
->assertEquals('default title', $img
->getAttribute('title'));
}
public function testCkeditorWidgetHasEditableCaption() {
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assignNameToCkeditorIframe();
$this
->pressEditorButton('test_media_entity_embed');
$this
->assertSession()
->waitForId('drupal-modal');
$this
->assertSession()
->fieldExists('entity_id')
->setValue('Screaming hairy armadillo (1)');
$this
->assertSession()
->elementExists('css', 'button.js-button-next')
->click();
$this
->assertSession()
->waitForElementVisible('css', 'form.entity-embed-dialog-step--embed');
$this
->assertSession()
->selectExists('attributes[data-entity-embed-display]')
->setValue('entity_reference:media_thumbnail');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldExists('attributes[data-caption]')
->setValue('Is this the real life? Is this just fantasy?');
$this
->submitDialog();
$figcaption = $this
->assertSession()
->elementExists('css', 'figcaption');
$this
->assertTrue($figcaption
->hasAttribute('contenteditable'));
$this
->assertSession()
->waitForElement('css', 'figcaption');
$this
->setCaption('Caught in a <strong>landslide</strong>! No escape from <em>reality</em>!');
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->elementExists('css', 'figcaption > em');
$this
->assertSession()
->elementExists('css', 'figcaption > strong')
->click();
$this
->clickPathLinkByTitleAttribute("strong element");
$this
->pressEditorButton('bold');
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->elementExists('css', 'figcaption > em');
$this
->assertSession()
->elementNotExists('css', 'figcaption > strong');
$this
->assertSession()
->elementExists('css', 'figcaption > em')
->click();
$this
->clickPathLinkByTitleAttribute("em element");
$this
->pressEditorButton('italic');
$this
->pressEditorButton('source');
$source = $this
->assertSession()
->elementExists('css', 'textarea.cke_source');
$value = $source
->getValue();
$dom = Html::load($value);
$xpath = new \DOMXPath($dom);
$drupal_entity = $xpath
->query('//drupal-entity')[0];
$this
->assertEquals('Caught in a landslide! No escape from reality!', $drupal_entity
->getAttribute('data-caption'));
$poor_boy_text = "I'm just a <strong>poor boy</strong>, I need no sympathy!";
$drupal_entity
->setAttribute("data-caption", $poor_boy_text);
$source
->setValue(Html::serialize($dom));
$this
->pressEditorButton('source');
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$figcaption = $this
->assertSession()
->waitForElement('css', 'figcaption');
$this
->assertEquals($poor_boy_text, $figcaption
->getHtml());
$figcaption
->find('css', 'strong')
->click();
$this
->pressEditorButton('bold');
$this
->clickPathLinkByTitleAttribute("Caption element");
$this
->pressEditorButton('drupallink');
$this
->assertSession()
->waitForId('drupal-modal');
$this
->assertSession()
->waitForElementVisible('css', '#editor-link-dialog-form')
->findField('attributes[href]')
->setValue('https://www.drupal.org');
$this
->assertSession()
->elementExists('css', 'button.form-submit')
->press();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->getSession()
->switchToIFrame('ckeditor');
$figcaption = $this
->assertSession()
->waitForElement('css', 'figcaption');
$figcaption
->find('css', 'a')
->click();
$this
->clickPathLinkByTitleAttribute("a element");
$this
->pressEditorButton('drupallink');
$this
->assertSession()
->waitForId('drupal-modal');
$this
->assertSession()
->waitForElementVisible('css', '#editor-link-dialog-form')
->findField('attributes[href]')
->setValue('https://www.drupal.org/project/drupal');
$this
->assertSession()
->elementExists('css', 'button.form-submit')
->press();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->pressEditorButton('source');
$source = $this
->assertSession()
->elementExists('css', "textarea.cke_source");
$value = $source
->getValue();
$this
->assertStringContainsString('https://www.drupal.org/project/drupal', $value);
$this
->assertStringNotContainsString('data-cke-saved-href', $value);
$this
->assertSession()
->buttonExists('Save')
->press();
$link = $this
->assertSession()
->elementExists('css', 'figcaption > a');
$this
->assertEquals('https://www.drupal.org/project/drupal', $link
->getAttribute('href'));
$this
->assertEquals("I'm just a poor boy, I need no sympathy!", $link
->getText());
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->waitForElementVisible('css', 'figcaption');
$this
->setCaption('Scaramouch, <em>Scaramouch</em>, will you do the <strong>Fandango</strong>?');
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->elementExists('xpath', '//figcaption//em')
->click();
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->elementTextContains('css', '#cke_1_path', 'Embedded Media Entity Embed');
$this
->reopenDialog();
$this
->assertSession()
->fieldExists('attributes[data-caption]')
->setValue('');
$this
->submitDialog();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->elementExists('css', 'drupal-entity');
$this
->assertSession()
->elementNotExists('css', 'figcaption');
$this
->reopenDialog();
$freddys_lament = "Mama, life had just begun. But now I've gone and <strong>thrown it all away</strong>! :(";
$this
->assertSession()
->fieldExists('attributes[data-caption]')
->setValue($freddys_lament);
$this
->submitDialog();
$this
->assertSession()
->elementExists('css', 'figcaption');
$wind_markup = '<a href="http://www.drupal.org">anyway the wind blows</a>';
$this
->reopenDialog();
$this
->assertSession()
->fieldExists('attributes[data-caption]')
->setValue($wind_markup);
$this
->submitDialog();
$figcaption = $this
->assertSession()
->waitForElementVisible('css', 'figcaption');
$this
->assertEquals('anyway the wind blows', $figcaption
->getText());
$gallileo = '<a href="http://www.drupal.org">Gallileo, figaro, magnifico</a>';
$this
->reopenDialog();
$this
->assertSession()
->fieldExists('attributes[data-caption]')
->setValue($gallileo);
$this
->submitDialog();
$figcaption = $this
->assertSession()
->waitForElementVisible('css', 'figcaption');
$this
->assertEquals('Gallileo, figaro, magnifico', $figcaption
->getText());
$this
->setCaption('');
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->elementContains('css', 'figcaption', '');
$this
->assertSession()
->elementAttributeContains('css', 'figcaption', 'data-placeholder', 'Enter caption here');
$this
->setCaption('Fin.');
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->elementContains('css', 'figcaption', 'Fin.');
}
public function testCkeditorWidgetIsLinkableWhenDrupalImageIsAbsent() {
$editor = Editor::load('full_html');
$settings = $editor
->getSettings();
$rows = $settings['toolbar']['rows'];
foreach ($rows as $row_key => $row) {
foreach ($row as $group_key => $group) {
foreach ($group['items'] as $item_key => $item) {
if ($item === 'DrupalImage') {
unset($settings['toolbar']['rows'][$row_key][$group_key]['items'][$item_key]);
}
}
}
}
$editor
->setSettings($settings);
$editor
->save();
$this
->testCkeditorWidgetIsLinkable();
}
public function testCkeditorWidgetIsLinkable() {
$this->host->body->value = '<drupal-entity data-caption="baz" data-embed-button="test_media_entity_embed" data-entity-embed-display="entity_reference:media_thumbnail" data-entity-embed-display-settings="{"image_style":"","image_link":""}" data-entity-type="media" data-entity-uuid="' . $this->media
->uuid() . '"></drupal-entity>';
$this->host
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$drupal_entity = $this
->assertSession()
->waitForElementVisible('css', 'drupal-entity');
$this
->assertNotEmpty($drupal_entity);
$drupal_entity
->click();
$this
->pressEditorButton('drupallink');
$this
->assertSession()
->waitForId('drupal-modal');
$this
->assertSession()
->waitForElementVisible('css', '#editor-link-dialog-form')
->findField('attributes[href]')
->setValue('https://www.drupal.org');
$this
->assertSession()
->elementExists('css', 'button.form-submit')
->press();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->buttonExists('Save')
->press();
$this
->assertSession()
->elementExists('css', 'figure > a[href="https://www.drupal.org"] > div[data-embed-button="test_media_entity_embed"] > img[src*="image-test.png"]');
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->pressEditorButton('drupallink');
$this
->assertSession()
->waitForId('drupal-modal');
$this
->assertSession()
->waitForElementVisible('css', '#editor-link-dialog-form')
->findField('attributes[href]')
->setValue('https://wikipedia.org');
$this
->assertSession()
->elementExists('css', 'button.form-submit')
->press();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->elementExists('css', 'body > a[href="https://wikipedia.org"]');
$this
->assertSession()
->elementExists('css', 'body > .cke_widget_drupalentity > drupal-entity > figure > a[href="https://www.drupal.org"]');
}
public function testOnlyDrupalEntityTagProcessed() {
$embed_code = '<drupal-entity data-caption="baz" data-embed-button="test_media_entity_embed" data-entity-embed-display="entity_reference:media_thumbnail" data-entity-embed-display-settings="{"image_style":"","image_link":""}" data-entity-type="media" data-entity-uuid="' . $this->media
->uuid() . '"></drupal-entity>';
$this->host->body->value = str_replace('drupal-entity', 'p', $embed_code);
$this->host
->save();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->waitForElementVisible('css', 'img[src*="example.jpg"]', 1000);
$this
->assertSession()
->elementNotExists('css', 'figure');
$this->host->body->value = $embed_code;
$this->host
->save();
$this
->getSession()
->reload();
$this
->waitForEditor();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->waitForElementVisible('css', 'img[src*="example.jpg"]');
$this
->assertSession()
->elementExists('css', 'figure');
}
public function testPreviewUsesDefaultThemeAndIsClientCacheable() {
$this
->config('node.settings')
->set('use_admin_theme', TRUE)
->save();
$this->container
->get('router.builder')
->rebuild();
$this->adminUser
->addRole($this
->drupalCreateRole([
'view the administration theme',
]));
$this->adminUser
->save();
$this
->config('system.theme')
->set('default', 'stable')
->set('admin', 'classy')
->save();
$this->host->body->value = '<drupal-entity data-caption="baz" data-embed-button="test_media_entity_embed" data-entity-embed-display="entity_reference:entity_reference_entity_view" data-entity-embed-display-settings="full" data-entity-type="media" data-entity-uuid="' . $this->media
->uuid() . '"></drupal-entity>';
$this->host
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->waitForElementVisible('css', 'img[src*="image-test.png"]');
$element = $this
->assertSession()
->elementExists('css', '[data-entity-embed-test-active-theme]');
$this
->assertSame('stable', $element
->getAttribute('data-entity-embed-test-active-theme'));
$this
->assertGreaterThan(0, $this
->getLastPreviewRequestTransferSize());
$this
->pressEditorButton('source');
$this
->assertSession()
->waitForElement('css', 'textarea.cke_source');
$this
->pressEditorButton('source');
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->waitForElementVisible('css', 'img[src*="image-test.png"]');
$this
->assertSame(0, $this
->getLastPreviewRequestTransferSize());
}
protected function getLastPreviewRequestTransferSize() {
$this
->getSession()
->switchToIFrame();
$javascript = <<<JS
(function(){
return window.performance
.getEntries()
.filter(function (entry) {
return entry.initiatorType == 'xmlhttprequest' && entry.name.indexOf('/entity-embed/preview/') !== -1;
})
.pop()
.transferSize;
})()
JS;
return $this
->getSession()
->evaluateScript($javascript);
}
public function testCkeditorWidgetWorksForAllEmbeds($data_embed_button_attribute) {
$this->host->body->value = '<drupal-entity data-caption="baz" ' . $data_embed_button_attribute . ' data-entity-embed-display="entity_reference:media_thumbnail" data-entity-embed-display-settings="{"image_style":"","image_link":""}" data-entity-type="media" data-entity-uuid="' . $this->media
->uuid() . '"></drupal-entity>';
$this->host
->save();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('node/' . $this->host
->id() . '/edit');
$this
->waitForEditor();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertNotNull($this
->assertSession()
->waitForElementVisible('css', 'figcaption'));
}
public function providerCkeditorWidgetWorksForAllEmbeds() {
return [
'present and active CKEditor button ID' => [
'data-embed-button="test_media_entity_embed"',
],
'present and inactive CKEditor button ID' => [
'data-embed-button="user"',
],
'present and nonsensical CKEditor button ID' => [
'data-embed-button="ceci nest pas une pipe"',
],
'absent' => [
'',
],
];
}
protected function submitDialog() {
$this
->assertSession()
->elementExists('css', 'button.button--primary')
->press();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->getSession()
->switchToIFrame('ckeditor');
}
protected function setCaption($text) {
$this
->getSession()
->switchToIFrame();
$select_and_edit_caption = "var editor = CKEDITOR.instances['edit-body-0-value'];\n var figcaption = editor.widgets.getByElement(editor.editable().findOne('figcaption'));\n figcaption.editables.caption.setData('" . $text . "')";
$this
->getSession()
->executeScript($select_and_edit_caption);
}
protected function clickPathLinkByTitleAttribute($text) {
$this
->getSession()
->switchToIFrame();
$selector = '//span[@id="cke_1_path"]//a[@title="' . $text . '"]';
$this
->assertSession()
->elementExists('xpath', $selector)
->click();
}
}