Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Plugin / PluginManagerBaseTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Plugin;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Component\Plugin\PluginManagerBase
10  * @group Plugin
11  */
12 class PluginManagerBaseTest extends TestCase {
13
14   /**
15    * A callback method for mocking FactoryInterface objects.
16    */
17   public function createInstanceCallback() {
18     $args = func_get_args();
19     $plugin_id = $args[0];
20     $configuration = $args[1];
21     if ('invalid' == $plugin_id) {
22       throw new PluginNotFoundException($plugin_id);
23     }
24     return [
25       'plugin_id' => $plugin_id,
26       'configuration' => $configuration,
27     ];
28   }
29
30   /**
31    * Generates a mocked FactoryInterface object with known properties.
32    */
33   public function getMockFactoryInterface($expects_count) {
34     $mock_factory = $this->getMockBuilder('Drupal\Component\Plugin\Factory\FactoryInterface')
35       ->setMethods(['createInstance'])
36       ->getMockForAbstractClass();
37     $mock_factory->expects($this->exactly($expects_count))
38       ->method('createInstance')
39       ->willReturnCallback([$this, 'createInstanceCallback']);
40     return $mock_factory;
41   }
42
43   /**
44    * Tests createInstance() with no fallback methods.
45    *
46    * @covers ::createInstance
47    */
48   public function testCreateInstance() {
49     $manager = $this->getMockBuilder('Drupal\Component\Plugin\PluginManagerBase')
50       ->getMockForAbstractClass();
51     // PluginManagerBase::createInstance() looks for a factory object and then
52     // calls createInstance() on it. So we have to mock a factory object.
53     $factory_ref = new \ReflectionProperty($manager, 'factory');
54     $factory_ref->setAccessible(TRUE);
55     $factory_ref->setValue($manager, $this->getMockFactoryInterface(1));
56
57     // Finally the test.
58     $configuration_array = ['config' => 'something'];
59     $result = $manager->createInstance('valid', $configuration_array);
60     $this->assertEquals('valid', $result['plugin_id']);
61     $this->assertEquals($configuration_array, $result['configuration']);
62   }
63
64   /**
65    * Tests createInstance() with a fallback method.
66    *
67    * @covers ::createInstance
68    */
69   public function testCreateInstanceFallback() {
70     // We use our special stub class which extends PluginManagerBase and also
71     // implements FallbackPluginManagerInterface.
72     $manager = new StubFallbackPluginManager();
73     // Put our stubbed factory on the base object.
74     $factory_ref = new \ReflectionProperty($manager, 'factory');
75     $factory_ref->setAccessible(TRUE);
76
77     // Set up the configuration array.
78     $configuration_array = ['config' => 'something'];
79
80     // Test with fallback interface and valid plugin_id.
81     $factory_ref->setValue($manager, $this->getMockFactoryInterface(1));
82     $no_fallback_result = $manager->createInstance('valid', $configuration_array);
83     $this->assertEquals('valid', $no_fallback_result['plugin_id']);
84     $this->assertEquals($configuration_array, $no_fallback_result['configuration']);
85
86     // Test with fallback interface and invalid plugin_id.
87     $factory_ref->setValue($manager, $this->getMockFactoryInterface(2));
88     $fallback_result = $manager->createInstance('invalid', $configuration_array);
89     $this->assertEquals('invalid_fallback', $fallback_result['plugin_id']);
90     $this->assertEquals($configuration_array, $fallback_result['configuration']);
91   }
92
93 }