3 namespace Drupal\Tests\Core\Plugin;
5 use Drupal\Component\Plugin\Exception\PluginNotFoundException;
6 use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
9 * @coversDefaultClass \Drupal\Core\Plugin\DefaultLazyPluginCollection
12 class DefaultLazyPluginCollectionTest extends LazyPluginCollectionTestBase {
15 * Stores all setup plugin instances.
17 * @var \Drupal\Component\Plugin\ConfigurablePluginInterface[]
19 protected $pluginInstances;
24 public function testHas() {
25 $this->setupPluginCollection();
26 $definitions = $this->getPluginDefinitions();
28 $this->assertFalse($this->defaultPluginCollection->has($this->randomMachineName()), 'Nonexistent plugin found.');
30 foreach (array_keys($definitions) as $plugin_id) {
31 $this->assertTrue($this->defaultPluginCollection->has($plugin_id));
38 public function testGet() {
39 $this->setupPluginCollection($this->once());
40 $apple = $this->pluginInstances['apple'];
42 $this->assertSame($apple, $this->defaultPluginCollection->get('apple'));
48 public function testGetNotExistingPlugin() {
49 $this->setupPluginCollection();
50 $this->setExpectedException(PluginNotFoundException::class, "Plugin ID 'pear' was not found.");
51 $this->defaultPluginCollection->get('pear');
55 * Provides test data for testSortHelper.
60 public function providerTestSortHelper() {
62 ['apple', 'apple', 0],
63 ['apple', 'cherry', -1],
64 ['cherry', 'apple', 1],
65 ['cherry', 'banana', 1],
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.
77 * @covers ::sortHelper
78 * @dataProvider providerTestSortHelper
80 public function testSortHelper($plugin_id_1, $plugin_id_2, $expected) {
81 $this->setupPluginCollection($this->any());
83 $expected = $expected > 0 ? 1 : -1;
85 $this->assertEquals($expected, $this->defaultPluginCollection->sortHelper($plugin_id_1, $plugin_id_2));
89 * @covers ::getConfiguration
91 public function testGetConfiguration() {
92 $this->setupPluginCollection($this->exactly(3));
93 // The expected order matches $this->config.
94 $expected = ['banana', 'cherry', 'apple'];
96 $config = $this->defaultPluginCollection->getConfiguration();
97 $this->assertSame($expected, array_keys($config), 'The order of the configuration is unchanged.');
99 $ids = $this->defaultPluginCollection->getInstanceIds();
100 $this->assertSame($expected, array_keys($ids), 'The order of the instances is unchanged.');
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.');
106 $ids = $this->defaultPluginCollection->getInstanceIds();
108 $this->assertSame($expected, array_keys($ids), 'After sorting, the order of the instances is also sorted.');
112 * @covers ::addInstanceId
114 public function testAddInstanceId() {
115 $this->setupPluginCollection($this->exactly(4));
117 'banana' => 'banana',
118 'cherry' => 'cherry',
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()));
127 'cherry' => 'cherry',
129 'banana' => 'banana',
131 $this->defaultPluginCollection->removeInstanceId('banana');
132 $this->defaultPluginCollection->addInstanceId('banana', $this->config['banana']);
134 $result = $this->defaultPluginCollection->getInstanceIds();
135 $this->assertSame($expected, $result);
136 $this->assertSame($expected, array_intersect_key($result, $this->defaultPluginCollection->getConfiguration()));
140 * @covers ::removeInstanceId
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.');
150 * @covers ::setInstanceConfiguration
152 public function testSetInstanceConfiguration() {
153 $this->setupPluginCollection($this->exactly(3));
157 'custom' => 'bananas',
159 $this->defaultPluginCollection->setInstanceConfiguration('cherry', $expected);
160 $config = $this->defaultPluginCollection->getConfiguration();
161 $this->assertSame($expected, $config['cherry']);
167 public function testCount() {
168 $this->setupPluginCollection();
169 $this->assertSame(3, $this->defaultPluginCollection->count());
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();
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']);
198 $config = $this->defaultPluginCollection->getConfiguration();
199 $this->assertSame($expected, array_keys($config));
205 protected function getPluginMock($plugin_id, array $definition) {
206 return new TestConfigurablePlugin($this->config[$plugin_id], $plugin_id, $definition);
210 * @covers ::getConfiguration
212 public function testConfigurableGetConfiguration() {
213 $this->setupPluginCollection($this->exactly(3));
214 $config = $this->defaultPluginCollection->getConfiguration();
215 $this->assertSame($this->config, $config);
219 * @covers ::setConfiguration
221 public function testConfigurableSetConfiguration() {
222 $this->setupPluginCollection($this->exactly(2));
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());
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);