You are here

public function UserRoleAddTest::testAddOneRoleNoSave in Rules 8.3

Tests adding of one role to user. User should not be saved.

@covers ::execute

File

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

Class

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

Namespace

Drupal\Tests\rules\Unit\Integration\RulesAction

Code

public function testAddOneRoleNoSave() {

  // Set-up a mock user.
  $account = $this
    ->prophesizeEntity(UserInterface::class);
  $account
    ->hasRole('administrator')
    ->willReturn(FALSE);
  $account
    ->addRole('administrator')
    ->shouldBeCalledTimes(1);

  // We do not expect to call the 'save' method because the user should be
  // auto-saved later.
  $account
    ->save()
    ->shouldNotBeCalled();

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

  // Test adding of one role.
  $this->action
    ->setContextValue('user', $account
    ->reveal())
    ->setContextValue('roles', [
    $administrator
      ->reveal(),
  ])
    ->execute();
  $this
    ->assertEquals($this->action
    ->autoSaveContext(), [
    'user',
  ], 'Action returns the user context name for auto saving.');
}