View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\RulesAction;
use Drupal\Tests\rules\Unit\Integration\RulesEntityIntegrationTestBase;
use Drupal\user\RoleInterface;
use Drupal\user\UserInterface;
use Prophecy\Argument;
class UserRoleAddTest extends RulesEntityIntegrationTestBase {
protected $action;
protected function setUp() : void {
parent::setUp();
$this
->enableModule('user');
$this->action = $this->actionManager
->createInstance('rules_user_role_add');
}
public function testSummary() {
$this
->assertEquals('Add user role', $this->action
->summary());
}
public function testAddOneRoleNoSave() {
$account = $this
->prophesizeEntity(UserInterface::class);
$account
->hasRole('administrator')
->willReturn(FALSE);
$account
->addRole('administrator')
->shouldBeCalledTimes(1);
$account
->save()
->shouldNotBeCalled();
$administrator = $this
->prophesize(RoleInterface::class);
$administrator
->id()
->willReturn('administrator');
$this->action
->setContextValue('user', $account
->reveal())
->setContextValue('roles', [
$administrator
->reveal(),
])
->execute();
$this
->assertEquals($this->action
->autoSaveContext(), [
'user',
], 'Action returns the user context name for auto saving.');
}
public function testAddThreeRoles() {
$account = $this
->prophesizeEntity(UserInterface::class);
$account
->hasRole('manager')
->willReturn(FALSE)
->shouldBeCalledTimes(1);
$account
->hasRole('editor')
->willReturn(FALSE)
->shouldBeCalledTimes(1);
$account
->hasRole('administrator')
->willReturn(FALSE)
->shouldBeCalledTimes(1);
$account
->addRole('manager')
->shouldBeCalledTimes(1);
$account
->addRole('editor')
->shouldBeCalledTimes(1);
$account
->addRole('administrator')
->shouldBeCalledTimes(1);
$manager = $this
->prophesize(RoleInterface::class);
$manager
->id()
->willReturn('manager');
$editor = $this
->prophesize(RoleInterface::class);
$editor
->id()
->willReturn('editor');
$administrator = $this
->prophesize(RoleInterface::class);
$administrator
->id()
->willReturn('administrator');
$this->action
->setContextValue('user', $account
->reveal())
->setContextValue('roles', [
$manager
->reveal(),
$editor
->reveal(),
$administrator
->reveal(),
])
->execute();
$this
->assertEquals($this->action
->autoSaveContext(), [
'user',
], 'Action returns the user context name for auto saving.');
}
public function testAddExistingRole() {
$account = $this
->prophesizeEntity(UserInterface::class);
$account
->hasRole('administrator')
->willReturn(TRUE);
$account
->addRole(Argument::any())
->shouldNotBeCalled();
$administrator = $this
->prophesize(RoleInterface::class);
$administrator
->id()
->willReturn('administrator');
$this->action
->setContextValue('user', $account
->reveal())
->setContextValue('roles', [
$administrator
->reveal(),
])
->execute();
$this
->assertEquals($this->action
->autoSaveContext(), [], 'Action returns nothing for auto saving since the user has the role already.');
}
public function testAddExistingAndNonexistentRole() {
$account = $this
->prophesizeEntity(UserInterface::class);
$account
->hasRole('administrator')
->willReturn(TRUE)
->shouldBeCalledTimes(1);
$account
->hasRole('editor')
->willReturn(FALSE)
->shouldBeCalledTimes(1);
$account
->addRole('editor')
->shouldBeCalledTimes(1);
$editor = $this
->prophesize(RoleInterface::class);
$editor
->id()
->willReturn('editor');
$administrator = $this
->prophesize(RoleInterface::class);
$administrator
->id()
->willReturn('administrator');
$this->action
->setContextValue('user', $account
->reveal())
->setContextValue('roles', [
$administrator
->reveal(),
$editor
->reveal(),
])
->execute();
$this
->assertEquals($this->action
->autoSaveContext(), [
'user',
], 'Action returns the user context name for auto saving.');
}
}