You are here

public function UserRoleAddTest::testAddExistingRole in Rules 8.3

Tests adding of existing role to user.

@covers ::execute

File

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

Class

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

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

public function testAddExistingRole() {

  // Set-up a mock user with role 'administrator'.
  $account = $this
    ->prophesizeEntity(UserInterface::class);
  $account
    ->hasRole('administrator')
    ->willReturn(TRUE);

  // We do not expect a call of the 'addRole' method.
  $account
    ->addRole(Argument::any())
    ->shouldNotBeCalled();

  // Mock the 'administrator' user role.
  $administrator = $this
    ->prophesize(RoleInterface::class);
  $administrator
    ->id()
    ->willReturn('administrator');

  // Test adding one role.
  $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.');
}