You are here

class PrivateAccessTest 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

Tests the private access plugin.

@coversDefaultClass \Drupal\field_permissions\Plugin\FieldPermissionType\PrivateAccess

@group field_permissions

Hierarchy

Expanded class hierarchy of PrivateAccessTest

File

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

Namespace

Drupal\Tests\field_permissions\Unit\Plugin\FieldPermissionType
View source
class PrivateAccessTest extends UnitTestCase {

  /**
   * The private access plugin.
   *
   * @var \Drupal\field_permissions\Plugin\FieldPermissionType\PrivateAccess
   */
  protected $plugin;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $storage = $this
      ->prophesize(FieldStorageConfigInterface::class);
    $this->plugin = new PrivateAccess([], 'private', [], $storage
      ->reveal());
  }

  /**
   * Test for `hasFieldAccess`.
   *
   * @covers ::hasFieldAccess
   *
   * @dataProvider providerTestHasFieldAccess
   */
  public function testHasFieldAccess($operation, EntityInterface $entity, AccountInterface $account, $access) {
    $this
      ->assertEquals($access, $this->plugin
      ->hasFieldAccess($operation, $entity, $account));
  }

  /**
   * Data provider for ::testHasFieldAccess.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
PrivateAccessTest::$plugin protected property The private access plugin.
PrivateAccessTest::providerTestHasFieldAccess public function Data provider for ::testHasFieldAccess.
PrivateAccessTest::setUp public function Overrides UnitTestCase::setUp
PrivateAccessTest::testHasFieldAccess public function Test for `hasFieldAccess`.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.