8c529cc1e1b93111dc82d2e1c770470714473c71
[yaffs-website] / web / core / modules / layout_builder / tests / src / Kernel / FieldBlockTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Kernel;
4
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;
17
18 /**
19  * @coversDefaultClass \Drupal\layout_builder\Plugin\Block\FieldBlock
20  * @group Field
21  */
22 class FieldBlockTest extends EntityKernelTestBase {
23
24   /**
25    * Tests entity access.
26    *
27    * @covers ::blockAccess
28    * @dataProvider providerTestBlockAccessNotAllowed
29    */
30   public function testBlockAccessEntityNotAllowed($expected, $entity_access) {
31     $entity = $this->prophesize(FieldableEntityInterface::class);
32     $block = $this->getTestBlock($entity);
33
34     $account = $this->prophesize(AccountInterface::class);
35     $entity->access('view', $account->reveal(), TRUE)->willReturn($entity_access);
36     $entity->hasField()->shouldNotBeCalled();
37
38     $access = $block->access($account->reveal(), TRUE);
39     $this->assertSame($expected, $access->isAllowed());
40   }
41
42   /**
43    * Provides test data for ::testBlockAccessEntityNotAllowed().
44    */
45   public function providerTestBlockAccessNotAllowed() {
46     $data = [];
47     $data['entity_forbidden'] = [
48       FALSE,
49       AccessResult::forbidden(),
50     ];
51     $data['entity_neutral'] = [
52       FALSE,
53       AccessResult::neutral(),
54     ];
55     return $data;
56   }
57
58   /**
59    * Tests unfieldable entity.
60    *
61    * @covers ::blockAccess
62    */
63   public function testBlockAccessEntityAllowedNotFieldable() {
64     $entity = $this->prophesize(EntityInterface::class);
65     $block = $this->getTestBlock($entity);
66
67     $account = $this->prophesize(AccountInterface::class);
68     $entity->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
69
70     $access = $block->access($account->reveal(), TRUE);
71     $this->assertSame(FALSE, $access->isAllowed());
72   }
73
74   /**
75    * Tests fieldable entity without a particular field.
76    *
77    * @covers ::blockAccess
78    */
79   public function testBlockAccessEntityAllowedNoField() {
80     $entity = $this->prophesize(FieldableEntityInterface::class);
81     $block = $this->getTestBlock($entity);
82
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();
87
88     $access = $block->access($account->reveal(), TRUE);
89     $this->assertSame(FALSE, $access->isAllowed());
90   }
91
92   /**
93    * Tests field access.
94    *
95    * @covers ::blockAccess
96    * @dataProvider providerTestBlockAccessNotAllowed
97    */
98   public function testBlockAccessEntityAllowedFieldNotAllowed($expected, $field_access) {
99     $entity = $this->prophesize(FieldableEntityInterface::class);
100     $block = $this->getTestBlock($entity);
101
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());
107
108     $field->access('view', $account->reveal(), TRUE)->willReturn($field_access);
109     $field->isEmpty()->shouldNotBeCalled();
110
111     $access = $block->access($account->reveal(), TRUE);
112     $this->assertSame($expected, $access->isAllowed());
113   }
114
115   /**
116    * Tests populated vs empty build.
117    *
118    * @covers ::blockAccess
119    * @covers ::build
120    * @dataProvider providerTestBlockAccessEntityAllowedFieldHasValue
121    */
122   public function testBlockAccessEntityAllowedFieldHasValue($expected, $is_empty) {
123     $entity = $this->prophesize(FieldableEntityInterface::class);
124     $block = $this->getTestBlock($entity);
125
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());
131
132     $field->access('view', $account->reveal(), TRUE)->willReturn(AccessResult::allowed());
133     $field->isEmpty()->willReturn($is_empty)->shouldBeCalled();
134
135     $access = $block->access($account->reveal(), TRUE);
136     $this->assertSame($expected, $access->isAllowed());
137   }
138
139   /**
140    * Provides test data for ::testBlockAccessEntityAllowedFieldHasValue().
141    */
142   public function providerTestBlockAccessEntityAllowedFieldHasValue() {
143     $data = [];
144     $data['empty'] = [
145       FALSE,
146       TRUE,
147     ];
148     $data['populated'] = [
149       TRUE,
150       FALSE,
151     ];
152     return $data;
153   }
154
155   /**
156    * Instantiates a block for testing.
157    *
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.
164    *
165    * @return \Drupal\layout_builder\Plugin\Block\FieldBlock
166    *   The block to test.
167    */
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);
172
173     $plugin_definition += [
174       'provider' => 'test',
175       'default_formatter' => '',
176       'category' => 'Test',
177       'admin_label' => 'Test Block',
178       'bundles' => ['entity_test'],
179       'context' => [
180         'entity' => new ContextDefinition('entity:entity_test', 'Test', TRUE),
181       ],
182     ];
183     $entity_field_manager = $this->prophesize(EntityFieldManagerInterface::class);
184     $formatter_manager = $this->prophesize(FormatterPluginManager::class);
185     $module_handler = $this->prophesize(ModuleHandlerInterface::class);
186
187     $block = new FieldBlock(
188       $configuration,
189       'field_block:entity_test:entity_test:the_field_name',
190       $plugin_definition,
191       $entity_field_manager->reveal(),
192       $formatter_manager->reveal(),
193       $module_handler->reveal()
194     );
195     $block->setContextValue('entity', $entity_prophecy->reveal());
196     return $block;
197   }
198
199 }