DefaultLazyPluginCollectionTest.php 8.8 KB

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