You are here

public function UserRoleAddTest::testAddExistingAndNonexistentRole in Rules 8.3

Tests adding of one existing and one nonexistent role to user.

@covers ::execute

File

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

Class

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

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

public function testAddExistingAndNonexistentRole() {

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

  // We expect only one call of the 'addRole' method.
  $account
    ->addRole('editor')
    ->shouldBeCalledTimes(1);

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

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