You are here

public function FieldBlockTest::testBlockAccessEntityAllowedFieldHasValue in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php \Drupal\Tests\layout_builder\Kernel\FieldBlockTest::testBlockAccessEntityAllowedFieldHasValue()

Tests populated vs empty build.

@covers ::blockAccess @covers ::build @dataProvider providerTestBlockAccessEntityAllowedFieldHasValue

File

core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php, line 155

Class

FieldBlockTest
@coversDefaultClass \Drupal\layout_builder\Plugin\Block\FieldBlock @group Field

Namespace

Drupal\Tests\layout_builder\Kernel

Code

public function testBlockAccessEntityAllowedFieldHasValue($expected, $is_empty) {
  $entity = $this
    ->prophesize(FieldableEntityInterface::class);
  $block = $this
    ->getTestBlock($entity);
  $account = $this
    ->prophesize(AccountInterface::class);
  $entity
    ->access('view', $account
    ->reveal(), TRUE)
    ->willReturn(AccessResult::allowed());
  $entity
    ->hasField('the_field_name')
    ->willReturn(TRUE);
  $field = $this
    ->prophesize(FieldItemListInterface::class);
  $entity
    ->get('the_field_name')
    ->willReturn($field
    ->reveal());
  $field
    ->access('view', $account
    ->reveal(), TRUE)
    ->willReturn(AccessResult::allowed());
  $field
    ->isEmpty()
    ->willReturn($is_empty)
    ->shouldBeCalled();
  $access = $block
    ->access($account
    ->reveal(), TRUE);
  $this
    ->assertSame($expected, $access
    ->isAllowed());
}