50da5cfa2c5907f286bb70e26c53de0ecf0aa7d1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Plugin / FactoryTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Plugin;
4
5 use Drupal\Component\Plugin\Exception\ExceptionInterface;
6
7 /**
8  * Tests that plugins are correctly instantiated.
9  *
10  * @group Plugin
11  */
12 class FactoryTest extends PluginTestBase {
13
14   /**
15    * Test that DefaultFactory can create a plugin instance.
16    */
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.');
22
23     // Ensure that attempting to instantiate non-existing plugins throws a
24     // PluginException.
25     try {
26       $this->testPluginManager->createInstance('non_existing');
27       $this->fail('Drupal\Component\Plugin\Exception\ExceptionInterface expected');
28     }
29     catch (ExceptionInterface $e) {
30       $this->pass('Drupal\Component\Plugin\Exception\ExceptionInterface expected and caught.');
31     }
32     catch (\Exception $e) {
33       $this->fail('Drupal\Component\Plugin\Exception\ExceptionInterface expected, but ' . get_class($e) . ' was thrown.');
34     }
35   }
36
37   /**
38    * Test that the Reflection factory can create a plugin instance.
39    *
40    * The mock plugin classes use different values for their constructors
41    * allowing us to test the reflection capabilities as well.
42    *
43    * We use derivative classes here because the block test type has the
44    * reflection factory and it provides some additional variety in plugin
45    * object creation.
46    */
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.');
52
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.');
56
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
60     // deriving.
61     foreach (['non_existing', 'menu:non_existing', 'menu'] as $invalid_id) {
62       try {
63         $this->mockBlockManager->createInstance($invalid_id);
64         $this->fail('Drupal\Component\Plugin\Exception\ExceptionInterface expected');
65       }
66       catch (ExceptionInterface $e) {
67         $this->pass('Drupal\Component\Plugin\Exception\ExceptionInterface expected and caught.');
68       }
69       catch (\Exception $e) {
70         $this->fail('An unexpected Exception of type "' . get_class($e) . '" was thrown with message ' . $e->getMessage());
71       }
72     }
73   }
74
75 }