Verified Commit c94dc2dc authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3274278 by Wim Leers, jcnventura, yogeshmpawar, andregp, bnjmnm:...

Issue #3274278 by Wim Leers, jcnventura, yogeshmpawar, andregp, bnjmnm: Migrate "codetag" contrib CKEditor 4 plugin to built-in equivalent in core's CKEditor 5

(cherry picked from commit 46903e6c)
parent d4bf3002
Loading
Loading
Loading
Loading
+59 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\ckeditor5\Plugin\CKEditor4To5Upgrade;

use Drupal\ckeditor5\HTMLRestrictions;
use Drupal\ckeditor5\Plugin\CKEditor4To5UpgradePluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\filter\FilterFormatInterface;

/**
 * Provides the CKEditor 4 to 5 upgrade path for contrib plugins now in core.
 *
 * @CKEditor4To5Upgrade(
 *   id = "contrib",
 *   cke4_buttons = {
 *     "Code"
 *   },
 *   cke4_plugin_settings = {
 *   },
 *   cke5_plugin_elements_subset_configuration = {
 *   }
 * )
 *
 * @internal
 *   Plugin classes are internal.
 */
class Contrib extends PluginBase implements CKEditor4To5UpgradePluginInterface {

  /**
   * {@inheritdoc}
   */
  public function mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem(string $cke4_button, HTMLRestrictions $text_format_html_restrictions): ?array {
    switch ($cke4_button) {
      // @see https://www.drupal.org/project/codetag
      case 'Code':
        return ['code'];

      default:
        throw new \OutOfBoundsException();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function mapCKEditor4SettingsToCKEditor5Configuration(string $cke4_plugin_id, array $cke4_plugin_settings): ?array {
    throw new \OutOfBoundsException();
  }

  /**
   * {@inheritdoc}
   */
  public function computeCKEditor5PluginSubsetConfiguration(string $cke5_plugin_id, FilterFormatInterface $text_format): ?array {
    throw new \OutOfBoundsException();
  }

}
+14 −0
Original line number Diff line number Diff line
@@ -19,6 +19,19 @@
 */
class CKEditor4to5UpgradeCompletenessTest extends KernelTestBase {

  /**
   * The CKEditor 4 toolbar buttons that no longer require a contrib module.
   *
   * @var string[]
   *
   * @see \Drupal\ckeditor5\Plugin\CKEditor4To5Upgrade\Contrib
   */
  const CONTRIB_BUTTONS_NOW_IN_CORE = [
    // @see https://www.drupal.org/project/codetag
    // @see ckeditor5_code's `basicStyles.Code` plugin
    'Code',
  ];

  /**
   * The "CKEditor 4 plugin" plugin manager.
   *
@@ -89,6 +102,7 @@ protected function setUp(): void {
   */
  public function testButtons(): void {
    $cke4_buttons = array_keys(NestedArray::mergeDeepArray($this->cke4PluginManager->getButtons()));
    $cke4_buttons = array_merge($cke4_buttons, self::CONTRIB_BUTTONS_NOW_IN_CORE);

    foreach ($cke4_buttons as $button) {
      $equivalent = $this->upgradePluginManager->mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem($button, HTMLRestrictions::emptySet());
+40 −0
Original line number Diff line number Diff line
@@ -261,6 +261,30 @@ protected function setUp(): void {
      ],
    ])->save();

    FilterFormat::create([
      'format' => 'cke4_contrib_plugins_now_in_core',
      'name' => 'All CKEditor 4 contrib plugins now in core',
    ])->save();
    Editor::create([
      'format' => 'cke4_contrib_plugins_now_in_core',
      'editor' => 'ckeditor',
      'settings' => [
        'toolbar' => [
          'rows' => [
            0 => [
              [
                'name' => 'Contributed modules providing buttons without settings',
                'items' => [
                  // @see https://www.drupal.org/project/codetag
                  'Code',
                ],
              ],
            ],
          ],
        ],
        'plugins' => [],
      ],
    ])->save();
  }

  /**
@@ -924,6 +948,22 @@ public function provider() {
      'expected_fundamental_compatibility_violations' => [],
      'expected_messages' => [],
    ];

    yield "cke4_contrib_plugins_now_in_core can be switched to CKEditor 5 without problems" => [
      'format_id' => 'cke4_contrib_plugins_now_in_core',
      'filters_to_drop' => [],
      'expected_ckeditor5_settings' => [
        'toolbar' => [
          'items' => [
            'code',
          ],
        ],
        'plugins' => [],
      ],
      'expected_superset' => '',
      'expected_fundamental_compatibility_violations' => [],
      'expected_messages' => [],
    ];
  }

}