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\FieldDefinitionInterface;
11 use Drupal\Core\Field\FieldItemListInterface;
12 use Drupal\Core\Field\FormatterPluginManager;
13 use Drupal\Core\Plugin\Context\EntityContextDefinition;
14 use Drupal\Core\Session\AccountInterface;
15 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
16 use Drupal\layout_builder\Plugin\Block\FieldBlock;
17 use Prophecy\Argument;
18 use Prophecy\Promise\PromiseInterface;
19 use Prophecy\Promise\ReturnPromise;
20 use Prophecy\Promise\ThrowPromise;
21 use Prophecy\Prophecy\ProphecyInterface;
22 use Psr\Log\LoggerInterface;
25 * @coversDefaultClass \Drupal\layout_builder\Plugin\Block\FieldBlock
28 class FieldBlockTest extends EntityKernelTestBase {
31 * The entity field manager.
33 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
35 protected $entityFieldManager;
40 * @var \Psr\Log\LoggerInterface
47 protected function setUp() {
50 $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class);
51 $this->logger = $this->prophesize(LoggerInterface::class);
55 * Tests entity access.
57 * @covers ::blockAccess
58 * @dataProvider providerTestBlockAccessNotAllowed
60 public function testBlockAccessEntityNotAllowed($expected, $entity_access) {
61 $entity = $this->prophesize(FieldableEntityInterface::class);
62 $block = $this->getTestBlock($entity);
64 $account = $this->prophesize(AccountInterface::class);
65 $entity->access('view', $account->reveal(), TRUE)->willReturn($entity_access);
66 $entity->hasField()->shouldNotBeCalled();
68 $access = $block->access($account->reveal(), TRUE);
69 $this->assertSame($expected, $access->isAllowed());
73 * Provides test data for ::testBlockAccessEntityNotAllowed().
75 public function providerTestBlockAccessNotAllowed() {
77 $data['entity_forbidden'] = [
79 AccessResult::forbidden(),
81 $data['entity_neutral'] = [
83 AccessResult::neutral(),
89 * Tests unfieldable entity.
91 * @covers ::blockAccess
93 public function testBlockAccessEntityAllowedNotFieldable() {
94 $entity = $this->prophesize(EntityInterface::class);
95 $block = $this->getTestBlock($entity);
97 $account = $this->prophesize(AccountInterface::class);
98 $entity->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
100 $access = $block->access($account->reveal(), TRUE);
101 $this->assertSame(FALSE, $access->isAllowed());
105 * Tests fieldable entity without a particular field.
107 * @covers ::blockAccess
109 public function testBlockAccessEntityAllowedNoField() {
110 $entity = $this->prophesize(FieldableEntityInterface::class);
111 $block = $this->getTestBlock($entity);
113 $account = $this->prophesize(AccountInterface::class);
114 $entity->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
115 $entity->hasField('the_field_name')->willReturn(FALSE);
116 $entity->get('the_field_name')->shouldNotBeCalled();
118 $access = $block->access($account->reveal(), TRUE);
119 $this->assertSame(FALSE, $access->isAllowed());
123 * Tests field access.
125 * @covers ::blockAccess
126 * @dataProvider providerTestBlockAccessNotAllowed
128 public function testBlockAccessEntityAllowedFieldNotAllowed($expected, $field_access) {
129 $entity = $this->prophesize(FieldableEntityInterface::class);
130 $block = $this->getTestBlock($entity);
132 $account = $this->prophesize(AccountInterface::class);
133 $entity->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
134 $entity->hasField('the_field_name')->willReturn(TRUE);
135 $field = $this->prophesize(FieldItemListInterface::class);
136 $entity->get('the_field_name')->willReturn($field->reveal());
138 $field->access('view', $account->reveal(), TRUE)->willReturn($field_access);
139 $field->isEmpty()->shouldNotBeCalled();
141 $access = $block->access($account->reveal(), TRUE);
142 $this->assertSame($expected, $access->isAllowed());
146 * Tests populated vs empty build.
148 * @covers ::blockAccess
150 * @dataProvider providerTestBlockAccessEntityAllowedFieldHasValue
152 public function testBlockAccessEntityAllowedFieldHasValue($expected, $is_empty) {
153 $entity = $this->prophesize(FieldableEntityInterface::class);
154 $block = $this->getTestBlock($entity);
156 $account = $this->prophesize(AccountInterface::class);
157 $entity->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
158 $entity->hasField('the_field_name')->willReturn(TRUE);
159 $field = $this->prophesize(FieldItemListInterface::class);
160 $entity->get('the_field_name')->willReturn($field->reveal());
162 $field->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
163 $field->isEmpty()->willReturn($is_empty)->shouldBeCalled();
165 $access = $block->access($account->reveal(), TRUE);
166 $this->assertSame($expected, $access->isAllowed());
170 * Provides test data for ::testBlockAccessEntityAllowedFieldHasValue().
172 public function providerTestBlockAccessEntityAllowedFieldHasValue() {
178 $data['populated'] = [
186 * Instantiates a block for testing.
188 * @param \Prophecy\Prophecy\ProphecyInterface $entity_prophecy
189 * An entity prophecy for use as an entity context value.
190 * @param array $configuration
191 * A configuration array containing information about the plugin instance.
192 * @param array $plugin_definition
193 * The plugin implementation definition.
195 * @return \Drupal\layout_builder\Plugin\Block\FieldBlock
198 protected function getTestBlock(ProphecyInterface $entity_prophecy, array $configuration = [], array $plugin_definition = []) {
199 $entity_prophecy->getCacheContexts()->willReturn([]);
200 $entity_prophecy->getCacheTags()->willReturn([]);
201 $entity_prophecy->getCacheMaxAge()->willReturn(0);
203 $plugin_definition += [
204 'provider' => 'test',
205 'default_formatter' => '',
206 'category' => 'Test',
207 'admin_label' => 'Test Block',
208 'bundles' => ['entity_test'],
210 'entity' => EntityContextDefinition::fromEntityTypeId('entity_test')->setLabel('Test'),
213 $formatter_manager = $this->prophesize(FormatterPluginManager::class);
214 $module_handler = $this->prophesize(ModuleHandlerInterface::class);
216 $block = new FieldBlock(
218 'field_block:entity_test:entity_test:the_field_name',
220 $this->entityFieldManager->reveal(),
221 $formatter_manager->reveal(),
222 $module_handler->reveal(),
223 $this->logger->reveal()
225 $block->setContextValue('entity', $entity_prophecy->reveal());
231 * @dataProvider providerTestBuild
233 public function testBuild(PromiseInterface $promise, $expected_markup, $log_message = '', $log_arguments = []) {
234 $entity = $this->prophesize(FieldableEntityInterface::class);
235 $field = $this->prophesize(FieldItemListInterface::class);
236 $entity->get('the_field_name')->willReturn($field->reveal());
237 $field->view(Argument::type('array'))->will($promise);
239 $field_definition = $this->prophesize(FieldDefinitionInterface::class);
240 $field_definition->getLabel()->willReturn('The Field Label');
241 $this->entityFieldManager->getFieldDefinitions('entity_test', 'entity_test')->willReturn(['the_field_name' => $field_definition]);
244 $this->logger->warning($log_message, $log_arguments)->shouldBeCalled();
247 $this->logger->warning(Argument::cetera())->shouldNotBeCalled();
250 $block = $this->getTestBlock($entity);
258 if ($expected_markup) {
259 $expected['content']['#markup'] = $expected_markup;
262 $actual = $block->build();
263 $this->assertEquals($expected, $actual);
267 * Provides test data for ::testBuild().
269 public function providerTestBuild() {
272 new ReturnPromise([['content' => ['#markup' => 'The field value']]]),
275 $data['empty array'] = [
276 new ReturnPromise([[]]),
279 $data['exception'] = [
280 new ThrowPromise(new \Exception('The exception message')),
282 'The field "%field" failed to render with the error of "%error".',
283 ['%field' => 'the_field_name', '%error' => 'The exception message'],