075b04c5e0e2fcf3ba1c2a2f98279d3d10e68f32
[yaffs-website] / web / core / modules / user / tests / src / Kernel / Condition / UserRoleConditionTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Kernel\Condition;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\user\Entity\Role;
8 use Drupal\user\Entity\User;
9 use Drupal\user\RoleInterface;
10
11 /**
12  * Tests the user role condition.
13  *
14  * @group user
15  */
16 class UserRoleConditionTest extends KernelTestBase {
17
18   /**
19    * The condition plugin manager.
20    *
21    * @var \Drupal\Core\Condition\ConditionManager
22    */
23   protected $manager;
24
25   /**
26    * An anonymous user for testing purposes.
27    *
28    * @var \Drupal\user\UserInterface
29    */
30   protected $anonymous;
31
32   /**
33    * An authenticated user for testing purposes.
34    *
35    * @var \Drupal\user\UserInterface
36    */
37   protected $authenticated;
38
39   /**
40    * A custom role for testing purposes.
41    *
42    * @var \Drupal\user\Entity\RoleInterface
43    */
44   protected $role;
45
46   /**
47    * Modules to enable.
48    *
49    * @var array
50    */
51   public static $modules = ['system', 'user', 'field'];
52
53   /**
54    * {@inheritdoc}
55    */
56   protected function setUp() {
57     parent::setUp();
58
59     $this->installSchema('system', 'sequences');
60     $this->installEntitySchema('user');
61
62     $this->manager = $this->container->get('plugin.manager.condition');
63
64     // Set up the authenticated and anonymous roles.
65     Role::create([
66       'id' => RoleInterface::ANONYMOUS_ID,
67       'label' => 'Anonymous user',
68     ])->save();
69     Role::create([
70       'id' => RoleInterface::AUTHENTICATED_ID,
71       'label' => 'Authenticated user',
72     ])->save();
73
74     // Create new role.
75     $rid = strtolower($this->randomMachineName(8));
76     $label = $this->randomString(8);
77     $role = Role::create([
78       'id' => $rid,
79       'label' => $label,
80     ]);
81     $role->save();
82     $this->role = $role;
83
84     // Setup an anonymous user for our tests.
85     $this->anonymous = User::create([
86       'name' => '',
87       'uid' => 0,
88     ]);
89     $this->anonymous->save();
90     // Loading the anonymous user adds the correct role.
91     $this->anonymous = User::load($this->anonymous->id());
92
93     // Setup an authenticated user for our tests.
94     $this->authenticated = User::create([
95       'name' => $this->randomMachineName(),
96     ]);
97     $this->authenticated->save();
98     // Add the custom role.
99     $this->authenticated->addRole($this->role->id());
100   }
101
102   /**
103    * Test the user_role condition.
104    */
105   public function testConditions() {
106     // Grab the user role condition and configure it to check against
107     // authenticated user roles.
108     /** @var $condition \Drupal\Core\Condition\ConditionInterface */
109     $condition = $this->manager->createInstance('user_role')
110       ->setConfig('roles', [RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID])
111       ->setContextValue('user', $this->anonymous);
112     $this->assertFalse($condition->execute(), 'Anonymous users fail role checks for authenticated.');
113     // Check for the proper summary.
114     // Summaries require an extra space due to negate handling in summary().
115     $this->assertEqual($condition->summary(), 'The user is a member of Authenticated user');
116
117     // Set the user role to anonymous.
118     $condition->setConfig('roles', [RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID]);
119     $this->assertTrue($condition->execute(), 'Anonymous users pass role checks for anonymous.');
120     // Check for the proper summary.
121     $this->assertEqual($condition->summary(), 'The user is a member of Anonymous user');
122
123     // Set the user role to check anonymous or authenticated.
124     $condition->setConfig('roles', [RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID, RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID]);
125     $this->assertTrue($condition->execute(), 'Anonymous users pass role checks for anonymous or authenticated.');
126     // Check for the proper summary.
127     $this->assertEqual($condition->summary(), 'The user is a member of Anonymous user, Authenticated user');
128
129     // Set the context to the authenticated user and check that they also pass
130     // against anonymous or authenticated roles.
131     $condition->setContextValue('user', $this->authenticated);
132     $this->assertTrue($condition->execute(), 'Authenticated users pass role checks for anonymous or authenticated.');
133
134     // Set the role to just authenticated and recheck.
135     $condition->setConfig('roles', [RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID]);
136     $this->assertTrue($condition->execute(), 'Authenticated users pass role checks for authenticated.');
137
138     // Test Constructor injection.
139     $condition = $this->manager->createInstance('user_role', ['roles' => [RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID], 'context' => ['user' => $this->authenticated]]);
140     $this->assertTrue($condition->execute(), 'Constructor injection of context and configuration working as anticipated.');
141
142     // Check the negated summary.
143     $condition->setConfig('negate', TRUE);
144     $this->assertEqual($condition->summary(), 'The user is not a member of Authenticated user');
145
146     // Check the complex negated summary.
147     $condition->setConfig('roles', [RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID, RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID]);
148     $this->assertEqual($condition->summary(), 'The user is not a member of Anonymous user, Authenticated user');
149
150     // Check a custom role.
151     $condition->setConfig('roles', [$this->role->id() => $this->role->id()]);
152     $condition->setConfig('negate', FALSE);
153     $this->assertTrue($condition->execute(), 'Authenticated user is a member of the custom role.');
154     $this->assertEqual($condition->summary(), SafeMarkup::format('The user is a member of @roles', ['@roles' => $this->role->label()]));
155   }
156
157 }