function UserRoleRemoveTest::testRemoveExistingRoleNoSave

Tests removing role from user. User should not be saved.

@covers ::execute

File

tests/src/Unit/Integration/RulesAction/UserRoleRemoveTest.php, line 45

Class

UserRoleRemoveTest
@coversDefaultClass \Drupal\rules\Plugin\RulesAction\UserRoleRemove @group RulesAction

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

public function testRemoveExistingRoleNoSave() {
    // Set-up a mock user with role 'editor'.
    $account = $this->prophesizeEntity(UserInterface::class);
    $account->hasRole('editor')
        ->willReturn(TRUE);
    $account->removeRole('editor')
        ->shouldBeCalledTimes(1);
    // We do not expect call of the 'save' method because user should be
    // auto-saved later.
    $account->save()
        ->shouldNotBeCalled();
    // Mock the 'editor' user role.
    $editor = $this->prophesize(RoleInterface::class);
    $editor->id()
        ->willReturn('editor');
    // Test removing of one role.
    $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.');
}