3 namespace Drupal\Tests\layout_builder\Kernel;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityFieldManagerInterface;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Entity\FieldableEntityInterface;
9 use Drupal\Core\Extension\ModuleHandlerInterface;
10 use Drupal\Core\Field\FieldItemListInterface;
11 use Drupal\Core\Field\FormatterPluginManager;
12 use Drupal\Core\Plugin\Context\ContextDefinition;
13 use Drupal\Core\Session\AccountInterface;
14 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
15 use Drupal\layout_builder\Plugin\Block\FieldBlock;
16 use Prophecy\Prophecy\ProphecyInterface;
19 * @coversDefaultClass \Drupal\layout_builder\Plugin\Block\FieldBlock
22 class FieldBlockTest extends EntityKernelTestBase {
25 * Tests entity access.
27 * @covers ::blockAccess
28 * @dataProvider providerTestBlockAccessNotAllowed
30 public function testBlockAccessEntityNotAllowed($expected, $entity_access) {
31 $entity = $this->prophesize(FieldableEntityInterface::class);
32 $block = $this->getTestBlock($entity);
34 $account = $this->prophesize(AccountInterface::class);
35 $entity->access('view', $account->reveal(), TRUE)->willReturn($entity_access);
36 $entity->hasField()->shouldNotBeCalled();
38 $access = $block->access($account->reveal(), TRUE);
39 $this->assertSame($expected, $access->isAllowed());
43 * Provides test data for ::testBlockAccessEntityNotAllowed().
45 public function providerTestBlockAccessNotAllowed() {
47 $data['entity_forbidden'] = [
49 AccessResult::forbidden(),
51 $data['entity_neutral'] = [
53 AccessResult::neutral(),
59 * Tests unfieldable entity.
61 * @covers ::blockAccess
63 public function testBlockAccessEntityAllowedNotFieldable() {
64 $entity = $this->prophesize(EntityInterface::class);
65 $block = $this->getTestBlock($entity);
67 $account = $this->prophesize(AccountInterface::class);
68 $entity->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
70 $access = $block->access($account->reveal(), TRUE);
71 $this->assertSame(FALSE, $access->isAllowed());
75 * Tests fieldable entity without a particular field.
77 * @covers ::blockAccess
79 public function testBlockAccessEntityAllowedNoField() {
80 $entity = $this->prophesize(FieldableEntityInterface::class);
81 $block = $this->getTestBlock($entity);
83 $account = $this->prophesize(AccountInterface::class);
84 $entity->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
85 $entity->hasField('the_field_name')->willReturn(FALSE);
86 $entity->get('the_field_name')->shouldNotBeCalled();
88 $access = $block->access($account->reveal(), TRUE);
89 $this->assertSame(FALSE, $access->isAllowed());
95 * @covers ::blockAccess
96 * @dataProvider providerTestBlockAccessNotAllowed
98 public function testBlockAccessEntityAllowedFieldNotAllowed($expected, $field_access) {
99 $entity = $this->prophesize(FieldableEntityInterface::class);
100 $block = $this->getTestBlock($entity);
102 $account = $this->prophesize(AccountInterface::class);
103 $entity->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
104 $entity->hasField('the_field_name')->willReturn(TRUE);
105 $field = $this->prophesize(FieldItemListInterface::class);
106 $entity->get('the_field_name')->willReturn($field->reveal());
108 $field->access('view', $account->reveal(), TRUE)->willReturn($field_access);
109 $field->isEmpty()->shouldNotBeCalled();
111 $access = $block->access($account->reveal(), TRUE);
112 $this->assertSame($expected, $access->isAllowed());
116 * Tests populated vs empty build.
118 * @covers ::blockAccess
120 * @dataProvider providerTestBlockAccessEntityAllowedFieldHasValue
122 public function testBlockAccessEntityAllowedFieldHasValue($expected, $is_empty) {
123 $entity = $this->prophesize(FieldableEntityInterface::class);
124 $block = $this->getTestBlock($entity);
126 $account = $this->prophesize(AccountInterface::class);
127 $entity->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
128 $entity->hasField('the_field_name')->willReturn(TRUE);
129 $field = $this->prophesize(FieldItemListInterface::class);
130 $entity->get('the_field_name')->willReturn($field->reveal());
132 $field->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
133 $field->isEmpty()->willReturn($is_empty)->shouldBeCalled();
135 $access = $block->access($account->reveal(), TRUE);
136 $this->assertSame($expected, $access->isAllowed());
140 * Provides test data for ::testBlockAccessEntityAllowedFieldHasValue().
142 public function providerTestBlockAccessEntityAllowedFieldHasValue() {
148 $data['populated'] = [
156 * Instantiates a block for testing.
158 * @param \Prophecy\Prophecy\ProphecyInterface $entity_prophecy
159 * An entity prophecy for use as an entity context value.
160 * @param array $configuration
161 * A configuration array containing information about the plugin instance.
162 * @param array $plugin_definition
163 * The plugin implementation definition.
165 * @return \Drupal\layout_builder\Plugin\Block\FieldBlock
168 protected function getTestBlock(ProphecyInterface $entity_prophecy, array $configuration = [], array $plugin_definition = []) {
169 $entity_prophecy->getCacheContexts()->willReturn([]);
170 $entity_prophecy->getCacheTags()->willReturn([]);
171 $entity_prophecy->getCacheMaxAge()->willReturn(0);
173 $plugin_definition += [
174 'provider' => 'test',
175 'default_formatter' => '',
176 'category' => 'Test',
177 'admin_label' => 'Test Block',
178 'bundles' => ['entity_test'],
180 'entity' => new ContextDefinition('entity:entity_test', 'Test', TRUE),
183 $entity_field_manager = $this->prophesize(EntityFieldManagerInterface::class);
184 $formatter_manager = $this->prophesize(FormatterPluginManager::class);
185 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
187 $block = new FieldBlock(
189 'field_block:entity_test:entity_test:the_field_name',
191 $entity_field_manager->reveal(),
192 $formatter_manager->reveal(),
193 $module_handler->reveal()
195 $block->setContextValue('entity', $entity_prophecy->reveal());