3 namespace Drupal\KernelTests\Core\Plugin;
5 use Drupal\Component\Plugin\Exception\ExceptionInterface;
8 * Tests that plugins are correctly instantiated.
12 class FactoryTest extends PluginTestBase {
15 * Test that DefaultFactory can create a plugin instance.
17 public function testDefaultFactory() {
18 // Ensure a non-derivative plugin can be instantiated.
19 $plugin = $this->testPluginManager->createInstance('user_login', ['title' => 'Please enter your login name and password']);
20 $this->assertIdentical(get_class($plugin), 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserLoginBlock', 'Correct plugin class instantiated with default factory.');
21 $this->assertIdentical($plugin->getTitle(), 'Please enter your login name and password', 'Plugin instance correctly configured.');
23 // Ensure that attempting to instantiate non-existing plugins throws a
26 $this->testPluginManager->createInstance('non_existing');
27 $this->fail('Drupal\Component\Plugin\Exception\ExceptionInterface expected');
29 catch (ExceptionInterface $e) {
30 $this->pass('Drupal\Component\Plugin\Exception\ExceptionInterface expected and caught.');
32 catch (\Exception $e) {
33 $this->fail('Drupal\Component\Plugin\Exception\ExceptionInterface expected, but ' . get_class($e) . ' was thrown.');
38 * Test that the Reflection factory can create a plugin instance.
40 * The mock plugin classes use different values for their constructors
41 * allowing us to test the reflection capabilities as well.
43 * We use derivative classes here because the block test type has the
44 * reflection factory and it provides some additional variety in plugin
47 public function testReflectionFactory() {
48 // Ensure a non-derivative plugin can be instantiated.
49 $plugin = $this->mockBlockManager->createInstance('user_login', ['title' => 'Please enter your login name and password']);
50 $this->assertIdentical(get_class($plugin), 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockUserLoginBlock', 'Correct plugin class instantiated.');
51 $this->assertIdentical($plugin->getTitle(), 'Please enter your login name and password', 'Plugin instance correctly configured.');
53 // Ensure a derivative plugin can be instantiated.
54 $plugin = $this->mockBlockManager->createInstance('menu:main_menu', ['depth' => 2]);
55 $this->assertIdentical($plugin->getContent(), '<ul><li>1<ul><li>1.1</li></ul></li></ul>', 'Derived plugin instance correctly instantiated and configured.');
57 // Ensure that attempting to instantiate non-existing plugins throws a
58 // PluginException. Test this for a non-existing base plugin, a non-existing
59 // derivative plugin, and a base plugin that may not be used without
61 foreach (['non_existing', 'menu:non_existing', 'menu'] as $invalid_id) {
63 $this->mockBlockManager->createInstance($invalid_id);
64 $this->fail('Drupal\Component\Plugin\Exception\ExceptionInterface expected');
66 catch (ExceptionInterface $e) {
67 $this->pass('Drupal\Component\Plugin\Exception\ExceptionInterface expected and caught.');
69 catch (\Exception $e) {
70 $this->fail('An unexpected Exception of type "' . get_class($e) . '" was thrown with message ' . $e->getMessage());