You are here

public function FieldPermissionsServiceTest::testIsCommentField in Field Permissions 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/FieldPermissionsServiceTest.php \Drupal\Tests\field_permissions\Unit\FieldPermissionsServiceTest::testIsCommentField()

Test the comment field method.

@covers ::isCommentField

File

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

Class

FieldPermissionsServiceTest
Tests the field permissions service.

Namespace

Drupal\Tests\field_permissions\Unit

Code

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()));
}