You are here

class CustomAccessTest in Field Permissions 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/Plugin/FieldPermissionType/CustomAccessTest.php \Drupal\Tests\field_permissions\Unit\Plugin\FieldPermissionType\CustomAccessTest

Tests for the custom access permission type plugin.

@coversDefaultClass \Drupal\field_permissions\Plugin\FieldPermissionType\CustomAccess

@group field_permissions

Hierarchy

Expanded class hierarchy of CustomAccessTest

File

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

Namespace

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

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

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $storage = $this
      ->prophesize(FieldStorageConfigInterface::class);
    $storage
      ->getName()
      ->willReturn('foo_field');
    $this->plugin = new CustomAccess([], 'custom', [], $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));
  }

  /**
   * Test an invalid operation.
   *
   * @covers ::hasFieldAccess
   */
  public function testInvalidOperation() {

    // Edit|view access allowed.
    $account = $this
      ->prophesize(AccountInterface::class);
    $entity = $this
      ->prophesize(EntityInterface::class);
    $this
      ->expectException(\AssertionError::class, 'The operation is either "edit" or "view", "bad operation" given instead.');
    $this->plugin
      ->hasFieldAccess('bad operation', $entity
      ->reveal(), $account
      ->reveal());
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
CustomAccessTest::$plugin protected property The custom access plugin.
CustomAccessTest::providerTestHasFieldAccess public function Data provider for ::testHasFieldAccess.
CustomAccessTest::setUp public function Overrides UnitTestCase::setUp
CustomAccessTest::testHasFieldAccess public function Test for `hasFieldAccess`.
CustomAccessTest::testInvalidOperation public function Test an invalid operation.
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.
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.