diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index 7ea8b62..992dbda 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -363,7 +363,7 @@ public function createDuplicate() { * Implements \Drupal\Core\Entity\EntityInterface::entityInfo(). */ public function entityInfo() { - return entity_get_info($this->entityType); + return \Drupal::entityManager()->getDefinition($this->entityType()); } /** diff --git a/core/modules/user/lib/Drupal/user/Plugin/views/argument/RolesRid.php b/core/modules/user/lib/Drupal/user/Plugin/views/argument/RolesRid.php index bbef57e..4b853f4 100644 --- a/core/modules/user/lib/Drupal/user/Plugin/views/argument/RolesRid.php +++ b/core/modules/user/lib/Drupal/user/Plugin/views/argument/RolesRid.php @@ -2,13 +2,16 @@ /** * @file - * Definition of views_handler_argument_users_roles_rid. + * Contains \Drupal\user\Plugin\views\argument\RolesRid. */ namespace Drupal\user\Plugin\views\argument; use Drupal\Component\Annotation\PluginID; +use Drupal\Component\Utility\String; +use Drupal\Core\Entity\EntityManager; use Drupal\views\Plugin\views\argument\ManyToOne; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Allow role ID(s) as argument. @@ -19,8 +22,46 @@ */ class RolesRid extends ManyToOne { - function title_query() { - return array(entity_load('user_role', $this->value)->label()); + /** + * The role entity storage controller + */ + protected $roleStorageController; + + /** + * Constructs a \Drupal\user\Plugin\views\argument\RolesRid object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param array $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Entity\EntityManager $entity_manager + * The entity manager. + */ + public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityManager $entity_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + + $this->roleStorageController = $entity_manager->getStorageController('user_role'); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return parent::create($container, $configuration, $plugin_id, $plugin_definition, $container->get('plugin.manager.entity')); + } + + /** + * @{inheritdoc} + */ + public function title_query() { + $entities = $this->roleStorageController->load($this->value); + $titles = array(); + foreach ($entities as $entity) { + $titles[] = String::checkPlain($entity->label()); + } + return $titles; } } diff --git a/core/modules/user/tests/Drupal/Tests/user/Views/Argument/RolesRidTest.php b/core/modules/user/tests/Drupal/Tests/user/Views/Argument/RolesRidTest.php new file mode 100644 index 0000000..762558a --- /dev/null +++ b/core/modules/user/tests/Drupal/Tests/user/Views/Argument/RolesRidTest.php @@ -0,0 +1,105 @@ + array( + 'id' => 'id', + 'label' => 'label', + ), + 'config_prefix' => 'user.role', + 'class' => 'Drupal\user\Plugin\Core\Entity\Role', + ); + + public static function getInfo() { + return array( + 'name' => 'User: Roles Rid Argument', + 'description' => 'Tests the role argument handler.', + 'group' => 'Views module integration', + ); + } + + /** + * Tests the title_query method. + * + * @see \Drupal\user\Plugin\views\argument\RolesRid::title_query() + */ + public function testTitleQuery() { + $config = array( + 'user.role.test_rid_1' => array( + 'id' => 'test_rid_1', + 'label' => 'test rid 1' + ), + 'user.role.test_rid_2' => array( + 'id' => 'test_rid_2', + 'label' => 'test rid 2', + ), + ); + $config_factory = $this->getConfigFactoryStub($config); + $config_storage = $this->getConfigStorageStub($config); + + // Creates a stub roles storage controller and replace the attachLoad method + // with a empty version, because attachLoad calls module_implements. + $role_storage_controller = $this->getMock('Drupal\user\RoleStorageController', array('attachLoad'), array('user_role', static::$entityInfo, $config_factory, $config_storage)); + + + $entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $entity_manager->expects($this->any()) + ->method('getDefinition') + ->with($this->equalTo('user_role')) + ->will($this->returnValue(static::$entityInfo)); + + $entity_manager + ->expects($this->once()) + ->method('getStorageController') + ->with($this->equalTo('user_role')) + ->will($this->returnValue($role_storage_controller)); + + // \Drupal\Core\Entity\Entity::entityInfo() uses an not injected global + // call so set it until entity has the entity info injected. + + $container = new ContainerBuilder(); + $container->set('plugin.manager.entity', $entity_manager); + \Drupal::setContainer($container); + + $roles_rid_argument = new RolesRid($config, 'users_roles_rid', array(), $entity_manager); + + $roles_rid_argument->value = array(); + $titles = $roles_rid_argument->title_query(); + $this->assertEquals(array(), $titles); + + $roles_rid_argument->value = array('test_rid_1'); + $titles = $roles_rid_argument->title_query(); + $this->assertEquals(array('test rid 1'), $titles); + + $roles_rid_argument->value = array('test_rid_1', 'test_rid_2'); + $titles = $roles_rid_argument->title_query(); + $this->assertEquals(array('test rid 1', String::checkPlain('test rid 2')), $titles); + } + +} diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php index c03d125..89724e2 100644 --- a/core/tests/Drupal/Tests/UnitTestCase.php +++ b/core/tests/Drupal/Tests/UnitTestCase.php @@ -7,6 +7,9 @@ namespace Drupal\Tests; +use Drupal\Core\Config\Config; +use Drupal\Core\Config\ConfigFactory; + class UnitTestCase extends \PHPUnit_Framework_TestCase { /** @@ -85,9 +88,13 @@ public function getConfigFactoryStub($configs) { foreach ($config_values as $key => $value) { $map[] = array($key, $value); } + // Also allow to pass in no argument. + $map[] = array('', $config_values); + $config_object->expects($this->any()) ->method('get') ->will($this->returnValueMap($map)); + $config_map[] = array($config_name, $config_object); } // Construct a config factory with the array of configuration object stubs @@ -100,4 +107,31 @@ public function getConfigFactoryStub($configs) { ->will($this->returnValueMap($config_map)); return $config_factory; } + + /** + * Returns a stub config storage that behaves according to the passed in array. + * + * @param array $configs + * An associative array of configuration settings whose keys are configuration + * object names and whose values are key => value arrays for the configuration + * object in question. + * + * @return \Drupal\Core\Config\StorageInterface + * A mocked config storage. + */ + public function getConfigStorageStub(array $configs) { + $config_storage = $this->getMock('Drupal\Core\Config\NullStorage'); + $config_storage->expects($this->any()) + ->method('listAll') + ->will($this->returnValue(array_keys($configs))); + + foreach ($configs as $name => $config) { + $config_storage->expects($this->any()) + ->method('read') + ->with($this->equalTo($name)) + ->will($this->returnValue($config)); + } + return $config_storage; + } + }