View source
<?php
namespace Drupal\Tests\ckeditor5\FunctionalJavascript;
use Drupal\Core\Entity\Entity\EntityViewMode;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Symfony\Component\Yaml\Yaml;
class CKEditor5AllowedTagsTest extends CKEditor5TestBase {
protected static $modules = [
'node',
'ckeditor',
'ckeditor5',
'media',
'media_library',
'ckeditor5_incompatible_filter_test',
];
protected $allowedElements = '<br> <p> <h2> <h3> <h4> <h5> <h6> <strong> <em>';
protected $defaultElementsWhenUpdatingNotCkeditor5 = '<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <img src alt data-entity-type data-entity-uuid>';
protected $defaultElementsAfterUpdatingToCkeditor5 = '<br> <p> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <cite> <dl> <dt> <dd> <img src alt data-entity-type data-entity-uuid> <a hreflang href> <blockquote cite> <ul type> <ol type start> <strong> <em> <code> <li>';
public function testEnablingToVersion5Validation() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$incompatible_filter_name = 'filters[filter_incompatible][status]';
$filter_warning = 'CKEditor 5 only works with HTML-based text formats. The "A TYPE_MARKUP_LANGUAGE filter incompatible with CKEditor 5" (filter_incompatible) filter implies this text format is not HTML anymore.';
$this
->createNewTextFormat($page, $assert_session, 'ckeditor');
$page
->selectFieldOption('editor[editor]', 'ckeditor');
$assert_session
->assertWaitOnAjaxRequest();
$page
->checkField('filters[filter_html][status]');
$page
->checkField($incompatible_filter_name);
$assert_session
->assertWaitOnAjaxRequest();
$page
->selectFieldOption('editor[editor]', 'ckeditor5');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextContains($filter_warning);
$page
->uncheckField($incompatible_filter_name);
$assert_session
->waitForElementRemoved('css', '[data-drupal-messages] [role="alert"]');
$this
->saveNewTextFormat($page, $assert_session);
}
public function testImageUploadsRemainEnabled() : void {
FilterFormat::create([
'format' => 'cke4_image_uploads',
'name' => 'CKEditor 4, image uploads',
])
->save();
Editor::create([
'format' => 'cke4_image_uploads',
'editor' => 'ckeditor',
'settings' => [
'toolbar' => [
'rows' => [
0 => [
[
'name' => 'Media',
'items' => [
'DrupalImage',
],
],
],
],
],
'plugins' => [],
],
'image_upload' => [
'status' => TRUE,
'scheme' => 'public',
'directory' => 'inline-images',
'max_size' => '',
'max_dimensions' => [
'width' => 0,
'height' => 0,
],
],
])
->save();
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('admin/config/content/formats/manage/cke4_image_uploads');
$this
->assertTrue($page
->hasCheckedField('Enable image uploads'));
$page
->selectFieldOption('editor[editor]', 'ckeditor5');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertTrue($page
->hasCheckedField('Enable image uploads'));
}
public function testSwitchToVersion5() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->createNewTextFormat($page, $assert_session, 'ckeditor');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertTrue($page
->hasUncheckedField('filters[filter_html][status]'));
$page
->checkField('filters[filter_html][status]');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $this->defaultElementsWhenUpdatingNotCkeditor5);
$this
->saveNewTextFormat($page, $assert_session);
$assert_session
->pageTextContains('Added text format ckeditor');
$this
->drupalGet('admin/config/content/formats/manage/ckeditor');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $this->defaultElementsWhenUpdatingNotCkeditor5);
$page
->selectFieldOption('editor[editor]', 'ckeditor5');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->pageTextContains('The <br>, <p> tags were added because they are required by CKEditor 5');
$this
->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $this->defaultElementsAfterUpdatingToCkeditor5);
$page
->pressButton('Save configuration');
$assert_session
->pageTextContains('The Image upload toolbar item requires image uploads to be enabled.');
$page
->clickLink('Image Upload');
$assert_session
->waitForText('Enable image uploads');
$this
->assertTrue($page
->hasUncheckedField('editor[settings][plugins][ckeditor5_imageUpload][status]'));
$page
->checkField('editor[settings][plugins][ckeditor5_imageUpload][status]');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save configuration');
$this
->assertSession()
->pageTextContains('The following attribute(s) are already supported by enabled plugins and should not be added to the Source Editing "Manually editable HTML tags" field: Image (<img src alt data-entity-uuid data-entity-type>)');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->waitForText('Manually editable HTML tags');
$source_edit_tags_field = $assert_session
->fieldExists('editor[settings][plugins][ckeditor5_sourceEditing][allowed_tags]');
$source_edit_tags_field_value = $source_edit_tags_field
->getValue();
$source_edit_tags_field
->setValue(str_replace('<img src alt data-entity-type data-entity-uuid>', '', $source_edit_tags_field_value));
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save configuration');
$assert_session
->pageTextContains('The text format ckeditor has been updated');
}
public function testImgAddedViaUploadPlugin() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->createNewTextFormat($page, $assert_session);
$assert_session
->assertWaitOnAjaxRequest();
$allowed_html_field = $assert_session
->fieldExists('filters[filter_html][settings][allowed_html]');
$this
->assertTrue($allowed_html_field
->hasAttribute('readonly'));
$this
->assertEquals($this->allowedElements, $allowed_html_field
->getValue());
$assert_session
->elementNotExists('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-imageupload"]');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ckeditor5-toolbar-item-uploadImage'));
$this
->triggerKeyUp('.ckeditor5-toolbar-item-uploadImage', 'ArrowDown');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->elementExists('css', '[data-drupal-selector="edit-editor-settings-plugins-ckeditor5-imageupload"]');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ckeditor5-toolbar-active .ckeditor5-toolbar-item-uploadImage'));
$this
->assertEquals($this->allowedElements, $allowed_html_field
->getValue());
$page
->clickLink('Image Upload');
$assert_session
->waitForText('Enable image uploads');
$this
->assertTrue($page
->hasUncheckedField('editor[settings][plugins][ckeditor5_imageUpload][status]'));
$page
->checkField('editor[settings][plugins][ckeditor5_imageUpload][status]');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertEquals($this->allowedElements . ' <img src alt data-entity-uuid data-entity-type height width>', $allowed_html_field
->getValue());
$this
->assertTrue($page
->hasUncheckedField('filters[filter_caption][status]'));
$page
->checkField('filters[filter_caption][status]');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertEquals($this->allowedElements . ' <img src alt data-entity-uuid data-entity-type height width data-caption>', $allowed_html_field
->getValue());
$this
->assertTrue($page
->hasUncheckedField('filters[filter_align][status]'));
$page
->checkField('filters[filter_align][status]');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertEquals($this->allowedElements . ' <img src alt data-entity-uuid data-entity-type height width data-caption data-align>', $allowed_html_field
->getValue());
$page
->clickLink('Image Upload');
$assert_session
->waitForText('Enable image uploads');
$this
->assertTrue($page
->hasCheckedField('editor[settings][plugins][ckeditor5_imageUpload][status]'));
$page
->uncheckField('editor[settings][plugins][ckeditor5_imageUpload][status]');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertEquals($this->allowedElements, $allowed_html_field
->getValue());
}
public function testAllowedTags() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->createNewTextFormat($page, $assert_session);
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.js-form-item-filters-filter-html-settings-allowed-html.form-disabled'));
$allowed_html_field = $assert_session
->fieldExists('filters[filter_html][settings][allowed_html]');
$this
->assertTrue($allowed_html_field
->hasAttribute('readonly'));
$this
->assertSame($this->allowedElements, $allowed_html_field
->getValue());
$this
->saveNewTextFormat($page, $assert_session);
$assert_session
->pageTextContains('Added text format ckeditor5');
$assert_session
->pageTextContains('Text formats and editors');
$this
->assertSame($this->allowedElements, FilterFormat::load('ckeditor5')
->filters('filter_html')
->getConfiguration()['settings']['allowed_html']);
$page
->find('css', '[data-drupal-selector="edit-formats-ckeditor5"]')
->clickLink('Configure');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ckeditor5-toolbar-item-blockQuote'));
$this
->triggerKeyUp('.ckeditor5-toolbar-item-blockQuote', 'ArrowDown');
$assert_session
->assertWaitOnAjaxRequest();
$allowed_with_blockquote = $this->allowedElements . ' <blockquote>';
$assert_session
->fieldExists('filters[filter_html][settings][allowed_html]');
$this
->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $allowed_with_blockquote);
$page
->pressButton('Save configuration');
$assert_session
->pageTextContains('The text format ckeditor5 has been updated.');
drupal_flush_all_caches();
$this
->assertSame($allowed_with_blockquote, FilterFormat::load('ckeditor5')
->filters('filter_html')
->getConfiguration()['settings']['allowed_html']);
$page
->find('css', '[data-drupal-selector="edit-formats-ckeditor5"]')
->clickLink('Configure');
$this
->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $allowed_with_blockquote);
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ckeditor5-toolbar-item-sourceEditing'));
$this
->triggerKeyUp('.ckeditor5-toolbar-item-sourceEditing', 'ArrowDown');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->waitForText('Source editing');
$page
->find('css', '[href^="#edit-editor-settings-plugins-ckeditor5-sourceediting"]')
->click();
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->waitForText('Manually editable HTML tags');
$source_edit_tags_field = $assert_session
->fieldExists('editor[settings][plugins][ckeditor5_sourceEditing][allowed_tags]');
$source_edit_tags_field
->setValue('<aside>');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', '<br> <p> <h2> <h3> <h4> <h5> <h6> <aside> <strong> <em> <blockquote>');
$allowed_html_field = $assert_session
->fieldExists('filters[filter_html][settings][allowed_html]');
$this
->assertTrue($allowed_html_field
->hasAttribute('readonly'));
$source_edit_tags_field
->setValue('<aside><strong>');
$assert_session
->waitForText('The following tag(s) are already supported by enabled plugins and should not be added to the Source Editing "Manually editable HTML tags" field: Bold (<strong>)');
$this
->assertTrue($page
->find('css', '[href^="#edit-editor-settings-plugins-ckeditor5-sourceediting"]')
->getParent()
->hasClass('is-selected'));
$this
->assertSame('true', $page
->findField('editor[settings][plugins][ckeditor5_sourceEditing][allowed_tags]')
->getAttribute('aria-invalid'));
$this
->assertTrue($allowed_html_field
->hasAttribute('readonly'));
$page
->pressButton('Save configuration');
$assert_session
->pageTextContains('The following tag(s) are already supported by enabled plugins and should not be added to the Source Editing "Manually editable HTML tags" field: Bold (<strong>)');
$this
->assertTrue($page
->find('css', '[href^="#edit-editor-settings-plugins-ckeditor5-sourceediting"]')
->getParent()
->hasClass('is-selected'));
$this
->assertSame('true', $page
->findField('editor[settings][plugins][ckeditor5_sourceEditing][allowed_tags]')
->getAttribute('aria-invalid'));
$assert_session
->pageTextNotContains('The text format ckeditor5 has been updated');
$assert_session
->waitForText('Source editing');
$page
->find('css', '[href^="#edit-editor-settings-plugins-ckeditor5-sourceediting"]')
->click();
$assert_session
->pageTextContains('Manually editable HTML tags');
$source_edit_tags_field = $assert_session
->fieldExists('editor[settings][plugins][ckeditor5_sourceEditing][allowed_tags]');
$source_edit_tags_field
->setValue('<aside>');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save configuration');
$assert_session
->pageTextContains('The text format ckeditor5 has been updated');
$assert_session
->pageTextNotContains('The following tag(s) are already supported by enabled plugins and should not be added to the Source Editing "Manually editable HTML tags" field: Bold (<strong>)');
$this
->drupalGet('node/add');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ck-editor'));
}
public function testMediaElementAllowedTags() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->createNewTextFormat($page, $assert_session);
EntityViewMode::create([
'id' => 'media.view_mode_1',
'targetEntityType' => 'media',
'status' => TRUE,
'enabled' => TRUE,
'label' => 'View Mode 1',
])
->save();
EntityViewMode::create([
'id' => 'media.view_mode_2',
'targetEntityType' => 'media',
'status' => TRUE,
'enabled' => TRUE,
'label' => 'View Mode 2',
])
->save();
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.js-form-item-filters-filter-html-settings-allowed-html.form-disabled'));
$allowed_html_field = $assert_session
->fieldExists('filters[filter_html][settings][allowed_html]');
$this
->assertTrue($allowed_html_field
->hasAttribute('readonly'));
$this
->assertEquals($this->allowedElements, $allowed_html_field
->getValue());
$this
->assertTrue($page
->hasUncheckedField('filters[media_embed][status]'));
$page
->checkField('filters[media_embed][status]');
$assert_session
->assertWaitOnAjaxRequest();
$assert_session
->responseContains('Media types selectable in the Media Library');
$page
->clickLink('Embed media');
$page
->checkField('filters[media_embed][settings][allowed_view_modes][view_mode_1]');
$page
->checkField('filters[media_embed][settings][allowed_view_modes][view_mode_2]');
$assert_session
->assertWaitOnAjaxRequest();
$allowed_with_media = $this->allowedElements . ' <drupal-media data-entity-type data-entity-uuid alt data-view-mode>';
$allowed_with_media_without_view_mode = $this->allowedElements . ' <drupal-media data-entity-type data-entity-uuid alt>';
$assert_session
->responseContains('Media types selectable in the Media Library');
$page
->clickLink('Media');
$assert_session
->waitForText('Allow the user to override the default view mode');
$this
->assertTrue($page
->hasUncheckedField('editor[settings][plugins][media_media][allow_view_mode_override]'));
$this
->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $allowed_with_media_without_view_mode);
$page
->checkField('editor[settings][plugins][media_media][allow_view_mode_override]');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $allowed_with_media);
$this
->saveNewTextFormat($page, $assert_session);
$assert_session
->pageTextContains('Added text format ckeditor5.');
$this
->assertSame($allowed_with_media, FilterFormat::load('ckeditor5')
->filters('filter_html')
->getConfiguration()['settings']['allowed_html']);
$page
->find('css', '[data-drupal-selector="edit-formats-ckeditor5"]')
->clickLink('Configure');
$this
->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $allowed_with_media);
$page
->checkField('filters[filter_align][status]');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertEquals($this->allowedElements . ' <drupal-media data-entity-type data-entity-uuid alt data-view-mode data-align>', $allowed_html_field
->getValue());
$this
->assertTrue($page
->hasCheckedField('filters[media_embed][status]'));
$page
->uncheckField('filters[media_embed][status]');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertHtmlEsqueFieldValueEquals('filters[filter_html][settings][allowed_html]', $this->allowedElements);
}
public function testFullHtml() {
FilterFormat::create(Yaml::parseFile('core/profiles/standard/config/install/filter.format.full_html.yml'))
->save();
FilterFormat::create(Yaml::parseFile('core/profiles/standard/config/install/filter.format.basic_html.yml'))
->save();
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('node/add');
$page
->fillField('title[0][value]', 'My test content');
$page
->fillField('body[0][value]', '<foo bar="baz">⬅️✌️➡️</foo><p><a style="color:#ff0000;" foo="bar" hreflang="en" href="https://example.com"><abbr title="National Aeronautics and Space Administration">NASA</abbr> is an acronym.</a></p>');
$page
->pressButton('Save');
$this
->drupalGet('admin/config/content/formats/manage/full_html');
$page
->checkField('roles[authenticated]');
$page
->selectFieldOption('editor[editor]', 'ckeditor5');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save configuration');
$this
->assertTrue($assert_session
->waitForText('The text format Full HTML has been updated.'));
$this
->drupalGet('node/1/edit');
$page
->selectFieldOption('body[0][format]', 'full_html');
$this
->assertNotEmpty($assert_session
->waitForText('Change text format?'));
$page
->pressButton('Continue');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ck-editor'));
$page
->pressButton('Save');
$assert_session
->responseContains('<foo bar="baz">⬅️✌️➡️</foo><p><a foo="bar" hreflang="en" href="https://example.com"><abbr title="National Aeronautics and Space Administration">NASA</abbr> is an acronym.</a></p>');
$this
->drupalGet('admin/config/content/formats/manage/full_html');
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ckeditor5-toolbar-item-link'));
$this
->triggerKeyUp('.ckeditor5-toolbar-item-link', 'ArrowDown');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save configuration');
$this
->drupalGet('node/1/edit');
$page
->pressButton('Save');
$assert_session
->responseContains('<p><a foo="bar" hreflang="en" href="https://example.com"><abbr title="National Aeronautics and Space Administration">NASA</abbr> is an acronym.</a></p>');
$this
->drupalGet('admin/config/content/formats/manage/basic_html');
$page
->checkField('roles[authenticated]');
$page
->selectFieldOption('editor[editor]', 'ckeditor5');
$assert_session
->assertWaitOnAjaxRequest();
$this
->assertNotEmpty($assert_session
->waitForElement('css', '.ckeditor5-toolbar-item-link'));
$this
->triggerKeyUp('.ckeditor5-toolbar-item-link', 'ArrowDown');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save configuration');
$this
->assertTrue($assert_session
->waitForText('The text format Basic HTML has been updated.'));
$this
->drupalGet('node/1/edit');
$page
->selectFieldOption('body[0][format]', 'basic_html');
$this
->assertNotEmpty($assert_session
->waitForText('Change text format?'));
$page
->pressButton('Continue');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$assert_session
->responseContains('<p>⬅️✌️➡️</p><p><a href="https://example.com" hreflang="en">NASA is an acronym.</a></p>');
}
}