You are here

public function CKEditor5PluginManagerTest::testEnabledPlugins in Drupal 10

Tests the enabling of plugins.

File

core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php, line 1044

Class

CKEditor5PluginManagerTest
Tests different ways of enabling CKEditor 5 plugins.

Namespace

Drupal\Tests\ckeditor5\Kernel

Code

public function testEnabledPlugins() {
  $editor = Editor::load('basic_html');

  // Case 1: no extra CKEditor 5 plugins.
  $definitions = array_keys($this->manager
    ->getEnabledDefinitions($editor));
  $default_plugins = [
    'ckeditor5_bold',
    'ckeditor5_emphasis',
    'ckeditor5_essentials',
    'ckeditor5_globalAttributeDir',
    'ckeditor5_globalAttributeLang',
    'ckeditor5_heading',
    'ckeditor5_paragraph',
    'ckeditor5_pasteFromOffice',
  ];
  $this
    ->assertSame($default_plugins, $definitions, 'No CKEditor 5 plugins found besides the built-in ones.');
  $default_libraries = [
    'ckeditor5/drupal.ckeditor5',
    'ckeditor5/drupal.ckeditor5.emphasis',
    'ckeditor5/drupal.ckeditor5.internal',
    'core/ckeditor5.basic',
    'core/ckeditor5.htmlSupport',
    'core/ckeditor5.internal',
    'core/ckeditor5.pasteFromOffice',
  ];
  $this
    ->assertSame($default_libraries, $this->manager
    ->getEnabledLibraries($editor));

  // Enable the CKEditor 5 Test module, which has the layercake plugin and
  // clear the editor manager's static cache so that it is picked up.
  $this
    ->enableModules([
    'ckeditor5_test',
  ]);
  $this->manager = $this->container
    ->get('plugin.manager.ckeditor5.plugin');
  $this->manager
    ->clearCachedDefinitions();

  // Case 2: The CKEditor 5 layercake plugin is available and library should
  // NOT be loaded if its toolbar items are not enabled.
  $this
    ->assertSame($default_plugins, array_keys($this->manager
    ->getEnabledDefinitions($editor)));
  $this
    ->assertSame($default_libraries, $this->manager
    ->getEnabledLibraries($editor));

  // Case 3: The CKEditor 5 layercake plugin is available and library should
  // be loaded without having to enable plugins.
  $settings = $editor
    ->getSettings();
  $settings['toolbar']['items'][] = 'simpleBox';
  $editor
    ->setSettings($settings);
  $plugin_ids = array_keys($this->manager
    ->getEnabledDefinitions($editor));
  $default_plugins_with_layercake = array_merge($default_plugins, [
    'ckeditor5_test_layercake',
  ]);

  // Sort on plugin id.
  asort($default_plugins_with_layercake);
  $this
    ->assertSame(array_values($default_plugins_with_layercake), $plugin_ids);
  $default_libraries_with_layercake = array_merge($default_libraries, [
    'ckeditor5_test/layercake',
  ]);
  sort($default_libraries_with_layercake);
  $this
    ->assertSame($default_libraries_with_layercake, $this->manager
    ->getEnabledLibraries($editor));

  // Enable media embed filter which the CKEditor 5 media plugin requires.
  $editor
    ->getFilterFormat()
    ->setFilterConfig('media_embed', [
    'status' => TRUE,
  ])
    ->save();

  // Case 4: The CKEditor 5 media plugin should be enabled and the library
  // should be available now that the media_embed is enabled.
  $plugin_ids = array_keys($this->manager
    ->getEnabledDefinitions($editor));
  $expected_plugins = array_merge($default_plugins, [
    'ckeditor5_drupalMediaCaption',
    'ckeditor5_test_layercake',
    'media_media',
    'media_mediaAlign',
  ]);
  sort($expected_plugins);
  $this
    ->assertSame($expected_plugins, $plugin_ids);
  $expected_libraries = array_merge($default_libraries, [
    'ckeditor5/drupal.ckeditor5.media',
    'ckeditor5/drupal.ckeditor5.mediaAlign',
    'ckeditor5_test/layercake',
  ]);
  sort($expected_libraries);
  $this
    ->assertSame($expected_libraries, $this->manager
    ->getEnabledLibraries($editor));

  // Enable the CKEditor 5 Plugin Conditions Test module, which has the
  // ckeditor5_plugin_conditions_test_plugins_condition plugin which is
  // conditionally enabled. Clear the editor manager's static cache so that it
  // is picked up.
  $this
    ->enableModules([
    'ckeditor5_plugin_conditions_test',
  ]);
  $this->manager = $this->container
    ->get('plugin.manager.ckeditor5.plugin');
  $this->manager
    ->clearCachedDefinitions();

  // Case 5: just installing the ckeditor5_plugin_conditions_test module does
  // not enable its conditionally enabled plugin.
  $this
    ->assertSame($expected_plugins, $plugin_ids);
  $this
    ->assertSame($expected_libraries, $this->manager
    ->getEnabledLibraries($editor));

  // Case 6: placing the table plugin's button enables the table plugin, but
  // also implicitly enables the conditionally enabled plugin.
  $settings['toolbar']['items'][] = 'insertTable';
  $editor
    ->setSettings($settings);
  $plugin_ids = array_keys($this->manager
    ->getEnabledDefinitions($editor));
  $expected_plugins = array_merge($expected_plugins, [
    'ckeditor5_table',
    'ckeditor5_plugin_conditions_test_plugins_condition',
  ]);
  sort($expected_plugins);
  $this
    ->assertSame(array_values($expected_plugins), $plugin_ids);
  $expected_libraries = array_merge($default_libraries, [
    'ckeditor5/drupal.ckeditor5.media',
    'ckeditor5/drupal.ckeditor5.mediaAlign',
    'ckeditor5_test/layercake',
    'core/ckeditor5.table',
  ]);
  sort($expected_libraries);
  $this
    ->assertSame($expected_libraries, $this->manager
    ->getEnabledLibraries($editor));

  // Case 7: GHS is enabled for other text editors if they are using a
  // CKEditor 5 plugin that uses wildcard tags.
  $settings['toolbar']['items'][] = 'alignment';
  $editor
    ->setSettings($settings);
  $plugin_ids = array_keys($this->manager
    ->getEnabledDefinitions($editor));
  $expected_plugins = array_merge($expected_plugins, [
    'ckeditor5_alignment',
    'ckeditor5_wildcardHtmlSupport',
  ]);
  sort($expected_plugins);
  $this
    ->assertSame(array_values($expected_plugins), $plugin_ids);
  $expected_libraries = array_merge($expected_libraries, [
    'core/ckeditor5.alignment',
  ]);
  sort($expected_libraries);
  $this
    ->assertSame($expected_libraries, $this->manager
    ->getEnabledLibraries($editor));

  // Case 8: GHS is enabled for Full HTML (or any other text format that has
  // no TYPE_HTML_RESTRICTOR filters).
  $editor = Editor::load('full_html');
  $definitions = array_keys($this->manager
    ->getEnabledDefinitions($editor));
  $default_plugins = [
    'ckeditor5_arbitraryHtmlSupport',
    'ckeditor5_bold',
    'ckeditor5_emphasis',
    'ckeditor5_essentials',
    'ckeditor5_heading',
    'ckeditor5_paragraph',
    'ckeditor5_pasteFromOffice',
  ];
  $this
    ->assertSame($default_plugins, $definitions, 'No CKEditor 5 plugins found besides the built-in ones.');
  $default_libraries = [
    'ckeditor5/drupal.ckeditor5',
    'ckeditor5/drupal.ckeditor5.emphasis',
    'ckeditor5/drupal.ckeditor5.internal',
    'core/ckeditor5.basic',
    'core/ckeditor5.htmlSupport',
    'core/ckeditor5.internal',
    'core/ckeditor5.pasteFromOffice',
  ];
  $this
    ->assertSame($default_libraries, $this->manager
    ->getEnabledLibraries($editor));
}