3 namespace Drupal\Tests\editor\Kernel;
5 use Drupal\editor\Entity\Editor;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\KernelTests\KernelTestBase;
10 * Tests detection of text editors and correct generation of attachments.
14 class EditorManagerTest extends KernelTestBase {
21 public static $modules = ['system', 'user', 'filter', 'editor'];
24 * The manager for text editor plugins.
26 * @var \Drupal\Component\Plugin\PluginManagerInterface
28 protected $editorManager;
30 protected function setUp() {
33 // Install the Filter module.
36 $filtered_html_format = FilterFormat::create([
37 'format' => 'filtered_html',
38 'name' => 'Filtered HTML',
42 $filtered_html_format->save();
43 $full_html_format = FilterFormat::create([
44 'format' => 'full_html',
45 'name' => 'Full HTML',
49 $full_html_format->save();
53 * Tests the configurable text editor manager.
55 public function testManager() {
56 $this->editorManager = $this->container->get('plugin.manager.editor');
58 // Case 1: no text editor available:
59 // - listOptions() should return an empty list of options
60 // - getAttachments() should return an empty #attachments array (and not
61 // a JS settings structure that is empty)
62 $this->assertIdentical([], $this->editorManager->listOptions(), 'When no text editor is enabled, the manager works correctly.');
63 $this->assertIdentical([], $this->editorManager->getAttachments([]), 'No attachments when no text editor is enabled and retrieving attachments for zero text formats.');
64 $this->assertIdentical([], $this->editorManager->getAttachments(['filtered_html', 'full_html']), 'No attachments when no text editor is enabled and retrieving attachments for multiple text formats.');
66 // Enable the Text Editor Test module, which has the Unicorn Editor and
67 // clear the editor manager's cache so it is picked up.
68 $this->enableModules(['editor_test']);
69 $this->editorManager = $this->container->get('plugin.manager.editor');
70 $this->editorManager->clearCachedDefinitions();
72 // Case 2: a text editor available.
73 $this->assertIdentical('Unicorn Editor', (string) $this->editorManager->listOptions()['unicorn'], 'When some text editor is enabled, the manager works correctly.');
75 // Case 3: a text editor available & associated (but associated only with
76 // the 'Full HTML' text format).
77 $unicorn_plugin = $this->editorManager->createInstance('unicorn');
78 $editor = Editor::create([
79 'format' => 'full_html',
80 'editor' => 'unicorn',
83 $this->assertIdentical([], $this->editorManager->getAttachments([]), 'No attachments when one text editor is enabled and retrieving attachments for zero text formats.');
86 0 => 'editor_test/unicorn',
92 'format' => 'full_html',
93 'editor' => 'unicorn',
94 'editorSettings' => $unicorn_plugin->getJSSettings($editor),
95 'editorSupportsContentFiltering' => TRUE,
102 $this->assertIdentical($expected, $this->editorManager->getAttachments(['filtered_html', 'full_html']), 'Correct attachments when one text editor is enabled and retrieving attachments for multiple text formats.');
104 // Case 4: a text editor available associated, but now with its JS settings
105 // being altered via hook_editor_js_settings_alter().
106 \Drupal::state()->set('editor_test_js_settings_alter_enabled', TRUE);
107 $expected['drupalSettings']['editor']['formats']['full_html']['editorSettings']['ponyModeEnabled'] = FALSE;
108 $this->assertIdentical($expected, $this->editorManager->getAttachments(['filtered_html', 'full_html']), 'hook_editor_js_settings_alter() works correctly.');