Commit b0d868fc authored by catch's avatar catch
Browse files

Issue #3229078 by scott_euser, Wim Leers, hooroomoo, brentg, yogeshmpawar,...

Issue #3229078 by scott_euser, Wim Leers, hooroomoo, brentg, yogeshmpawar, catch: Unit tests for all @CKEditor5Plugin plugin classes
parent ad44453c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -170,7 +170,7 @@ private static function generateCheckboxForHeadingOption(array $heading_option):
   * Filters the header options to those chosen in editor config.
   */
  public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor): array {
    $enabled_headings = $this->getEnabledHeadings($editor);
    $enabled_headings = $this->getEnabledHeadings();
    $all_heading_options = $static_plugin_config['heading']['options'];

    $configured_heading_options = array_filter($all_heading_options, function ($option) use ($enabled_headings) {
+120 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\ckeditor5\Unit;

use Drupal\ckeditor5\Plugin\CKEditor5Plugin\Heading;
use Drupal\editor\EditorInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Yaml\Yaml;

/**
 * @coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Language
 * @group ckeditor5
 * @internal
 */
class HeadingPluginTest extends UnitTestCase {

  /**
   * Provides a list of configs to test.
   */
  public function providerGetDynamicPluginConfig(): array {
    // Prepare headings matching ckeditor5.ckeditor5.yml to also protect
    // against unexpected changes to the YAML file given the YAML file is used
    // to generate the dynamic plugin configuration.
    $paragraph = [
      'model' => 'paragraph',
      'title' => 'Paragraph',
      'class' => 'ck-heading_paragraph',
    ];
    $headings = [];
    foreach (range(2, 6) as $number) {
      $headings[$number] = [
        'model' => 'heading' . $number,
        'view' => 'h' . $number,
        'title' => 'Heading ' . $number,
        'class' => 'ck-heading_heading' . $number,
      ];
    }

    return [
      'All headings' => [
        Heading::DEFAULT_CONFIGURATION,
        [
          'heading' => [
            'options' => [
              $paragraph,
              $headings[2],
              $headings[3],
              $headings[4],
              $headings[5],
              $headings[6],
            ],
          ],
        ],
      ],
      'Only required headings' => [
        [
          'enabled_headings' => [],
        ],
        [
          'heading' => [
            'options' => [
              $paragraph,
            ],
          ],
        ],
      ],
      'Heading 2 only' => [
        [
          'enabled_headings' => [
            'heading2',
          ],
        ],
        [
          'heading' => [
            'options' => [
              $paragraph,
              $headings[2],
            ],
          ],
        ],
      ],
      'Heading 2 and 3 only' => [
        [
          'enabled_headings' => [
            'heading2',
            'heading3',
          ],
        ],
        [
          'heading' => [
            'options' => [
              $paragraph,
              $headings[2],
              $headings[3],
            ],
          ],
        ],
      ],
    ];
  }

  /**
   * @covers ::getDynamicPluginConfig
   * @dataProvider providerGetDynamicPluginConfig
   */
  public function testGetDynamicPluginConfig(array $configuration, array $expected_dynamic_config): void {
    // Read the CKEditor 5 plugin's static configuration from YAML.
    $ckeditor5_plugin_definitions = Yaml::parseFile(__DIR__ . '/../../../ckeditor5.ckeditor5.yml');
    $static_plugin_config = $ckeditor5_plugin_definitions['ckeditor5_heading']['ckeditor5']['config'];

    $plugin = new Heading($configuration, 'ckeditor5_heading', NULL);
    $dynamic_plugin_config = $plugin->getDynamicPluginConfig($static_plugin_config, $this->prophesize(EditorInterface::class)
      ->reveal());

    $this->assertSame($expected_dynamic_config, $dynamic_plugin_config);
  }

}
+110 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\ckeditor5\Unit;

use Drupal\ckeditor5\Plugin\CKEditor5Plugin\Language;
use Drupal\Core\Language\LanguageManager;
use Drupal\editor\EditorInterface;
use Drupal\Tests\UnitTestCase;

/**
 * @coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Language
 * @group ckeditor5
 * @internal
 */
class LanguagePluginTest extends UnitTestCase {

  /**
   * Provides a list of configs to test.
   */
  public function providerGetDynamicPluginConfig(): array {
    $un_expected_output = [
      'language' => [
        'textPartLanguage' => [
          [
            'title' => 'Arabic',
            'languageCode' => 'ar',
            'textDirection' => 'rtl',
          ],
          [
            'title' => 'Chinese, Simplified',
            'languageCode' => 'zh-hans',
          ],
          [
            'title' => 'English',
            'languageCode' => 'en',
          ],
          [
            'title' => 'French',
            'languageCode' => 'fr',
          ],
          [
            'title' => 'Russian',
            'languageCode' => 'ru',
          ],
          [
            'title' => 'Spanish',
            'languageCode' => 'es',
          ],
        ],
      ],
    ];
    return [
      'un' => [
        ['language_list' => 'un'],
        $un_expected_output,
      ],
      'all' => [
        ['language_list' => 'all'],
        [
          'language' => [
            'textPartLanguage' => $this->buildExpectedDynamicConfig(LanguageManager::getStandardLanguageList()),
          ],
        ],
      ],
      'default configuration' => [
        [],
        $un_expected_output,
      ],
    ];
  }

  /**
   * Builds the expected dynamic configuration output given a language list.
   *
   * @param array $language_list
   *   The languages list from the language manager.
   *
   * @return array
   *   The expected output of the dynamic plugin configuration.
   */
  protected static function buildExpectedDynamicConfig(array $language_list) {
    $expected_language_config = [];
    foreach ($language_list as $language_code => $language_list_item) {
      $item = [
        'title' => $language_list_item[0],
        'languageCode' => $language_code,
      ];
      if (isset($language_list_item[2])) {
        $item['textDirection'] = $language_list_item[2];
      }
      $expected_language_config[$item['title']] = $item;
    }
    ksort($expected_language_config);
    return array_values($expected_language_config);
  }

  /**
   * @covers ::getDynamicPluginConfig
   * @dataProvider providerGetDynamicPluginConfig
   */
  public function testGetDynamicPluginConfig(array $configuration, array $expected_dynamic_config): void {
    $plugin = new Language($configuration, 'ckeditor5_language', NULL);
    $dynamic_config = $plugin->getDynamicPluginConfig([], $this->prophesize(EditorInterface::class)
      ->reveal());
    $this->assertSame($expected_dynamic_config, $dynamic_config);
  }

}
+179 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\ckeditor5\Unit;

use Drupal\ckeditor5\Plugin\CKEditor5Plugin\SourceEditing;
use Drupal\editor\EditorInterface;
use Drupal\Tests\UnitTestCase;

/**
 * @coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\SourceEditing
 * @group ckeditor5
 * @internal
 */
class SourceEditingPluginTest extends UnitTestCase {

  /**
   * Provides a list of configs to test.
   */
  public function providerGetDynamicPluginConfig(): array {
    return [
      'Empty array of allowed tags' => [
        [
          'allowed_tags' => [],
        ],
        [
          'htmlSupport' => [
            'allow' => [],
          ],
        ],
      ],
      'Simple' => [
        [
          'allowed_tags' => [
            '<foo1>',
            '<foo2 bar>',
            '<foo3 bar="baz">',
            '<foo4 bar="baz qux">',
            '<foo5 bar="baz" qux="foo">',
          ],
        ],
        [
          'htmlSupport' => [
            'allow' => [
              [
                'name' => 'foo1',
              ],
              [
                'name' => 'foo2',
                'attributes' => [
                  [
                    'key' => 'bar',
                    'value' => TRUE,
                  ],
                ],
              ],
              [
                'name' => 'foo3',
                'attributes' => [
                  [
                    'key' => 'bar',
                    'value' => [
                      'regexp' => [
                        'pattern' => '/^(baz)$/',
                      ],
                    ],
                  ],
                ],
              ],
              [
                'name' => 'foo4',
                'attributes' => [
                  [
                    'key' => 'bar',
                    'value' => [
                      'regexp' => [
                        'pattern' => '/^(baz|qux)$/',
                      ],
                    ],
                  ],
                ],
              ],
              [
                'name' => 'foo5',
                'attributes' => [
                  [
                    'key' => 'bar',
                    'value' => [
                      'regexp' => [
                        'pattern' => '/^(baz)$/',
                      ],
                    ],
                  ],
                  [
                    'key' => 'qux',
                    'value' => [
                      'regexp' => [
                        'pattern' => '/^(foo)$/',
                      ],
                    ],
                  ],
                ],
              ],
            ],
          ],
        ],
      ],
      'Prefix wildcards' => [
        [
          'allowed_tags' => [
            '<foo1 bar-*>',
            '<foo2 bar-*="baz">',
            '<foo3 bar-*="baz qux-*">',
            '<foo2 bar="baz-*">',
            '<foo3 bar="baz qux-*">',
          ],
        ],
        [
          'htmlSupport' => [
            'allow' => [
              [
                'name' => 'foo1',
                'attributes' => [
                  [
                    'key' => [
                      'regexp' => [
                        'pattern' => '/^bar-.*$/',
                      ],
                    ],
                    'value' => TRUE,
                  ],
                ],
              ],
              [
                'name' => 'foo2',
                'attributes' => [
                  [
                    'key' => 'bar',
                    'value' => [
                      'regexp' => [
                        'pattern' => '/^(baz-.*)$/',
                      ],
                    ],
                  ],
                ],
              ],
              [
                'name' => 'foo3',
                'attributes' => [
                  [
                    'key' => 'bar',
                    'value' => [
                      'regexp' => [
                        'pattern' => '/^(baz|qux-.*)$/',
                      ],
                    ],
                  ],
                ],
              ],
            ],
          ],
        ],
      ],
    ];
  }

  /**
   * @covers ::getDynamicPluginConfig
   * @dataProvider providerGetDynamicPluginConfig
   */
  public function testGetDynamicPluginConfig(array $configuration, array $expected_dynamic_config): void {
    $plugin = new SourceEditing($configuration, 'ckeditor5_sourceEditing', NULL);
    $dynamic_plugin_config = $plugin->getDynamicPluginConfig([], $this->prophesize(EditorInterface::class)
      ->reveal());
    $this->assertSame($expected_dynamic_config, $dynamic_plugin_config);
  }

}