Issue #1816916 by tim.plunkett: Recursively merge in defaults.
parent
4365f5079d
commit
a4d4a17c1c
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains Drupal\plugin_test\Plugin\DefaultsTestPluginManager.
|
||||
*/
|
||||
|
||||
namespace Drupal\plugin_test\Plugin;
|
||||
|
||||
use Drupal\Component\Plugin\PluginManagerBase;
|
||||
use Drupal\Component\Plugin\Discovery\StaticDiscovery;
|
||||
use Drupal\Component\Plugin\Factory\DefaultFactory;
|
||||
|
||||
/**
|
||||
* Defines a plugin manager used by Plugin API unit tests.
|
||||
*/
|
||||
class DefaultsTestPluginManager extends PluginManagerBase {
|
||||
|
||||
public function __construct() {
|
||||
// Create the object that can be used to return definitions for all the
|
||||
// plugins available for this type. Most real plugin managers use a richer
|
||||
// discovery implementation, but StaticDiscovery lets us add some simple
|
||||
// mock plugins for unit testing.
|
||||
$this->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,
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains Drupal\plugin_test\Plugin\plugin_test\mock_block\MockTestBlock.
|
||||
*/
|
||||
|
||||
namespace Drupal\plugin_test\Plugin\plugin_test\mock_block;
|
||||
|
||||
use Drupal\Component\Plugin\PluginBase;
|
||||
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
|
||||
|
||||
/**
|
||||
* Mock implementation of a test block plugin used by Plugin API unit tests.
|
||||
*
|
||||
* @see Drupal\plugin_test\Plugin\DefaultsTestPluginManager
|
||||
*/
|
||||
class MockTestBlock extends PluginBase {
|
||||
|
||||
}
|
Loading…
Reference in New Issue