Commit 4c83e776 authored by catch's avatar catch

Issue #2248151 by tim.plunkett, xjm, alexpott: Configurable plugins can not...

Issue #2248151 by tim.plunkett, xjm, alexpott: Configurable plugins can not control instance-specific config dependencies.
parent e48eab8a
......@@ -36,4 +36,28 @@ public function setConfiguration(array $configuration);
*/
public function defaultConfiguration();
/**
* Calculates dependencies for the configured plugin.
*
* Dependencies are saved in the plugin's configuration entity and are used to
* determine configuration synchronization order. For example, if the plugin
* integrates with specific user roles, this method should return an array of
* dependencies listing the specified roles.
*
* @return array
* An array of dependencies grouped by type (module, theme, entity). For
* example:
* @code
* array(
* 'entity' => array('user.role.anonymous', 'user.role.authenticated'),
* 'module' => array('node', 'user'),
* 'theme' => array('seven'),
* );
* @endcode
*
* @see \Drupal\Core\Config\Entity\ConfigDependencyManager
* @see \Drupal\Core\Config\Entity\ConfigEntityInterface::getConfigDependencyName()
*/
public function calculateDependencies();
}
......@@ -52,4 +52,11 @@ public function setConfiguration(array $configuration) {
public function validateConfigurationForm(array &$form, array &$form_state) {
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return array();
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Config\Entity;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Utility\String;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Entity;
......@@ -307,11 +308,11 @@ public function calculateDependencies() {
$this->addDependency('module', $definition['provider']);
// Plugins can declare additional dependencies in their definition.
if (isset($definition['config_dependencies'])) {
foreach ($definition['config_dependencies'] as $type => $dependencies) {
foreach ($dependencies as $dependency) {
$this->addDependency($type, $dependency);
}
}
$this->addDependencies($definition['config_dependencies']);
}
// If a plugin is configurable, calculate its dependencies.
if ($instance instanceof ConfigurablePluginInterface && $plugin_dependencies = $instance->calculateDependencies()) {
$this->addDependencies($plugin_dependencies);
}
}
}
......
......@@ -29,4 +29,11 @@ public function defaultConfiguration() {
public function validateConfigurationForm(array &$form, array &$form_state) {
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return array();
}
}
......@@ -84,6 +84,13 @@ public function setConfigurationValue($key, $value) {
$this->configuration[$key] = $value;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return array();
}
/**
* {@inheritdoc}
*/
......
......@@ -113,6 +113,13 @@ public function defaultConfiguration() {
return array();
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return array();
}
/**
* {@inheritdoc}
*/
......
......@@ -116,4 +116,11 @@ public function defaultConfiguration() {
return array();
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return array();
}
}
......@@ -57,6 +57,13 @@ public function setConfiguration(array $configuration) {
public function validateConfigurationForm(array &$form, array &$form_state) {
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return array();
}
/**
* {@inheritdoc}
*/
......
......@@ -83,4 +83,31 @@ public function testOperations() {
$this->assertEqual($name, $account->label());
}
/**
* Tests the dependency calculation of actions.
*/
public function testDependencies() {
// Create a new action that depends on a user role.
$action = entity_create('action', array(
'id' => 'user_add_role_action.' . DRUPAL_ANONYMOUS_RID,
'type' => 'user',
'label' => t('Add the anonymous role to the selected users'),
'configuration' => array(
'rid' => DRUPAL_ANONYMOUS_RID,
),
'plugin' => 'user_add_role_action',
));
$action->save();
$expected = array(
'entity' => array(
'user.role.' . DRUPAL_ANONYMOUS_RID,
),
'module' => array(
'user',
),
);
$this->assertIdentical($expected, $action->calculateDependencies());
}
}
......@@ -8,11 +8,41 @@
namespace Drupal\user\Plugin\Action;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a base class for operations to change a user's role.
*/
abstract class ChangeUserRoleBase extends ConfigurableActionBase {
abstract class ChangeUserRoleBase extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
/**
* The user role entity type.
*
* @var \Drupal\Core\Entity\EntityTypeInterface
*/
protected $entityType;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeInterface $entity_type) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityType = $entity_type;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager')->getDefinition('user_role')
);
}
/**
* {@inheritdoc}
......@@ -46,4 +76,16 @@ public function submitConfigurationForm(array &$form, array &$form_state) {
$this->configuration['rid'] = $form_state['values']['rid'];
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$dependencies = array();
if (!empty($this->configuration['rid'])) {
$prefix = $this->entityType->getConfigPrefix() . '.';
$dependencies['entity'][] = $prefix . $this->configuration['rid'];
}
return $dependencies;
}
}
......@@ -7,7 +7,6 @@
namespace Drupal\user\Tests\Plugin\Action;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Plugin\Action\AddRoleUser;
/**
......@@ -15,15 +14,11 @@
*
* @see \Drupal\user\Plugin\Action\AddRoleUser
*/
class AddRoleUserTest extends UnitTestCase {
class AddRoleUserTest extends RoleUserTestBase {
/**
* The mocked account.
*
* @var \Drupal\user\UserInterface
* {@inheritdoc}
*/
protected $account;
public static function getInfo() {
return array(
'name' => 'Add user plugin',
......@@ -32,18 +27,6 @@ public static function getInfo() {
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->account = $this
->getMockBuilder('Drupal\user\Entity\User')
->disableOriginalConstructor()
->getMock();
}
/**
* Tests the execute method on a user with a role.
*/
......@@ -57,7 +40,7 @@ public function testExecuteAddExistingRole() {
->will($this->returnValue(TRUE));
$config = array('rid' => 'test_role_1');
$remove_role_plugin = new AddRoleUser($config, 'user_add_role_action', array('type' => 'user'));
$remove_role_plugin = new AddRoleUser($config, 'user_add_role_action', array('type' => 'user'), $this->userRoleEntityType);
$remove_role_plugin->execute($this->account);
}
......@@ -75,7 +58,7 @@ public function testExecuteAddNonExistingRole() {
->will($this->returnValue(FALSE));
$config = array('rid' => 'test_role_1');
$remove_role_plugin = new AddRoleUser($config, 'user_remove_role_action', array('type' => 'user'));
$remove_role_plugin = new AddRoleUser($config, 'user_remove_role_action', array('type' => 'user'), $this->userRoleEntityType);
$remove_role_plugin->execute($this->account);
}
......
......@@ -7,7 +7,6 @@
namespace Drupal\user\Tests\Plugin\Action;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Plugin\Action\RemoveRoleUser;
/**
......@@ -15,15 +14,11 @@
*
* @see \Drupal\user\Plugin\Action\RemoveRoleUser
*/
class RemoveRoleUserTest extends UnitTestCase {
class RemoveRoleUserTest extends RoleUserTestBase {
/**
* The mocked account.
*
* @var \Drupal\user\UserInterface
* {@inheritdoc}
*/
protected $account;
public static function getInfo() {
return array(
'name' => 'Remove user plugin',
......@@ -32,18 +27,6 @@ public static function getInfo() {
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->account = $this
->getMockBuilder('Drupal\user\Entity\User')
->disableOriginalConstructor()
->getMock();
}
/**
* Tests the execute method on a user with a role.
*/
......@@ -57,7 +40,7 @@ public function testExecuteRemoveExistingRole() {
->will($this->returnValue(TRUE));
$config = array('rid' => 'test_role_1');
$remove_role_plugin = new RemoveRoleUser($config, 'user_remove_role_action', array('type' => 'user'));
$remove_role_plugin = new RemoveRoleUser($config, 'user_remove_role_action', array('type' => 'user'), $this->userRoleEntityType);
$remove_role_plugin->execute($this->account);
}
......@@ -75,7 +58,7 @@ public function testExecuteRemoveNonExistingRole() {
->will($this->returnValue(FALSE));
$config = array('rid' => 'test_role_1');
$remove_role_plugin = new RemoveRoleUser($config, 'user_remove_role_action', array('type' => 'user'));
$remove_role_plugin = new RemoveRoleUser($config, 'user_remove_role_action', array('type' => 'user'), $this->userRoleEntityType);
$remove_role_plugin->execute($this->account);
}
......
<?php
/**
* @file
* Contains \Drupal\user\Tests\Plugin\Action\RoleUserTestBase.
*/
namespace Drupal\user\Tests\Plugin\Action;
use Drupal\Tests\UnitTestCase;
/**
* Provides a base class for user role action tests.
*/
abstract class RoleUserTestBase extends UnitTestCase {
/**
* The mocked account.
*
* @var \Drupal\user\UserInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $account;
/**
* The user role entity type.
*
* @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $userRoleEntityType;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->account = $this
->getMockBuilder('Drupal\user\Entity\User')
->disableOriginalConstructor()
->getMock();
$this->userRoleEntityType = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
}
}
......@@ -33,4 +33,11 @@ public function defaultConfiguration() {
return array();
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return array();
}
}
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