You are here

public function FilteredPluginManagerTraitTest::testGetFilteredDefinitions in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php \Drupal\Tests\Core\Plugin\FilteredPluginManagerTraitTest::testGetFilteredDefinitions()
  2. 9 core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php \Drupal\Tests\Core\Plugin\FilteredPluginManagerTraitTest::testGetFilteredDefinitions()

@covers ::getFilteredDefinitions @dataProvider providerTestGetFilteredDefinitions

File

core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php, line 23

Class

FilteredPluginManagerTraitTest
@coversDefaultClass \Drupal\Core\Plugin\FilteredPluginManagerTrait @group Plugin

Namespace

Drupal\Tests\Core\Plugin

Code

public function testGetFilteredDefinitions($contexts, $expected) {

  // Start with two plugins.
  $definitions = [];
  $definitions['plugin1'] = [
    'id' => 'plugin1',
  ];
  $definitions['plugin2'] = [
    'id' => 'plugin2',
  ];
  $type = 'the_type';
  $consumer = 'the_consumer';
  $extra = [
    'foo' => 'bar',
  ];
  $context_handler = $this
    ->prophesize(ContextHandlerInterface::class);

  // Remove the second plugin when context1 is provided.
  $context_handler
    ->filterPluginDefinitionsByContexts([
    'context1' => 'fake context',
  ], $definitions)
    ->willReturn([
    'plugin1' => $definitions['plugin1'],
  ]);

  // Remove the first plugin when no contexts are provided.
  $context_handler
    ->filterPluginDefinitionsByContexts([], $definitions)
    ->willReturn([
    'plugin2' => $definitions['plugin2'],
  ]);

  // After context filtering, the alter hook will be invoked.
  $module_handler = $this
    ->prophesize(ModuleHandlerInterface::class);
  $hooks = [
    "plugin_filter_{$type}",
    "plugin_filter_{$type}__{$consumer}",
  ];
  $module_handler
    ->alter($hooks, $expected, $extra, $consumer)
    ->shouldBeCalled();
  $theme_manager = $this
    ->prophesize(ThemeManagerInterface::class);
  $theme_manager
    ->alter($hooks, $expected, $extra, $consumer)
    ->shouldBeCalled();
  $plugin_manager = new TestFilteredPluginManager($definitions, $module_handler
    ->reveal(), $theme_manager
    ->reveal(), $context_handler
    ->reveal());
  $result = $plugin_manager
    ->getFilteredDefinitions($consumer, $contexts, $extra);
  $this
    ->assertSame($expected, $result);
}