tests/src/Kernel/EntityEmbedFilterTest.php | 126 +++++++++++++---------------- 1 file changed, 56 insertions(+), 70 deletions(-) diff --git a/tests/src/Kernel/EntityEmbedFilterTest.php b/tests/src/Kernel/EntityEmbedFilterTest.php index b41ad66..1beae9c 100644 --- a/tests/src/Kernel/EntityEmbedFilterTest.php +++ b/tests/src/Kernel/EntityEmbedFilterTest.php @@ -56,12 +56,6 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { $embedded_entity_container = $this->cssSelect('div.embedded-entity')[0]; $this->assertHasAttributes($embedded_entity_container, $expected_attributes); - // Specifically assert the absence of `data-entity-id` if it wasn't present - // on input either. - if (empty($embed_attributes['data-entity-id'])) { - $this->assertFalse(isset($embedded_entity_container['data-entity-id'])); - } - // Expected bubbleable metadata. $this->assertSame($result->getCacheTags(), $expected_cacheability->getCacheTags()); $this->assertSame($result->getCacheContexts(), $expected_cacheability->getCacheContexts()); @@ -81,10 +75,10 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { $expected_asset_libraries = ['entity_embed/caption']; return [ - 'using entity ID and view mode' => [ + '`data-entity-uuid` + `data-view-mode` ⇒ rendered' => [ 'embed_attributes' => [ 'data-entity-type' => 'node', - 'data-entity-id' => 1, + 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', 'data-view-mode' => 'teaser', ], 'contains' => [ @@ -93,7 +87,6 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { 'not_contains' => [], 'expected_attributes' => [ 'data-entity-type' => 'node', - 'data-entity-id' => 1, 'data-view-mode' => 'teaser', 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', 'data-langcode' => 'en', @@ -105,10 +98,10 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { 'expected_cacheability' => $expected_cacheability, 'expected_asset_libraries' => $expected_asset_libraries, ], - 'embedded entity is not rendered if unpublished' => [ + 'data-entity-uuid + data-view-mode; unpublished ⇒ NOT rendered' => [ 'embed_attributes' => [ 'data-entity-type' => 'node', - 'data-entity-id' => 1, + 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', 'data-view-mode' => 'teaser', ], 'contains' => [], @@ -119,17 +112,16 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { 'expected_cacheability' => $expected_cacheability, 'expected_asset_libraries' => $expected_asset_libraries, ], - 'Unpublished and current user has view unpublished permission' => [ + 'data-entity-uuid + data-view-mode; unpublished + `view own unpublished` ⇒ rendered' => [ 'embed_attributes' => [ 'data-entity-type' => 'node', - 'data-entity-id' => 1, + 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', 'data-view-mode' => 'teaser', ], 'contains' => ['This node is to be used for embedding in other nodes.'], 'not_contains' => [], 'expected_attributes' => [ 'data-entity-type' => 'node', - 'data-entity-id' => 1, 'data-view-mode' => 'teaser', 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', 'data-langcode' => 'en', @@ -142,54 +134,6 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { ->setCacheContexts(['timezone', 'user', 'user.permissions']), 'expected_asset_libraries' => $expected_asset_libraries, ], - 'using entity UUID and view mode' => [ - 'embed_attributes' => [ - 'data-entity-type' => 'node', - 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', - 'data-view-mode' => 'teaser', - ], - 'contains' => [ - 'This node is to be used for embedding in other nodes.', - ], - 'not_contains' => [], - 'expected_attributes' => [ - 'data-entity-type' => 'node', - 'data-view-mode' => 'teaser', - 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', - 'data-langcode' => 'en', - 'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view', - 'data-entity-embed-display-settings' => 'teaser', - ], - 'published' => TRUE, - 'view_unpublished' => FALSE, - 'expected_cacheability' => $expected_cacheability, - 'expected_asset_libraries' => $expected_asset_libraries, - ], - 'Ensure that UUID is preferred over ID when both attributes are present' => [ - 'embed_attributes' => [ - 'data-entity-type' => 'node', - 'data-entity-id' => 2, - 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', - 'data-view-mode' => 'teaser', - ], - 'contains' => [ - 'This node is to be used for embedding in other nodes.', - ], - 'not_contains' => [], - 'expected_attributes' => [ - 'data-entity-type' => 'node', - 'data-entity-id' => 2, - 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', - 'data-view-mode' => 'teaser', - 'data-langcode' => 'en', - 'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view', - 'data-entity-embed-display-settings' => 'teaser', - ], - 'published' => TRUE, - 'view_unpublished' => FALSE, - 'expected_cacheability' => $expected_cacheability, - 'expected_asset_libraries' => $expected_asset_libraries, - ], 'Test deprecated default Entity Embed Display plugin.' => [ 'embed_attributes' => [ 'data-entity-type' => 'node', @@ -292,7 +236,7 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { "Ensure that content that can't be loaded is replaced with warning" => [ 'embed_attributes' => [ 'data-entity-type' => 'node', - 'data-entity-id' => 'InvalidID', + 'data-entity-uuid' => 'InvalidID', 'data-view-mode' => 'teaser', ], 'contains' => [], @@ -305,10 +249,9 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { * Test HTML tag with same attributes as drupal-entity not affected. */ public function testContainerTagElementReplace() { - $node_id = $this->node->id(); $content = $this->createEmbedCode([ 'data-entity-type' => 'node', - 'data-entity-id' => $node_id, + 'data-entity-uuid' => $this->node->uuid(), 'data-view-mode' => 'teaser', ]); @@ -438,6 +381,54 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { $this->assertRaw($this->node->body->value); } + /** + * Tests BC for `data-entity-uuid`'s predecessor, `data-entity-id`. + * + * @group legacy + */ + public function testEntityIdBackwardsCompatibility() { + $content = $this->createEmbedCode([ + 'data-entity-type' => 'node', + 'data-entity-id' => 1, + 'data-view-mode' => 'teaser', + ]); + $this->applyFilter($content); + $this->assertHasAttributes($this->cssSelect('div.embedded-entity')[0], [ + 'data-entity-type' => 'node', + 'data-entity-id' => 1, + 'data-view-mode' => 'teaser', + 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', + 'data-langcode' => 'en', + 'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view', + 'data-entity-embed-display-settings' => 'teaser', + ]); + } + + /** + * Verifies `data-entity-id` is ignored when `data-entity-uuid` is present. + * + * @group legacy + */ + public function testEntityIdIgnoredIfEntityUuidPresent() { + $nonsensical_id = $this->randomMachineName(); + $content = $this->createEmbedCode([ + 'data-entity-type' => 'node', + 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', + 'data-entity-id' => $nonsensical_id, + 'data-view-mode' => 'teaser', + ]); + $this->applyFilter($content); + $this->assertHasAttributes($this->cssSelect('div.embedded-entity')[0], [ + 'data-entity-type' => 'node', + 'data-entity-id' => $nonsensical_id, + 'data-view-mode' => 'teaser', + 'data-entity-uuid' => 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63', + 'data-langcode' => 'en', + 'data-entity-embed-display' => 'entity_reference:entity_reference_entity_view', + 'data-entity-embed-display-settings' => 'teaser', + ]); + } + /** * Tests BC for`data-entity-embed-display-settings`'s predecessor. * @@ -500,11 +491,6 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { ->loadByProperties(['uuid' => $attributes['data-entity-uuid']]); $entity = current($entity); } - elseif (!empty($attributes['data-entity-id'])) { - $entity = $this->container->get('entity_type.manager') - ->getStorage($attributes['data-entity-type']) - ->load($attributes['data-entity-id']); - } if ($entity && $entity instanceof TranslatableInterface && !empty($attributes['data-langcode'])) { if ($entity->hasTranslation($attributes['data-langcode'])) { $entity = $entity->getTranslation($attributes['data-langcode']);