You are here

class FieldPermissionsServiceTest in Field Permissions 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/FieldPermissionsServiceTest.php \Drupal\Tests\field_permissions\Unit\FieldPermissionsServiceTest

Tests the field permissions service.

@group field_permissions

@coversDefaultClass \Drupal\field_permissions\FieldPermissionsService

Hierarchy

Expanded class hierarchy of FieldPermissionsServiceTest

File

tests/src/Unit/FieldPermissionsServiceTest.php, line 25

Namespace

Drupal\Tests\field_permissions\Unit
View source
class FieldPermissionsServiceTest extends UnitTestCase {

  /**
   * Mock entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Mock permission type manager.
   *
   * @var \Drupal\field_permissions\Plugin\FieldPermissionType\Manager
   */
  protected $permissionTypeManager;

  /**
   * The field permissions service.
   *
   * @var \Drupal\field_permissions\FieldPermissionsServiceInterface
   */
  protected $fieldPermissionsService;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $entity_type_manager = $this
      ->prophesize(EntityTypeManagerInterface::class);
    $this->entityTypeManager = $entity_type_manager
      ->reveal();
    $permission_type_manager = $this
      ->prophesize(Manager::class);
    $this->permissionTypeManager = $permission_type_manager
      ->reveal();
    $this->fieldPermissionsService = new FieldPermissionsService($this->entityTypeManager, $this->permissionTypeManager);
  }

  /**
   * Test field access method.
   *
   * @covers ::getFieldAccess
   *
   * @dataProvider providerTestGetFieldAccess
   */
  public function testGetFieldAccess($operation, FieldItemListInterface $items, AccountInterface $account, FieldDefinitionInterface $field_definition, $expected_access) {
    $this
      ->assertEquals($expected_access, $this->fieldPermissionsService
      ->getFieldAccess($operation, $items, $account, $field_definition));
  }

  /**
   * Data provider for ::testGetFieldAccess.
   */
  public function providerTestGetFieldAccess() {
    $cases = [];
    $field_item_list = $this
      ->prophesize(FieldItemListInterface::class)
      ->reveal();

    // Administrator role.
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->getRoles()
      ->willReturn([
      'administrator',
    ]);
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);
    $storage = $this
      ->prophesize(FieldStorageConfigInterface::class);
    $storage
      ->getThirdPartySetting('field_permissions', 'permission_type', FieldPermissionTypeInterface::ACCESS_PUBLIC)
      ->willReturn('foo');
    $field_definition
      ->getFieldStorageDefinition()
      ->willReturn($storage
      ->reveal());
    $cases[] = [
      'view',
      $field_item_list,
      $account
        ->reveal(),
      $field_definition
        ->reveal(),
      TRUE,
    ];

    // No admin roles, but public access.
    $account = $this
      ->prophesize(AccountInterface::class);
    $account
      ->getRoles()
      ->willReturn([
      'blah',
    ]);
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);
    $storage = $this
      ->prophesize(FieldStorageConfigInterface::class);
    $storage
      ->getThirdPartySetting('field_permissions', 'permission_type', FieldPermissionTypeInterface::ACCESS_PUBLIC)
      ->willReturn(FieldPermissionTypeInterface::ACCESS_PUBLIC);
    $field_definition
      ->getFieldStorageDefinition()
      ->willReturn($storage
      ->reveal());
    $cases[] = [
      'view',
      $field_item_list,
      $account
        ->reveal(),
      $field_definition
        ->reveal(),
      TRUE,
    ];
    return $cases;
  }

  /**
   * Test the comment field method.
   *
   * @covers ::isCommentField
   */
  public function testIsCommentField() {
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);

    // Comment module not enabled.
    $container = new Container();
    \Drupal::setContainer($container);
    $this
      ->assertFalse($this->fieldPermissionsService
      ->isCommentField($field_definition
      ->reveal()));

    // Comment module enabled, no comment fields.
    $comment_manager = $this
      ->prophesize(CommentManagerInterface::class);
    $comment_manager
      ->getFields(Argument::any())
      ->willReturn([]);
    $container
      ->set('comment.manager', $comment_manager
      ->reveal());
    $this
      ->assertFalse($this->fieldPermissionsService
      ->isCommentField($field_definition
      ->reveal()));

    // Comment module enabled, no matching fields.
    $field_definition
      ->getName()
      ->willReturn('foo_field');
    $field_definition
      ->getTargetEntityTypeId()
      ->willReturn('foo');
    $comment_manager
      ->getFields('foo')
      ->willReturn([
      'bar_field' => 'bar',
    ]);
    $container
      ->set('comment.manager', $comment_manager
      ->reveal());
    $this
      ->assertFalse($this->fieldPermissionsService
      ->isCommentField($field_definition
      ->reveal()));

    // A comment field!
    $comment_manager
      ->getFields('foo')
      ->willReturn([
      'bar_field' => 'bar',
      'foo_field' => 'foo',
    ]);
    $container
      ->set('comment.manager', $comment_manager
      ->reveal());
    $this
      ->assertTrue($this->fieldPermissionsService
      ->isCommentField($field_definition
      ->reveal()));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldPermissionsServiceTest::$entityTypeManager protected property Mock entity type manager.
FieldPermissionsServiceTest::$fieldPermissionsService protected property The field permissions service.
FieldPermissionsServiceTest::$permissionTypeManager protected property Mock permission type manager.
FieldPermissionsServiceTest::providerTestGetFieldAccess public function Data provider for ::testGetFieldAccess.
FieldPermissionsServiceTest::setUp public function Overrides UnitTestCase::setUp
FieldPermissionsServiceTest::testGetFieldAccess public function Test field access method.
FieldPermissionsServiceTest::testIsCommentField public function Test the comment field method.
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.