function CKEditor5PluginManagerTest::providerTestDerivedPluginDefinitions

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

Data provider.

Return value

\Generator Test scenarios.

File

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

Class

CKEditor5PluginManagerTest
Tests different ways of enabling CKEditor 5 plugins.

Namespace

Drupal\Tests\ckeditor5\Kernel

Code

public static function providerTestDerivedPluginDefinitions() : \Generator {
    // Defaults inherited from CKEditor5AspectsOfCKEditor5Plugin.
    $ckeditor5_aspects_defaults = get_class_vars(CKEditor5AspectsOfCKEditor5Plugin::class);
    // Defaults inherited from DrupalAspectsOfCKEditor5Plugin.
    $drupal_aspects_defaults = get_class_vars(DrupalAspectsOfCKEditor5Plugin::class);
    $simple_deriver_additional_files = [
        'src' => [
            'Plugin' => [
                'CKEditor5Plugin' => [
                    'SimpleDeriver.php' => <<<'PHP'
<?php
namespace Drupal\ckeditor5_derived_plugin\Plugin\CKEditor5Plugin;
use Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition;
use Drupal\Component\Plugin\Derivative\DeriverBase;
class SimpleDeriver extends DeriverBase {
  public function getDerivativeDefinitions($base_plugin_definition) {
    assert($base_plugin_definition instanceof CKEditor5PluginDefinition);
    foreach (['bar', 'baz'] as $id) {
      $definition = $base_plugin_definition->toArray();
      $definition['id'] = $id;
      $definition['drupal']['label'] = sprintf("Foo %s", $id);
      $this->derivatives[$id] = new CKEditor5PluginDefinition($definition);
    }
    return $this->derivatives;
  }
}
PHP
,
                ],
            ],
        ],
    ];
    (yield 'INVALID: simple deriver but without `drupal.elements` in the base definition and it not getting set by the deriver' => [
        <<<YAML
ckeditor5_derived_plugin_foo:
  ckeditor5:
    plugins: {}
  drupal:
    deriver: Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver
YAML
,
        InvalidPluginDefinitionException::class,
        'The "ckeditor5_derived_plugin_foo:bar" CKEditor 5 derived plugin definition must contain a "drupal.elements" key.',
        $simple_deriver_additional_files,
    ]);
    (yield 'INVALID: simple deriver but without `ckeditor5.plugins` in the base definition and it not getting set by the deriver' => [
        <<<YAML
ckeditor5_derived_plugin_foo:
  ckeditor5: {}
  drupal:
    elements: false
    deriver: Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver
YAML
,
        \ArgumentCountError::class,
        NULL,
        $simple_deriver_additional_files,
    ]);
    (yield 'INVALID: simple deriver but without `ckeditor5` in the base definition and it not getting set by the deriver' => [
        <<<YAML
ckeditor5_derived_plugin_foo:
  drupal:
    elements: false
    deriver: Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver
YAML
,
        InvalidPluginDefinitionException::class,
        'The "ckeditor5_derived_plugin_foo:bar" CKEditor 5 derived plugin definition must contain a "ckeditor5" key.',
        $simple_deriver_additional_files,
    ]);
    (yield 'INVALID: simple deriver which returns arrays instead of CKEditor5PluginDefinition instances' => [
        <<<YAML
ckeditor5_derived_plugin_foo:
  ckeditor5:
    plugins: {}
  drupal:
    elements: false
    deriver: Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver
YAML
,
        InvalidPluginDefinitionException::class,
        'The "ckeditor5_derived_plugin_foo:bar" CKEditor 5 plugin definition must extend Drupal\\ckeditor5\\Plugin\\CKEditor5PluginDefinition',
        [
            'src' => [
                'Plugin' => [
                    'CKEditor5Plugin' => [
                        'SimpleDeriver.php' => <<<'PHP'
<?php
namespace Drupal\ckeditor5_derived_plugin\Plugin\CKEditor5Plugin;
use Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition;
use Drupal\Component\Plugin\Derivative\DeriverBase;
class SimpleDeriver extends DeriverBase {
  public function getDerivativeDefinitions($base_plugin_definition) {
    assert($base_plugin_definition instanceof CKEditor5PluginDefinition);
    foreach (['bar', 'baz'] as $id) {
      $definition = $base_plugin_definition->toArray();
      $definition['id'] = $id;
      $definition['drupal']['label'] = sprintf("Foo %s", $id);
      $this->derivatives[$id] = $definition;
    }
    return $this->derivatives;
  }
}
PHP
,
                    ],
                ],
            ],
        ],
    ]);
    (yield 'VALID: simple deriver, base definition in YAML' => [
        <<<YAML
ckeditor5_derived_plugin_foo:
  ckeditor5:
    plugins: {}
  drupal:
    elements: false
    deriver: Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver
YAML
,
        NULL,
        NULL,
        $simple_deriver_additional_files,
        [
            'ckeditor5_derived_plugin_foo:bar' => new CKEditor5PluginDefinition([
                'provider' => 'ckeditor5_derived_plugin',
                'id' => 'ckeditor5_derived_plugin_foo:bar',
                'ckeditor5' => [
                    'plugins' => [],
                ] + $ckeditor5_aspects_defaults,
                'drupal' => [
                    'label' => 'Foo bar',
                    'elements' => FALSE,
                    'deriver' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver',
                ] + $drupal_aspects_defaults,
            ]),
            'ckeditor5_derived_plugin_foo:baz' => new CKEditor5PluginDefinition([
                'provider' => 'ckeditor5_derived_plugin',
                'id' => 'ckeditor5_derived_plugin_foo:baz',
                'ckeditor5' => [
                    'plugins' => [],
                ] + $ckeditor5_aspects_defaults,
                'drupal' => [
                    'label' => 'Foo baz',
                    'elements' => FALSE,
                    'deriver' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver',
                ] + $drupal_aspects_defaults,
            ]),
        ],
    ]);
    (yield 'VALID: simple deriver, base definition in PHP with Attribute' => [
        '',
        NULL,
        NULL,
        [
            'src' => [
                'Plugin' => [
                    'CKEditor5Plugin' => [
                        'Foo.php' => <<<'PHP'
<?php
declare(strict_types = 1);
namespace Drupal\ckeditor5_derived_plugin\Plugin\CKEditor5Plugin;
use Drupal\ckeditor5\Attribute\CKEditor5AspectsOfCKEditor5Plugin;
use Drupal\ckeditor5\Attribute\CKEditor5Plugin;
use Drupal\ckeditor5\Attribute\DrupalAspectsOfCKEditor5Plugin;
use Drupal\ckeditor5\Plugin\CKEditor5PluginDefault;
use Drupal\Core\StringTranslation\TranslatableMarkup;

#[CKEditor5Plugin(
  id: 'ckeditor5_derived_plugin_foo',
  ckeditor5: new CKEditor5AspectsOfCKEditor5Plugin(
    plugins: [],
  ),
  drupal: new DrupalAspectsOfCKEditor5Plugin(
    elements: false,
    deriver: 'Drupal\ckeditor5_derived_plugin\Plugin\CKEditor5Plugin\SimpleDeriver',
  ),
)]
class Foo extends CKEditor5PluginDefault {
}
PHP
,
                        'SimpleDeriver.php' => $simple_deriver_additional_files['src']['Plugin']['CKEditor5Plugin']['SimpleDeriver.php'],
                    ],
                ],
            ],
        ],
        [
            'ckeditor5_derived_plugin_foo:bar' => new CKEditor5PluginDefinition([
                'provider' => 'ckeditor5_derived_plugin',
                'id' => 'ckeditor5_derived_plugin_foo:bar',
                'ckeditor5' => [
                    'plugins' => [],
                ] + $ckeditor5_aspects_defaults,
                'drupal' => [
                    'class' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\Foo',
                    'label' => 'Foo bar',
                    'elements' => FALSE,
                    'deriver' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver',
                ] + $drupal_aspects_defaults,
            ]),
            'ckeditor5_derived_plugin_foo:baz' => new CKEditor5PluginDefinition([
                'provider' => 'ckeditor5_derived_plugin',
                'id' => 'ckeditor5_derived_plugin_foo:baz',
                'ckeditor5' => [
                    'plugins' => [],
                ] + $ckeditor5_aspects_defaults,
                'drupal' => [
                    'class' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\Foo',
                    'label' => 'Foo baz',
                    'elements' => FALSE,
                    'deriver' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver',
                ] + $drupal_aspects_defaults,
            ]),
        ],
    ]);
    (yield 'VALID: simple deriver, base definition in PHP with Annotation' => [
        '',
        NULL,
        NULL,
        [
            'src' => [
                'Plugin' => [
                    'CKEditor5Plugin' => [
                        'Foo.php' => <<<'PHP'
<?php
declare(strict_types = 1);
namespace Drupal\ckeditor5_derived_plugin\Plugin\CKEditor5Plugin;
use Drupal\ckeditor5\Plugin\CKEditor5PluginDefault;
/**
 * @CKEditor5Plugin(
 *   id = "ckeditor5_derived_plugin_foo",
 *   ckeditor5 = @CKEditor5AspectsOfCKEditor5Plugin(
 *     plugins = {},
 *   ),
 *   drupal = @DrupalAspectsOfCKEditor5Plugin(
 *     elements = false,
 *     deriver = "Drupal\ckeditor5_derived_plugin\Plugin\CKEditor5Plugin\SimpleDeriver",
 *   )
 * )
 */
class Foo extends CKEditor5PluginDefault {
}
PHP
,
                        'SimpleDeriver.php' => $simple_deriver_additional_files['src']['Plugin']['CKEditor5Plugin']['SimpleDeriver.php'],
                    ],
                ],
            ],
        ],
        [
            'ckeditor5_derived_plugin_foo:bar' => new CKEditor5PluginDefinition([
                'provider' => 'ckeditor5_derived_plugin',
                'id' => 'ckeditor5_derived_plugin_foo:bar',
                'ckeditor5' => [
                    'plugins' => [],
                ] + $ckeditor5_aspects_defaults,
                'drupal' => [
                    'class' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\Foo',
                    'label' => 'Foo bar',
                    'elements' => FALSE,
                    'deriver' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver',
                ] + $drupal_aspects_defaults,
            ]),
            'ckeditor5_derived_plugin_foo:baz' => new CKEditor5PluginDefinition([
                'provider' => 'ckeditor5_derived_plugin',
                'id' => 'ckeditor5_derived_plugin_foo:baz',
                'ckeditor5' => [
                    'plugins' => [],
                ] + $ckeditor5_aspects_defaults,
                'drupal' => [
                    'class' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\Foo',
                    'label' => 'Foo baz',
                    'elements' => FALSE,
                    'deriver' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\SimpleDeriver',
                ] + $drupal_aspects_defaults,
            ]),
        ],
    ]);
    (yield 'VALID: minimal base plugin definition, maximal deriver' => [
        <<<YAML
# Minimal annotation key-value pairs set in the YAML, most set in the deriver.
ckeditor5_derived_plugin_foo:
  drupal:
    deriver: Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\MaximalDeriver
YAML
,
        NULL,
        NULL,
        [
            'src' => [
                'Plugin' => [
                    'CKEditor5Plugin' => [
                        'MaximalDeriver.php' => <<<'PHP'
<?php
namespace Drupal\ckeditor5_derived_plugin\Plugin\CKEditor5Plugin;
use Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition;
use Drupal\Component\Plugin\Derivative\DeriverBase;
class MaximalDeriver extends DeriverBase {
  public function getDerivativeDefinitions($base_plugin_definition) {
    assert($base_plugin_definition instanceof CKEditor5PluginDefinition);
    foreach (['A', 'B'] as $id) {
      $definition = $base_plugin_definition->toArray();
      $definition['id'] = $id;
      $definition['drupal']['label'] = sprintf("Foo %s", $id);
      $definition['drupal']['elements'] = FALSE;
      $definition['ckeditor5']['plugins'] = [];
      $this->derivatives[$id] = new CKEditor5PluginDefinition($definition);
    }
    return $this->derivatives;
  }
}
PHP
,
                    ],
                ],
            ],
        ],
        [
            'ckeditor5_derived_plugin_foo:A' => new CKEditor5PluginDefinition([
                'provider' => 'ckeditor5_derived_plugin',
                'id' => 'ckeditor5_derived_plugin_foo:A',
                'ckeditor5' => [
                    'plugins' => [],
                ],
                'drupal' => [
                    'label' => 'Foo A',
                    'elements' => FALSE,
                    'deriver' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\MaximalDeriver',
                ] + $drupal_aspects_defaults,
            ]),
            'ckeditor5_derived_plugin_foo:B' => new CKEditor5PluginDefinition([
                'provider' => 'ckeditor5_derived_plugin',
                'id' => 'ckeditor5_derived_plugin_foo:B',
                'ckeditor5' => [
                    'plugins' => [],
                ],
                'drupal' => [
                    'label' => 'Foo B',
                    'elements' => FALSE,
                    'deriver' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\MaximalDeriver',
                ] + $drupal_aspects_defaults,
            ]),
        ],
    ]);
    (yield 'VALID: container-dependent deriver' => [
        <<<YAML
ckeditor5_derived_plugin_foo:
  ckeditor5:
    plugins: {}
  drupal:
    elements: false
    deriver: Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\ContainerDependentDeriver
YAML
,
        NULL,
        NULL,
        [
            'config' => [
                'schema' => [
                    'ckeditor5_derived_plugin.schema.yml' => <<<YAML
ckeditor5.plugin.ckeditor5_derived_plugin:
  type: mapping
  label: 'Foo'
  mapping:
    foo:
      type: boolean
      label: 'Foo'
YAML
,
                ],
            ],
            'src' => [
                'Plugin' => [
                    'CKEditor5Plugin' => [
                        'ContainerDependentDeriver.php' => <<<'PHP'
<?php
namespace Drupal\ckeditor5_derived_plugin\Plugin\CKEditor5Plugin;
use Drupal\ckeditor5\Plugin\CKEditor5PluginDefinition;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Authentication\AuthenticationCollectorInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContainerDependentDeriver extends DeriverBase implements ContainerDeriverInterface {
  protected $authenticationCollector;
  public function __construct(AuthenticationCollectorInterface $authentication_collector) {
    $this->authenticationCollector = $authentication_collector;
  }
  public static function create(ContainerInterface $container, $base_plugin_id) {
    assert($base_plugin_id === 'ckeditor5_derived_plugin_foo');
    return new static($container->get('authentication_collector'));
  }
  public function getDerivativeDefinitions($base_plugin_definition) {
    assert($base_plugin_definition instanceof CKEditor5PluginDefinition);
    $authentication_providers = array_keys($this->authenticationCollector->getSortedProviders());
    foreach ($authentication_providers as $id) {
      $definition = $base_plugin_definition->toArray();
      $definition['id'] = $id;
      $definition['drupal']['label'] = sprintf("Foo %s", $id);
      $this->derivatives[$definition['id']] = new CKEditor5PluginDefinition($definition);
    }
    return $this->derivatives;
  }
}
PHP
,
                    ],
                ],
            ],
        ],
        [
            'ckeditor5_derived_plugin_foo:cookie' => new CKEditor5PluginDefinition([
                'provider' => 'ckeditor5_derived_plugin',
                'id' => 'ckeditor5_derived_plugin_foo:cookie',
                'ckeditor5' => [
                    'plugins' => [],
                ] + $ckeditor5_aspects_defaults,
                'drupal' => [
                    'label' => 'Foo cookie',
                    'elements' => FALSE,
                    'deriver' => 'Drupal\\ckeditor5_derived_plugin\\Plugin\\CKEditor5Plugin\\ContainerDependentDeriver',
                ] + $drupal_aspects_defaults,
            ]),
        ],
    ]);
}

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