function UserHasRoleTest::testConditionEvaluation
Tests evaluating the condition.
@covers ::evaluate
File
-
tests/
src/ Unit/ Integration/ Condition/ UserHasRoleTest.php, line 38
Class
- UserHasRoleTest
- @coversDefaultClass \Drupal\rules\Plugin\Condition\UserHasRole @group RulesCondition
Namespace
Drupal\Tests\rules\Unit\Integration\ConditionCode
public function testConditionEvaluation() {
// Set-up a mock object with roles 'authenticated' and 'editor', but not
// 'administrator'.
$account = $this->prophesizeEntity(UserInterface::class);
$account->getRoles()
->willReturn([
'authenticated',
'editor',
])
->shouldBeCalledTimes(7);
$this->condition
->setContextValue('user', $account->reveal());
$authenticated = $this->prophesize(RoleInterface::class);
$authenticated->id()
->willReturn('authenticated');
$editor = $this->prophesize(RoleInterface::class);
$editor->id()
->willReturn('editor');
$administrator = $this->prophesize(RoleInterface::class);
$administrator->id()
->willReturn('administrator');
// First test the default AND condition with both roles the user has.
$this->condition
->setContextValue('roles', [
$authenticated->reveal(),
$editor->reveal(),
]);
$this->assertTrue($this->condition
->evaluate());
// User doesn't have the administrator role, this should fail.
$this->condition
->setContextValue('roles', [
$authenticated->reveal(),
$administrator->reveal(),
]);
$this->assertFalse($this->condition
->evaluate());
// Only one role, should succeed.
$this->condition
->setContextValue('roles', [
$authenticated->reveal(),
]);
$this->assertTrue($this->condition
->evaluate());
// A role the user doesn't have.
$this->condition
->setContextValue('roles', [
$administrator->reveal(),
]);
$this->assertFalse($this->condition
->evaluate());
// Only one role, the user has with OR condition, should succeed.
$this->condition
->setContextValue('roles', [
$authenticated->reveal(),
]);
$this->condition
->setContextValue('operation', 'OR');
$this->assertTrue($this->condition
->evaluate());
// User doesn't have the administrator role, but has the authenticated,
// should succeed.
$this->condition
->setContextValue('roles', [
$authenticated->reveal(),
$administrator->reveal(),
]);
$this->condition
->setContextValue('operation', 'OR');
$this->assertTrue($this->condition
->evaluate());
// User doesn't have the administrator role. This should fail.
$this->condition
->setContextValue('roles', [
$administrator->reveal(),
]);
$this->condition
->setContextValue('operation', 'OR');
$this->assertFalse($this->condition
->evaluate());
}