function UserRoleAddTest::testAddThreeRoles

Tests adding of three roles to user.

@covers ::execute

File

tests/src/Unit/Integration/RulesAction/UserRoleAddTest.php, line 74

Class

UserRoleAddTest
@coversDefaultClass \Drupal\rules\Plugin\RulesAction\UserRoleAdd @group RulesAction

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

public function testAddThreeRoles() {
    // Set-up a mock user.
    $account = $this->prophesizeEntity(UserInterface::class);
    // Mock hasRole.
    $account->hasRole('manager')
        ->willReturn(FALSE)
        ->shouldBeCalledTimes(1);
    $account->hasRole('editor')
        ->willReturn(FALSE)
        ->shouldBeCalledTimes(1);
    $account->hasRole('administrator')
        ->willReturn(FALSE)
        ->shouldBeCalledTimes(1);
    // Mock addRole.
    $account->addRole('manager')
        ->shouldBeCalledTimes(1);
    $account->addRole('editor')
        ->shouldBeCalledTimes(1);
    $account->addRole('administrator')
        ->shouldBeCalledTimes(1);
    // Mock user roles.
    $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');
    // Test adding of three roles.
    $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.');
}