summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-05-27 10:11:48 (GMT)
committerAlex Pott2016-05-27 10:11:48 (GMT)
commit7c0adf9e2941d20d69d11aa5f56d65e729ea8734 (patch)
treebf5386adb33e0ec2c24310763928bbf3b6479af0
parentd27d90ce8a47ffe6c48374bf3bd2093db7954eeb (diff)
Issue #2726837 by tim.plunkett, dipakmdhrm, dawehner: \Drupal\Core\Plugin\DefaultSingleLazyPluginCollection::addInstanceId should set $this->instanceId
-rw-r--r--core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php3
-rw-r--r--core/tests/Drupal/Tests/Core/Plugin/DefaultSingleLazyPluginCollectionTest.php54
2 files changed, 54 insertions, 3 deletions
diff --git a/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php b/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php
index 403e909..bbcb0f2 100644
--- a/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php
+++ b/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php
@@ -82,11 +82,11 @@ class DefaultSingleLazyPluginCollection extends LazyPluginCollection {
* {@inheritdoc}
*/
public function setConfiguration($configuration) {
+ $this->configuration = $configuration;
$plugin = $this->get($this->instanceId);
if ($plugin instanceof ConfigurablePluginInterface) {
$plugin->setConfiguration($configuration);
}
- $this->configuration = $configuration;
return $this;
}
@@ -94,6 +94,7 @@ class DefaultSingleLazyPluginCollection extends LazyPluginCollection {
* {@inheritdoc}
*/
public function addInstanceId($id, $configuration = NULL) {
+ $this->instanceId = $id;
parent::addInstanceId($id, $configuration);
if ($configuration !== NULL) {
$this->setConfiguration($configuration);
diff --git a/core/tests/Drupal/Tests/Core/Plugin/DefaultSingleLazyPluginCollectionTest.php b/core/tests/Drupal/Tests/Core/Plugin/DefaultSingleLazyPluginCollectionTest.php
index a0303e6..22ecd3c 100644
--- a/core/tests/Drupal/Tests/Core/Plugin/DefaultSingleLazyPluginCollectionTest.php
+++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultSingleLazyPluginCollectionTest.php
@@ -2,6 +2,8 @@
namespace Drupal\Tests\Core\Plugin;
+use Drupal\Component\Plugin\ConfigurablePluginInterface;
+use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\DefaultSingleLazyPluginCollection;
/**
@@ -15,11 +17,15 @@ class DefaultSingleLazyPluginCollectionTest extends LazyPluginCollectionTestBase
*/
protected function setupPluginCollection(\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder $create_count = NULL) {
$definitions = $this->getPluginDefinitions();
- $this->pluginInstances['apple'] = $this->getPluginMock('apple', $definitions['apple']);
+ $this->pluginInstances['apple'] = new ConfigurablePlugin(['id' => 'apple', 'key' => 'value'], 'apple', $definitions['apple']);
+ $this->pluginInstances['banana'] = new ConfigurablePlugin(['id' => 'banana', 'key' => 'other_value'], 'banana', $definitions['banana']);
+
$create_count = $create_count ?: $this->never();
$this->pluginManager->expects($create_count)
->method('createInstance')
- ->will($this->returnValue($this->pluginInstances['apple']));
+ ->willReturnCallback(function ($id) {
+ return $this->pluginInstances[$id];
+ });
$this->defaultPluginCollection = new DefaultSingleLazyPluginCollection($this->pluginManager, 'apple', array('id' => 'apple', 'key' => 'value'));
}
@@ -34,4 +40,48 @@ class DefaultSingleLazyPluginCollectionTest extends LazyPluginCollectionTestBase
$this->assertSame($apple, $this->defaultPluginCollection->get('apple'));
}
+ /**
+ * @covers ::addInstanceId
+ * @covers ::getConfiguration
+ * @covers ::setConfiguration
+ */
+ public function testAddInstanceId() {
+ $this->setupPluginCollection($this->any());
+
+ $this->assertEquals(['id' => 'apple', 'key' => 'value'], $this->defaultPluginCollection->get('apple')->getConfiguration());
+ $this->assertEquals(['id' => 'apple', 'key' => 'value'], $this->defaultPluginCollection->getConfiguration());
+
+ $this->defaultPluginCollection->addInstanceId('banana', ['id' => 'banana', 'key' => 'other_value']);
+
+ $this->assertEquals(['id' => 'apple', 'key' => 'value'], $this->defaultPluginCollection->get('apple')->getConfiguration());
+ $this->assertEquals(['id' => 'banana', 'key' => 'other_value'], $this->defaultPluginCollection->getConfiguration());
+ $this->assertEquals(['id' => 'banana', 'key' => 'other_value'], $this->defaultPluginCollection->get('banana')->getConfiguration());
+ }
+
+}
+
+class ConfigurablePlugin extends PluginBase implements ConfigurablePluginInterface {
+
+ public function __construct(array $configuration, $plugin_id, $plugin_definition) {
+ parent::__construct($configuration, $plugin_id, $plugin_definition);
+
+ $this->configuration = $configuration + $this->defaultConfiguration();
+ }
+
+ public function defaultConfiguration() {
+ return [];
+ }
+
+ public function getConfiguration() {
+ return $this->configuration;
+ }
+
+ public function setConfiguration(array $configuration) {
+ $this->configuration = $configuration;
+ }
+
+ public function calculateDependencies() {
+ return [];
+ }
+
}