More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / DefaultLazyPluginCollectionTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin;
4
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Plugin\DefaultLazyPluginCollection
10  * @group Plugin
11  */
12 class DefaultLazyPluginCollectionTest extends LazyPluginCollectionTestBase {
13
14   /**
15    * Stores all setup plugin instances.
16    *
17    * @var \Drupal\Component\Plugin\ConfigurablePluginInterface[]
18    */
19   protected $pluginInstances;
20
21   /**
22    * @covers ::has
23    */
24   public function testHas() {
25     $this->setupPluginCollection();
26     $definitions = $this->getPluginDefinitions();
27
28     $this->assertFalse($this->defaultPluginCollection->has($this->randomMachineName()), 'Nonexistent plugin found.');
29
30     foreach (array_keys($definitions) as $plugin_id) {
31       $this->assertTrue($this->defaultPluginCollection->has($plugin_id));
32     }
33   }
34
35   /**
36    * @covers ::get
37    */
38   public function testGet() {
39     $this->setupPluginCollection($this->once());
40     $apple = $this->pluginInstances['apple'];
41
42     $this->assertSame($apple, $this->defaultPluginCollection->get('apple'));
43   }
44
45   /**
46    * @covers ::get
47    */
48   public function testGetNotExistingPlugin() {
49     $this->setupPluginCollection();
50     $this->setExpectedException(PluginNotFoundException::class, "Plugin ID 'pear' was not found.");
51     $this->defaultPluginCollection->get('pear');
52   }
53
54   /**
55    * Provides test data for testSortHelper.
56    *
57    * @return array
58    *   The test data.
59    */
60   public function providerTestSortHelper() {
61     return [
62       ['apple', 'apple', 0],
63       ['apple', 'cherry', -1],
64       ['cherry', 'apple', 1],
65       ['cherry', 'banana', 1],
66     ];
67   }
68
69   /**
70    * @param string $plugin_id_1
71    *   The first plugin ID.
72    * @param string $plugin_id_2
73    *   The second plugin ID.
74    * @param int $expected
75    *   The expected result.
76    *
77    * @covers ::sortHelper
78    * @dataProvider providerTestSortHelper
79    */
80   public function testSortHelper($plugin_id_1, $plugin_id_2, $expected) {
81     $this->setupPluginCollection($this->any());
82     if ($expected != 0) {
83       $expected = $expected > 0 ? 1 : -1;
84     }
85     $this->assertEquals($expected, $this->defaultPluginCollection->sortHelper($plugin_id_1, $plugin_id_2));
86   }
87
88   /**
89    * @covers ::getConfiguration
90    */
91   public function testGetConfiguration() {
92     $this->setupPluginCollection($this->exactly(3));
93     // The expected order matches $this->config.
94     $expected = ['banana', 'cherry', 'apple'];
95
96     $config = $this->defaultPluginCollection->getConfiguration();
97     $this->assertSame($expected, array_keys($config), 'The order of the configuration is unchanged.');
98
99     $ids = $this->defaultPluginCollection->getInstanceIds();
100     $this->assertSame($expected, array_keys($ids), 'The order of the instances is unchanged.');
101
102     $this->defaultPluginCollection->sort();
103     $config = $this->defaultPluginCollection->getConfiguration();
104     $this->assertSame($expected, array_keys($config), 'After sorting, the order of the configuration is unchanged.');
105
106     $ids = $this->defaultPluginCollection->getInstanceIds();
107     sort($expected);
108     $this->assertSame($expected, array_keys($ids), 'After sorting, the order of the instances is also sorted.');
109   }
110
111   /**
112    * @covers ::addInstanceId
113    */
114   public function testAddInstanceId() {
115     $this->setupPluginCollection($this->exactly(4));
116     $expected = [
117       'banana' => 'banana',
118       'cherry' => 'cherry',
119       'apple' => 'apple',
120     ];
121     $this->defaultPluginCollection->addInstanceId('apple');
122     $result = $this->defaultPluginCollection->getInstanceIds();
123     $this->assertSame($expected, $result);
124     $this->assertSame($expected, array_intersect_key($result, $this->defaultPluginCollection->getConfiguration()));
125
126     $expected = [
127       'cherry' => 'cherry',
128       'apple' => 'apple',
129       'banana' => 'banana',
130     ];
131     $this->defaultPluginCollection->removeInstanceId('banana');
132     $this->defaultPluginCollection->addInstanceId('banana', $this->config['banana']);
133
134     $result = $this->defaultPluginCollection->getInstanceIds();
135     $this->assertSame($expected, $result);
136     $this->assertSame($expected, array_intersect_key($result, $this->defaultPluginCollection->getConfiguration()));
137   }
138
139   /**
140    * @covers ::removeInstanceId
141    */
142   public function testRemoveInstanceId() {
143     $this->setupPluginCollection($this->exactly(2));
144     $this->defaultPluginCollection->removeInstanceId('cherry');
145     $config = $this->defaultPluginCollection->getConfiguration();
146     $this->assertArrayNotHasKey('cherry', $config, 'After removing an instance, the configuration is updated.');
147   }
148
149   /**
150    * @covers ::setInstanceConfiguration
151    */
152   public function testSetInstanceConfiguration() {
153     $this->setupPluginCollection($this->exactly(3));
154     $expected = [
155       'id' => 'cherry',
156       'key' => 'value',
157       'custom' => 'bananas',
158     ];
159     $this->defaultPluginCollection->setInstanceConfiguration('cherry', $expected);
160     $config = $this->defaultPluginCollection->getConfiguration();
161     $this->assertSame($expected, $config['cherry']);
162   }
163
164   /**
165    * @covers ::count
166    */
167   public function testCount() {
168     $this->setupPluginCollection();
169     $this->assertSame(3, $this->defaultPluginCollection->count());
170   }
171
172   /**
173    * @covers ::clear
174    */
175   public function testClear() {
176     $this->setupPluginCollection($this->exactly(6));
177     $this->defaultPluginCollection->getConfiguration();
178     $this->defaultPluginCollection->getConfiguration();
179     $this->defaultPluginCollection->clear();
180     $this->defaultPluginCollection->getConfiguration();
181   }
182
183   /**
184    * @covers ::set
185    */
186   public function testSet() {
187     $this->setupPluginCollection($this->exactly(4));
188     $instance = $this->pluginManager->createInstance('cherry', $this->config['cherry']);
189     $this->defaultPluginCollection->set('cherry2', $instance);
190     $this->defaultPluginCollection->setInstanceConfiguration('cherry2', $this->config['cherry']);
191
192     $expected = [
193       'banana',
194       'cherry',
195       'apple',
196       'cherry2',
197     ];
198     $config = $this->defaultPluginCollection->getConfiguration();
199     $this->assertSame($expected, array_keys($config));
200   }
201
202   /**
203    * {@inheritdoc}
204    */
205   protected function getPluginMock($plugin_id, array $definition) {
206     return new TestConfigurablePlugin($this->config[$plugin_id], $plugin_id, $definition);
207   }
208
209   /**
210    * @covers ::getConfiguration
211    */
212   public function testConfigurableGetConfiguration() {
213     $this->setupPluginCollection($this->exactly(3));
214     $config = $this->defaultPluginCollection->getConfiguration();
215     $this->assertSame($this->config, $config);
216   }
217
218   /**
219    * @covers ::setConfiguration
220    */
221   public function testConfigurableSetConfiguration() {
222     $this->setupPluginCollection($this->exactly(2));
223
224     $this->defaultPluginCollection->setConfiguration(['apple' => ['value' => 'pineapple', 'id' => 'apple']]);
225     $config = $this->defaultPluginCollection->getConfiguration();
226     $this->assertSame(['apple' => ['value' => 'pineapple', 'id' => 'apple']], $config);
227     $plugin = $this->pluginInstances['apple'];
228     $this->assertSame(['value' => 'pineapple', 'id' => 'apple'], $plugin->getConfiguration());
229
230     $this->defaultPluginCollection->setConfiguration(['cherry' => ['value' => 'kiwi', 'id' => 'cherry']]);
231     $expected['cherry'] = ['value' => 'kiwi', 'id' => 'cherry'];
232     $config = $this->defaultPluginCollection->getConfiguration();
233     $this->assertSame(['cherry' => ['value' => 'kiwi', 'id' => 'cherry']], $config);
234
235   }
236
237 }