You are here

public function PrivateAccessTest::providerTestHasFieldAccess in Field Permissions 8

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

Data provider for ::testHasFieldAccess.

File

tests/src/Unit/Plugin/FieldPermissionType/PrivateAccessTest.php, line 54

Class

PrivateAccessTest
Tests the private access plugin.

Namespace

Drupal\Tests\field_permissions\Unit\Plugin\FieldPermissionType

Code

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

  // Has 'access private fields' permission.
  $account = $this
    ->prophesize(AccountInterface::class);
  $account
    ->hasPermission('access private fields')
    ->willReturn(TRUE);
  $entity = $this
    ->prophesize(EntityInterface::class)
    ->reveal();
  $cases[] = [
    'view',
    $entity,
    $account
      ->reveal(),
    TRUE,
  ];
  $cases[] = [
    'edit',
    $entity,
    $account
      ->reveal(),
    TRUE,
  ];

  // New entities always grant permission.
  $account = $this
    ->prophesize(AccountInterface::class)
    ->reveal();
  $entity = $this
    ->prophesize(EntityInterface::class);
  $entity
    ->isNew()
    ->willReturn(TRUE);
  $cases[] = [
    'view',
    $entity
      ->reveal(),
    $account,
    TRUE,
  ];
  $cases[] = [
    'edit',
    $entity
      ->reveal(),
    $account,
    TRUE,
  ];

  // Special handling for user entities.
  // Account same as user entity.
  $account = $this
    ->prophesize(AccountInterface::class);
  $account
    ->id()
    ->willReturn(42);
  $account
    ->hasPermission('access private fields')
    ->willReturn(FALSE);
  $entity = $this
    ->prophesize(UserInterface::class);
  $entity
    ->id()
    ->willReturn(42);
  $entity
    ->isNew()
    ->willReturn(FALSE);
  $cases[] = [
    'view',
    $entity
      ->reveal(),
    $account
      ->reveal(),
    TRUE,
  ];
  $cases[] = [
    'edit',
    $entity
      ->reveal(),
    $account
      ->reveal(),
    TRUE,
  ];

  // Different user than account will deny access.
  $account = $this
    ->prophesize(AccountInterface::class);
  $account
    ->id()
    ->willReturn(27);
  $account
    ->hasPermission('access private fields')
    ->willReturn(FALSE);
  $entity = $this
    ->prophesize(UserInterface::class);
  $entity
    ->id()
    ->willReturn(42);
  $entity
    ->isNew()
    ->willReturn(FALSE);
  $cases[] = [
    'view',
    $entity
      ->reveal(),
    $account
      ->reveal(),
    FALSE,
  ];
  $cases[] = [
    'edit',
    $entity
      ->reveal(),
    $account
      ->reveal(),
    FALSE,
  ];

  // EntityOwnerInterface entities with access.
  $account = $this
    ->prophesize(AccountInterface::class);
  $account
    ->id()
    ->willReturn(42);
  $account
    ->hasPermission('access private fields')
    ->willReturn(FALSE);
  $entity = $this
    ->prophesize(EntityInterface::class)
    ->willImplement(EntityOwnerInterface::class);
  $entity
    ->getOwnerId()
    ->willReturn(42);
  $entity
    ->isNew()
    ->willReturn(FALSE);
  $cases[] = [
    'view',
    $entity
      ->reveal(),
    $account
      ->reveal(),
    TRUE,
  ];
  $cases[] = [
    'edit',
    $entity
      ->reveal(),
    $account
      ->reveal(),
    TRUE,
  ];

  // EntityOwnerInterface entities without access.
  $account = $this
    ->prophesize(AccountInterface::class);
  $account
    ->id()
    ->willReturn(42);
  $account
    ->hasPermission('access private fields')
    ->willReturn(FALSE);
  $entity = $this
    ->prophesize(EntityInterface::class)
    ->willImplement(EntityOwnerInterface::class);
  $entity
    ->getOwnerId()
    ->willReturn(27);
  $entity
    ->isNew()
    ->willReturn(FALSE);
  $cases[] = [
    'view',
    $entity
      ->reveal(),
    $account
      ->reveal(),
    FALSE,
  ];
  $cases[] = [
    'edit',
    $entity
      ->reveal(),
    $account
      ->reveal(),
    FALSE,
  ];

  // Non-user, or none owner interface entity should always have access.
  $account = $this
    ->prophesize(AccountInterface::class)
    ->reveal();
  $entity = $this
    ->prophesize(EntityInterface::class)
    ->reveal();
  $cases[] = [
    'view',
    $entity,
    $account,
    TRUE,
  ];
  $cases[] = [
    'edit',
    $entity,
    $account,
    TRUE,
  ];
  return $cases;
}