3 namespace Drupal\entity_embed\Tests;
6 * Tests the entity_embed filter.
10 class EntityEmbedFilterTest extends EntityEmbedTestBase {
17 public static $modules = [
27 * Tests the entity_embed filter.
29 * Ensures that entities are getting rendered when correct data attributes
30 * are passed. Also tests situations when embed fails.
32 public function testFilter() {
33 // Tests entity embed using entity ID and view mode.
34 $content = '<drupal-entity data-entity-type="node" data-entity-id="' . $this->node->id() . '" data-view-mode="teaser">This placeholder should not be rendered.</drupal-entity>';
36 $settings['type'] = 'page';
37 $settings['title'] = 'Test entity embed with entity-id and view-mode';
38 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
39 $node = $this->drupalCreateNode($settings);
40 $this->drupalGet('node/' . $node->id());
41 $this->assertNoRaw('<drupal-entity data-entity-type="node" data-entity');
42 $this->assertText($this->node->body->value, 'Embedded node exists in page');
43 $this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
44 $this->assertRaw('<article class="embedded-entity">', 'Embed container found.');
46 // Tests that embedded entity is not rendered if not accessible.
47 $this->node->setPublished(FALSE)->save();
49 $settings['type'] = 'page';
50 $settings['title'] = 'Test un-accessible entity embed with entity-id and view-mode';
51 $settings['body'] = [['value' => $content, 'format' => 'custom_format']];
52 $node = $this->drupalCreateNode($settings);
53 $this->drupalGet('node/' . $node->id());
54 $this->assertNoRaw('<drupal-entity data-entity-type="node" data-entity');
55 $this->assertNoText($this->node->body->value, 'Embedded node does not exist in the page.');
56 $this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
57 // Tests that embedded entity is displayed to the user who has the view
58 // unpublished content permission.
59 $this->createRole(['view own unpublished content'], 'access_unpublished');
60 $this->webUser->addRole('access_unpublished');
61 $this->webUser->save();
62 $this->drupalGet('node/' . $node->id());
63 $this->assertNoRaw('<drupal-entity data-entity-type="node" data-entity');
64 $this->assertText($this->node->body->value, 'Embedded node exists in the page.');
65 $this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
66 $this->assertRaw('<article class="embedded-entity">', 'Embed container found.');
67 $this->webUser->removeRole('access_unpublished');
68 $this->webUser->save();
69 $this->node->setPublished(TRUE)->save();
71 // Tests entity embed using entity UUID and view mode.
72 $content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-view-mode="teaser">This placeholder should not be rendered.</drupal-entity>';
74 $settings['type'] = 'page';
75 $settings['title'] = 'Test entity embed with entity-uuid and view-mode';
76 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
77 $node = $this->drupalCreateNode($settings);
78 $this->drupalGet('node/' . $node->id());
79 $this->assertNoRaw('<drupal-entity data-entity-type="node" data-entity');
80 $this->assertText($this->node->body->value, 'Embedded node exists in page.');
81 $this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
82 $this->assertRaw('<article class="embedded-entity">', 'Embed container found.');
83 $this->assertCacheTag('foo:' . $this->node->id());
85 // Ensure that placeholder is not replaced when embed is unsuccessful.
86 $content = '<drupal-entity data-entity-type="node" data-entity-id="InvalidID" data-view-mode="teaser">This placeholder should be rendered since specified entity does not exists.</drupal-entity>';
88 $settings['type'] = 'page';
89 $settings['title'] = 'Test that placeholder is retained when specified entity does not exists';
90 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
91 $node = $this->drupalCreateNode($settings);
92 $this->drupalGet('node/' . $node->id());
93 $this->assertNoRaw('<drupal-entity data-entity-type="node" data-entity');
94 $this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is unsuccessful.');
96 // Ensure that UUID is preferred over ID when both attributes are present.
97 $sample_node = $this->drupalCreateNode();
98 $content = '<drupal-entity data-entity-type="node" data-entity-id="' . $sample_node->id() . '" data-entity-uuid="' . $this->node->uuid() . '" data-view-mode="teaser">This placeholder should not be rendered.</drupal-entity>';
100 $settings['type'] = 'page';
101 $settings['title'] = 'Test that entity-uuid is preferred over entity-id when both attributes are present';
102 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
103 $node = $this->drupalCreateNode($settings);
104 $this->drupalGet('node/' . $node->id());
105 $this->assertNoRaw('<drupal-entity data-entity-type="node" data-entity');
106 $this->assertText($this->node->body->value, 'Entity specifed with UUID exists in the page.');
107 $this->assertNoText($sample_node->body->value, 'Entity specifed with ID does not exists in the page.');
108 $this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
109 $this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
111 // Test deprecated 'default' Entity Embed Display plugin.
112 $content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-entity-embed-display="default" data-entity-embed-display-settings=\'{"view_mode":"teaser"}\'>This placeholder should not be rendered.</drupal-entity>';
114 $settings['type'] = 'page';
115 $settings['title'] = 'Test entity embed with entity-embed-display and data-entity-embed-display-settings';
116 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
117 $node = $this->drupalCreateNode($settings);
118 $this->drupalGet('node/' . $node->id());
119 $this->assertText($this->node->body->value, 'Embedded node exists in page.');
120 $this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
121 $this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
123 // Ensure that Entity Embed Display plugin is preferred over view mode when
124 // both attributes are present.
125 $content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-entity-embed-display="default" data-entity-embed-display-settings=\'{"view_mode":"full"}\' data-view-mode="some-invalid-view-mode" data-align="left" data-caption="test caption">This placeholder should not be rendered.</drupal-entity>';
127 $settings['type'] = 'page';
128 $settings['title'] = 'Test entity embed with entity-embed-display and data-entity-embed-display-settings';
129 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
130 $node = $this->drupalCreateNode($settings);
131 $this->drupalGet('node/' . $node->id());
132 $this->assertText($this->node->body->value, 'Embedded node exists in page with the view mode specified by entity-embed-settings.');
133 $this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
134 $this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
136 // Ensure the embedded node doesn't contain data tags on the full page.
137 $this->drupalGet('node/' . $this->node->id());
138 $this->assertNoRaw('data-align="left"', 'Align data attribute not found.');
139 $this->assertNoRaw('data-caption="test caption"', 'Caption data attribute not found.');
141 // Test that tag of container element is not replaced when it's not
143 $content = '<not-drupal-entity data-entity-type="node" data-entity-id="' . $this->node->id() . '" data-view-mode="teaser">this placeholder should not be rendered.</not-drupal-entity>';
145 $settings['type'] = 'page';
146 $settings['title'] = 'test entity embed with entity-id and view-mode';
147 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
148 $node = $this->drupalCreateNode($settings);
149 $this->drupalget('node/' . $node->id());
150 $this->assertNoText($this->node->body->value, 'embedded node exists in page');
151 $this->assertRaw('</not-drupal-entity>');
152 $content = '<div data-entity-type="node" data-entity-id="' . $this->node->id() . '" data-view-mode="teaser">this placeholder should not be rendered.</div>';
154 $settings['type'] = 'page';
155 $settings['title'] = 'test entity embed with entity-id and view-mode';
156 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
157 $node = $this->drupalCreateNode($settings);
158 $this->drupalget('node/' . $node->id());
159 $this->assertNoText($this->node->body->value, 'embedded node exists in page');
160 $this->assertRaw('<div data-entity-type="node" data-entity-id');
162 // Test that attributes are correctly added when image formatter is used.
163 /** @var \Drupal\file\FileInterface $image */
164 $image = $this->getTestFile('image');
165 $image->setPermanent();
167 $content = '<drupal-entity data-entity-type="file" data-entity-uuid="' . $image->uuid() . '" data-entity-embed-display="image:image" data-entity-embed-display-settings=\'{"image_style":"","image_link":""}\' data-align="left" data-caption="test caption" alt="This is alt text" title="This is title text">This placeholder should not be rendered.</drupal-entity>';
169 $settings['type'] = 'page';
170 $settings['title'] = 'test entity image formatter';
171 $settings['body'] = [['value' => $content, 'format' => 'custom_format']];
172 $node = $this->drupalCreateNode($settings);
173 $this->drupalget('node/' . $node->id());
174 $this->assertRaw('<img src', 'IMG tag found.');
175 $this->assertRaw('data-caption="test caption"', 'Caption found.');
176 $this->assertRaw('data-align="left"', 'Alignment information found.');
177 $this->assertTrue((bool) $this->xpath("//img[@alt='This is alt text']"), 'Alt text found');
178 $this->assertTrue((bool) $this->xpath("//img[@title='This is title text']"), 'Title text found');
179 $this->assertRaw('<article class="embedded-entity"', 'Embed container found.');
181 // data-entity-embed-settings is replaced with
182 // data-entity-embed-display-settings. Check to see if
183 // data-entity-embed-settings is still working.
184 $content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-entity-embed-display="entity_reference:entity_reference_label" data-entity-embed-settings=\'{"link":"0"}\' data-align="left" data-caption="test caption">This placeholder should not be rendered.</drupal-entity>';
186 $settings['type'] = 'page';
187 $settings['title'] = 'Test entity embed with data-entity-embed-settings';
188 $settings['body'] = [['value' => $content, 'format' => 'custom_format']];
189 $node = $this->drupalCreateNode($settings);
190 $this->drupalGet('node/' . $node->id());
191 $this->assertText($this->node->getTitle(), 'Embeded node title is displayed.');
192 $this->assertNoLink($this->node->getTitle(), 'Embed settings are respected.');
193 $this->assertNoText($this->node->body->value, 'Embedded node exists in page.');
194 $this->assertNoText(strip_tags($content), 'Placeholder does not appear in the output when embed is successful.');
195 $this->assertRaw('<article class="embedded-entity"', 'Embed container found.');