94d00ef9f2b78ea1182c5b58c98e127210571668
[yaffs-website] / web / core / modules / file / tests / src / Functional / FileManagedFileElementTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Functional;
4
5 /**
6  * Tests the 'managed_file' element type.
7  *
8  * @group file
9  * @todo Create a FileTestBase class and move FileFieldTestBase methods
10  *   that aren't related to fields into it.
11  */
12 class FileManagedFileElementTest extends FileFieldTestBase {
13
14   /**
15    * Tests the managed_file element type.
16    */
17   public function testManagedFile() {
18     // Check that $element['#size'] is passed to the child upload element.
19     $this->drupalGet('file/test');
20     $this->assertFieldByXpath('//input[@name="files[nested_file]" and @size="13"]', NULL, 'The custom #size attribute is passed to the child upload element.');
21
22     // Perform the tests with all permutations of $form['#tree'],
23     // $element['#extended'], and $element['#multiple'].
24     $test_file = $this->getTestFile('text');
25     foreach ([0, 1] as $tree) {
26       foreach ([0, 1] as $extended) {
27         foreach ([0, 1] as $multiple) {
28           $path = 'file/test/' . $tree . '/' . $extended . '/' . $multiple;
29           $input_base_name = $tree ? 'nested_file' : 'file';
30           $file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']';
31
32           // Submit without a file.
33           $this->drupalPostForm($path, [], t('Save'));
34           $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [])]), 'Submitted without a file.');
35
36           // Submit with a file, but with an invalid form token. Ensure the file
37           // was not saved.
38           $last_fid_prior = $this->getLastFileId();
39           $this->drupalGet($path);
40           $form_token_field = $this->assertSession()->hiddenFieldExists('form_token');
41           $form_token_field->setValue('invalid token');
42           $edit = [
43             $file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri()),
44           ];
45           $this->drupalPostForm(NULL, $edit, t('Save'));
46           $this->assertText('The form has become outdated. Copy any unsaved work in the form below');
47           $last_fid = $this->getLastFileId();
48           $this->assertEqual($last_fid_prior, $last_fid, 'File was not saved when uploaded with an invalid form token.');
49
50           // Submit a new file, without using the Upload button.
51           $last_fid_prior = $this->getLastFileId();
52           $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
53           $this->drupalPostForm($path, $edit, t('Save'));
54           $last_fid = $this->getLastFileId();
55           $this->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');
56           $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [$last_fid])]), 'Submit handler has correct file info.');
57
58           // Submit no new input, but with a default file.
59           $this->drupalPostForm($path . '/' . $last_fid, [], t('Save'));
60           $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [$last_fid])]), 'Empty submission did not change an existing file.');
61
62           // Upload, then Submit.
63           $last_fid_prior = $this->getLastFileId();
64           $this->drupalGet($path);
65           $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
66           $this->drupalPostForm(NULL, $edit, t('Upload'));
67           $last_fid = $this->getLastFileId();
68           $this->assertTrue($last_fid > $last_fid_prior, 'New file got uploaded.');
69           $this->drupalPostForm(NULL, [], t('Save'));
70           $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [$last_fid])]), 'Submit handler has correct file info.');
71
72           // Remove, then Submit.
73           $remove_button_title = $multiple ? t('Remove selected') : t('Remove');
74           $remove_edit = [];
75           if ($multiple) {
76             $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
77             $remove_edit = [$selected_checkbox => '1'];
78           }
79           $this->drupalGet($path . '/' . $last_fid);
80           $this->drupalPostForm(NULL, $remove_edit, $remove_button_title);
81           $this->drupalPostForm(NULL, [], t('Save'));
82           $this->assertRaw(t('The file ids are %fids.', ['%fids' => '']), 'Submission after file removal was successful.');
83
84           // Upload, then Remove, then Submit.
85           $this->drupalGet($path);
86           $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
87           $this->drupalPostForm(NULL, $edit, t('Upload'));
88           $remove_edit = [];
89           if ($multiple) {
90             $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this->getLastFileId() . '][selected]';
91             $remove_edit = [$selected_checkbox => '1'];
92           }
93           $this->drupalPostForm(NULL, $remove_edit, $remove_button_title);
94
95           $this->drupalPostForm(NULL, [], t('Save'));
96           $this->assertRaw(t('The file ids are %fids.', ['%fids' => '']), 'Submission after file upload and removal was successful.');
97         }
98       }
99     }
100
101     // The multiple file upload has additional conditions that need checking.
102     $path = 'file/test/1/1/1';
103     $edit = ['files[nested_file][]' => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
104     $fid_list = [];
105
106     $this->drupalGet($path);
107
108     // Add a single file to the upload field.
109     $this->drupalPostForm(NULL, $edit, t('Upload'));
110     $fid_list[] = $this->getLastFileId();
111     $this->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[0] . '][selected]"]', NULL, 'First file successfully uploaded to multiple file element.');
112
113     // Add another file to the same upload field.
114     $this->drupalPostForm(NULL, $edit, t('Upload'));
115     $fid_list[] = $this->getLastFileId();
116     $this->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[1] . '][selected]"]', NULL, 'Second file successfully uploaded to multiple file element.');
117
118     // Save the entire form.
119     $this->drupalPostForm(NULL, [], t('Save'));
120     $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', $fid_list)]), 'Two files saved into a single multiple file element.');
121
122     // Delete only the first file.
123     $edit = [
124       'nested[file][file_' . $fid_list[0] . '][selected]' => '1',
125     ];
126     $this->drupalPostForm($path . '/' . implode(',', $fid_list), $edit, t('Remove selected'));
127
128     // Check that the first file has been deleted but not the second.
129     $this->assertNoFieldByXpath('//input[@name="nested[file][file_' . $fid_list[0] . '][selected]"]', NULL, 'An individual file can be deleted from a multiple file element.');
130     $this->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[1] . '][selected]"]', NULL, 'Second individual file not deleted when the first file is deleted from a multiple file element.');
131   }
132
133   /**
134    * Ensure that warning is shown if file on the field has been removed.
135    */
136   public function testManagedFileRemoved() {
137     $this->drupalGet('file/test/1/0/1');
138     $test_file = $this->getTestFile('text');
139     $file_field_name = 'files[nested_file][]';
140
141     $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
142     $this->drupalPostForm(NULL, $edit, t('Upload'));
143
144     $fid = $this->getLastFileId();
145     $file = \Drupal::entityManager()->getStorage('file')->load($fid);
146     $file->delete();
147
148     $this->drupalPostForm(NULL, $edit, t('Upload'));
149     // We expect the title 'Managed <em>file & butter</em>' which got escaped
150     // via a t() call before.
151     $this->assertRaw('The file referenced by the Managed <em>file &amp; butter</em> field does not exist.');
152   }
153
154   /**
155    * Ensure a file entity can be saved when the file does not exist on disk.
156    */
157   public function testFileRemovedFromDisk() {
158     $this->drupalGet('file/test/1/0/1');
159     $test_file = $this->getTestFile('text');
160     $file_field_name = 'files[nested_file][]';
161
162     $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
163     $this->drupalPostForm(NULL, $edit, t('Upload'));
164     $this->drupalPostForm(NULL, [], t('Save'));
165
166     $fid = $this->getLastFileId();
167     /** @var $file \Drupal\file\FileInterface */
168     $file = $this->container->get('entity_type.manager')->getStorage('file')->load($fid);
169     $file->setPermanent();
170     $file->save();
171     $this->assertTrue(file_unmanaged_delete($file->getFileUri()));
172     $file->save();
173     $this->assertTrue($file->isPermanent());
174     $file->delete();
175   }
176
177   /**
178    * Verify that unused permanent files can be used.
179    */
180   public function testUnusedPermanentFileValidation() {
181
182     // Create a permanent file without usages.
183     $file = $this->getTestFile('image');
184     $file->setPermanent();
185     $file->save();
186
187     // By default, unused files are no longer marked temporary, and it must be
188     // allowed to reference an unused file.
189     $this->drupalGet('file/test/1/0/1/' . $file->id());
190     $this->drupalPostForm(NULL, [], 'Save');
191     $this->assertNoText('The file used in the Managed file &amp; butter field may not be referenced.');
192     $this->assertText('The file ids are ' . $file->id());
193
194     // Enable marking unused files as temporary, unused permanent files must not
195     // be referenced now.
196     $this->config('file.settings')
197       ->set('make_unused_managed_files_temporary', TRUE)
198       ->save();
199     $this->drupalGet('file/test/1/0/1/' . $file->id());
200     $this->drupalPostForm(NULL, [], 'Save');
201     $this->assertText('The file used in the Managed file &amp; butter field may not be referenced.');
202     $this->assertNoText('The file ids are ' . $file->id());
203
204     // Make the file temporary, now using it is allowed.
205     $file->setTemporary();
206     $file->save();
207
208     $this->drupalGet('file/test/1/0/1/' . $file->id());
209     $this->drupalPostForm(NULL, [], 'Save');
210     $this->assertNoText('The file used in the Managed file &amp; butter field may not be referenced.');
211     $this->assertText('The file ids are ' . $file->id());
212
213     // Make the file permanent again and add a usage from itself, referencing is
214     // still allowed.
215     $file->setPermanent();
216     $file->save();
217
218     /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
219     $file_usage = \Drupal::service('file.usage');
220     $file_usage->add($file, 'file', 'file', $file->id());
221
222     $this->drupalGet('file/test/1/0/1/' . $file->id());
223     $this->drupalPostForm(NULL, [], 'Save');
224     $this->assertNoText('The file used in the Managed file &amp; butter field may not be referenced.');
225     $this->assertText('The file ids are ' . $file->id());
226   }
227
228 }