3 namespace Drupal\Tests\file\Functional;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\file\Entity\File;
7 use Drupal\node\Entity\Node;
10 * Tests the display of file fields in node and views.
14 class FileFieldDisplayTest extends FileFieldTestBase {
17 * Tests normal formatter display on node display.
19 public function testNodeDisplay() {
20 $field_name = strtolower($this->randomMachineName());
21 $type_name = 'article';
22 $field_storage_settings = [
23 'display_field' => '1',
24 'display_default' => '1',
25 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
28 'description_field' => '1',
30 $widget_settings = [];
31 $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
33 // Create a new node *without* the file field set, and check that the field
34 // is not shown for each node display.
35 $node = $this->drupalCreateNode(['type' => $type_name]);
36 // Check file_default last as the assertions below assume that this is the
38 $file_formatters = ['file_table', 'file_url_plain', 'hidden', 'file_default'];
39 foreach ($file_formatters as $formatter) {
40 if ($formatter === 'hidden') {
42 "fields[$field_name][region]" => 'hidden',
47 "fields[$field_name][type]" => $formatter,
48 "fields[$field_name][region]" => 'content',
51 $this->drupalPostForm("admin/structure/types/manage/$type_name/display", $edit, t('Save'));
52 $this->drupalGet('node/' . $node->id());
53 $this->assertNoText($field_name, format_string('Field label is hidden when no file attached for formatter %formatter', ['%formatter' => $formatter]));
56 $this->generateFile('escaped-&-text', 64, 10, 'text');
57 $test_file = File::create([
58 'uri' => 'public://escaped-&-text.txt',
59 'name' => 'escaped-&-text',
60 'filesize' => filesize('public://escaped-&-text.txt'),
63 // Create a new node with the uploaded file.
64 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
66 // Check that the default formatter is displaying with the file name.
67 $node_storage = $this->container->get('entity.manager')->getStorage('node');
68 $node_storage->resetCache([$nid]);
69 $node = $node_storage->load($nid);
70 $node_file = File::load($node->{$field_name}->target_id);
72 '#theme' => 'file_link',
73 '#file' => $node_file,
75 $default_output = \Drupal::service('renderer')->renderRoot($file_link);
76 $this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
78 // Turn the "display" option off and check that the file is no longer displayed.
79 $edit = [$field_name . '[0][display]' => FALSE];
80 $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save'));
82 $this->assertNoRaw($default_output, 'Field is hidden when "display" option is unchecked.');
84 // Add a description and make sure that it is displayed.
85 $description = $this->randomMachineName();
87 $field_name . '[0][description]' => $description,
88 $field_name . '[0][display]' => TRUE,
90 $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save'));
91 $this->assertText($description);
93 // Ensure the filename in the link's title attribute is escaped.
94 $this->assertRaw('title="escaped-&-text.txt"');
96 // Test that fields appear as expected after during the preview.
98 $name = 'files[' . $field_name . '_1][]';
99 $edit_upload[$name] = \Drupal::service('file_system')->realpath($test_file->getFileUri());
100 $this->drupalPostForm("node/$nid/edit", $edit_upload, t('Upload'));
102 // Uncheck the display checkboxes and go to the preview.
103 $edit[$field_name . '[0][display]'] = FALSE;
104 $edit[$field_name . '[1][display]'] = FALSE;
105 $this->drupalPostForm(NULL, $edit, t('Preview'));
106 $this->clickLink(t('Back to content editing'));
107 $this->assertRaw($field_name . '[0][display]', 'First file appears as expected.');
108 $this->assertRaw($field_name . '[1][display]', 'Second file appears as expected.');
109 $this->assertSession()->responseContains($field_name . '[1][description]', 'Description of second file appears as expected.');
113 * Tests default display of File Field.
115 public function testDefaultFileFieldDisplay() {
116 $field_name = strtolower($this->randomMachineName());
117 $type_name = 'article';
118 $field_storage_settings = [
119 'display_field' => '1',
120 'display_default' => '0',
121 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
124 'description_field' => '1',
126 $widget_settings = [];
127 $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
129 $test_file = $this->getTestFile('text');
131 // Create a new node with the uploaded file.
132 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
134 $this->drupalGet('node/' . $nid . '/edit');
135 $this->assertFieldByXPath('//input[@type="checkbox" and @name="' . $field_name . '[0][display]"]', NULL, 'Default file display checkbox field exists.');
136 $this->assertFieldByXPath('//input[@type="checkbox" and @name="' . $field_name . '[0][display]" and not(@checked)]', NULL, 'Default file display is off.');
140 * Tests description toggle for field instance configuration.
142 public function testDescToggle() {
144 $field_type = 'file';
145 $field_name = strtolower($this->randomMachineName());
146 // Use the UI to add a new content type that also contains a file field.
148 'name' => $type_name,
149 'type' => $type_name,
151 $this->drupalPostForm('admin/structure/types/add', $edit, t('Save and manage fields'));
153 'new_storage_type' => $field_type,
154 'field_name' => $field_name,
155 'label' => $this->randomString(),
157 $this->drupalPostForm('/admin/structure/types/manage/' . $type_name . '/fields/add-field', $edit, t('Save and continue'));
158 $this->drupalPostForm(NULL, [], t('Save field settings'));
159 // Ensure the description field is selected on the field instance settings
160 // form. That's what this test is all about.
162 'settings[description_field]' => TRUE,
164 $this->drupalPostForm(NULL, $edit, t('Save settings'));
165 // Add a node of our new type and upload a file to it.
166 $file = current($this->drupalGetTestFiles('text'));
167 $title = $this->randomString();
169 'title[0][value]' => $title,
170 'files[field_' . $field_name . '_0]' => \Drupal::service('file_system')->realpath($file->uri),
172 $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
173 $node = $this->drupalGetNodeByTitle($title);
174 $this->drupalGet('node/' . $node->id() . '/edit');
175 $this->assertText(t('The description may be used as the label of the link to the file.'));
179 * Tests description display of File Field.
181 public function testDescriptionDefaultFileFieldDisplay() {
182 $field_name = strtolower($this->randomMachineName());
183 $type_name = 'article';
184 $field_storage_settings = [
185 'display_field' => '1',
186 'display_default' => '1',
187 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
190 'description_field' => '1',
192 $widget_settings = [];
193 $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
195 $test_file = $this->getTestFile('text');
197 // Create a new node with the uploaded file.
198 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
200 // Add file description.
201 $description = 'This is the test file description';
202 $this->drupalPostForm("node/$nid/edit", [$field_name . '[0][description]' => $description], t('Save'));
204 // Load uncached node.
205 \Drupal::entityTypeManager()->getStorage('node')->resetCache([$nid]);
206 $node = Node::load($nid);
208 // Test default formatter.
209 $this->drupalGet('node/' . $nid);
210 $this->assertFieldByXPath('//a[@href="' . $node->{$field_name}->entity->url() . '"]', $description);
212 // Change formatter to "Table of files".
213 $display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->load('node.' . $type_name . '.default');
214 $display->setComponent($field_name, [
216 'type' => 'file_table',
219 $this->drupalGet('node/' . $nid);
220 $this->assertFieldByXPath('//a[@href="' . $node->{$field_name}->entity->url() . '"]', $description);