3 namespace Drupal\Tests\user\Kernel\Condition;
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;
12 * Tests the user role condition.
16 class UserRoleConditionTest extends KernelTestBase {
19 * The condition plugin manager.
21 * @var \Drupal\Core\Condition\ConditionManager
26 * An anonymous user for testing purposes.
28 * @var \Drupal\user\UserInterface
33 * An authenticated user for testing purposes.
35 * @var \Drupal\user\UserInterface
37 protected $authenticated;
40 * A custom role for testing purposes.
42 * @var \Drupal\user\Entity\RoleInterface
51 public static $modules = ['system', 'user', 'field'];
56 protected function setUp() {
59 $this->installSchema('system', 'sequences');
60 $this->installEntitySchema('user');
62 $this->manager = $this->container->get('plugin.manager.condition');
64 // Set up the authenticated and anonymous roles.
66 'id' => RoleInterface::ANONYMOUS_ID,
67 'label' => 'Anonymous user',
70 'id' => RoleInterface::AUTHENTICATED_ID,
71 'label' => 'Authenticated user',
75 $rid = strtolower($this->randomMachineName(8));
76 $label = $this->randomString(8);
77 $role = Role::create([
84 // Setup an anonymous user for our tests.
85 $this->anonymous = User::create([
89 $this->anonymous->save();
90 // Loading the anonymous user adds the correct role.
91 $this->anonymous = User::load($this->anonymous->id());
93 // Setup an authenticated user for our tests.
94 $this->authenticated = User::create([
95 'name' => $this->randomMachineName(),
97 $this->authenticated->save();
98 // Add the custom role.
99 $this->authenticated->addRole($this->role->id());
103 * Test the user_role condition.
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');
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');
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');
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.');
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.');
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.');
142 // Check the negated summary.
143 $condition->setConfig('negate', TRUE);
144 $this->assertEqual($condition->summary(), 'The user is not a member of Authenticated user');
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');
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()]));