3 namespace Drupal\Tests\user\Unit\Views\Argument;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Entity\EntityManager;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Tests\UnitTestCase;
9 use Drupal\user\Entity\Role;
10 use Drupal\user\Plugin\views\argument\RolesRid;
13 * @coversDefaultClass \Drupal\user\Plugin\views\argument\RolesRid
16 class RolesRidTest extends UnitTestCase {
19 * Tests the titleQuery method.
21 * @covers ::titleQuery
23 public function testTitleQuery() {
26 'label' => 'test rid 1'
30 'label' => 'test <strong>rid 2</strong>',
33 // Creates a stub entity storage;
34 $role_storage = $this->getMockForAbstractClass('Drupal\Core\Entity\EntityStorageInterface');
35 $role_storage->expects($this->any())
36 ->method('loadMultiple')
37 ->will($this->returnValueMap([
39 [['test_rid_1'], ['test_rid_1' => $role1]],
40 [['test_rid_1', 'test_rid_2'], ['test_rid_1' => $role1, 'test_rid_2' => $role2]],
43 $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
44 $entity_type->expects($this->any())
47 ->will($this->returnValue('label'));
49 $entity_manager = new EntityManager();
50 $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
51 $entity_type_manager->expects($this->any())
52 ->method('getDefinition')
53 ->with($this->equalTo('user_role'))
54 ->will($this->returnValue($entity_type));
57 ->expects($this->once())
58 ->method('getStorage')
59 ->with($this->equalTo('user_role'))
60 ->will($this->returnValue($role_storage));
62 // Set up a minimal container to satisfy Drupal\Core\Entity\Entity's
64 $container = new ContainerBuilder();
65 $container->set('entity.manager', $entity_manager);
66 $container->set('entity_type.manager', $entity_type_manager);
67 // Inject the container into entity.manager so it can defer to
68 // entity_type.manager.
69 $entity_manager->setContainer($container);
70 \Drupal::setContainer($container);
72 $roles_rid_argument = new RolesRid([], 'user__roles_rid', [], $entity_manager);
74 $roles_rid_argument->value = [];
75 $titles = $roles_rid_argument->titleQuery();
76 $this->assertEquals([], $titles);
78 $roles_rid_argument->value = ['test_rid_1'];
79 $titles = $roles_rid_argument->titleQuery();
80 $this->assertEquals(['test rid 1'], $titles);
82 $roles_rid_argument->value = ['test_rid_1', 'test_rid_2'];
83 $titles = $roles_rid_argument->titleQuery();
84 $this->assertEquals(['test rid 1', 'test <strong>rid 2</strong>'], $titles);