View source
<?php
namespace Drupal\Tests\ckeditor5\FunctionalJavascript;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\Core\Database\Database;
use Drupal\editor\Entity\Editor;
use Drupal\file\Entity\File;
use Drupal\filter\Entity\FilterFormat;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\media\Entity\Media;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\Tests\ckeditor5\Traits\CKEditor5TestTrait;
use Drupal\ckeditor5\Plugin\Editor\CKEditor5;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Symfony\Component\Validator\ConstraintViolation;
class MediaTest extends WebDriverTestBase {
use CKEditor5TestTrait;
use MediaTypeCreationTrait;
use TestFileCreationTrait;
protected $adminUser;
protected $media;
protected $mediaFile;
protected $host;
protected static $modules = [
'ckeditor5',
'media',
'node',
'text',
'media_test_embed',
'media_library',
'ckeditor5_test',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
EntityViewMode::create([
'id' => 'media.view_mode_1',
'targetEntityType' => 'media',
'status' => TRUE,
'enabled' => TRUE,
'label' => 'View Mode 1',
])
->save();
EntityViewMode::create([
'id' => 'media.22222',
'targetEntityType' => 'media',
'status' => TRUE,
'enabled' => TRUE,
'label' => 'View Mode 2 has Numeric ID',
])
->save();
FilterFormat::create([
'format' => 'test_format',
'name' => 'Test format',
'filters' => [
'filter_html' => [
'status' => TRUE,
'settings' => [
'allowed_html' => '<p> <br> <strong> <em> <a href> <drupal-media data-entity-type data-entity-uuid data-align data-view-mode data-caption alt>',
],
],
'filter_align' => [
'status' => TRUE,
],
'filter_caption' => [
'status' => TRUE,
],
'media_embed' => [
'status' => TRUE,
'settings' => [
'default_view_mode' => 'view_mode_1',
'allowed_view_modes' => [
'view_mode_1' => 'view_mode_1',
'22222' => '22222',
],
'allowed_media_types' => [],
],
],
],
])
->save();
Editor::create([
'editor' => 'ckeditor5',
'format' => 'test_format',
'settings' => [
'toolbar' => [
'items' => [
'sourceEditing',
'link',
'bold',
'italic',
],
],
'plugins' => [
'ckeditor5_sourceEditing' => [
'allowed_tags' => [],
],
'media_media' => [
'allow_view_mode_override' => TRUE,
],
],
],
'image_upload' => [
'status' => FALSE,
],
])
->save();
$this
->assertSame([], array_map(function (ConstraintViolation $v) {
return (string) $v
->getMessage();
}, iterator_to_array(CKEditor5::validatePair(Editor::load('test_format'), FilterFormat::load('test_format')))));
$this->adminUser = $this
->drupalCreateUser([
'use text format test_format',
'bypass node access',
]);
$this
->createMediaType('image', [
'id' => 'image',
]);
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
->createMediaType('file', [
'id' => 'file',
]);
File::create([
'uri' => $this
->getTestFiles('text')[0]->uri,
])
->save();
$this->mediaFile = Media::create([
'bundle' => 'file',
'name' => 'Information about screaming hairy armadillo',
'field_media_file' => [
[
'target_id' => 2,
],
],
]);
$this->mediaFile
->save();
EntityViewDisplay::create([
'id' => 'media.image.view_mode_1',
'targetEntityType' => 'media',
'status' => TRUE,
'bundle' => 'image',
'mode' => 'view_mode_1',
])
->save();
EntityViewDisplay::create([
'id' => 'media.image.22222',
'targetEntityType' => 'media',
'status' => TRUE,
'bundle' => 'image',
'mode' => '22222',
])
->save();
$this
->drupalCreateContentType([
'type' => 'blog',
]);
$this->host = $this
->createNode([
'type' => 'blog',
'title' => 'Animals with strange names',
'body' => [
'value' => '<drupal-media data-entity-type="media" data-entity-uuid="' . $this->media
->uuid() . '" data-caption="baz"></drupal-media>',
'format' => 'test_format',
],
]);
$this->host
->save();
$this
->drupalLogin($this->adminUser);
}
public function testConversion() {
$original_value = $this->host->body->value;
$this->host->body->value = '<p>foo' . $original_value . '</p>';
$this->host
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$assert_session = $this
->assertSession();
$assert_session
->waitForElementVisible('css', 'img[src*="image-test.png"]', 1000);
$editor_html = $this
->getEditorDataAsHtmlString();
$this
->assertEquals('<p>foo</p>' . $original_value, str_replace(' ', '', $editor_html));
}
public function testOnlyDrupalMediaTagProcessed() {
$original_value = $this->host->body->value;
$this->host->body->value = str_replace('drupal-media', 'p', $original_value);
$this->host
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$assert_session = $this
->assertSession();
$this
->assertEmpty($assert_session
->waitForElementVisible('css', 'img[src*="image-test.png"]', 1000));
$assert_session
->elementNotExists('css', '.ck-widget.drupal-media');
$this->host->body->value = $original_value;
$this->host
->save();
$this
->getSession()
->reload();
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'img[src*="image-test.png"]'));
$assert_session
->elementExists('css', '.ck-widget.drupal-media');
}
public function testMediaArbitraryHtml() {
$assert_session = $this
->assertSession();
$editor = Editor::load('test_format');
$settings = $editor
->getSettings();
$settings['plugins']['ckeditor5_sourceEditing']['allowed_tags'] = [
'<drupal-media data-foo>',
'<div data-bar>',
];
$editor
->setSettings($settings);
$editor
->save();
$filter_format = $editor
->getFilterFormat();
$filter_format
->setFilterConfig('filter_html', [
'status' => TRUE,
'settings' => [
'allowed_html' => '<p> <br> <strong> <em> <a href> <drupal-media data-entity-type data-entity-uuid data-align data-caption alt data-foo data-view-mode> <div data-bar>',
],
]);
$filter_format
->save();
$this
->assertSame([], array_map(function (ConstraintViolation $v) {
return (string) $v
->getMessage();
}, iterator_to_array(CKEditor5::validatePair(Editor::load('test_format'), FilterFormat::load('test_format')))));
$original_value = $this->host->body->value;
$this->host->body->value = '<div data-bar="baz">' . str_replace('drupal-media', 'drupal-media data-foo="bar" ', $original_value) . '</div>';
$this->host
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->assertNotEmpty($upcasted_media = $assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media'));
$this
->assertFalse($upcasted_media
->hasAttribute('data-foo'));
$this
->assertNotEmpty($preview = $assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media > [data-drupal-media-preview="ready"] > .media', 30000));
$this
->assertEquals('bar', $preview
->getAttribute('data-foo'));
$assert_session
->elementExists('css', 'div[data-bar="baz"] > .drupal-media');
$this
->assertSourceAttributeSame('data-foo', 'bar');
$editor_dom = new \DOMXPath($this
->getEditorDataAsDom());
$this
->assertNotEmpty($editor_dom
->query('//div[@data-bar="baz"]/drupal-media'));
}
public function testLinkedMediaArbitraryHtml(bool $unrestricted) : void {
$assert_session = $this
->assertSession();
$editor = Editor::load('test_format');
$settings = $editor
->getSettings();
$filter_format = $editor
->getFilterFormat();
if ($unrestricted) {
$filter_format
->setFilterConfig('filter_html', [
'status' => FALSE,
]);
}
else {
$settings['plugins']['ckeditor5_sourceEditing']['allowed_tags'] = [
'<a data-foo>',
'<div data-bar>',
];
$editor
->setSettings($settings);
$filter_format
->setFilterConfig('filter_html', [
'status' => TRUE,
'settings' => [
'allowed_html' => '<p> <br> <strong> <em> <a href data-foo> <drupal-media data-entity-type data-entity-uuid data-align data-caption alt data-view-mode> <div data-bar>',
],
]);
}
$editor
->save();
$filter_format
->save();
$this
->assertSame([], array_map(function (ConstraintViolation $v) {
return (string) $v
->getMessage();
}, iterator_to_array(CKEditor5::validatePair(Editor::load('test_format'), FilterFormat::load('test_format')))));
$original_value = $this->host->body->value;
$this->host->body->value = '<div data-bar="baz"><a href="https://drupal.org" data-foo="bar">' . $original_value . '</a></div>';
$this->host
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->assertNotEmpty($link = $assert_session
->waitForElementVisible('css', 'a[href="https://drupal.org"]'));
$this
->assertEquals('bar', $link
->getAttribute('data-foo'));
$assert_session
->elementExists('css', 'div[data-bar="baz"] > .drupal-media > a[href="https://drupal.org"] > div[data-drupal-media-preview]');
$editor_dom = new \DOMXPath($this
->getEditorDataAsDom());
$this
->assertNotEmpty($editor_dom
->query('//div[@data-bar="baz"]/a[@data-foo="bar"]/drupal-media'));
}
public function testErrorMessages() {
if (Database::getConnection()
->driver() === 'sqlite') {
$this
->markTestSkipped('Test frequently causes a locked database on SQLite');
}
$this->container
->get('state')
->set('test_media_filter_controller_throw_error', TRUE);
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$assert_session = $this
->assertSession();
$assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media');
$this
->assertEmpty($assert_session
->waitForElementVisible('css', 'img[src*="image-test.png"]', 1000));
$assert_session
->elementNotExists('css', '.ck-widget.drupal-media .media');
$this
->assertNotEmpty($assert_session
->waitForText('An error occurred while trying to preview the media. Please save your work and reload this page.'));
$this->container
->get('state')
->set('test_media_filter_controller_throw_error', FALSE);
$this
->getSession()
->reload();
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'img[src*="image-test.png"]'));
$original_value = $this->host->body->value;
$this->host->body->value = str_replace($this->media
->uuid(), 'invalid_uuid', $original_value);
$this->host
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ck-widget.drupal-media .this-error-message-is-themeable'));
$this
->assertTrue($this->container
->get('theme_installer')
->install([
'classy',
]));
$this
->config('system.theme')
->set('default', 'classy')
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ck-widget.drupal-media .this-error-message-is-themeable.media-embed-error--missing-source'));
$this->host->body->value = $original_value;
$this->host
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'img[src*="image-test.png"]'));
$assert_session
->elementNotExists('css', '.ck-widget.drupal-media .this-error-message-is-themeable');
}
public function testPreviewUsesDefaultThemeAndIsClientCacheable() {
$this
->config('node.settings')
->set('use_admin_theme', TRUE)
->save();
$this->adminUser
->addRole($this
->drupalCreateRole([
'view the administration theme',
]));
$this->adminUser
->save();
$this
->config('system.theme')
->set('default', 'stable')
->set('admin', 'classy')
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$assert_session = $this
->assertSession();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'img[src*="image-test.png"]'));
$element = $assert_session
->elementExists('css', '[data-media-embed-test-active-theme]');
$this
->assertSame('stable', $element
->getAttribute('data-media-embed-test-active-theme'));
$this
->assertGreaterThan(500, $this
->getLastPreviewRequestTransferSize());
$this
->pressEditorButton('Source');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ck-source-editing-area'));
$source_text_area = $this
->getSession()
->getPage()
->find('css', '[name="body[0][value]"] + .ck-editor textarea');
$source_text_area
->setValue($source_text_area
->getValue() . ' ');
$this
->pressEditorButton('Source');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'img[src*="image-test.png"]'));
$this
->assertSame(0, $this
->getLastPreviewRequestTransferSize());
}
public function testEditableCaption() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$original_value = $this->host->body->value;
$this->host->body->value = str_replace('data-caption="baz"', 'data-caption=""', $original_value);
$this->host
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img'));
$assert_session
->elementExists('css', '[data-drupal-media-preview][aria-label="Screaming hairy armadillo"]');
$assert_session
->elementContains('css', 'figcaption', '');
$assert_session
->elementAttributeContains('css', 'figcaption', 'data-placeholder', 'Enter media caption');
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('Override media image alternative text')
->click();
$this
->assertVisibleBalloon('.ck-media-alternative-text-form');
$alt_override_input = $page
->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
$alt_override_input
->setValue('Gold star for robot boy.');
$this
->getBalloonButton('Save')
->click();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.drupal-media img[alt*="Gold star for robot boy."]'));
$this
->assertEquals('', $assert_session
->waitForElement('css', '.drupal-media figcaption')
->getText());
$assert_session
->elementAttributeContains('css', '.drupal-media figcaption', 'data-placeholder', 'Enter media caption');
$original_value = $this->host->body->value;
$this->host->body->value = str_replace('data-caption=""', 'data-caption="baz"', $original_value);
$this->host
->save();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img'));
$this
->assertNotEmpty($figcaption = $assert_session
->waitForElement('css', '.drupal-media figcaption'));
$this
->assertSame('baz', $figcaption
->getHtml());
$this
->selectTextInsideElement('.drupal-media figcaption');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$expected_buttons = [
'Toggle caption off',
'Link media',
'Override media image alternative text',
'Break text',
];
foreach ($expected_buttons as $expected_button) {
$this
->assertNotEmpty($this
->getBalloonButton($expected_button));
}
$this
->getBalloonButton('Toggle caption off')
->click();
$assert_session
->assertNoElementAfterWait('css', 'figcaption');
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('Toggle caption on')
->click();
$this
->assertNotEmpty($figcaption = $assert_session
->waitForElementVisible('css', '.drupal-media figcaption'));
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->selectTextInsideElement('.drupal-media figcaption');
$figcaption
->setValue('Llamas are the most awesome ever');
$editor_dom = $this
->getEditorDataAsDom();
$this
->assertEquals('Llamas are the most awesome ever', $editor_dom
->getElementsByTagName('drupal-media')
->item(0)
->getAttribute('data-caption'));
$this
->assertNotEmpty($figcaption = $assert_session
->waitForElement('css', '.drupal-media figcaption'));
$this
->selectTextInsideElement('.drupal-media figcaption');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.drupal-media figcaption.ck-editor__nested-editable'));
$this
->pressEditorButton('Bold');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.drupal-media figcaption > strong'));
$this
->assertEquals('<strong>Llamas are the most awesome ever</strong>', $figcaption
->getHtml());
$editor_dom = $this
->getEditorDataAsDom();
$this
->assertEquals('<strong>Llamas are the most awesome ever</strong>', $editor_dom
->getElementsByTagName('drupal-media')
->item(0)
->getAttribute('data-caption'));
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.drupal-media figcaption > strong'));
$this
->selectTextInsideElement('.drupal-media figcaption > strong');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.drupal-media figcaption.ck-editor__nested-editable'));
$this
->pressEditorButton('Bold');
$this
->assertTrue($assert_session
->waitForElementRemoved('css', '.drupal-media figcaption > strong'));
$this
->assertNotEmpty($figcaption = $assert_session
->waitForElement('css', '.drupal-media figcaption'));
$this
->assertEquals('Llamas are the most awesome ever', $figcaption
->getHtml());
$editor_dom = $this
->getEditorDataAsDom();
$this
->assertEquals('Llamas are the most awesome ever', $editor_dom
->getElementsByTagName('drupal-media')
->item(0)
->getAttribute('data-caption'));
$this
->assertNotEmpty($figcaption = $assert_session
->waitForElement('css', '.drupal-media figcaption'));
$this
->selectTextInsideElement('.drupal-media figcaption');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.drupal-media figcaption.ck-editor__nested-editable'));
$this
->pressEditorButton('Link');
$this
->assertVisibleBalloon('.ck-link-form');
$link_input = $page
->find('css', '.ck-balloon-panel .ck-link-form input[type=text]');
$link_input
->setValue('https://drupal.org');
$page
->find('css', '.ck-balloon-panel .ck-link-form button[type=submit]')
->click();
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.drupal-media figcaption > a'));
$this
->assertEquals('<a class="ck-link_selected" href="https://drupal.org">Llamas are the most awesome ever</a>', $figcaption
->getHtml());
$editor_dom = $this
->getEditorDataAsDom();
$this
->assertEquals('<a href="https://drupal.org">Llamas are the most awesome ever</a>', $editor_dom
->getElementsByTagName('drupal-media')
->item(0)
->getAttribute('data-caption'));
}
public function testDialogAccess() {
$this
->markTestSkipped('Blocked on https://www.drupal.org/project/ckeditor5/issues/3245720.');
}
public function testAlt() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img'));
$this
->assertSourceAttributeSame('alt', NULL);
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img[alt*="default alt"]'));
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('Override media image alternative text')
->click();
$this
->assertVisibleBalloon('.ck-media-alternative-text-form');
$assert_session
->elementTextEquals('css', '.ck-media-alternative-text-form__default-alt-text-value', 'default alt');
$alt_override_input = $page
->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
$this
->assertSame('', $alt_override_input
->getValue());
$who_is_zartan = 'Zartan is the leader of the Dreadnoks.';
$alt_override_input
->setValue($who_is_zartan);
$this
->getBalloonButton('Save')
->click();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img[alt*="' . $who_is_zartan . '"]'));
$assert_session
->elementNotExists('css', '.ck-widget.drupal-media[alt]');
$assert_session
->elementExists('css', '.ck-widget.drupal-media [aria-label="Screaming hairy armadillo"]');
$this
->assertSourceAttributeSame('alt', $who_is_zartan);
$this
->getBalloonButton('Override media image alternative text')
->click();
$this
->assertVisibleBalloon('.ck-media-alternative-text-form');
$alt_override_input = $page
->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
$this
->assertSame($who_is_zartan, $alt_override_input
->getValue());
$assert_session
->elementTextEquals('css', '.ck-media-alternative-text-form__default-alt-text-value', 'default alt');
$cobra_commander_bio = 'The supreme leader of the terrorist organization Cobra';
$alt_override_input
->setValue($cobra_commander_bio);
$this
->getBalloonButton('Save')
->click();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img[alt*="' . $cobra_commander_bio . '"]'));
$this
->assertSourceAttributeSame('alt', $cobra_commander_bio);
$this
->getBalloonButton('Override media image alternative text')
->click();
$this
->assertVisibleBalloon('.ck-media-alternative-text-form');
$alt_override_input = $page
->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
$this
->assertSame($cobra_commander_bio, $alt_override_input
->getValue());
$alt_override_input
->setValue('"" ');
$this
->getBalloonButton('Save')
->click();
$this
->assertSourceAttributeSame('alt', '""');
$this
->getBalloonButton('Override media image alternative text')
->click();
$this
->assertVisibleBalloon('.ck-media-alternative-text-form');
$alt_override_input = $page
->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
$alt_override_input
->setValue('');
$this
->getBalloonButton('Save')
->click();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img[alt*="default alt"]'));
$this
->assertSourceAttributeSame('alt', NULL);
}
public function testTranslationAlt() {
\Drupal::service('module_installer')
->install([
'language',
'content_translation',
]);
$this
->resetAll();
ConfigurableLanguage::create([
'id' => 'fr',
])
->save();
ContentLanguageSettings::loadByEntityTypeBundle('media', 'image')
->setDefaultLangcode('en')
->setLanguageAlterable(TRUE)
->save();
$media = Media::create([
'bundle' => 'image',
'name' => 'Screaming hairy armadillo',
'field_media_image' => [
[
'target_id' => 1,
'alt' => 'default alt',
'title' => 'default title',
],
],
]);
$media
->save();
$media_fr = $media
->addTranslation('fr');
$media_fr->name = "Tatou poilu hurlant";
$media_fr->field_media_image
->setValue([
[
'target_id' => '1',
'alt' => "texte alternatif par défaut",
'title' => "titre alternatif par défaut",
],
]);
$media_fr
->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', 'blog')
->setDefaultLangcode('en')
->setLanguageAlterable(TRUE)
->save();
$host = $this
->createNode([
'type' => 'blog',
'title' => 'Animals with strange names',
'body' => [
'value' => '<drupal-media data-caption="baz" data-entity-type="media" data-entity-uuid="' . $media
->uuid() . '"></drupal-media>',
'format' => 'test_format',
],
]);
$host
->save();
$translation = $host
->addTranslation('fr');
$translation->title = 'Animaux avec des noms étranges';
$translation->body->value = $host->body->value;
$translation->body->format = $host->body->format;
$translation
->save();
Role::load(RoleInterface::AUTHENTICATED_ID)
->grantPermission('translate any entity')
->save();
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('/fr/node/' . $host
->id() . '/edit');
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('xpath', '//img[contains(@alt, "texte alternatif par défaut")]'));
$assert_session
->elementExists('css', '[data-drupal-media-preview][aria-label="Tatou poilu hurlant"]');
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('Override media image alternative text')
->click();
$this
->assertVisibleBalloon('.ck-media-alternative-text-form');
$assert_session
->elementTextEquals('css', '.ck-media-alternative-text-form__default-alt-text-value', 'texte alternatif par défaut');
$qui_est_zartan = 'Zartan est le chef des Dreadnoks.';
$alt_override_input = $page
->find('css', '.ck-balloon-panel .ck-media-alternative-text-form input[type=text]');
$alt_override_input
->setValue($qui_est_zartan);
$this
->getBalloonButton('Save')
->click();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('xpath', '//img[contains(@alt, "' . $qui_est_zartan . '")]'));
$this
->getSession()
->switchToIFrame();
$page
->pressButton('Save');
$assert_session
->elementExists('xpath', '//img[contains(@alt, "' . $qui_est_zartan . '")]');
}
public function testLinkability(bool $unrestricted) {
if ($unrestricted) {
FilterFormat::load('test_format')
->setFilterConfig('filter_html', [
'status' => FALSE,
])
->save();
}
$page = $this
->getSession()
->getPage();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$assert_session = $this
->assertSession();
$drupalmedia = $assert_session
->waitForElementVisible('css', '.ck-content .ck-widget.drupal-media');
$this
->assertNotEmpty($drupalmedia);
$this
->assertFalse($drupalmedia
->hasClass('.ck-widget_selected'));
$assert_session
->elementExists('css', '.ck-content .ck-widget.drupal-media > [data-drupal-media-preview]');
$xpath = new \DOMXPath($this
->getEditorDataAsDom());
$this
->assertNotEmpty($xpath
->query('//drupal-media'));
$this
->assertEmpty($xpath
->query('//a'));
$link_button = $this
->getEditorButton('Link');
$this
->assertSame('false', $link_button
->getAttribute('aria-pressed'));
$preview = $assert_session
->waitForElement('css', '.ck-content .ck-widget.drupal-media [data-drupal-media-preview="ready"]');
$this
->assertNotEmpty($preview);
$drupalmedia
->click();
$this
->assertTrue($drupalmedia
->hasClass('ck-widget_selected'));
$this
->assertEditorButtonEnabled('Link');
$this
->assertVisibleBalloon('.ck-toolbar[aria-label="Drupal Media toolbar"]');
$link_media_button = $this
->getBalloonButton('Link media');
$this
->assertSame('false', $link_media_button
->getAttribute('aria-pressed'));
$link_media_button
->press();
$balloon = $this
->assertVisibleBalloon('.ck-link-form');
$url_input = $balloon
->find('css', '.ck-labeled-field-view__input-wrapper .ck-input-text');
$url_input
->setValue('http://linking-embedded-media.com');
$balloon
->pressButton('Save');
$assert_session
->elementExists('css', '.ck-content a[href="http://linking-embedded-media.com"]');
$assert_session
->elementExists('css', '.ck-content .drupal-media.ck-widget > a[href="http://linking-embedded-media.com"] > div[aria-label] > article > div > img[src*="image-test.png"]');
$xpath = new \DOMXPath($this
->getEditorDataAsDom());
$this
->assertNotEmpty($xpath
->query('//drupal-media'));
$this
->assertNotEmpty($xpath
->query('//a[@href="http://linking-embedded-media.com"]'));
$this
->assertNotEmpty($xpath
->query('//a[@href="http://linking-embedded-media.com"]/drupal-media'));
$this
->assertNotEmpty($xpath
->query('//a[@href="http://linking-embedded-media.com"]/drupal-media[@data-caption="baz"]'));
$this
->assertEmpty($xpath
->query('//a[@href="http://linking-embedded-media.com" and @class="trusted"]'));
$this
->pressEditorButton('Source');
$source_text_area = $assert_session
->waitForElement('css', '.ck-source-editing-area textarea');
$this
->assertNotEmpty($source_text_area);
$new_value = str_replace('<a ', '<a class="trusted" ', $source_text_area
->getValue());
$source_text_area
->setValue('<p>temp</p>');
$source_text_area
->setValue($new_value);
$this
->pressEditorButton('Source');
$xpath = new \DOMXPath($this
->getEditorDataAsDom());
$this
->assertCount($unrestricted ? 1 : 0, $xpath
->query('//a[@href="http://linking-embedded-media.com" and @class="trusted"]'));
$page
->pressButton('Save');
$assert_session
->elementExists('css', $unrestricted ? 'a[href="http://linking-embedded-media.com"].trusted img[src*="image-test.png"]' : 'a[href="http://linking-embedded-media.com"] img[src*="image-test.png"]');
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$xpath = new \DOMXPath($this
->getEditorDataAsDom());
$this
->assertNotEmpty($xpath
->query('//drupal-media'));
$this
->assertNotEmpty($xpath
->query('//a[@href="http://linking-embedded-media.com"]'));
$this
->assertNotEmpty($xpath
->query('//a[@href="http://linking-embedded-media.com"]/drupal-media'));
$drupalmedia
->click();
$this
->assertEditorButtonEnabled('Link');
$this
->assertSame('true', $this
->getEditorButton('Link')
->getAttribute('aria-pressed'));
$this
->assertVisibleBalloon('.ck-toolbar[aria-label="Drupal Media toolbar"]');
$link_media_button = $this
->getBalloonButton('Link media');
$this
->assertSame('true', $link_media_button
->getAttribute('aria-pressed'));
$link_media_button
->click();
$this
->getBalloonButton('Edit link');
$unlink_image_button = $this
->getBalloonButton('Unlink');
$unlink_image_button
->click();
$this
->assertSame('false', $this
->getEditorButton('Link')
->getAttribute('aria-pressed'));
$assert_session
->elementNotExists('css', '.ck-content a');
$assert_session
->elementExists('css', '.ck-content .drupal-media.ck-widget > div[aria-label] > article > div > img[src*="image-test.png"]');
$assert_session
->elementExists('css', '.ck-content .drupal-media.ck-widget > figcaption');
$xpath = new \DOMXPath($this
->getEditorDataAsDom());
$this
->assertNotEmpty($xpath
->query('//drupal-media'));
$this
->assertEmpty($xpath
->query('//a'));
}
public function providerLinkability() : array {
return [
'restricted' => [
FALSE,
],
'unrestricted' => [
TRUE,
],
];
}
public function testLinkManualDecorator(bool $unrestricted) {
\Drupal::service('module_installer')
->install([
'ckeditor5_manual_decorator_test',
]);
$this
->resetAll();
$decorator = 'Open in a new tab';
$decorator_attributes = '[@target="_blank"][@rel="noopener noreferrer"][@class="link-new-tab"]';
if ($unrestricted) {
FilterFormat::load('test_format')
->setFilterConfig('filter_html', [
'status' => FALSE,
])
->save();
$decorator = 'Pink color';
$decorator_attributes = '[@style="color:pink;"]';
}
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->assertNotEmpty($drupalmedia = $assert_session
->waitForElementVisible('css', '.ck-content .ck-widget.drupal-media'));
$drupalmedia
->click();
$this
->assertVisibleBalloon('.ck-toolbar[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('Link media')
->click();
$balloon = $this
->assertVisibleBalloon('.ck-link-form');
$url_input = $balloon
->find('css', '.ck-labeled-field-view__input-wrapper .ck-input-text');
$url_input
->setValue('http://linking-embedded-media.com');
$this
->getBalloonButton($decorator)
->click();
$balloon
->pressButton('Save');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.drupal-media a'));
$this
->assertVisibleBalloon('.ck-link-actions');
$xpath = new \DOMXPath($this
->getEditorDataAsDom());
$this
->assertNotEmpty($xpath
->query("//a[@href='http://linking-embedded-media.com']{$decorator_attributes}"));
$this
->assertNotEmpty($xpath
->query("//a[@href='http://linking-embedded-media.com']{$decorator_attributes}/drupal-media"));
$page
->pressButton('Save');
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->assertNotEmpty($drupalmedia = $assert_session
->waitForElementVisible('css', '.ck-content .ck-widget.drupal-media'));
$xpath = new \DOMXPath($this
->getEditorDataAsDom());
$this
->assertNotEmpty($xpath
->query("//a[@href='http://linking-embedded-media.com']{$decorator_attributes}"));
$this
->assertNotEmpty($xpath
->query("//a[@href='http://linking-embedded-media.com']{$decorator_attributes}/drupal-media"));
$drupalmedia
->click();
$this
->assertVisibleBalloon('.ck-toolbar[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('Link media')
->click();
$this
->assertVisibleBalloon('.ck-link-actions');
$this
->getBalloonButton('Unlink')
->click();
$this
->assertTrue($assert_session
->waitForElementRemoved('css', '.drupal-media a'));
$xpath = new \DOMXPath($this
->getEditorDataAsDom());
$this
->assertEmpty($xpath
->query('//a'));
$this
->assertNotEmpty($xpath
->query('//drupal-media'));
}
public function testEmbedPreviewAccess($media_embed_enabled, $can_use_format) {
$format = FilterFormat::load($this->host->body->format);
$format
->set('filters', [
'filter_align' => [
'status' => TRUE,
],
'filter_caption' => [
'status' => TRUE,
],
'media_embed' => [
'status' => $media_embed_enabled,
],
]);
$format
->save();
$permissions = [
'bypass node access',
];
if ($can_use_format) {
$permissions[] = $format
->getPermissionName();
}
$this
->drupalLogin($this
->drupalCreateUser($permissions));
$this
->drupalGet($this->host
->toUrl('edit-form'));
$assert_session = $this
->assertSession();
if ($can_use_format) {
$this
->waitForEditor();
if ($media_embed_enabled) {
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'article.media'));
}
else {
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->elementNotExists('css', 'article.media');
}
}
else {
$assert_session
->pageTextContains('This field has been disabled because you do not have sufficient permissions to edit it.');
}
}
public function previewAccessProvider() {
return [
'media_embed filter enabled' => [
TRUE,
TRUE,
],
'media_embed filter disabled' => [
FALSE,
TRUE,
],
'media_embed filter enabled, user not allowed to use text format' => [
TRUE,
FALSE,
],
];
}
public function testAlignment() {
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img'));
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->assertTrue(($align_button = $this
->getBalloonButton('Break text'))
->hasClass('ck-on'));
$editor_dom = $this
->getEditorDataAsDom();
$drupal_media_element = $editor_dom
->getElementsByTagName('drupal-media')
->item(0);
$this
->assertFalse($drupal_media_element
->hasAttribute('data-align'));
$this
->getBalloonButton('Align center and break text')
->click();
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ck-widget.drupal-media.drupal-media-style-align-center'));
$editor_dom = $this
->getEditorDataAsDom();
$drupal_media_element = $editor_dom
->getElementsByTagName('drupal-media')
->item(0);
$this
->assertEquals('center', $drupal_media_element
->getAttribute('data-align'));
$page
->pressButton('Save');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.messages.messages--status'));
$assert_session
->elementExists('css', 'figure.align-center');
$edit_url = $this
->getSession()
->getCurrentURL() . '/edit';
$this
->drupalGet($edit_url);
$this
->waitForEditor();
$assert_session
->elementExists('css', '.ck-widget.drupal-media.drupal-media-style-align-center');
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->assertTrue($this
->getBalloonButton('Align center and break text')
->hasClass('ck-on'));
$this
->getBalloonButton('Break text')
->click();
$this
->assertTrue($assert_session
->waitForElementRemoved('css', '.ck-widget.drupal-media.drupal-media-style-align-center'));
$editor_dom = $this
->getEditorDataAsDom();
$drupal_media_element = $editor_dom
->getElementsByTagName('drupal-media')
->item(0);
$this
->assertFalse($drupal_media_element
->hasAttribute('data-align'));
}
public function testDrupalMediaStyleInDropdown() {
\Drupal::service('module_installer')
->install([
'ckeditor5_drupalelementstyle_test',
]);
$this
->resetAll();
$assert_session = $this
->assertSession();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img'));
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->assertNotEmpty(($split_button = $this
->getBalloonButton('Test title: Break text'))
->hasClass('ck-on'));
$split_button
->click();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-dropdown__panel-visible'));
$this
->assertNotEmpty($align_button = $this
->getBalloonButton('Align center and break text'));
$align_button
->click();
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ck-widget.drupal-media.drupal-media-style-align-center'));
$editor_dom = $this
->getEditorDataAsDom();
$drupal_media_element = $editor_dom
->getElementsByTagName('drupal-media')
->item(0);
$this
->assertEquals('center', $drupal_media_element
->getAttribute('data-align'));
$this
->assertNotEmpty($this
->getBalloonButton('Test title: Align center and break text')
->hasClass('ck-on'));
}
public function testDrupalMediaStyleWithClass() {
$editor = Editor::load('test_format');
$editor
->setSettings([
'toolbar' => [
'items' => [
'heading',
'sourceEditing',
'simpleBox',
],
],
'plugins' => [
'ckeditor5_heading' => [
'enabled_headings' => [
'heading1',
],
],
'ckeditor5_sourceEditing' => [
'allowed_tags' => [
'<div>',
'<section>',
],
],
'media_media' => [
'allow_view_mode_override' => TRUE,
],
],
]);
$filter_format = $editor
->getFilterFormat();
$filter_format
->setFilterConfig('filter_html', [
'status' => TRUE,
'settings' => [
'allowed_html' => '<p> <br> <h1 class> <div class> <section class> <drupal-media data-entity-type data-entity-uuid data-align data-caption data-view-mode alt class="layercake-side">',
],
]);
$filter_format
->save();
$editor
->save();
$this
->assertSame([], array_map(function (ConstraintViolation $v) {
return (string) $v
->getMessage();
}, iterator_to_array(CKEditor5::validatePair(Editor::load('test_format'), FilterFormat::load('test_format')))));
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$page
->pressButton('Source');
$editor_dom = $this
->getEditorDataAsDom();
$drupal_media_element = $editor_dom
->getElementsByTagName('drupal-media')
->item(0);
$drupal_media_element
->setAttribute('class', 'layercake-side arbitrary-class');
$textarea = $page
->find('css', '.ck-source-editing-area > textarea');
$textarea
->setValue($editor_dom
->C14N());
$page
->pressButton('Source');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ck-widget.drupal-media.layercake-side'));
$assert_session
->elementNotExists('css', '.ck-widget.drupal-media.arbitrary-class');
$page
->pressButton('Save');
$assert_session
->waitForElementVisible('css', 'messages messages--status');
$assert_session
->elementExists('css', 'figure.layercake-side');
$assert_session
->elementNotExists('css', 'figure.arbitrary-class');
}
public function testViewMode(bool $with_alignment) {
EntityViewMode::create([
'id' => 'media.view_mode_3',
'targetEntityType' => 'media',
'status' => TRUE,
'enabled' => TRUE,
'label' => 'View Mode 3',
])
->save();
EntityViewMode::create([
'id' => 'media.view_mode_4',
'targetEntityType' => 'media',
'status' => TRUE,
'enabled' => TRUE,
'label' => 'View Mode 4',
])
->save();
EntityViewDisplay::create([
'id' => 'media.image.view_mode_4',
'targetEntityType' => 'media',
'status' => TRUE,
'bundle' => 'image',
'mode' => 'view_mode_4',
])
->save();
$filter_format = FilterFormat::load('test_format');
$filter_format
->setFilterConfig('media_embed', [
'status' => TRUE,
'settings' => [
'default_view_mode' => 'view_mode_1',
'allowed_media_types' => [],
'allowed_view_modes' => [
'view_mode_1' => 'view_mode_1',
'22222' => '22222',
'view_mode_3' => 'view_mode_3',
],
],
])
->save();
if (!$with_alignment) {
$filter_format
->filters('filter_align')
->setConfiguration(array_merge($filter_format
->filters('filter_align')
->getConfiguration(), [
'status' => FALSE,
]));
}
$expected_config_dependencies = [
'core.entity_view_mode.media.view_mode_1',
'core.entity_view_mode.media.22222',
'core.entity_view_mode.media.view_mode_3',
];
$dependencies = $filter_format
->getDependencies();
$this
->assertArrayHasKey('config', $dependencies);
$this
->assertEqualsCanonicalizing($expected_config_dependencies, $dependencies['config']);
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$this
->drupalGet($this->host
->toUrl('edit-form'));
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img'));
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$editor_dom = $this
->getEditorDataAsDom();
$drupal_media_element = $editor_dom
->getElementsByTagName('drupal-media')
->item(0);
$this
->assertFalse($drupal_media_element
->hasAttribute('data-view-mode'));
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('View Mode 1')
->click();
$this
->getBalloonButton('View Mode 2 has Numeric ID')
->click();
$editor_dom = $this
->getEditorDataAsDom();
$drupal_media_element = $editor_dom
->getElementsByTagName('drupal-media')
->item(0);
$this
->assertEquals('22222', $drupal_media_element
->getAttribute('data-view-mode'));
$dropdown_button = $page
->find('css', 'button.ck-dropdown__button > span.ck-button__label');
$this
->assertEquals('View Mode 2 has Numeric ID', $dropdown_button
->getText());
$this
->pressEditorButton('Source');
$this
->pressEditorButton('Source');
$this
->click('.ck-widget.drupal-media');
$dropdown_button = $page
->find('css', 'button.ck-dropdown__button > span.ck-button__label');
$this
->assertEquals('View Mode 2 has Numeric ID', $dropdown_button
->getText());
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('Toggle caption off')
->click();
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('Toggle caption on')
->click();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.drupal-media figcaption'));
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$dropdown_button = $page
->find('css', 'button.ck-dropdown__button > span.ck-button__label');
$this
->assertEquals('View Mode 2 has Numeric ID', $dropdown_button
->getText());
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('View Mode 2 has Numeric ID')
->click();
$this
->getBalloonButton('View Mode 1')
->click();
$this
->waitForEditor();
$editor_dom = $this
->getEditorDataAsDom();
$drupal_media_element = $editor_dom
->getElementsByTagName('drupal-media')
->item(0);
$this
->assertFalse($drupal_media_element
->hasAttribute('data-view-mode'));
$assert_session
->elementExists('css', 'article.media--view-mode-view-mode-1');
$dropdown_button = $page
->find('css', 'button.ck-dropdown__button > span.ck-button__label');
$this
->assertEquals('View Mode 1', $dropdown_button
->getText());
$filter_format
->setFilterConfig('media_embed', [
'status' => TRUE,
'settings' => [
'default_view_mode' => 'view_mode_1',
'allowed_media_types' => [],
'allowed_view_modes' => [
'view_mode_1' => 'view_mode_1',
],
],
])
->save();
$dependencies = $filter_format
->getDependencies();
$this
->assertArrayHasKey('config', $dependencies);
$this
->assertSame([
'core.entity_view_mode.media.view_mode_1',
], $dependencies['config']);
$this
->getSession()
->reload();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img'));
$this
->click('.ck-widget.drupal-media');
$this
->assertEmpty($assert_session
->waitForElementVisible('css', '.ck.ck-dropdown', 1000));
$editor_dom = $this
->getEditorDataAsDom();
$drupal_media_element = $editor_dom
->getElementsByTagName('drupal-media')
->item(0);
$this
->assertFalse($drupal_media_element
->hasAttribute('data-view-mode'));
$assert_session
->elementExists('css', 'article.media--view-mode-view-mode-1');
$filter_format
->setFilterConfig('media_embed', [
'status' => TRUE,
'settings' => [
'default_view_mode' => 'view_mode_1',
'allowed_media_types' => [],
'allowed_view_modes' => [
'view_mode_1' => 'view_mode_1',
'22222' => '22222',
],
],
])
->save();
$expected_config_dependencies = [
'core.entity_view_mode.media.view_mode_1',
'core.entity_view_mode.media.22222',
];
$dependencies = $filter_format
->getDependencies();
$this
->assertArrayHasKey('config', $dependencies);
$this
->assertEqualsCanonicalizing($expected_config_dependencies, $dependencies['config']);
$this
->getSession()
->reload();
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.drupal-media figcaption'));
$original_value = $this->host->body->value;
$this->host->body->value = str_replace('data-caption="baz"', '', $original_value);
$this->host
->save();
$this
->getSession()
->reload();
$this
->waitForEditor();
$assert_session
->elementExists('css', 'article.media--view-mode-view-mode-1');
$this
->assertEmpty($assert_session
->waitForElementVisible('css', '.drupal-media figcaption'));
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->getBalloonButton('View Mode 1')
->click();
$this
->getBalloonButton('View Mode 2 has Numeric ID')
->click();
$assert_session
->elementExists('css', 'article.media--view-mode-_2222');
$this
->assertEmpty($assert_session
->waitForElementVisible('css', '.drupal-media figcaption'));
$filter_format
->setFilterConfig('media_embed', [
'status' => TRUE,
'settings' => [
'default_view_mode' => 'view_mode_1',
'allowed_media_types' => [],
'allowed_view_modes' => [],
],
])
->save();
$dependencies = $filter_format
->getDependencies();
$this
->assertArrayHasKey('config', $dependencies);
$this
->assertSame([
'core.entity_view_mode.media.view_mode_1',
], $dependencies['config']);
$this->host->body->value = '<drupal-media data-caption="armadillo" data-entity-type="media" data-entity-uuid="' . $this->mediaFile
->uuid() . '"></drupal-media>';
$this->host
->save();
$this
->getSession()
->reload();
$this
->waitForEditor();
$assert_session
->waitForElementVisible('css', 'article.media--view-mode-view-mode-1');
$filter_format
->setFilterConfig('media_embed', [
'status' => TRUE,
'settings' => [
'default_view_mode' => 'view_mode_1',
'allowed_media_types' => [],
'allowed_view_modes' => [
'22222' => '22222',
'view_mode_4' => 'view_mode_4',
],
],
])
->save();
$expected_config_dependencies = [
'core.entity_view_mode.media.22222',
'core.entity_view_mode.media.view_mode_1',
'core.entity_view_mode.media.view_mode_4',
];
$dependencies = $filter_format
->getDependencies();
$this
->assertArrayHasKey('config', $dependencies);
$this
->assertEqualsCanonicalizing($expected_config_dependencies, $dependencies['config']);
$this->host->body->value = '<drupal-media data-entity-type="media" data-entity-uuid="' . $this->media
->uuid() . '" data-caption="baz"></drupal-media>';
$this->host
->save();
$this
->getSession()
->reload();
$this
->waitForEditor();
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.ck-widget.drupal-media img'));
$this
->click('.ck-widget.drupal-media');
$this
->assertVisibleBalloon('[aria-label="Drupal Media toolbar"]');
$this
->click('.ck-widget.drupal-media');
$this
->assertNotEmpty($this
->getBalloonButton('View Mode 1'));
$this
->assertNotEmpty($this
->getBalloonButton('View Mode 2 has Numeric ID'));
$this
->assertNotEmpty($this
->getBalloonButton('View Mode 4'));
}
public function providerTestViewMode() : array {
return [
'with alignment' => [
TRUE,
],
'without alignment' => [
FALSE,
],
];
}
protected function assertSourceAttributeSame(string $attribute, ?string $value) : void {
$dom = $this
->getEditorDataAsDom();
$drupal_media = (new \DOMXPath($dom))
->query('//drupal-media');
$this
->assertNotEmpty($drupal_media);
if ($value === NULL) {
$this
->assertFalse($drupal_media[0]
->hasAttribute($attribute));
}
else {
$this
->assertSame($value, $drupal_media[0]
->getAttribute($attribute));
}
}
protected function getLastPreviewRequestTransferSize() {
$javascript = <<<JS
(function(){
return window.performance
.getEntries()
.filter(function (entry) {
return entry.initiatorType == 'fetch' && entry.name.indexOf('/media/test_format/preview') !== -1;
})
.pop()
.transferSize;
})()
JS;
return $this
->getSession()
->evaluateScript($javascript);
}
}