You are here

public function CKEditor5PluginManagerTest::testInvalidPluginDefinitions in Drupal 10

@covers \Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition::__construct() @dataProvider providerTestInvalidPluginDefinitions

File

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

Class

CKEditor5PluginManagerTest
Tests different ways of enabling CKEditor 5 plugins.

Namespace

Drupal\Tests\ckeditor5\Kernel

Code

public function testInvalidPluginDefinitions(string $yaml, ?string $expected_message, array $additional_files = []) : void {
  if ($expected_message) {
    $this
      ->expectException(InvalidPluginDefinitionException::class);
    $this
      ->expectExceptionMessage($expected_message);
  }
  $site_directory = ltrim(parse_url($this->siteDirectory)['path'], '/');
  vfsStream::create([
    'modules' => [
      'ckeditor5_invalid_plugin' => [
        'ckeditor5_invalid_plugin.info.yml' => <<<YAML
name: CKEditor 5 Invalid Plugin Definition Test
type: module
core_version_requirement: ^9
YAML
,
        'ckeditor5_invalid_plugin.ckeditor5.yml' => $yaml,
      ] + $additional_files,
    ],
  ], $this->vfsRoot
    ->getChild($site_directory));
  if (!empty($additional_files)) {
    $additional_class_loader = new ClassLoader();
    $additional_class_loader
      ->addPsr4("Drupal\\ckeditor5_invalid_plugin\\Plugin\\CKEditor5Plugin\\", vfsStream::url("root/{$site_directory}/modules/ckeditor5_invalid_plugin/src/Plugin/CKEditor5Plugin"));
    $additional_class_loader
      ->register(TRUE);
  }
  $config_sync = \Drupal::service('config.storage');
  $config_data = $this
    ->config('core.extension')
    ->get();
  $config_data['module']['ckeditor5_invalid_plugin'] = 1;
  $config_sync
    ->write('core.extension', $config_data);

  // Construct a new container for testing a plugin definition in isolation,
  // without needing a separate module directory structure for it, and instead
  // allowing it to be provided entirely by a PHPUnit data provider. Inherit
  // all definitions from the successfully installed Drupal site for this
  // kernel test, but do not use $this->container. This is a hybrid of kernel
  // and unit test, to get the best of both worlds: test a unit, but ensure
  // the service definitions are in sync.
  $root = vfsStream::url("root/{$site_directory}");
  $container = new ContainerBuilder(new FrozenParameterBag([
    'app.root' => $root,
    'container.modules' => [
      'ckeditor5_invalid_plugin' => [
        'type' => 'module',
        'pathname' => 'modules/ckeditor5_invalid_plugin/ckeditor5_invalid_plugin.info.yml',
        'filename' => NULL,
      ] + $this->container
        ->getParameter('container.modules'),
    ],
  ] + $this->container
    ->getParameterBag()
    ->all()));
  $container
    ->setDefinitions($this->container
    ->getDefinitions());

  // The exception to the above elegance: re-resolve the '%app_root%' param.
  // @see \Symfony\Component\DependencyInjection\Compiler\ResolveParameterPlaceHoldersPass
  // @see \Drupal\Core\DrupalKernel::guessApplicationRoot()
  $container
    ->getDefinition('module_handler')
    ->setArgument(0, '%app.root%');

  // To discover per-test case config schema YAML files, work around the
  // static file cache in \Drupal\Core\Extension\ExtensionDiscovery. There is
  // no work-around that allows using both the files on disk and some in vfs.
  // To make matters worse, decorating a service within the test only is not
  // an option either, because \Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition
  // is a pure value object, so it uses the global container. Therefore the
  // only work-around possible is to manipulate the config schema definition
  // cache.
  // @todo Remove this in https://www.drupal.org/project/drupal/issues/2961541.
  if (isset($additional_files['config']['schema']['ckeditor5_invalid_plugin.schema.yml'])) {
    $cache = \Drupal::service('cache.discovery')
      ->get('typed_config_definitions');
    $typed_config_definitions = $cache->data;
    $typed_config_definitions += Yaml::parse($additional_files['config']['schema']['ckeditor5_invalid_plugin.schema.yml']);
    \Drupal::service('config.typed')
      ->clearCachedDefinitions();
    \Drupal::service('cache.discovery')
      ->set('typed_config_definitions', $typed_config_definitions, $cache->expire, $cache->tags);
  }
  $container
    ->get('plugin.manager.ckeditor5.plugin')
    ->getDefinitions();
}