function FieldBlockTest::testBlockAccessEntityAllowedFieldHasValue

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php \Drupal\Tests\layout_builder\Kernel\FieldBlockTest::testBlockAccessEntityAllowedFieldHasValue()
  2. 8.9.x core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php \Drupal\Tests\layout_builder\Kernel\FieldBlockTest::testBlockAccessEntityAllowedFieldHasValue()
  3. 11.x 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 157

Class

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

Namespace

Drupal\Tests\layout_builder\Kernel

Code

public function testBlockAccessEntityAllowedFieldHasValue($expected, $is_empty, $default_value) : void {
    $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);
    $field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $field->getFieldDefinition()
        ->willReturn($field_definition->reveal());
    $field_definition->getDefaultValue($entity->reveal())
        ->willReturn($default_value);
    $field_definition->getType()
        ->willReturn('not_an_image');
    $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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.