FileFieldFormatterTest.php in Entity Embed 8
File
tests/src/Functional/FileFieldFormatterTest.php
View source
<?php
namespace Drupal\Tests\entity_embed\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormState;
class FileFieldFormatterTest extends EntityEmbedTestBase {
public static $modules = [
'file',
'image',
];
protected $file;
protected function setUp() {
parent::setUp();
$this->file = $this
->getTestFile('text');
}
public function testFileFieldFormatter() {
$this
->assertAvailableDisplayPlugins($this->file, [
'entity_reference:entity_reference_label',
'entity_reference:entity_reference_entity_id',
'file:file_default',
'file:file_table',
'file:file_url_plain',
]);
$form = [];
$form_state = new FormState();
$plugins = [
'file:file_table',
'file:file_default',
'file:file_url_plain',
];
foreach ($plugins as $plugin) {
$display = $this->container
->get('plugin.manager.entity_embed.display')
->createInstance($plugin, []);
$display
->setContextValue('entity', $this->file);
$conf_form = $display
->buildConfigurationForm($form, $form_state);
$this
->assertArrayHasKey('description', $conf_form);
$this
->assertSame('textfield', $conf_form['description']['#type']);
$this
->assertSame('Description', (string) $conf_form['description']['#title']);
}
$embed_settings = [
'description' => 'This is sample description',
];
$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>';
$settings = [];
$settings['type'] = 'page';
$settings['title'] = 'Test entity embed with file:file_default';
$settings['body'] = [
[
'value' => $content,
'format' => 'custom_format',
],
];
$node = $this
->drupalCreateNode($settings);
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->responseContains($embed_settings['description']);
$this
->assertSession()
->responseNotContains('This placeholder should not be rendered.');
$this
->assertSession()
->linkByHrefExists(file_create_url($this->file
->getFileUri()), 0, 'Link to the embedded file exists.');
}
}