3 namespace Drupal\entity_embed\Tests;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Form\FormState;
9 * Tests the file field formatter provided by entity_embed.
13 class FileFieldFormatterTest extends EntityEmbedTestBase {
20 public static $modules = ['file', 'image'];
23 * Created file entity.
25 * @var \Drupal\file\FileInterface
32 protected function setUp() {
34 $this->file = $this->getTestFile('text');
38 * Tests file field formatter Entity Embed Display plugins.
40 public function testFileFieldFormatter() {
41 // Ensure that file field formatters are available as plugins.
42 $this->assertAvailableDisplayPlugins($this->file, [
43 'entity_reference:entity_reference_label',
44 'entity_reference:entity_reference_entity_id',
47 'file:file_url_plain',
50 // Ensure that correct form attributes are returned for the file field
53 $form_state = new FormState();
57 'file:file_url_plain',
59 // Ensure that description field is available for all the 'file' plugins.
60 foreach ($plugins as $plugin) {
61 $display = $this->container->get('plugin.manager.entity_embed.display')
62 ->createInstance($plugin, []);
63 $display->setContextValue('entity', $this->file);
64 $conf_form = $display->buildConfigurationForm($form, $form_state);
65 $this->assertIdentical(array_keys($conf_form), array('description'));
66 $this->assertIdentical($conf_form['description']['#type'], 'textfield');
67 $this->assertIdentical((string) $conf_form['description']['#title'], 'Description');
70 // Test entity embed using 'Generic file' Entity Embed Display plugin.
71 $embed_settings = array('description' => "This is sample description");
72 $content = '<drupal-entity data-entity-type="file" data-entity-uuid="' . $this->file->uuid() . '" data-entity-embed-display="file:file_default" data-entity-embed-display-settings=\'' . Json::encode($embed_settings) . '\'>This placeholder should not be rendered.</drupal-entity>';
74 $settings['type'] = 'page';
75 $settings['title'] = 'Test entity embed with file:file_default';
76 $settings['body'] = array(array('value' => $content, 'format' => 'custom_format'));
77 $node = $this->drupalCreateNode($settings);
78 $this->drupalGet('node/' . $node->id());
79 $this->assertText($embed_settings['description'], 'Description of the embedded file exists in page.');
80 $this->assertNoText(strip_tags($content), 'Placeholder does not appears in the output when embed is successful.');
81 $this->assertLinkByHref(file_create_url($this->file->getFileUri()), 0, 'Link to the embedded file exists.');