function CKEditor5PluginManagerTest::mockModuleInVfs

Same name and namespace in other branches
  1. 9 core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php \Drupal\Tests\ckeditor5\Kernel\CKEditor5PluginManagerTest::mockModuleInVfs()
  2. 11.x core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php \Drupal\Tests\ckeditor5\Kernel\CKEditor5PluginManagerTest::mockModuleInVfs()

Mocks a module providing a CKEditor 5 plugin in VFS.

Parameters

string $module_name: The name of the module.

string $yaml: The YAML to be stored in the *.ckeditor5.yml file.

array $additional_files: The additional files to create.

Return value

\Symfony\Component\DependencyInjection\ContainerInterface The container that has the VFS-mocked CKEditor 5 plugin-providing module installed in it; this container must be used to simulate this module being installed.

2 calls to CKEditor5PluginManagerTest::mockModuleInVfs()
CKEditor5PluginManagerTest::testDerivedPluginDefinitions in core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php
@covers \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::getDiscovery[[api-linebreak]] @dataProvider providerTestDerivedPluginDefinitions
CKEditor5PluginManagerTest::testInvalidPluginDefinitions in core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php
@covers \Drupal\ckeditor5\Plugin\CKEditor5PluginManager::processDefinition[[api-linebreak]] @dataProvider providerTestInvalidPluginDefinitions

File

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

Class

CKEditor5PluginManagerTest
Tests different ways of enabling CKEditor 5 plugins.

Namespace

Drupal\Tests\ckeditor5\Kernel

Code

private function mockModuleInVfs(string $module_name, string $yaml, array $additional_files = []) : ContainerInterface {
  $site_directory = ltrim(parse_url($this->siteDirectory)['path'], '/');
  vfsStream::create([
    'modules' => [
      $module_name => [
        "{$module_name}.info.yml" => <<<YAML
name: CKEditor 5 Test {<span class="php-variable">$module_name</span>}
type: module
core_version_requirement: ^9
YAML
,
        "{$module_name}.ckeditor5.yml" => $yaml,
      ] + $additional_files,
    ],
  ], $this->vfsRoot
    ->getChild($site_directory));
  if (!empty($additional_files)) {
    $additional_class_loader = new ClassLoader();
    $additional_class_loader->addPsr4("Drupal\\{$module_name}\\Plugin\\CKEditor5Plugin\\", vfsStream::url("root/{$site_directory}/modules/{$module_name}/src/Plugin/CKEditor5Plugin"));
    $additional_class_loader->register(TRUE);
  }
  $config_sync = \Drupal::service('config.storage');
  $config_data = $this->config('core.extension')
    ->get();
  $config_data['module'][$module_name] = 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' => [
      $module_name => [
        'type' => 'module',
        'pathname' => "modules/{$module_name}/{$module_name}.info.yml",
        'filename' => NULL,
      ] + $this->container
        ->getParameter('container.modules'),
    ],
    'container.namespaces' => [
      "Drupal\\{$module_name}" => vfsStream::url("root/{$site_directory}/modules/{$module_name}/src"),
    ] + $this->container
      ->getParameter('container.namespaces'),
  ] + $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']["{$module_name}.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']["{$module_name}.schema.yml"]);
    \Drupal::service('config.typed')->clearCachedDefinitions();
    \Drupal::service('cache.discovery')->set('typed_config_definitions', $typed_config_definitions, $cache->expire, $cache->tags);
  }
  return $container;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.