Commit 358de91a authored by alexpott's avatar alexpott

Issue #2062367 by tim.plunkett, dawehner: Fixed Prevent PluginBags from...

Issue #2062367 by tim.plunkett, dawehner: Fixed Prevent PluginBags from reordering the export based on their sort.
parent cb599de3
......@@ -43,6 +43,13 @@ class DefaultPluginBag extends PluginBag {
*/
protected $pluginKey = 'id';
/**
* The original order of the instances.
*
* @var array
*/
protected $originalOrder = array();
/**
* Constructs a new DefaultPluginBag object.
*
......@@ -58,6 +65,8 @@ public function __construct(PluginManagerInterface $manager, array $configuratio
if (!empty($configurations)) {
$this->instanceIDs = MapArray::copyValuesToKeys(array_keys($configurations));
// Store the original order of the instance IDs for export.
$this->originalOrder = $this->instanceIDs;
}
}
......@@ -65,7 +74,7 @@ public function __construct(PluginManagerInterface $manager, array $configuratio
* {@inheritdoc}
*/
protected function initializePlugin($instance_id) {
$configuration = $this->configurations[$instance_id];
$configuration = isset($this->configurations[$instance_id]) ? $this->configurations[$instance_id] : array();
if (!isset($configuration[$this->pluginKey])) {
throw new PluginException(String::format("Unknown plugin ID '@instance'.", array('@instance' => $instance_id)));
}
......@@ -103,6 +112,12 @@ public function sortHelper($aID, $bID) {
public function getConfiguration() {
$instances = array();
$this->rewind();
// Store the current order of the instances.
$current_order = $this->instanceIDs;
// Reorder the instances to match the original order, adding new instances
// to the end.
$this->instanceIDs = $this->originalOrder + $current_order;
foreach ($this as $instance_id => $instance) {
if ($instance instanceof ConfigurablePluginInterface) {
$instances[$instance_id] = $instance->getConfiguration();
......@@ -111,6 +126,8 @@ public function getConfiguration() {
$instances[$instance_id] = $this->configurations[$instance_id];
}
}
// Restore the current order.
$this->instanceIDs = $current_order;
return $instances;
}
......@@ -138,6 +155,7 @@ public function setConfiguration($instance_id, array $configuration) {
*/
public function removeInstanceID($instance_id) {
parent::removeInstanceID($instance_id);
unset($this->originalOrder[$instance_id]);
unset($this->configurations[$instance_id]);
}
......
......@@ -107,7 +107,7 @@ function testFilterFormatAPI() {
);
$this->assertIdentical(
filter_get_filter_types_by_format('filtered_html'),
array(FILTER_TYPE_HTML_RESTRICTOR, FILTER_TYPE_MARKUP_LANGUAGE),
array(FILTER_TYPE_MARKUP_LANGUAGE, FILTER_TYPE_HTML_RESTRICTOR),
'filter_get_filter_types_by_format() works as expected for the filtered_html format.'
);
......
<?php
/**
* @file
* Contains \Drupal\Tests\Component\Plugin\DefaultPluginBagTest.
*/
namespace Drupal\Tests\Component\Plugin;
use Drupal\Component\Plugin\DefaultPluginBag;
use Drupal\Tests\UnitTestCase;
/**
* Tests the default plugin bag.
*
* @see \Drupal\Component\Plugin\DefaultPluginBag
*
* @group Drupal
* @group Drupal_Plugin
*/
class DefaultPluginBagTest extends UnitTestCase {
/**
* The mocked plugin manager.
*
* @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Component\Plugin\PluginManagerInterface
*/
protected $pluginManager;
/**
* The tested plugin bag.
*
* @var \PHPUnit_Framework_MockObject_MockObject|\Drupal\Component\Plugin\DefaultPluginBag
*/
protected $defaultPluginBag;
/**
* Stores all setup plugin instances.
*
* @var array
*/
protected $pluginInstances;
/**
* Contains the plugin configuration.
*
* @var array
*/
protected $config = array(
'banana' => array('id' => 'banana', 'key' => 'value'),
'cherry' => array('id' => 'cherry', 'key' => 'value'),
'apple' => array('id' => 'apple', 'key' => 'value'),
);
public static function getInfo() {
return array(
'name' => 'Default plugin bag',
'description' => 'Tests the default plugin bag.',
'group' => 'PHP Storage',
);
}
protected function setUp() {
$this->pluginManager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface');
$definitions = $this->getPluginDefinitions();
$this->pluginManager->expects($this->any())
->method('getDefinitions')
->will($this->returnValue($definitions));
$this->pluginInstances = array();
$map = array();
foreach ($definitions as $plugin_id => $definition) {
// Create a mock plugin instance.
$mock = $this->getMock('Drupal\Component\Plugin\PluginInspectionInterface');
$mock->expects($this->any())
->method('getPluginId')
->will($this->returnValue($plugin_id));
$this->pluginInstances[$plugin_id] = $mock;
$map[] = array($plugin_id, $this->config[$plugin_id], $this->pluginInstances[$plugin_id]);
}
$this->pluginManager->expects($this->any())
->method('createInstance')
->will($this->returnValueMap($map));
$this->defaultPluginBag = new DefaultPluginBag($this->pluginManager, $this->config);
}
/**
* Tests the has method.
*
* @see \Drupal\Component\Plugin\DefaultPluginBag::has()
*/
public function testHas() {
$definitions = $this->getPluginDefinitions();
$this->assertFalse($this->defaultPluginBag->has($this->randomName()), 'Nonexistent plugin found.');
foreach (array_keys($definitions) as $plugin_id) {
$this->assertTrue($this->defaultPluginBag->has($plugin_id));
}
}
/**
* Tests the get method.
*
* @see \Drupal\Component\Plugin\DefaultPluginBag::get()
*/
public function testGet() {
$apple = $this->pluginInstances['apple'];
$this->assertEquals($apple, $this->defaultPluginBag->get('apple'));
}
/**
* Tests the get method with an non existing plugin ID.
*
* @expectedException \Drupal\Component\Plugin\Exception\PluginException
*/
public function testGetNotExistingPlugin() {
$this->defaultPluginBag->get('pear');
}
/**
* Returns some example plugin definitions.
*
* @return array
* The example plugin definitions.
*/
protected function getPluginDefinitions() {
$definitions = array(
'apple' => array(
'id' => 'apple',
'label' => 'Apple',
'color' => 'green',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Apple',
'provider' => 'plugin_test',
),
'banana' => array(
'id' => 'banana',
'label' => 'Banana',
'color' => 'yellow',
'uses' => array(
'bread' => 'Banana bread',
),
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Banana',
'provider' => 'plugin_test',
),
'cherry' => array(
'id' => 'cherry',
'label' => 'Cherry',
'color' => 'red',
'class' => 'Drupal\plugin_test\Plugin\plugin_test\fruit\Cherry',
'provider' => 'plugin_test',
),
);
return $definitions;
}
/**
* Provides test data for testSortHelper.
*
* @return array
* The test data.
*/
public function providerTestSortHelper() {
return array(
array('apple', 'apple', 0),
array('apple', 'cherry', -1),
array('cherry', 'apple', 1),
array('cherry', 'banana', 1),
);
}
/**
* Tests the sort helper.
*
* @param string $plugin_id_1
* The first plugin ID.
* @param string $plugin_id_2
* The second plugin ID.
* @param int $expected
* The expected result.
*
* @dataProvider providerTestSortHelper
*/
public function testSortHelper($plugin_id_1, $plugin_id_2, $expected) {
if ($expected != 0) {
$expected = $expected > 0 ? 1 : -1;
}
$this->assertEquals($expected, $this->defaultPluginBag->sortHelper($plugin_id_1, $plugin_id_2));
}
/**
* Tests the configuration getter method.
*
* @see \Drupal\Component\Plugin\DefaultPluginBag::getConfiguration()
*/
public function testGetConfiguration() {
// The expected order matches $this->config.
$expected = array('banana', 'cherry', 'apple');
$config = $this->defaultPluginBag->getConfiguration();
$this->assertSame($expected, array_keys($config), 'The order of the configuration is unchanged.');
$ids = $this->defaultPluginBag->getInstanceIDs();
$this->assertSame($expected, array_keys($ids), 'The order of the instances is unchanged.');
$this->defaultPluginBag->sort();
$config = $this->defaultPluginBag->getConfiguration();
$this->assertSame($expected, array_keys($config), 'After sorting, the order of the configuration is unchanged.');
$ids = $this->defaultPluginBag->getInstanceIDs();
sort($expected);
$this->assertSame($expected, array_keys($ids), 'After sorting, the order of the instances is also sorted.');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment