6150a1119b30e7bef93f0501ea16f4e778f2228f
[yaffs-website] / web / modules / contrib / linkit / src / Tests / Matchers / UserMatcherTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Tests\Matchers\UserMatcherTest.
6  */
7
8 namespace Drupal\linkit\Tests\Matchers;
9
10 use Drupal\linkit\Tests\LinkitTestBase;
11
12 /**
13  * Tests user matcher.
14  *
15  * @group linkit
16  */
17 class UserMatcherTest extends LinkitTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['user'];
25
26   /**
27    * The matcher manager.
28    *
29    * @var \Drupal\linkit\MatcherManager
30    */
31   protected $manager;
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38     $this->drupalLogin($this->drupalCreateUser(['access user profiles']));
39     $this->manager = $this->container->get('plugin.manager.linkit.matcher');
40
41     $custom_role = $this->drupalCreateRole(array(), 'custom_role', 'custom_role');
42     $custom_role_admin = $this->drupalCreateRole(array(), 'custom_role_admin', 'custom_role_admin');
43
44     $this->drupalCreateUser([], 'lorem');
45     $this->drupalCreateUser([], 'foo');
46
47     $account = $this->drupalCreateUser([], 'ipsumlorem');
48     $account->addRole($custom_role);
49     $account->save();
50
51     $account = $this->drupalCreateUser([], 'lorem_custom_role');
52     $account->addRole($custom_role);
53     $account->save();
54
55     $account = $this->drupalCreateUser([], 'lorem_custom_role_admin');
56     $account->addRole($custom_role_admin);
57     $account->save();
58
59     $account = $this->drupalCreateUser([], 'blocked_lorem');
60     $account->block();
61     $account->save();
62   }
63
64   /**
65    * Tests user matcher.
66    */
67   function testUserMatcherWidthDefaultConfiguration() {
68     /** @var \Drupal\linkit\MatcherInterface $plugin */
69     $plugin = $this->manager->createInstance('entity:user', []);
70     $matches = $plugin->getMatches('Lorem');
71     $this->assertEqual(4, count($matches), 'Correct number of matches');
72   }
73
74   /**
75    * Tests user matcher with role filer.
76    */
77   function testUserMatcherWidthRoleFiler() {
78     /** @var \Drupal\linkit\MatcherInterface $plugin */
79     $plugin = $this->manager->createInstance('entity:user', [
80       'settings' => [
81         'roles' => [
82           'custom_role' => 'custom_role'
83         ],
84       ],
85     ]);
86
87     $matches = $plugin->getMatches('Lorem');
88     $this->assertEqual(2, count($matches), 'Correct number of matches');
89   }
90
91   /**
92    * Tests user matcher with include blocked setting activated.
93    */
94   function testUserMatcherWidthIncludeBlocked() {
95     /** @var \Drupal\linkit\MatcherInterface $plugin */
96     $plugin = $this->manager->createInstance('entity:user', [
97       'settings' => [
98         'include_blocked' => TRUE,
99       ],
100     ]);
101
102     // Test without permissions to see blocked users.
103     $matches = $plugin->getMatches('blocked');
104     $this->assertEqual(0, count($matches), 'Correct number of matches');
105
106     $account = $this->drupalCreateUser(['administer users']);
107     $this->drupalLogin($account);
108
109     // Test with permissions to see blocked users.
110     $matches = $plugin->getMatches('blocked');
111     $this->assertEqual(1, count($matches), 'Correct number of matches');
112   }
113
114 }