X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Flayout_builder%2Ftests%2Fsrc%2FKernel%2FFieldBlockTest.php;h=aaeb4ca8115e28c4f64162c9c73a989ea0c6cd4d;hb=1c1cb0980bfa6caf0c24cce671b6bb541dc87583;hp=8c529cc1e1b93111dc82d2e1c770470714473c71;hpb=af6d1fb995500ae68849458ee10d66abbdcfb252;p=yaffs-website diff --git a/web/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php b/web/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php index 8c529cc1e..aaeb4ca81 100644 --- a/web/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php +++ b/web/core/modules/layout_builder/tests/src/Kernel/FieldBlockTest.php @@ -7,13 +7,19 @@ use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterPluginManager; -use Drupal\Core\Plugin\Context\ContextDefinition; +use Drupal\Core\Plugin\Context\EntityContextDefinition; use Drupal\Core\Session\AccountInterface; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; use Drupal\layout_builder\Plugin\Block\FieldBlock; +use Prophecy\Argument; +use Prophecy\Promise\PromiseInterface; +use Prophecy\Promise\ReturnPromise; +use Prophecy\Promise\ThrowPromise; use Prophecy\Prophecy\ProphecyInterface; +use Psr\Log\LoggerInterface; /** * @coversDefaultClass \Drupal\layout_builder\Plugin\Block\FieldBlock @@ -21,6 +27,30 @@ use Prophecy\Prophecy\ProphecyInterface; */ class FieldBlockTest extends EntityKernelTestBase { + /** + * The entity field manager. + * + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + protected $entityFieldManager; + + /** + * The logger. + * + * @var \Psr\Log\LoggerInterface + */ + protected $logger; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class); + $this->logger = $this->prophesize(LoggerInterface::class); + } + /** * Tests entity access. * @@ -177,10 +207,9 @@ class FieldBlockTest extends EntityKernelTestBase { 'admin_label' => 'Test Block', 'bundles' => ['entity_test'], 'context' => [ - 'entity' => new ContextDefinition('entity:entity_test', 'Test', TRUE), + 'entity' => EntityContextDefinition::fromEntityTypeId('entity_test')->setLabel('Test'), ], ]; - $entity_field_manager = $this->prophesize(EntityFieldManagerInterface::class); $formatter_manager = $this->prophesize(FormatterPluginManager::class); $module_handler = $this->prophesize(ModuleHandlerInterface::class); @@ -188,12 +217,72 @@ class FieldBlockTest extends EntityKernelTestBase { $configuration, 'field_block:entity_test:entity_test:the_field_name', $plugin_definition, - $entity_field_manager->reveal(), + $this->entityFieldManager->reveal(), $formatter_manager->reveal(), - $module_handler->reveal() + $module_handler->reveal(), + $this->logger->reveal() ); $block->setContextValue('entity', $entity_prophecy->reveal()); return $block; } + /** + * @covers ::build + * @dataProvider providerTestBuild + */ + public function testBuild(PromiseInterface $promise, $expected_markup, $log_message = '', $log_arguments = []) { + $entity = $this->prophesize(FieldableEntityInterface::class); + $field = $this->prophesize(FieldItemListInterface::class); + $entity->get('the_field_name')->willReturn($field->reveal()); + $field->view(Argument::type('array'))->will($promise); + + $field_definition = $this->prophesize(FieldDefinitionInterface::class); + $field_definition->getLabel()->willReturn('The Field Label'); + $this->entityFieldManager->getFieldDefinitions('entity_test', 'entity_test')->willReturn(['the_field_name' => $field_definition]); + + if ($log_message) { + $this->logger->warning($log_message, $log_arguments)->shouldBeCalled(); + } + else { + $this->logger->warning(Argument::cetera())->shouldNotBeCalled(); + } + + $block = $this->getTestBlock($entity); + $expected = [ + '#cache' => [ + 'contexts' => [], + 'tags' => [], + 'max-age' => 0, + ], + ]; + if ($expected_markup) { + $expected['content']['#markup'] = $expected_markup; + } + + $actual = $block->build(); + $this->assertEquals($expected, $actual); + } + + /** + * Provides test data for ::testBuild(). + */ + public function providerTestBuild() { + $data = []; + $data['array'] = [ + new ReturnPromise([['content' => ['#markup' => 'The field value']]]), + 'The field value', + ]; + $data['empty array'] = [ + new ReturnPromise([[]]), + '', + ]; + $data['exception'] = [ + new ThrowPromise(new \Exception('The exception message')), + '', + 'The field "%field" failed to render with the error of "%error".', + ['%field' => 'the_field_name', '%error' => 'The exception message'], + ]; + return $data; + } + }