diff -u b/src/Entity/BlockContent.php b/src/Entity/BlockContent.php --- b/src/Entity/BlockContent.php +++ b/src/Entity/BlockContent.php @@ -70,2 +70,11 @@ + /** + * {@inheritdoc} + */ + public static function load($id) { + return \Drupal::entityTypeManager() + ->getStorage('block_content') + ->load($id); + } + } diff -u b/tests/src/Kernel/BlockContentTest.php b/tests/src/Kernel/BlockContentTest.php --- b/tests/src/Kernel/BlockContentTest.php +++ b/tests/src/Kernel/BlockContentTest.php @@ -2,6 +2,11 @@ namespace Drupal\Tests\panelizer\Kernel; +use Drupal\block_content\Entity\BlockContent as CoreBlockContent; +use Drupal\block_content\Entity\BlockContentType; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; +use Drupal\panelizer\Entity\BlockContent as ShadowBlockContent; use Drupal\KernelTests\KernelTestBase; /** @@ -18,6 +23,7 @@ 'ctools', 'field', 'field_ui', + 'filter', 'panelizer', 'panels', 'text', @@ -29,43 +35,96 @@ */ protected function setUp() { parent::setUp(); + $this->installEntitySchema('block_content'); + $this->installEntitySchema('panelizer_block_content'); - $this->container->get('entity_type.manager') - ->getStorage('block_content_type') - ->create([ - 'id' => 'test_type', - 'label' => 'Test block type', - ]) - ->save(); - - $field_storage = $this->container->get('entity_type.manager') - ->getStorage('field_storage_config') - ->create([ - 'field_name' => 'body', - 'entity_type' => 'block_content', - 'type' => 'text_with_summary', - ]); + // Create a custom block type with a formatted text field. + BlockContentType::create([ + 'id' => 'test_type', + 'label' => 'Test block type', + ])->save(); + + $field_storage = FieldStorageConfig::create([ + 'field_name' => 'body', + 'entity_type' => 'block_content', + 'type' => 'text_with_summary', + ]); $field_storage->save(); - $this->container->get('entity_type.manager') - ->getStorage('field_config') - ->create([ - 'field_storage' => $field_storage, - 'bundle' => 'test_type', + FieldConfig::create([ + 'field_storage' => $field_storage, + 'bundle' => 'test_type', + ])->save(); + + entity_get_display('block_content', 'test_type', 'default') + ->setComponent('body', [ + 'type' => 'text_default', ]) ->save(); } - public function testBlockWithField() { + /** + * Tests that shadow blocks can transparently load real blocks. + */ + public function testLoadRealBlock() { + $original = CoreBlockContent::create([ + 'type' => 'test_type', + 'info' => 'A standard block_content', + 'body' => $this->randomString(128), + ]); + $original->save(); + + $shadow = ShadowBlockContent::load($original->id()); + + $this->assertEquals($original->id(), $shadow->id()); + $this->assertEquals($original->label(), $shadow->label()); + $this->assertEquals($original->type->target_id, $shadow->type->target_id); + $this->assertEquals($original->body->value, $shadow->body->value); + } + + /** + * Tests that shadow blocks render identically to real blocks in isolation. + */ + public function testRenderShadowBlock() { + $this->installConfig('filter'); + + $original = CoreBlockContent::create([ + 'type' => 'test_type', + 'info' => 'A standard block_content', + 'body' => [ + 'value' => $this->randomString(128), + 'format' => 'plain_text', + ], + ]); + $original->save(); + + $original_build = \Drupal::entityTypeManager() + ->getViewBuilder('block_content') + ->view($original); + + $shadow = ShadowBlockContent::load($original->id()); + + $shadow_build = \Drupal::entityTypeManager() + ->getViewBuilder('panelizer_block_content') + ->view($shadow); + + $renderer = \Drupal::service('renderer'); + $original_render = $renderer->renderRoot($original_build); + $shadow_render = $renderer->renderRoot($shadow_build); + $this->assertEquals($original_render, $shadow_render); + } + + /** + * Tests that shadow block fields are never written to the database. + */ + public function testShadowBlockWithField() { $body = $this->randomMachineName(32); - $block_content = $this->container->get('entity_type.manager') - ->getStorage('panelizer_block_content') - ->create([ - 'type' => 'test_type', - 'info' => 'A test of a Panelizer block_content entity.', - 'body' => $body, - ]); + $block_content = ShadowBlockContent::create([ + 'type' => 'test_type', + 'info' => 'A test of a Panelizer block_content entity.', + 'body' => $body, + ]); $this->assertTrue($block_content->hasField('body')); $this->assertEquals($body, $block_content->body->value); @@ -74,7 +133,7 @@ $this->assertEquals($body, $block_content->body->value); // The table should be empty. - $count = $this->container->get('database') + $count = \Drupal::database() ->select('block_content__body') ->countQuery() ->execute()