X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Ftests%2FDrupal%2FTests%2FCore%2FPlugin%2FFilteredPluginManagerTraitTest.php;fp=web%2Fcore%2Ftests%2FDrupal%2FTests%2FCore%2FPlugin%2FFilteredPluginManagerTraitTest.php;h=9642949de2501c11b797023e350e30f6774a47e4;hp=0000000000000000000000000000000000000000;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php b/web/core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php new file mode 100644 index 000000000..9642949de --- /dev/null +++ b/web/core/tests/Drupal/Tests/Core/Plugin/FilteredPluginManagerTraitTest.php @@ -0,0 +1,121 @@ + '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); + } + + /** + * Provides test data for ::testGetFilteredDefinitions(). + */ + public function providerTestGetFilteredDefinitions() { + $data = []; + $data['populated context'] = [ + ['context1' => 'fake context'], + ['plugin1' => ['id' => 'plugin1']], + ]; + $data['empty context'] = [ + [], + ['plugin2' => ['id' => 'plugin2']], + ]; + $data['null context'] = [ + NULL, + [ + 'plugin1' => ['id' => 'plugin1'], + 'plugin2' => ['id' => 'plugin2'], + ], + ]; + return $data; + } + +} + +/** + * Class that allows testing the trait. + */ +class TestFilteredPluginManager extends PluginManagerBase implements FilteredPluginManagerInterface { + + use FilteredPluginManagerTrait; + + protected $definitions = []; + + protected $moduleHandler; + + protected $themeManager; + + protected $contextHandler; + + public function __construct(array $definitions, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, ContextHandlerInterface $context_handler) { + $this->definitions = $definitions; + $this->moduleHandler = $module_handler; + $this->themeManager = $theme_manager; + $this->contextHandler = $context_handler; + } + + protected function contextHandler() { + return $this->contextHandler; + } + + protected function moduleHandler() { + return $this->moduleHandler; + } + + protected function themeManager() { + return $this->themeManager; + } + + protected function getType() { + return 'the_type'; + } + + public function getDefinitions() { + return $this->definitions; + } + +}