ddb53d8303e888b58ab13703e19f74051cab70e4
[yaffs-website] / tests / src / FunctionalJavascript / AjaxCssTest.php
1 <?php
2
3 namespace Drupal\Tests\ckeditor\FunctionalJavascript;
4
5 use Drupal\editor\Entity\Editor;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8
9 /**
10  * Tests delivery of CSS to CKEditor via AJAX.
11  *
12  * @group ckeditor
13  */
14 class AjaxCssTest extends WebDriverTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = ['ckeditor', 'ckeditor_test'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26
27     FilterFormat::create([
28       'format' => 'test_format',
29       'name' => $this->randomMachineName(),
30     ])->save();
31
32     Editor::create([
33       'editor' => 'ckeditor',
34       'format' => 'test_format',
35     ])->save();
36
37     user_role_grant_permissions('anonymous', ['use text format test_format']);
38   }
39
40   /**
41    * Tests adding style sheets dynamically to CKEditor.
42    */
43   public function testCkeditorAjaxAddCss() {
44     $this->drupalGet('/ckeditor_test/ajax_css');
45
46     $session = $this->getSession();
47     $page = $session->getPage();
48
49     $this->waitOnCkeditorInstance('edit-iframe-value');
50     $this->waitOnCkeditorInstance('edit-inline');
51
52     $style_color = 'rgb(255, 0, 0)';
53
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');
57
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);
61     });
62     $this->assertTrue($result);
63
64     $session->reload();
65
66     $this->waitOnCkeditorInstance('edit-iframe-value');
67     $this->waitOnCkeditorInstance('edit-inline');
68
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');
72
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);
76     });
77
78     $this->assertTrue($result);
79   }
80
81   /**
82    * Gets a computed style value for a CKEditor instance.
83    *
84    * @param string $instance_id
85    *   The CKEditor instance ID.
86    * @param string $attribute
87    *   The style attribute.
88    *
89    * @return string
90    *   The computed style value.
91    */
92   protected function getEditorStyle($instance_id, $attribute) {
93     $js = sprintf(
94       'CKEDITOR.instances["%s"].document.getBody().getComputedStyle("%s")',
95       $instance_id,
96       $attribute
97     );
98     return $this->getSession()->evaluateScript($js);
99   }
100
101   /**
102    * Wait for a CKEditor instance to finish loading and initializing.
103    *
104    * @param string $instance_id
105    *   The CKEditor instance ID.
106    * @param int $timeout
107    *   (optional) Timeout in milliseconds, defaults to 10000.
108    */
109   protected function waitOnCkeditorInstance($instance_id, $timeout = 10000) {
110     $condition = <<<JS
111       (function() {
112         return (
113           typeof CKEDITOR !== 'undefined'
114           && typeof CKEDITOR.instances["$instance_id"] !== 'undefined'
115           && CKEDITOR.instances["$instance_id"].instanceReady
116         );
117       }());
118 JS;
119
120     $this->getSession()->wait($timeout, $condition);
121   }
122
123 }