View source  
  <?php
namespace Drupal\Tests\rules\Unit\Integration\Condition;
use Drupal\Tests\rules\Unit\Integration\RulesEntityIntegrationTestBase;
use Drupal\rules\Exception\InvalidArgumentException;
use Drupal\user\RoleInterface;
use Drupal\user\UserInterface;
class UserHasRoleTest extends RulesEntityIntegrationTestBase {
  
  protected $condition;
  
  protected function setUp() : void {
    parent::setUp();
    $this
      ->enableModule('user');
    $this->condition = $this->conditionManager
      ->createInstance('rules_user_has_role');
  }
  
  public function testConditionEvaluation() {
    
    $account = $this
      ->prophesizeEntity(UserInterface::class);
    $account
      ->getRoles()
      ->willReturn([
      'authenticated',
      'editor',
    ])
      ->shouldBeCalledTimes(7);
    $this->condition
      ->setContextValue('user', $account
      ->reveal());
    $authenticated = $this
      ->prophesize(RoleInterface::class);
    $authenticated
      ->id()
      ->willReturn('authenticated');
    $editor = $this
      ->prophesize(RoleInterface::class);
    $editor
      ->id()
      ->willReturn('editor');
    $administrator = $this
      ->prophesize(RoleInterface::class);
    $administrator
      ->id()
      ->willReturn('administrator');
    
    $this->condition
      ->setContextValue('roles', [
      $authenticated
        ->reveal(),
      $editor
        ->reveal(),
    ]);
    $this
      ->assertTrue($this->condition
      ->evaluate());
    
    $this->condition
      ->setContextValue('roles', [
      $authenticated
        ->reveal(),
      $administrator
        ->reveal(),
    ]);
    $this
      ->assertFalse($this->condition
      ->evaluate());
    
    $this->condition
      ->setContextValue('roles', [
      $authenticated
        ->reveal(),
    ]);
    $this
      ->assertTrue($this->condition
      ->evaluate());
    
    $this->condition
      ->setContextValue('roles', [
      $administrator
        ->reveal(),
    ]);
    $this
      ->assertFalse($this->condition
      ->evaluate());
    
    $this->condition
      ->setContextValue('roles', [
      $authenticated
        ->reveal(),
    ]);
    $this->condition
      ->setContextValue('operation', 'OR');
    $this
      ->assertTrue($this->condition
      ->evaluate());
    
    $this->condition
      ->setContextValue('roles', [
      $authenticated
        ->reveal(),
      $administrator
        ->reveal(),
    ]);
    $this->condition
      ->setContextValue('operation', 'OR');
    $this
      ->assertTrue($this->condition
      ->evaluate());
    
    $this->condition
      ->setContextValue('roles', [
      $administrator
        ->reveal(),
    ]);
    $this->condition
      ->setContextValue('operation', 'OR');
    $this
      ->assertFalse($this->condition
      ->evaluate());
  }
  
  public function testInvalidOperationException() {
    
    $this
      ->expectException(InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('Either use "AND" or "OR". Leave empty for default "AND" behavior.');
    
    $account = $this
      ->prophesizeEntity(UserInterface::class);
    $account
      ->getRoles()
      ->willReturn([
      'authenticated',
      'editor',
    ]);
    $this->condition
      ->setContextValue('user', $account
      ->reveal());
    $authenticated = $this
      ->prophesize(RoleInterface::class);
    $authenticated
      ->id()
      ->willReturn('authenticated');
    
    $this->condition
      ->setContextValue('roles', [
      $authenticated
        ->reveal(),
    ]);
    $this->condition
      ->setContextValue('operation', 'INVALID');
    $this->condition
      ->evaluate();
  }
}