Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Component / Plugin / Discovery / DiscoveryTraitTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\Plugin\Discovery;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * @group Plugin
10  * @coversDefaultClass \Drupal\Component\Plugin\Discovery\DiscoveryTrait
11  */
12 class DiscoveryTraitTest extends TestCase {
13
14   /**
15    * Data provider for testDoGetDefinition().
16    *
17    * @return array
18    *   - Expected plugin definition.
19    *   - Plugin definition array, to pass to doGetDefinition().
20    *   - Plugin ID to get, passed to doGetDefinition().
21    */
22   public function providerDoGetDefinition() {
23     return [
24       ['definition', ['plugin_name' => 'definition'], 'plugin_name'],
25       [NULL, ['plugin_name' => 'definition'], 'bad_plugin_name'],
26     ];
27   }
28
29   /**
30    * @covers ::doGetDefinition
31    * @dataProvider providerDoGetDefinition
32    */
33   public function testDoGetDefinition($expected, $definitions, $plugin_id) {
34     // Mock the trait.
35     $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
36     // Un-protect the method using reflection.
37     $method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
38     $method_ref->setAccessible(TRUE);
39     // Call doGetDefinition, with $exception_on_invalid always FALSE.
40     $this->assertSame(
41       $expected,
42       $method_ref->invoke($trait, $definitions, $plugin_id, FALSE)
43     );
44   }
45
46   /**
47    * Data provider for testDoGetDefinitionException()
48    *
49    * @return array
50    *   - Expected plugin definition.
51    *   - Plugin definition array, to pass to doGetDefinition().
52    *   - Plugin ID to get, passed to doGetDefinition().
53    */
54   public function providerDoGetDefinitionException() {
55     return [
56       [FALSE, ['plugin_name' => 'definition'], 'bad_plugin_name'],
57     ];
58   }
59
60   /**
61    * @covers ::doGetDefinition
62    * @dataProvider providerDoGetDefinitionException
63    * @uses \Drupal\Component\Plugin\Exception\PluginNotFoundException
64    */
65   public function testDoGetDefinitionException($expected, $definitions, $plugin_id) {
66     // Mock the trait.
67     $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
68     // Un-protect the method using reflection.
69     $method_ref = new \ReflectionMethod($trait, 'doGetDefinition');
70     $method_ref->setAccessible(TRUE);
71     // Call doGetDefinition, with $exception_on_invalid always TRUE.
72     $this->setExpectedException(PluginNotFoundException::class);
73     $method_ref->invoke($trait, $definitions, $plugin_id, TRUE);
74   }
75
76   /**
77    * @covers ::getDefinition
78    * @dataProvider providerDoGetDefinition
79    */
80   public function testGetDefinition($expected, $definitions, $plugin_id) {
81     // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
82     // its data provider. We just have to tell abstract method getDefinitions()
83     // to use the $definitions array.
84     $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
85     $trait->expects($this->once())
86       ->method('getDefinitions')
87       ->willReturn($definitions);
88     // Call getDefinition(), with $exception_on_invalid always FALSE.
89     $this->assertSame(
90       $expected,
91       $trait->getDefinition($plugin_id, FALSE)
92     );
93   }
94
95   /**
96    * @covers ::getDefinition
97    * @dataProvider providerDoGetDefinitionException
98    * @uses \Drupal\Component\Plugin\Exception\PluginNotFoundException
99    */
100   public function testGetDefinitionException($expected, $definitions, $plugin_id) {
101     // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
102     // its data provider. We just have to tell abstract method getDefinitions()
103     // to use the $definitions array.
104     $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
105     $trait->expects($this->once())
106       ->method('getDefinitions')
107       ->willReturn($definitions);
108     // Call getDefinition(), with $exception_on_invalid always TRUE.
109     $this->setExpectedException(PluginNotFoundException::class);
110     $trait->getDefinition($plugin_id, TRUE);
111   }
112
113   /**
114    * Data provider for testHasDefinition().
115    *
116    * @return array
117    *   - Expected TRUE or FALSE.
118    *   - Plugin ID to look for.
119    */
120   public function providerHasDefinition() {
121     return [
122       [TRUE, 'valid'],
123       [FALSE, 'not_valid'],
124     ];
125   }
126
127   /**
128    * @covers ::hasDefinition
129    * @dataProvider providerHasDefinition
130    */
131   public function testHasDefinition($expected, $plugin_id) {
132     $trait = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryTrait')
133       ->setMethods(['getDefinition'])
134       ->getMockForTrait();
135     // Set up our mocked getDefinition() to return TRUE for 'valid' and FALSE
136     // for 'not_valid'.
137     $trait->expects($this->once())
138       ->method('getDefinition')
139       ->will($this->returnValueMap([
140         ['valid', FALSE, TRUE],
141         ['not_valid', FALSE, FALSE],
142       ]));
143     // Call hasDefinition().
144     $this->assertSame(
145       $expected,
146       $trait->hasDefinition($plugin_id)
147     );
148   }
149
150 }