You are here

public function CustomAccessTest::providerTestHasFieldAccess in Field Permissions 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/Plugin/FieldPermissionType/CustomAccessTest.php \Drupal\Tests\field_permissions\Unit\Plugin\FieldPermissionType\CustomAccessTest::providerTestHasFieldAccess()

Data provider for ::testHasFieldAccess.

File

tests/src/Unit/Plugin/FieldPermissionType/CustomAccessTest.php, line 68

Class

CustomAccessTest
Tests for the custom access permission type plugin.

Namespace

Drupal\Tests\field_permissions\Unit\Plugin\FieldPermissionType

Code

public function providerTestHasFieldAccess() {
  $cases = [];

  // Create access allowed.
  $account = $this
    ->prophesize(AccountInterface::class);
  $account
    ->hasPermission('create foo_field')
    ->willReturn(TRUE);
  $entity = $this
    ->prophesize(EntityInterface::class);
  $entity
    ->isNew()
    ->willReturn(TRUE);
  $cases[] = [
    'edit',
    $entity
      ->reveal(),
    $account
      ->reveal(),
    TRUE,
  ];

  // Create access denied.
  $account = $this
    ->prophesize(AccountInterface::class);
  $account
    ->hasPermission('create foo_field')
    ->willReturn(FALSE);
  $cases[] = [
    'edit',
    $entity
      ->reveal(),
    $account
      ->reveal(),
    FALSE,
  ];

  // Add edit and view.
  foreach ([
    'edit',
    'view',
  ] as $operation) {

    // Edit|view access allowed.
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission($operation . ' foo_field')
      ->willReturn(TRUE);
    $entity = $this
      ->prophesize(EntityInterface::class);
    $cases[] = [
      $operation,
      $entity
        ->reveal(),
      $account
        ->reveal(),
      TRUE,
    ];

    // Edit|view access denied.
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission($operation . ' foo_field')
      ->willReturn(FALSE);
    $entity = $this
      ->prophesize(EntityInterface::class);
    $cases[] = [
      $operation,
      $entity
        ->reveal(),
      $account
        ->reveal(),
      FALSE,
    ];

    // User entity, edit|view own allowed.
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission($operation . ' foo_field')
      ->willReturn(FALSE);
    $account
      ->hasPermission($operation . ' own foo_field')
      ->willReturn(TRUE);
    $account
      ->id()
      ->willReturn(42);
    $entity = $this
      ->prophesize(UserInterface::class);
    $entity
      ->id()
      ->willReturn(42);
    $entity
      ->isNew()
      ->willReturn(FALSE);
    $cases[] = [
      $operation,
      $entity
        ->reveal(),
      $account
        ->reveal(),
      TRUE,
    ];

    // User entity, edit|view own denied.
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission($operation . ' foo_field')
      ->willReturn(FALSE);
    $account
      ->hasPermission($operation . ' own foo_field')
      ->willReturn(FALSE);
    $account
      ->id()
      ->willReturn(42);
    $entity = $this
      ->prophesize(UserInterface::class);
    $entity
      ->id()
      ->willReturn(42);
    $entity
      ->isNew()
      ->willReturn(FALSE);
    $cases[] = [
      $operation,
      $entity
        ->reveal(),
      $account
        ->reveal(),
      FALSE,
    ];

    // User entity, edit|view own allowed, non-matching entity.
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission($operation . ' foo_field')
      ->willReturn(FALSE);
    $account
      ->hasPermission($operation . ' own foo_field')
      ->willReturn(TRUE);
    $account
      ->id()
      ->willReturn(42);
    $entity = $this
      ->prophesize(UserInterface::class);
    $entity
      ->id()
      ->willReturn(27);
    $entity
      ->isNew()
      ->willReturn(FALSE);
    $cases[] = [
      $operation,
      $entity
        ->reveal(),
      $account
        ->reveal(),
      FALSE,
    ];

    // Entity implementing EntityOwnerInterface, edit|view own allowed.
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission($operation . ' foo_field')
      ->willReturn(FALSE);
    $account
      ->hasPermission($operation . ' own foo_field')
      ->willReturn(TRUE);
    $account
      ->id()
      ->willReturn(42);
    $entity = $this
      ->prophesize(EntityInterface::class);
    $entity
      ->willImplement(EntityOwnerInterface::class);
    $entity
      ->getOwnerId()
      ->willReturn(42);
    $entity
      ->isNew()
      ->willReturn(FALSE);
    $cases[] = [
      $operation,
      $entity
        ->reveal(),
      $account
        ->reveal(),
      TRUE,
    ];

    // Entity implementing EntityOwnerInterface, edit|view own denied.
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission($operation . ' foo_field')
      ->willReturn(FALSE);
    $account
      ->hasPermission($operation . ' own foo_field')
      ->willReturn(FALSE);
    $account
      ->id()
      ->willReturn(42);
    $entity = $this
      ->prophesize(EntityInterface::class);
    $entity
      ->willImplement(EntityOwnerInterface::class);
    $entity
      ->getOwnerId()
      ->willReturn(42);
    $entity
      ->isNew()
      ->willReturn(FALSE);
    $cases[] = [
      $operation,
      $entity
        ->reveal(),
      $account
        ->reveal(),
      FALSE,
    ];

    // Entity implementing EntityOwnerInterface, edit|view own allowed, but
    // non-matching entity owner.
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->hasPermission($operation . ' foo_field')
      ->willReturn(FALSE);
    $account
      ->hasPermission($operation . ' own foo_field')
      ->willReturn(TRUE);
    $account
      ->id()
      ->willReturn(27);
    $entity = $this
      ->prophesize(EntityInterface::class);
    $entity
      ->willImplement(EntityOwnerInterface::class);
    $entity
      ->getOwnerId()
      ->willReturn(42);
    $entity
      ->isNew()
      ->willReturn(FALSE);
    $cases[] = [
      $operation,
      $entity
        ->reveal(),
      $account
        ->reveal(),
      FALSE,
    ];
  }
  return $cases;
}