3 namespace Drupal\Tests\user\Unit\Views\Argument;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\user\Entity\Role;
8 use Drupal\user\Plugin\views\argument\RolesRid;
11 * @coversDefaultClass \Drupal\user\Plugin\views\argument\RolesRid
14 class RolesRidTest extends UnitTestCase {
17 * Tests the titleQuery method.
19 * @covers ::titleQuery
21 public function testTitleQuery() {
24 'label' => 'test rid 1'
28 'label' => 'test <strong>rid 2</strong>',
31 // Creates a stub entity storage;
32 $role_storage = $this->getMockForAbstractClass('Drupal\Core\Entity\EntityStorageInterface');
33 $role_storage->expects($this->any())
34 ->method('loadMultiple')
35 ->will($this->returnValueMap([
37 [['test_rid_1'], ['test_rid_1' => $role1]],
38 [['test_rid_1', 'test_rid_2'], ['test_rid_1' => $role1, 'test_rid_2' => $role2]],
41 $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
42 $entity_type->expects($this->any())
45 ->will($this->returnValue('label'));
47 $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
48 $entity_manager->expects($this->any())
49 ->method('getDefinition')
50 ->with($this->equalTo('user_role'))
51 ->will($this->returnValue($entity_type));
54 ->expects($this->once())
55 ->method('getStorage')
56 ->with($this->equalTo('user_role'))
57 ->will($this->returnValue($role_storage));
59 // @todo \Drupal\Core\Entity\Entity::entityType() uses a global call to
60 // entity_get_info(), which in turn wraps \Drupal::entityManager(). Set
61 // the entity manager until this is fixed.
62 $container = new ContainerBuilder();
63 $container->set('entity.manager', $entity_manager);
64 \Drupal::setContainer($container);
66 $roles_rid_argument = new RolesRid([], 'user__roles_rid', [], $entity_manager);
68 $roles_rid_argument->value = [];
69 $titles = $roles_rid_argument->titleQuery();
70 $this->assertEquals([], $titles);
72 $roles_rid_argument->value = ['test_rid_1'];
73 $titles = $roles_rid_argument->titleQuery();
74 $this->assertEquals(['test rid 1'], $titles);
76 $roles_rid_argument->value = ['test_rid_1', 'test_rid_2'];
77 $titles = $roles_rid_argument->titleQuery();
78 $this->assertEquals(['test rid 1', 'test <strong>rid 2</strong>'], $titles);