public function UserRoleConditionTest::testConditions in Drupal 8
Same name and namespace in other branches
- 9 core/modules/user/tests/src/Kernel/Condition/UserRoleConditionTest.php \Drupal\Tests\user\Kernel\Condition\UserRoleConditionTest::testConditions()
Test the user_role condition.
File
- core/
modules/ user/ tests/ src/ Kernel/ Condition/ UserRoleConditionTest.php, line 105
Class
- UserRoleConditionTest
- Tests the user role condition.
Namespace
Drupal\Tests\user\Kernel\ConditionCode
public function testConditions() {
// Grab the user role condition and configure it to check against
// authenticated user roles.
/** @var $condition \Drupal\Core\Condition\ConditionInterface */
$condition = $this->manager
->createInstance('user_role')
->setConfig('roles', [
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
])
->setContextValue('user', $this->anonymous);
$this
->assertFalse($condition
->execute(), 'Anonymous users fail role checks for authenticated.');
// Check for the proper summary.
// Summaries require an extra space due to negate handling in summary().
$this
->assertEqual($condition
->summary(), 'The user is a member of Authenticated user');
// Set the user role to anonymous.
$condition
->setConfig('roles', [
RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID,
]);
$this
->assertTrue($condition
->execute(), 'Anonymous users pass role checks for anonymous.');
// Check for the proper summary.
$this
->assertEqual($condition
->summary(), 'The user is a member of Anonymous user');
// Set the user role to check anonymous or authenticated.
$condition
->setConfig('roles', [
RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID,
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
]);
$this
->assertTrue($condition
->execute(), 'Anonymous users pass role checks for anonymous or authenticated.');
// Check for the proper summary.
$this
->assertEqual($condition
->summary(), 'The user is a member of Anonymous user, Authenticated user');
// Set the context to the authenticated user and check that they also pass
// against anonymous or authenticated roles.
$condition
->setContextValue('user', $this->authenticated);
$this
->assertTrue($condition
->execute(), 'Authenticated users pass role checks for anonymous or authenticated.');
// Set the role to just authenticated and recheck.
$condition
->setConfig('roles', [
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
]);
$this
->assertTrue($condition
->execute(), 'Authenticated users pass role checks for authenticated.');
// Test Constructor injection.
$condition = $this->manager
->createInstance('user_role', [
'roles' => [
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
],
'context' => [
'user' => $this->authenticated,
],
]);
$this
->assertTrue($condition
->execute(), 'Constructor injection of context and configuration working as anticipated.');
// Check the negated summary.
$condition
->setConfig('negate', TRUE);
$this
->assertEqual($condition
->summary(), 'The user is not a member of Authenticated user');
// Check the complex negated summary.
$condition
->setConfig('roles', [
RoleInterface::ANONYMOUS_ID => RoleInterface::ANONYMOUS_ID,
RoleInterface::AUTHENTICATED_ID => RoleInterface::AUTHENTICATED_ID,
]);
$this
->assertEqual($condition
->summary(), 'The user is not a member of Anonymous user, Authenticated user');
// Check a custom role.
$condition
->setConfig('roles', [
$this->role
->id() => $this->role
->id(),
]);
$condition
->setConfig('negate', FALSE);
$this
->assertTrue($condition
->execute(), 'Authenticated user is a member of the custom role.');
$this
->assertEqual($condition
->summary(), new FormattableMarkup('The user is a member of @roles', [
'@roles' => $this->role
->label(),
]));
}