07ed6a19cf321dfb635bd170d653f51117dfbb4d
[yaffs-website] / web / core / modules / file / tests / src / Functional / FileFieldDisplayTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Functional;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\file\Entity\File;
7 use Drupal\node\Entity\Node;
8
9 /**
10  * Tests the display of file fields in node and views.
11  *
12  * @group file
13  */
14 class FileFieldDisplayTest extends FileFieldTestBase {
15
16   /**
17    * Tests normal formatter display on node display.
18    */
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,
26     ];
27     $field_settings = [
28       'description_field' => '1',
29     ];
30     $widget_settings = [];
31     $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
32
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
37     // case.
38     $file_formatters = ['file_table', 'file_url_plain', 'hidden', 'file_default'];
39     foreach ($file_formatters as $formatter) {
40       if ($formatter === 'hidden') {
41         $edit = [
42           "fields[$field_name][region]" => 'hidden',
43         ];
44       }
45       else {
46         $edit = [
47           "fields[$field_name][type]" => $formatter,
48           "fields[$field_name][region]" => 'content',
49         ];
50       }
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]));
54     }
55
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'),
61     ]);
62
63     // Create a new node with the uploaded file.
64     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
65
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);
71     $file_link = [
72       '#theme' => 'file_link',
73       '#file' => $node_file,
74     ];
75     $default_output = \Drupal::service('renderer')->renderRoot($file_link);
76     $this->assertRaw($default_output, 'Default formatter displaying correctly on full node view.');
77
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'));
81
82     $this->assertNoRaw($default_output, 'Field is hidden when "display" option is unchecked.');
83
84     // Add a description and make sure that it is displayed.
85     $description = $this->randomMachineName();
86     $edit = [
87       $field_name . '[0][description]' => $description,
88       $field_name . '[0][display]' => TRUE,
89     ];
90     $this->drupalPostForm('node/' . $nid . '/edit', $edit, t('Save'));
91     $this->assertText($description);
92
93     // Ensure the filename in the link's title attribute is escaped.
94     $this->assertRaw('title="escaped-&amp;-text.txt"');
95
96     // Test that fields appear as expected after during the preview.
97     // Add a second file.
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'));
101
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.');
110   }
111
112   /**
113    * Tests default display of File Field.
114    */
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,
122     ];
123     $field_settings = [
124       'description_field' => '1',
125     ];
126     $widget_settings = [];
127     $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
128
129     $test_file = $this->getTestFile('text');
130
131     // Create a new node with the uploaded file.
132     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
133
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.');
137   }
138
139   /**
140    * Tests description toggle for field instance configuration.
141    */
142   public function testDescToggle() {
143     $type_name = 'test';
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.
147     $edit = [
148       'name' => $type_name,
149       'type' => $type_name,
150     ];
151     $this->drupalPostForm('admin/structure/types/add', $edit, t('Save and manage fields'));
152     $edit = [
153       'new_storage_type' => $field_type,
154       'field_name' => $field_name,
155       'label' => $this->randomString(),
156     ];
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.
161     $edit = [
162       'settings[description_field]' => TRUE,
163     ];
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();
168     $edit = [
169       'title[0][value]' => $title,
170       'files[field_' . $field_name . '_0]' => \Drupal::service('file_system')->realpath($file->uri),
171     ];
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.'));
176   }
177
178   /**
179    * Tests description display of File Field.
180    */
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,
188     ];
189     $field_settings = [
190       'description_field' => '1',
191     ];
192     $widget_settings = [];
193     $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
194
195     $test_file = $this->getTestFile('text');
196
197     // Create a new node with the uploaded file.
198     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
199
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'));
203
204     // Load uncached node.
205     \Drupal::entityTypeManager()->getStorage('node')->resetCache([$nid]);
206     $node = Node::load($nid);
207
208     // Test default formatter.
209     $this->drupalGet('node/' . $nid);
210     $this->assertFieldByXPath('//a[@href="' . $node->{$field_name}->entity->url() . '"]', $description);
211
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, [
215       'label' => 'hidden',
216       'type' => 'file_table',
217     ])->save();
218
219     $this->drupalGet('node/' . $nid);
220     $this->assertFieldByXPath('//a[@href="' . $node->{$field_name}->entity->url() . '"]', $description);
221   }
222
223 }