From a4d4a17c1c98839e721abdf5264f23cb1470fa9e Mon Sep 17 00:00:00 2001 From: webchick Date: Tue, 23 Oct 2012 15:18:36 -0700 Subject: [PATCH] Issue #1816916 by tim.plunkett: Recursively merge in defaults. --- .../Component/Plugin/PluginManagerBase.php | 5 +- .../system/Tests/Plugin/InspectionTest.php | 10 +++- .../system/Tests/Plugin/PluginTestBase.php | 21 ++++++++ .../Plugin/DefaultsTestPluginManager.php | 51 +++++++++++++++++++ .../plugin_test/mock_block/MockTestBlock.php | 20 ++++++++ 5 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/DefaultsTestPluginManager.php create mode 100644 core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockTestBlock.php diff --git a/core/lib/Drupal/Component/Plugin/PluginManagerBase.php b/core/lib/Drupal/Component/Plugin/PluginManagerBase.php index 73f1b5542da..e4131fcd805 100644 --- a/core/lib/Drupal/Component/Plugin/PluginManagerBase.php +++ b/core/lib/Drupal/Component/Plugin/PluginManagerBase.php @@ -7,6 +7,8 @@ namespace Drupal\Component\Plugin; +use Drupal\Component\Utility\NestedArray; + /** * Base class for plugin managers. */ @@ -87,6 +89,7 @@ abstract class PluginManagerBase implements PluginManagerInterface { * method. */ protected function processDefinition(&$definition, $plugin_id) { - $definition += $this->defaults; + $definition = NestedArray::mergeDeep($this->defaults, $definition); } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/InspectionTest.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/InspectionTest.php index dcdaa34f30b..5daf1e92e0e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/InspectionTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/InspectionTest.php @@ -27,14 +27,20 @@ class InspectionTest extends PluginTestBase { foreach (array('user_login') as $id) { $plugin = $this->testPluginManager->createInstance($id); $this->assertIdentical($plugin->getPluginId(), $id); - $this->assertIdentical($plugin->getDefinition(), $this->testPluginExpectedDefinitions[$id]); + $this->assertIdentical($this->testPluginManager->getDefinition($id), $this->testPluginExpectedDefinitions[$id]); } // Skip the 'menu' derived blocks, because MockMenuBlock does not implement // PluginInspectionInterface. The others do by extending PluginBase. foreach (array('user_login', 'layout') as $id) { $plugin = $this->mockBlockManager->createInstance($id); $this->assertIdentical($plugin->getPluginId(), $id); - $this->assertIdentical($plugin->getDefinition(), $this->mockBlockExpectedDefinitions[$id]); + $this->assertIdentical($this->mockBlockManager->getDefinition($id), $this->mockBlockExpectedDefinitions[$id]); + } + // Test a plugin manager that provides defaults. + foreach (array('test_block1', 'test_block2') as $id) { + $plugin = $this->defaultsTestPluginManager->createInstance($id); + $this->assertIdentical($plugin->getPluginId(), $id); + $this->assertIdentical($this->defaultsTestPluginManager->getDefinition($id), $this->defaultsTestPluginExpectedDefinitions[$id]); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php index 5595e28bce5..5db932203ca 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Plugin/PluginTestBase.php @@ -10,6 +10,7 @@ namespace Drupal\system\Tests\Plugin; use Drupal\simpletest\UnitTestBase; use Drupal\plugin_test\Plugin\TestPluginManager; use Drupal\plugin_test\Plugin\MockBlockManager; +use Drupal\plugin_test\Plugin\DefaultsTestPluginManager; /** * Base class for Plugin API unit tests. @@ -19,6 +20,8 @@ abstract class PluginTestBase extends UnitTestBase { protected $testPluginExpectedDefinitions; protected $mockBlockManager; protected $mockBlockExpectedDefinitions; + protected $defaultsTestPluginManager; + protected $defaultsTestPluginExpectedDefinitions; public function setUp() { parent::setUp(); @@ -33,6 +36,7 @@ abstract class PluginTestBase extends UnitTestBase { // as derivatives and ReflectionFactory. $this->testPluginManager = new TestPluginManager(); $this->mockBlockManager = new MockBlockManager(); + $this->defaultsTestPluginManager = new DefaultsTestPluginManager(); // The expected plugin definitions within each manager. Several tests assert // that these plugins and their definitions are found and returned by the @@ -67,5 +71,22 @@ abstract class PluginTestBase extends UnitTestBase { 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockLayoutBlock', ), ); + $this->defaultsTestPluginExpectedDefinitions = array( + 'test_block1' => array( + 'metadata' => array( + 'default' => TRUE, + 'custom' => TRUE, + ), + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock', + ), + 'test_block2' => array( + 'metadata' => array( + 'default' => FALSE, + 'custom' => TRUE, + ), + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock', + ), + ); } + } diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/DefaultsTestPluginManager.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/DefaultsTestPluginManager.php new file mode 100644 index 00000000000..607b100bc01 --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/DefaultsTestPluginManager.php @@ -0,0 +1,51 @@ +discovery = new StaticDiscovery(); + $this->factory = new DefaultFactory($this); + + // Specify default values. + $this->defaults = array( + 'metadata' => array( + 'default' => TRUE, + ), + ); + + // Add a plugin with a custom value. + $this->discovery->setDefinition('test_block1', array( + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock', + 'metadata' => array( + 'custom' => TRUE, + ), + )); + // Add a plugin that overrides the default value. + $this->discovery->setDefinition('test_block2', array( + 'class' => 'Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock', + 'metadata' => array( + 'custom' => TRUE, + 'default' => FALSE, + ), + )); + } + +} diff --git a/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockTestBlock.php b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockTestBlock.php new file mode 100644 index 00000000000..63ea928fa02 --- /dev/null +++ b/core/modules/system/tests/modules/plugin_test/lib/Drupal/plugin_test/Plugin/plugin_test/mock_block/MockTestBlock.php @@ -0,0 +1,20 @@ +