3 namespace Drupal\Tests\ckeditor\FunctionalJavascript;
5 use Drupal\editor\Entity\Editor;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
10 * Tests delivery of CSS to CKEditor via AJAX.
14 class AjaxCssTest extends WebDriverTestBase {
19 public static $modules = ['ckeditor', 'ckeditor_test'];
24 protected function setUp() {
27 FilterFormat::create([
28 'format' => 'test_format',
29 'name' => $this->randomMachineName(),
33 'editor' => 'ckeditor',
34 'format' => 'test_format',
37 user_role_grant_permissions('anonymous', ['use text format test_format']);
41 * Tests adding style sheets dynamically to CKEditor.
43 public function testCkeditorAjaxAddCss() {
44 $this->drupalGet('/ckeditor_test/ajax_css');
46 $session = $this->getSession();
47 $page = $session->getPage();
49 $this->waitOnCkeditorInstance('edit-iframe-value');
50 $this->waitOnCkeditorInstance('edit-inline');
52 $style_color = 'rgb(255, 0, 0)';
54 // Add the inline CSS and assert that the style is applied to the main body,
55 // but not the iframe.
56 $page->pressButton('Add CSS to inline CKEditor instance');
58 $result = $page->waitFor(10, function () use ($style_color) {
59 return ($this->getEditorStyle('edit-inline', 'color') == $style_color)
60 && ($this->getEditorStyle('edit-iframe-value', 'color') != $style_color);
62 $this->assertTrue($result);
66 $this->waitOnCkeditorInstance('edit-iframe-value');
67 $this->waitOnCkeditorInstance('edit-inline');
69 // Add the iframe CSS and assert that the style is applied to the iframe,
70 // but not the main body.
71 $page->pressButton('Add CSS to iframe CKEditor instance');
73 $result = $page->waitFor(10, function () use ($style_color) {
74 return ($this->getEditorStyle('edit-inline', 'color') != $style_color)
75 && ($this->getEditorStyle('edit-iframe-value', 'color') == $style_color);
78 $this->assertTrue($result);
82 * Gets a computed style value for a CKEditor instance.
84 * @param string $instance_id
85 * The CKEditor instance ID.
86 * @param string $attribute
87 * The style attribute.
90 * The computed style value.
92 protected function getEditorStyle($instance_id, $attribute) {
94 'CKEDITOR.instances["%s"].document.getBody().getComputedStyle("%s")',
98 return $this->getSession()->evaluateScript($js);
102 * Wait for a CKEditor instance to finish loading and initializing.
104 * @param string $instance_id
105 * The CKEditor instance ID.
106 * @param int $timeout
107 * (optional) Timeout in milliseconds, defaults to 10000.
109 protected function waitOnCkeditorInstance($instance_id, $timeout = 10000) {
113 typeof CKEDITOR !== 'undefined'
114 && typeof CKEDITOR.instances["$instance_id"] !== 'undefined'
115 && CKEDITOR.instances["$instance_id"].instanceReady
120 $this->getSession()->wait($timeout, $condition);