AjaxCssTest.php in Drupal 9
File
core/modules/ckeditor/tests/src/FunctionalJavascript/AjaxCssTest.php
View source
<?php
namespace Drupal\Tests\ckeditor\FunctionalJavascript;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class AjaxCssTest extends WebDriverTestBase {
protected static $modules = [
'ckeditor',
'ckeditor_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
FilterFormat::create([
'format' => 'test_format',
'name' => $this
->randomMachineName(),
])
->save();
Editor::create([
'editor' => 'ckeditor',
'format' => 'test_format',
])
->save();
user_role_grant_permissions('anonymous', [
'use text format test_format',
]);
}
public function testCkeditorAjaxAddCss() {
$this
->drupalGet('/ckeditor_test/ajax_css');
$session = $this
->getSession();
$page = $session
->getPage();
$this
->waitOnCkeditorInstance('edit-iframe-value');
$this
->waitOnCkeditorInstance('edit-inline');
$style_color = 'rgb(255, 0, 0)';
$page
->pressButton('Add CSS to inline CKEditor instance');
$result = $page
->waitFor(10, function () use ($style_color) {
return $this
->getEditorStyle('edit-inline', 'color') == $style_color && $this
->getEditorStyle('edit-iframe-value', 'color') != $style_color;
});
$this
->assertTrue($result);
$session
->reload();
$this
->waitOnCkeditorInstance('edit-iframe-value');
$this
->waitOnCkeditorInstance('edit-inline');
$page
->pressButton('Add CSS to iframe CKEditor instance');
$result = $page
->waitFor(10, function () use ($style_color) {
return $this
->getEditorStyle('edit-inline', 'color') != $style_color && $this
->getEditorStyle('edit-iframe-value', 'color') == $style_color;
});
$this
->assertTrue($result);
}
protected function getEditorStyle($instance_id, $attribute) {
$js = sprintf('CKEDITOR.instances["%s"].document.getBody().getComputedStyle("%s")', $instance_id, $attribute);
return $this
->getSession()
->evaluateScript($js);
}
protected function waitOnCkeditorInstance($instance_id, $timeout = 10000) {
$condition = <<<JS
(function() {
return (
typeof CKEDITOR !== 'undefined'
&& typeof CKEDITOR.instances["{<span class="php-variable">$instance_id</span>}"] !== 'undefined'
&& CKEDITOR.instances["{<span class="php-variable">$instance_id</span>}"].instanceReady
);
}())
JS;
$this
->getSession()
->wait($timeout, $condition);
}
}