Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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     if (method_exists($this, 'expectException')) {
73       $this->expectException(PluginNotFoundException::class);
74     }
75     else {
76       $this->setExpectedException(PluginNotFoundException::class);
77     }
78     $method_ref->invoke($trait, $definitions, $plugin_id, TRUE);
79   }
80
81   /**
82    * @covers ::getDefinition
83    * @dataProvider providerDoGetDefinition
84    */
85   public function testGetDefinition($expected, $definitions, $plugin_id) {
86     // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
87     // its data provider. We just have to tell abstract method getDefinitions()
88     // to use the $definitions array.
89     $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
90     $trait->expects($this->once())
91       ->method('getDefinitions')
92       ->willReturn($definitions);
93     // Call getDefinition(), with $exception_on_invalid always FALSE.
94     $this->assertSame(
95       $expected,
96       $trait->getDefinition($plugin_id, FALSE)
97     );
98   }
99
100   /**
101    * @covers ::getDefinition
102    * @dataProvider providerDoGetDefinitionException
103    * @uses \Drupal\Component\Plugin\Exception\PluginNotFoundException
104    */
105   public function testGetDefinitionException($expected, $definitions, $plugin_id) {
106     // Since getDefinition is a wrapper around doGetDefinition(), we can re-use
107     // its data provider. We just have to tell abstract method getDefinitions()
108     // to use the $definitions array.
109     $trait = $this->getMockForTrait('Drupal\Component\Plugin\Discovery\DiscoveryTrait');
110     $trait->expects($this->once())
111       ->method('getDefinitions')
112       ->willReturn($definitions);
113     // Call getDefinition(), with $exception_on_invalid always TRUE.
114     if (method_exists($this, 'expectException')) {
115       $this->expectException(PluginNotFoundException::class);
116     }
117     else {
118       $this->setExpectedException(PluginNotFoundException::class);
119     }
120     $trait->getDefinition($plugin_id, TRUE);
121   }
122
123   /**
124    * Data provider for testHasDefinition().
125    *
126    * @return array
127    *   - Expected TRUE or FALSE.
128    *   - Plugin ID to look for.
129    */
130   public function providerHasDefinition() {
131     return [
132       [TRUE, 'valid'],
133       [FALSE, 'not_valid'],
134     ];
135   }
136
137   /**
138    * @covers ::hasDefinition
139    * @dataProvider providerHasDefinition
140    */
141   public function testHasDefinition($expected, $plugin_id) {
142     $trait = $this->getMockBuilder('Drupal\Component\Plugin\Discovery\DiscoveryTrait')
143       ->setMethods(['getDefinition'])
144       ->getMockForTrait();
145     // Set up our mocked getDefinition() to return TRUE for 'valid' and FALSE
146     // for 'not_valid'.
147     $trait->expects($this->once())
148       ->method('getDefinition')
149       ->will($this->returnValueMap([
150         ['valid', FALSE, TRUE],
151         ['not_valid', FALSE, FALSE],
152       ]));
153     // Call hasDefinition().
154     $this->assertSame(
155       $expected,
156       $trait->hasDefinition($plugin_id)
157     );
158   }
159
160 }