Interim commit.
[yaffs-website] / web / modules / contrib / entity_embed / src / Tests / FileFieldFormatterTest.php
1 <?php
2
3 namespace Drupal\entity_embed\Tests;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Form\FormState;
7
8 /**
9  * Tests the file field formatter provided by entity_embed.
10  *
11  * @group entity_embed
12  */
13 class FileFieldFormatterTest extends EntityEmbedTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['file', 'image'];
21
22   /**
23    * Created file entity.
24    *
25    * @var \Drupal\file\FileInterface
26    */
27   protected $file;
28
29   /**
30    *
31    */
32   protected function setUp() {
33     parent::setUp();
34     $this->file = $this->getTestFile('text');
35   }
36
37   /**
38    * Tests file field formatter Entity Embed Display plugins.
39    */
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',
45       'file:file_default',
46       'file:file_table',
47       'file:file_url_plain',
48     ]);
49
50     // Ensure that correct form attributes are returned for the file field
51     // formatter plugins.
52     $form = array();
53     $form_state = new FormState();
54     $plugins = array(
55       'file:file_table',
56       'file:file_default',
57       'file:file_url_plain',
58     );
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');
68     }
69
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>';
73     $settings = array();
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.');
82   }
83
84 }