Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / file / src / Tests / FileFieldDisplayTest.php
1 <?php
2
3 namespace Drupal\file\Tests;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\file\Entity\File;
7
8 /**
9  * Tests the display of file fields in node and views.
10  *
11  * @group file
12  */
13 class FileFieldDisplayTest extends FileFieldTestBase {
14
15   /**
16    * Tests normal formatter display on node display.
17    */
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,
25     ];
26     $field_settings = [
27       'description_field' => '1',
28     ];
29     $widget_settings = [];
30     $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
31
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
36     // case.
37     $file_formatters = ['file_table', 'file_url_plain', 'hidden', 'file_default'];
38     foreach ($file_formatters as $formatter) {
39       if ($formatter === 'hidden') {
40         $edit = [
41           "fields[$field_name][region]" => 'hidden',
42         ];
43       }
44       else {
45         $edit = [
46           "fields[$field_name][type]" => $formatter,
47           "fields[$field_name][region]" => 'content',
48         ];
49       }
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]));
53     }
54
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'),
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[$name] = drupal_realpath($test_file->getFileUri());
100
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.');
108   }
109
110   /**
111    * Tests default display of File Field.
112    */
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,
120     ];
121     $field_settings = [
122       'description_field' => '1',
123     ];
124     $widget_settings = [];
125     $this->createFileField($field_name, 'node', $type_name, $field_storage_settings, $field_settings, $widget_settings);
126
127     $test_file = $this->getTestFile('text');
128
129     // Create a new node with the uploaded file.
130     $nid = $this->uploadNodeFile($test_file, $field_name, $type_name);
131
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.');
135   }
136
137   /**
138    * Tests description toggle for field instance configuration.
139    */
140   public function testDescToggle() {
141     $type_name = 'test';
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.
145     $edit = [
146       'name' => $type_name,
147       'type' => $type_name,
148     ];
149     $this->drupalPostForm('admin/structure/types/add', $edit, t('Save and manage fields'));
150     $edit = [
151       'new_storage_type' => $field_type,
152       'field_name' => $field_name,
153       'label' => $this->randomString(),
154     ];
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.
159     $edit = [
160       'settings[description_field]' => TRUE,
161     ];
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();
166     $edit = [
167       'title[0][value]' => $title,
168       'files[field_' . $field_name . '_0]' => drupal_realpath($file->uri),
169     ];
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.'));
174   }
175
176 }