3 namespace Drupal\Tests\ckeditor\FunctionalJavascript;
5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
6 use Drupal\editor\Entity\Editor;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\filter\Entity\FilterFormat;
10 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
11 use Drupal\node\Entity\NodeType;
14 * Tests the integration of CKEditor.
18 class CKEditorIntegrationTest extends WebDriverTestBase {
23 * @var \Drupal\user\UserInterface
28 * The FilterFormat config entity used for testing.
30 * @var \Drupal\filter\FilterFormatInterface
32 protected $filterFormat;
37 public static $modules = ['node', 'ckeditor', 'filter'];
42 protected function setUp() {
45 // Create a text format and associate CKEditor.
46 $this->filterFormat = FilterFormat::create([
47 'format' => 'filtered_html',
48 'name' => 'Filtered HTML',
51 $this->filterFormat->save();
54 'format' => 'filtered_html',
55 'editor' => 'ckeditor',
58 // Create a node type for testing.
59 NodeType::create(['type' => 'page', 'name' => 'page'])->save();
61 $field_storage = FieldStorageConfig::loadByName('node', 'body');
63 // Create a body field instance for the 'page' node type.
65 'field_storage' => $field_storage,
68 'settings' => ['display_summary' => TRUE],
72 // Assign widget settings for the 'default' form mode.
73 EntityFormDisplay::create([
74 'targetEntityType' => 'node',
78 ])->setComponent('body', ['type' => 'text_textarea_with_summary'])
81 $this->account = $this->drupalCreateUser([
83 'create page content',
84 'use text format filtered_html',
86 $this->drupalLogin($this->account);
90 * Tests if the fragment link to a textarea works with CKEditor enabled.
92 public function testFragmentLink() {
93 $session = $this->getSession();
94 $web_assert = $this->assertSession();
95 $ckeditor_id = '#cke_edit-body-0-value';
97 $this->drupalGet('node/add/page');
101 // Add a bottom margin to the title field to be sure the body field is not
103 $session->executeScript("document.getElementById('edit-title-0-value').style.marginBottom = window.innerHeight*2 +'px';");
105 $this->assertSession()->waitForElementVisible('css', $ckeditor_id);
106 // Check that the CKEditor-enabled body field is currently not visible in
108 $web_assert->assertNotVisibleInViewport('css', $ckeditor_id, 'topLeft', 'CKEditor-enabled body field is visible.');
110 $before_url = $session->getCurrentUrl();
112 // Trigger a hash change with as target the hidden textarea.
113 $session->executeScript("location.hash = '#edit-body-0-value';");
115 // Check that the CKEditor-enabled body field is visible in the viewport.
116 $web_assert->assertVisibleInViewport('css', $ckeditor_id, 'topLeft', 'CKEditor-enabled body field is not visible.');
118 // Use JavaScript to go back in the history instead of
119 // \Behat\Mink\Session::back() because that function doesn't work after a
121 $session->executeScript("history.back();");
123 $after_url = $session->getCurrentUrl();
125 // Check that going back in the history worked.
126 self::assertEquals($before_url, $after_url, 'History back works.');
130 * Tests if the Image button appears and works as expected.
132 public function testDrupalImageDialog() {
133 $session = $this->getSession();
134 $web_assert = $this->assertSession();
136 $this->drupalGet('node/add/page');
139 // Asserts the Image button is present in the toolbar.
140 $web_assert->elementExists('css', '#cke_edit-body-0-value .cke_button__drupalimage');
142 // Asserts the image dialog opens when clicking the Image button.
143 $this->click('.cke_button__drupalimage');
144 $this->assertNotEmpty($web_assert->waitForElement('css', '.ui-dialog'));
146 $web_assert->elementContains('css', '.ui-dialog .ui-dialog-titlebar', 'Insert Image');
150 * Tests if the Drupal Image Caption plugin appears and works as expected.
152 public function testDrupalImageCaptionDialog() {
153 $web_assert = $this->assertSession();
155 // Disable the caption filter.
156 $this->filterFormat->setFilterConfig('filter_caption', [
159 $this->filterFormat->save();
161 // If the caption filter is disabled, its checkbox should be absent.
162 $this->drupalGet('node/add/page');
163 $this->click('.cke_button__drupalimage');
164 $this->assertNotEmpty($web_assert->waitForElement('css', '.ui-dialog'));
165 $web_assert->elementNotExists('css', '.ui-dialog input[name="attributes[hasCaption]"]');
167 // Enable the caption filter again.
168 $this->filterFormat->setFilterConfig('filter_caption', [
171 $this->filterFormat->save();
173 // If the caption filter is enabled, its checkbox should be present.
174 $this->drupalGet('node/add/page');
175 $this->click('.cke_button__drupalimage');
176 $this->assertNotEmpty($web_assert->waitForElement('css', '.ui-dialog'));
177 $web_assert->elementExists('css', '.ui-dialog input[name="attributes[hasCaption]"]');