You are here

trait ProphesizePermissionsTrait in GraphQL 8.3

Trait to simulate user permissions.

Hierarchy

1 file declares its use of ProphesizePermissionsTrait
GraphQLTestBase.php in tests/src/Kernel/GraphQLTestBase.php

File

tests/src/Traits/ProphesizePermissionsTrait.php, line 11

Namespace

Drupal\Tests\graphql\Traits
View source
trait ProphesizePermissionsTrait {
  protected $accountProphecy;

  /**
   * Set the prophesized permissions.
   *
   * @return string[]
   *   The permissions to set on the prophesized user.
   */
  protected function userPermissions() {
    return [
      'execute graphql requests',
      'bypass graphql field security',
    ];
  }

  /**
   * Set the prophesized roles.
   *
   * @return string[]
   */
  protected function userRoles() {
    return [
      'anonymous',
    ];
  }

  /**
   * Bypass user access.
   */
  protected function injectAccount() {

    // Replace the current user with a prophecy that has the defined
    // permissions.
    $user = $this
      ->prophesize(AccountProxyInterface::class);
    $permissions = $this
      ->userPermissions();
    $user
      ->hasPermission(Argument::type('string'), Argument::cetera())
      ->will(function ($args) use ($permissions) {
      return in_array($args[0], $permissions);
    });
    $user
      ->getRoles(Argument::any())
      ->willReturn($this
      ->userRoles());
    $user
      ->id()
      ->willReturn(0);
    $user
      ->isAnonymous()
      ->willReturn(TRUE);
    $user
      ->isAuthenticated()
      ->willReturn(FALSE);
    $this->accountProphecy = $user;
    $this->container
      ->set('current_user', $user
      ->reveal());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProphesizePermissionsTrait::$accountProphecy protected property
ProphesizePermissionsTrait::injectAccount protected function Bypass user access.
ProphesizePermissionsTrait::userPermissions protected function Set the prophesized permissions. 3
ProphesizePermissionsTrait::userRoles protected function Set the prophesized roles.