Version 1
[yaffs-website] / web / core / modules / editor / tests / src / Kernel / EditorManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\editor\Kernel;
4
5 use Drupal\editor\Entity\Editor;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10  * Tests detection of text editors and correct generation of attachments.
11  *
12  * @group editor
13  */
14 class EditorManagerTest extends KernelTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['system', 'user', 'filter', 'editor'];
22
23   /**
24    * The manager for text editor plugins.
25    *
26    * @var \Drupal\Component\Plugin\PluginManagerInterface
27    */
28   protected $editorManager;
29
30   protected function setUp() {
31     parent::setUp();
32
33     // Install the Filter module.
34
35     // Add text formats.
36     $filtered_html_format = FilterFormat::create([
37       'format' => 'filtered_html',
38       'name' => 'Filtered HTML',
39       'weight' => 0,
40       'filters' => [],
41     ]);
42     $filtered_html_format->save();
43     $full_html_format = FilterFormat::create([
44       'format' => 'full_html',
45       'name' => 'Full HTML',
46       'weight' => 1,
47       'filters' => [],
48     ]);
49     $full_html_format->save();
50   }
51
52   /**
53    * Tests the configurable text editor manager.
54    */
55   public function testManager() {
56     $this->editorManager = $this->container->get('plugin.manager.editor');
57
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.');
65
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();
71
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.');
74
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',
81     ]);
82     $editor->save();
83     $this->assertIdentical([], $this->editorManager->getAttachments([]), 'No attachments when one text editor is enabled and retrieving attachments for zero text formats.');
84     $expected = [
85       'library' => [
86         0 => 'editor_test/unicorn',
87       ],
88       'drupalSettings' => [
89         'editor' => [
90           'formats' => [
91             'full_html' => [
92               'format'  => 'full_html',
93               'editor' => 'unicorn',
94               'editorSettings' => $unicorn_plugin->getJSSettings($editor),
95               'editorSupportsContentFiltering' => TRUE,
96               'isXssSafe' => FALSE,
97             ],
98           ],
99         ],
100       ],
101     ];
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.');
103
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.');
109   }
110
111 }