diff -u b/core/lib/Drupal/Component/Plugin/PluginBag.php b/core/lib/Drupal/Component/Plugin/PluginBag.php --- b/core/lib/Drupal/Component/Plugin/PluginBag.php +++ b/core/lib/Drupal/Component/Plugin/PluginBag.php @@ -8,7 +8,7 @@ namespace Drupal\Component\Plugin; /** - * Defines an object which stores multiple plugin instances to allow lazy initialization. + * Defines an object which stores multiple plugin instances to lazy load them. */ abstract class PluginBag implements \ArrayAccess, \Iterator, \Countable { only in patch2: unchanged: --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginBagTest.php @@ -0,0 +1,63 @@ + 'Plugin Bag', + 'description' => 'Tests the generic plugin bag.', + 'group' => 'Plugin API', + ); + } + + /** + * Tests the plugin bag. + */ + protected function testPluginBag() { + // Setup the plugin bag as well as the available plugin definitions. + $plugin_bag = new TestPluginBag($this->mockBlockManager); + $definitions = $this->mockBlockManager->getDefinitions(); + $first_instance_id = key($definitions); + + foreach ($definitions as $instance_id => $definition) { + $this->assertTrue(isset($plugin_bag[$instance_id]), format_string('Plugin instance @instance_id exits on the bag', array('@instance_id' => $instance_id))); + } + + // A non existing instance_id shouldn't exists on the bag. + $random_name = $this->randomName(); + $this->assertFalse(isset($plugin_bag[$random_name]), 'A random instance_id should not exist on the plugin bag.'); + + // Set a new plugin instance to the bag, to test offsetSet. + $plugin_bag[$random_name] = $this->mockBlockManager->createInstance($first_instance_id, array()); + $this->assertTrue(isset($plugin_bag[$random_name]), 'A random instance_id should exist after manual setting on the plugin bag.'); + + // Remove the previous added element and check whether it still exists. + unset($plugin_bag[$random_name]); + $this->assertFalse(isset($plugin_bag[$random_name]), 'A random instance_id should not exist on the plugin bag after removing.'); + + // Check that iterating over the plugins work. + $expected_instance_ids = array_keys($definitions); + $counter = 0; + foreach ($plugin_bag as $instance_id => $plugin) { + $this->assertEqual($expected_instance_ids[$counter], $instance_id, format_string('The iteration works as expected for plugin instance @instance_id', array('@instance_id' => $instance_id))); + $counter++; + } + + $this->assertEqual(count($plugin_bag), count($expected_instance_ids), 'The amount of items in plugin bag is as expected.'); + } +} only in patch2: unchanged: --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/TestPluginBag.php @@ -0,0 +1,49 @@ +manager = $manager; + + $this->instanceIDs = drupal_map_assoc(array_keys($this->manager->getDefinitions())); + } + + /** + * Implements \Drupal\Component\Plugin\PluginBag::initializePlugin(). + */ + protected function initializePlugin($instance_id) { + // If the plugin was initialized before, just return. + if (isset($this->pluginInstances[$instance_id])) { + return; + } + + $this->pluginInstances[$instance_id] = $this->manager->createInstance($instance_id, array()); + } + +}