3 namespace Drupal\entity_embed\Tests;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Form\FormState;
9 * Tests the image field formatter provided by entity_embed.
13 class ImageFieldFormatterTest extends EntityEmbedTestBase {
20 public static $modules = ['file', 'image', 'responsive_image'];
23 * Created file entity.
25 * @var \Drupal\file\FileInterface
30 * Created file entity.
32 * @var \Drupal\file\FileInterface
39 protected function setUp() {
41 $this->image = $this->getTestFile('image');
42 $this->file = $this->getTestFile('text');
46 * Tests image field formatter Entity Embed Display plugin.
48 public function testImageFieldFormatter() {
49 // Ensure that image field formatters are available as plugins.
50 $this->assertAvailableDisplayPlugins($this->image, [
51 'entity_reference:entity_reference_label',
52 'entity_reference:entity_reference_entity_id',
55 'file:file_url_plain',
56 'image:responsive_image',
60 // Ensure that correct form attributes are returned for the image plugin.
62 $form_state = new FormState();
63 $display = $this->container->get('plugin.manager.entity_embed.display')
64 ->createInstance('image:image', []);
65 $display->setContextValue('entity', $this->image);
66 $conf_form = $display->buildConfigurationForm($form, $form_state);
67 $this->assertIdentical(array_keys($conf_form), array(
73 $this->assertIdentical($conf_form['image_style']['#type'], 'select');
74 $this->assertIdentical((string) $conf_form['image_style']['#title'], 'Image style');
75 $this->assertIdentical($conf_form['image_link']['#type'], 'select');
76 $this->assertIdentical((string) $conf_form['image_link']['#title'], 'Link image to');
77 $this->assertIdentical($conf_form['alt']['#type'], 'textfield');
78 $this->assertIdentical((string) $conf_form['alt']['#title'], 'Alternate text');
79 $this->assertIdentical($conf_form['title']['#type'], 'textfield');
80 $this->assertIdentical((string) $conf_form['title']['#title'], 'Title');
82 // Test entity embed using 'Image' Entity Embed Display plugin.
83 $alt_text = "This is sample description";
84 $title = "This is sample title";
85 $embed_settings = array('image_link' => 'file');
86 $content = '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->image->uuid() . '" data-entity-embed-display="image:image" data-entity-embed-display-settings=\'' . Json::encode($embed_settings) . '\' alt="' . $alt_text . '" title="' . $title . '">This placeholder should not be rendered.</drupal-entity>';
88 $settings['type'] = 'page';
89 $settings['title'] = 'Test entity embed with image:image';
90 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
91 $node = $this->drupalCreateNode($settings);
92 $this->drupalGet('node/' . $node->id());
93 $this->assertRaw($alt_text, 'Alternate text for the embedded image is visible when embed is successful.');
94 $this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.');
95 $this->assertLinkByHref(file_create_url($this->image->getFileUri()), 0, 'Link to the embedded image exists.');
97 // Embed all three field types in one, to ensure they all render correctly.
98 $content = '<drupal-entity data-entity-type="node" data-entity-uuid="' . $this->node->uuid() . '" data-entity-embed-display="entity_reference:entity_reference_label"></drupal-entity>';
99 $content .= '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->file->uuid() . '" data-entity-embed-display="file:file_default"></drupal-entity>';
100 $content .= '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->image->uuid() . '" data-entity-embed-display="image:image"></drupal-entity>';
102 $settings['type'] = 'page';
103 $settings['title'] = 'Test node entity embedded first then a file entity';
104 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
105 $node = $this->drupalCreateNode($settings);
106 $this->drupalGet('node/' . $node->id());