function FieldBlockTest::testBlockAccessEntityAllowedFieldHasValue

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

Class

FieldBlockTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21layout_builder%21src%21Plugin%21Block%21FieldBlock.php/class/FieldBlock/9" title="Provides a block that renders a field from an entity." class="local">\Drupal\layout_builder\Plugin\Block\FieldBlock</a> @group Field

Namespace

Drupal\Tests\layout_builder\Kernel

Code

public function testBlockAccessEntityAllowedFieldHasValue($expected, $is_empty, $default_value) {
    $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.