3 namespace Drupal\file\Tests;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\file\Entity\File;
9 * Tests the display of file fields in node and views.
13 class FileFieldDisplayTest extends FileFieldTestBase {
16 * Tests normal formatter display on node display.
18 public function testNodeDisplay() {
19 $field_name = strtolower($this->randomMachineName());
20 $type_name = 'article';
21 $field_storage_settings = [
22 'display_field' => '1',
23 'display_default' => '1',
24 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
27 'description_field' => '1',
29 $widget_settings = [];
30 $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
32 // Create a new node *without* the file field set, and check that the field
33 // is not shown for each node display.
34 $node = $this->drupalCreateNode(['type' => $type_name]);
35 // Check file_default last as the assertions below assume that this is the
37 $file_formatters = ['file_table', 'file_url_plain', 'hidden', 'file_default'];
38 foreach ($file_formatters as $formatter) {
39 if ($formatter === 'hidden') {
41 "fields[$field_name][region]" => 'hidden',
46 "fields[$field_name][type]" => $formatter,
47 "fields[$field_name][region]" => 'content',
50 $this->drupalPostForm("admin/structure/types/manage/$type_name/display", $edit, t('Save'));
51 $this->drupalGet('node/' . $node->id());
52 $this->assertNoText($field_name, format_string('Field label is hidden when no file attached for formatter %formatter', ['%formatter' => $formatter]));
55 $test_file = $this->getTestFile('text');
56 simpletest_generate_file('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[$name] = drupal_realpath($test_file->getFileUri());
101 // Uncheck the display checkboxes and go to the preview.
102 $edit[$field_name . '[0][display]'] = FALSE;
103 $edit[$field_name . '[1][display]'] = FALSE;
104 $this->drupalPostForm("node/$nid/edit", $edit, t('Preview'));
105 $this->clickLink(t('Back to content editing'));
106 $this->assertRaw($field_name . '[0][display]', 'First file appears as expected.');
107 $this->assertRaw($field_name . '[1][display]', 'Second file appears as expected.');
111 * Tests default display of File Field.
113 public function testDefaultFileFieldDisplay() {
114 $field_name = strtolower($this->randomMachineName());
115 $type_name = 'article';
116 $field_storage_settings = [
117 'display_field' => '1',
118 'display_default' => '0',
119 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
122 'description_field' => '1',
124 $widget_settings = [];
125 $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
127 $test_file = $this->getTestFile('text');
129 // Create a new node with the uploaded file.
130 $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
132 $this->drupalGet('node/' . $nid . '/edit');
133 $this->assertFieldByXPath('//input[@type="checkbox" and @name="' . $field_name . '[0][display]"]', NULL, 'Default file display checkbox field exists.');
134 $this->assertFieldByXPath('//input[@type="checkbox" and @name="' . $field_name . '[0][display]" and not(@checked)]', NULL, 'Default file display is off.');
138 * Tests description toggle for field instance configuration.
140 public function testDescToggle() {
142 $field_type = 'file';
143 $field_name = strtolower($this->randomMachineName());
144 // Use the UI to add a new content type that also contains a file field.
146 'name' => $type_name,
147 'type' => $type_name,
149 $this->drupalPostForm('admin/structure/types/add', $edit, t('Save and manage fields'));
151 'new_storage_type' => $field_type,
152 'field_name' => $field_name,
153 'label' => $this->randomString(),
155 $this->drupalPostForm('/admin/structure/types/manage/' . $type_name . '/fields/add-field', $edit, t('Save and continue'));
156 $this->drupalPostForm(NULL, [], t('Save field settings'));
157 // Ensure the description field is selected on the field instance settings
158 // form. That's what this test is all about.
160 'settings[description_field]' => TRUE,
162 $this->drupalPostForm(NULL, $edit, t('Save settings'));
163 // Add a node of our new type and upload a file to it.
164 $file = current($this->drupalGetTestFiles('text'));
165 $title = $this->randomString();
167 'title[0][value]' => $title,
168 'files[field_' . $field_name . '_0]' => drupal_realpath($file->uri),
170 $this->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
171 $node = $this->drupalGetNodeByTitle($title);
172 $this->drupalGet('node/' . $node->id() . '/edit');
173 $this->assertText(t('The description may be used as the label of the link to the file.'));