Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / user / tests / src / Unit / Views / Argument / RolesRidTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Unit\Views\Argument;
4
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;
9
10 /**
11  * @coversDefaultClass \Drupal\user\Plugin\views\argument\RolesRid
12  * @group user
13  */
14 class RolesRidTest extends UnitTestCase {
15
16   /**
17    * Tests the titleQuery method.
18    *
19    * @covers ::titleQuery
20    */
21   public function testTitleQuery() {
22     $role1 = new Role([
23       'id' => 'test_rid_1',
24       'label' => 'test rid 1'
25     ], 'user_role');
26     $role2 = new Role([
27       'id' => 'test_rid_2',
28       'label' => 'test <strong>rid 2</strong>',
29     ], 'user_role');
30
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([
36         [[], []],
37         [['test_rid_1'], ['test_rid_1' => $role1]],
38         [['test_rid_1', 'test_rid_2'], ['test_rid_1' => $role1, 'test_rid_2' => $role2]],
39       ]));
40
41     $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface');
42     $entity_type->expects($this->any())
43       ->method('getKey')
44       ->with('label')
45       ->will($this->returnValue('label'));
46
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));
52
53     $entity_manager
54       ->expects($this->once())
55       ->method('getStorage')
56       ->with($this->equalTo('user_role'))
57       ->will($this->returnValue($role_storage));
58
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);
65
66     $roles_rid_argument = new RolesRid([], 'user__roles_rid', [], $entity_manager);
67
68     $roles_rid_argument->value = [];
69     $titles = $roles_rid_argument->titleQuery();
70     $this->assertEquals([], $titles);
71
72     $roles_rid_argument->value = ['test_rid_1'];
73     $titles = $roles_rid_argument->titleQuery();
74     $this->assertEquals(['test rid 1'], $titles);
75
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);
79   }
80
81 }