class DefaultPluginManagerTest

Same name in this branch
  1. 10 core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php \Drupal\Tests\Core\Plugin\DefaultPluginManagerTest
Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php \Drupal\Tests\Core\Plugin\DefaultPluginManagerTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php \Drupal\Tests\Core\Plugin\DefaultPluginManagerTest
  3. 11.x core/tests/Drupal/KernelTests/Core/Plugin/DefaultPluginManagerTest.php \Drupal\KernelTests\Core\Plugin\DefaultPluginManagerTest
  4. 11.x core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php \Drupal\Tests\Core\Plugin\DefaultPluginManagerTest

Tests the default plugin manager.

@group Plugin

Hierarchy

Expanded class hierarchy of DefaultPluginManagerTest

File

core/tests/Drupal/KernelTests/Core/Plugin/DefaultPluginManagerTest.php, line 18

Namespace

Drupal\KernelTests\Core\Plugin
View source
class DefaultPluginManagerTest extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'plugin_test',
  ];
  
  /**
   * Tests annotations and attributes on the default plugin manager.
   */
  public function testDefaultPluginManager() : void {
    $subdir = 'Plugin/plugin_test/custom_annotation';
    $base_directory = $this->root . '/core/modules/system/tests/modules/plugin_test/src';
    $namespaces = new \ArrayObject([
      'Drupal\\plugin_test' => $base_directory,
    ]);
    $module_handler = $this->container
      ->get('module_handler');
    // Ensure broken files exist as expected.
    try {
      $e = NULL;
      new \ReflectionClass('\\Drupal\\plugin_test\\Plugin\\plugin_test\\custom_annotation\\ExtendingNonInstalledClass');
    } catch (\Throwable $e) {
    } finally {
      $this->assertInstanceOf(\Throwable::class, $e);
      $this->assertSame('Class "Drupal\\non_installed_module\\NonExisting" not found', $e->getMessage());
    }
    // Ensure there is a class with the expected name. We cannot reflect on this
    // as it triggers a fatal error.
    $this->assertFileExists($base_directory . '/' . $subdir . '/UsingNonInstalledTraitClass.php');
    // Annotation only.
    $manager = new DefaultPluginManager($subdir, $namespaces, $module_handler, NULL, AnnotationPluginExample::class);
    $definitions = $manager->getDefinitions();
    $this->assertArrayHasKey('example_1', $definitions);
    $this->assertArrayHasKey('example_2', $definitions);
    $this->assertArrayNotHasKey('example_3', $definitions);
    $this->assertArrayNotHasKey('example_4', $definitions);
    $this->assertArrayNotHasKey('example_5', $definitions);
    // Annotations and attributes together.
    $manager = new DefaultPluginManager($subdir, $namespaces, $module_handler, NULL, AttributePluginExample::class, AnnotationPluginExample::class);
    $definitions = $manager->getDefinitions();
    $this->assertArrayHasKey('example_1', $definitions);
    $this->assertArrayHasKey('example_2', $definitions);
    $this->assertArrayHasKey('example_3', $definitions);
    $this->assertArrayHasKey('example_4', $definitions);
    $this->assertArrayHasKey('example_5', $definitions);
    // Attributes only.
    // \Drupal\Component\Plugin\Discovery\AttributeClassDiscovery does not
    // support parsing classes that cannot be reflected. Therefore, we use VFS
    // to create a directory remove plugin_test's plugins and remove the broken
    // plugins.
    vfsStream::setup('plugin_test');
    $dir = vfsStream::create([
      'src' => [
        'Plugin' => [
          'plugin_test' => [
            'custom_annotation' => [],
          ],
        ],
      ],
    ]);
    $plugin_directory = $dir->getChild('src/' . $subdir);
    vfsStream::copyFromFileSystem($base_directory . '/' . $subdir, $plugin_directory);
    $plugin_directory->removeChild('ExtendingNonInstalledClass.php');
    $plugin_directory->removeChild('UsingNonInstalledTraitClass.php');
    $namespaces = new \ArrayObject([
      'Drupal\\plugin_test' => vfsStream::url('plugin_test/src'),
    ]);
    $manager = new DefaultPluginManager($subdir, $namespaces, $module_handler, NULL, AttributePluginExample::class);
    $definitions = $manager->getDefinitions();
    $this->assertArrayNotHasKey('example_1', $definitions);
    $this->assertArrayNotHasKey('example_2', $definitions);
    $this->assertArrayHasKey('example_3', $definitions);
    $this->assertArrayHasKey('example_4', $definitions);
    $this->assertArrayHasKey('example_5', $definitions);
    $this->assertArrayNotHasKey('extending_non_installed_class', $definitions);
    $this->assertArrayNotHasKey('using_non_installed_trait', $definitions);
  }

}

Members

Title Sort descending Modifiers Object type Summary
DefaultPluginManagerTest::$modules protected static property Modules to install.
DefaultPluginManagerTest::testDefaultPluginManager public function Tests annotations and attributes on the default plugin manager.
ExtensionListTestTrait::getModulePath protected function Gets the path for the specified module.
ExtensionListTestTrait::getThemePath protected function Gets the path for the specified theme.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.

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