UserRoleRemoveTest.php in Rules 8.3
File
tests/src/Unit/Integration/RulesAction/UserRoleRemoveTest.php
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;
class UserRoleRemoveTest extends RulesEntityIntegrationTestBase {
protected $action;
protected function setUp() : void {
parent::setUp();
$this
->enableModule('user');
$this->action = $this->actionManager
->createInstance('rules_user_role_remove');
}
public function testSummary() {
$this
->assertEquals('Remove user role', $this->action
->summary());
}
public function testRemoveExistingRoleNoSave() {
$account = $this
->prophesizeEntity(UserInterface::class);
$account
->hasRole('editor')
->willReturn(TRUE);
$account
->removeRole('editor')
->shouldBeCalledTimes(1);
$account
->save()
->shouldNotBeCalled();
$editor = $this
->prophesize(RoleInterface::class);
$editor
->id()
->willReturn('editor');
$this->action
->setContextValue('user', $account
->reveal())
->setContextValue('roles', [
$editor
->reveal(),
])
->execute();
$this
->assertEquals($this->action
->autoSaveContext(), [
'user',
], 'Action returns the user context name for auto saving.');
}
public function testRemoveNonExistingRole() {
$account = $this
->prophesizeEntity(UserInterface::class);
$account
->hasRole('editor')
->willReturn(FALSE);
$account
->removeRole('editor')
->shouldNotBeCalled();
$editor = $this
->prophesize(RoleInterface::class);
$editor
->id()
->willReturn('editor');
$this->action
->setContextValue('user', $account
->reveal())
->setContextValue('roles', [
$editor
->reveal(),
])
->execute();
$this
->assertNotEquals($this->action
->autoSaveContext(), [
'user',
], 'Action returns the user context name for auto saving.');
}
}