Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / DefaultSingleLazyPluginCollectionTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin;
4
5 use Drupal\Component\Plugin\ConfigurablePluginInterface;
6 use Drupal\Component\Plugin\PluginBase;
7 use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
8
9 /**
10  * @coversDefaultClass \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection
11  * @group Plugin
12  */
13 class DefaultSingleLazyPluginCollectionTest extends LazyPluginCollectionTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected function setupPluginCollection(\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $create_count = NULL) {
19     $definitions = $this->getPluginDefinitions();
20     $this->pluginInstances['apple'] = new ConfigurablePlugin(['id' => 'apple', 'key' => 'value'], 'apple', $definitions['apple']);
21     $this->pluginInstances['banana'] = new ConfigurablePlugin(['id' => 'banana', 'key' => 'other_value'], 'banana', $definitions['banana']);
22
23     $create_count = $create_count ?: $this->never();
24     $this->pluginManager->expects($create_count)
25       ->method('createInstance')
26       ->willReturnCallback(function ($id) {
27         return $this->pluginInstances[$id];
28       });
29
30     $this->defaultPluginCollection = new DefaultSingleLazyPluginCollection($this->pluginManager, 'apple', ['id' => 'apple', 'key' => 'value']);
31   }
32
33   /**
34    * Tests the get() method.
35    */
36   public function testGet() {
37     $this->setupPluginCollection($this->once());
38     $apple = $this->pluginInstances['apple'];
39
40     $this->assertSame($apple, $this->defaultPluginCollection->get('apple'));
41   }
42
43   /**
44    * @covers ::addInstanceId
45    * @covers ::getConfiguration
46    * @covers ::setConfiguration
47    */
48   public function testAddInstanceId() {
49     $this->setupPluginCollection($this->any());
50
51     $this->assertEquals(['id' => 'apple', 'key' => 'value'], $this->defaultPluginCollection->get('apple')->getConfiguration());
52     $this->assertEquals(['id' => 'apple', 'key' => 'value'], $this->defaultPluginCollection->getConfiguration());
53
54     $this->defaultPluginCollection->addInstanceId('banana', ['id' => 'banana', 'key' => 'other_value']);
55
56     $this->assertEquals(['id' => 'apple', 'key' => 'value'], $this->defaultPluginCollection->get('apple')->getConfiguration());
57     $this->assertEquals(['id' => 'banana', 'key' => 'other_value'], $this->defaultPluginCollection->getConfiguration());
58     $this->assertEquals(['id' => 'banana', 'key' => 'other_value'], $this->defaultPluginCollection->get('banana')->getConfiguration());
59   }
60
61   /**
62    * @covers ::getInstanceIds
63    */
64   public function testGetInstanceIds() {
65     $this->setupPluginCollection($this->any());
66     $this->assertEquals(['apple' => 'apple'], $this->defaultPluginCollection->getInstanceIds());
67
68     $this->defaultPluginCollection->addInstanceId('banana', ['id' => 'banana', 'key' => 'other_value']);
69     $this->assertEquals(['banana' => 'banana'], $this->defaultPluginCollection->getInstanceIds());
70   }
71
72 }
73
74 class ConfigurablePlugin extends PluginBase implements ConfigurablePluginInterface {
75
76   public function __construct(array $configuration, $plugin_id, $plugin_definition) {
77     parent::__construct($configuration, $plugin_id, $plugin_definition);
78
79     $this->configuration = $configuration + $this->defaultConfiguration();
80   }
81
82   public function defaultConfiguration() {
83     return [];
84   }
85
86   public function getConfiguration() {
87     return $this->configuration;
88   }
89
90   public function setConfiguration(array $configuration) {
91     $this->configuration = $configuration;
92   }
93
94   public function calculateDependencies() {
95     return [];
96   }
97
98 }