View source
<?php
namespace Drupal\Tests\editor\Kernel;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\KernelTests\KernelTestBase;
class EditorManagerTest extends KernelTestBase {
protected static $modules = [
'system',
'user',
'filter',
'editor',
];
protected $editorManager;
protected function setUp() : void {
parent::setUp();
$filtered_html_format = FilterFormat::create([
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => [],
]);
$filtered_html_format
->save();
$full_html_format = FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => [],
]);
$full_html_format
->save();
}
public function testManager() {
$this->editorManager = $this->container
->get('plugin.manager.editor');
$this
->assertSame([], $this->editorManager
->listOptions(), 'When no text editor is enabled, the manager works correctly.');
$this
->assertSame([], $this->editorManager
->getAttachments([]), 'No attachments when no text editor is enabled and retrieving attachments for zero text formats.');
$this
->assertSame([], $this->editorManager
->getAttachments([
'filtered_html',
'full_html',
]), 'No attachments when no text editor is enabled and retrieving attachments for multiple text formats.');
$this
->enableModules([
'editor_test',
]);
$this->editorManager = $this->container
->get('plugin.manager.editor');
$this->editorManager
->clearCachedDefinitions();
$this
->assertSame('Unicorn Editor', (string) $this->editorManager
->listOptions()['unicorn'], 'When some text editor is enabled, the manager works correctly.');
$unicorn_plugin = $this->editorManager
->createInstance('unicorn');
$editor = Editor::create([
'format' => 'full_html',
'editor' => 'unicorn',
]);
$editor
->save();
$this
->assertSame([], $this->editorManager
->getAttachments([]), 'No attachments when one text editor is enabled and retrieving attachments for zero text formats.');
$expected = [
'library' => [
0 => 'editor_test/unicorn',
],
'drupalSettings' => [
'editor' => [
'formats' => [
'full_html' => [
'format' => 'full_html',
'editor' => 'unicorn',
'editorSettings' => $unicorn_plugin
->getJSSettings($editor),
'editorSupportsContentFiltering' => TRUE,
'isXssSafe' => FALSE,
],
],
],
],
];
$this
->assertSame($expected, $this->editorManager
->getAttachments([
'filtered_html',
'full_html',
]), 'Correct attachments when one text editor is enabled and retrieving attachments for multiple text formats.');
\Drupal::state()
->set('editor_test_js_settings_alter_enabled', TRUE);
$expected['drupalSettings']['editor']['formats']['full_html']['editorSettings']['ponyModeEnabled'] = FALSE;
$this
->assertSame($expected, $this->editorManager
->getAttachments([
'filtered_html',
'full_html',
]), 'hook_editor_js_settings_alter() works correctly.');
}
}