diff --git a/core/lib/Drupal/Component/Plugin/PluginBag.php b/core/lib/Drupal/Component/Plugin/PluginBag.php index e799d2d..4aef593 100644 --- a/core/lib/Drupal/Component/Plugin/PluginBag.php +++ b/core/lib/Drupal/Component/Plugin/PluginBag.php @@ -161,7 +161,11 @@ public function key() { */ public function valid() { $key = key($this->instanceIDs); - return ($key !== NULL) && ($key !== FALSE) && $this->get($key); + // Check the key is valid but also that this key yields a plugin from get(). + // There can be situations where configuration contains data for a plugin + // that cannot be instantiated. In this case, this enables us to skip that + // plugin during iteration. + return $key !== NULL && $key !== FALSE && $this->get($key); } /** diff --git a/core/tests/Drupal/Tests/Component/Plugin/DefaultPluginBagTest.php b/core/tests/Drupal/Tests/Component/Plugin/DefaultPluginBagTest.php index f12c146..ae92285 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/DefaultPluginBagTest.php +++ b/core/tests/Drupal/Tests/Component/Plugin/DefaultPluginBagTest.php @@ -144,7 +144,7 @@ public function testRemoveInstanceId() { * @see \Drupal\Component\Plugin\DefaultPluginBag::setConfiguration() */ public function testSetConfiguration() { - $this->setupPluginBag($this->exactly(4)); + $this->setupPluginBag($this->exactly(3)); $expected = array( 'id' => 'cherry', 'key' => 'value', diff --git a/core/tests/Drupal/Tests/Component/Plugin/PluginBagTestBase.php b/core/tests/Drupal/Tests/Component/Plugin/PluginBagTestBase.php index c9e543b..461321d 100644 --- a/core/tests/Drupal/Tests/Component/Plugin/PluginBagTestBase.php +++ b/core/tests/Drupal/Tests/Component/Plugin/PluginBagTestBase.php @@ -75,12 +75,27 @@ protected function setupPluginBag(\PHPUnit_Framework_MockObject_Matcher_InvokedR $create_count = $create_count ?: $this->never(); $this->pluginManager->expects($create_count) ->method('createInstance') - ->will($this->returnValueMap($map)); + ->will($this->returnCallback(array($this, 'returnPluginMap'))); $this->defaultPluginBag = new DefaultPluginBag($this->pluginManager, $this->config); } /** + * Return callback for createInstance. + * + * @param string $plugin_id + * The plugin ID to return the mock plugin for. + * + * @return \Drupal\Component\Plugin\PluginInspectionInterface|\PHPUnit_Framework_MockObject_MockObject + * The mock plugin object. + */ + public function returnPluginMap($plugin_id) { + if (isset($this->pluginInstances[$plugin_id])) { + return $this->pluginInstances[$plugin_id]; + } + } + + /** * Returns a mocked plugin object. * * @param string $plugin_id