3 namespace Drupal\file\Tests;
6 * Tests the 'managed_file' element type.
9 * @todo Create a FileTestBase class and move FileFieldTestBase methods
10 * that aren't related to fields into it.
12 class FileManagedFileElementTest extends FileFieldTestBase {
14 * Tests the managed_file element type.
16 public function testManagedFile() {
17 // Check that $element['#size'] is passed to the child upload element.
18 $this->drupalGet('file/test');
19 $this->assertFieldByXpath('//input[@name="files[nested_file]" and @size="13"]', NULL, 'The custom #size attribute is passed to the child upload element.');
21 // Perform the tests with all permutations of $form['#tree'],
22 // $element['#extended'], and $element['#multiple'].
23 $test_file = $this->getTestFile('text');
24 foreach ([0, 1] as $tree) {
25 foreach ([0, 1] as $extended) {
26 foreach ([0, 1] as $multiple) {
27 $path = 'file/test/' . $tree . '/' . $extended . '/' . $multiple;
28 $input_base_name = $tree ? 'nested_file' : 'file';
29 $file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']';
31 // Submit without a file.
32 $this->drupalPostForm($path, [], t('Save'));
33 $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [])]), 'Submitted without a file.');
35 // Submit with a file, but with an invalid form token. Ensure the file
37 $last_fid_prior = $this->getLastFileId();
39 $file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri()),
40 'form_token' => 'invalid token',
42 $this->drupalPostForm($path, $edit, t('Save'));
43 $this->assertText('The form has become outdated. Copy any unsaved work in the form below');
44 $last_fid = $this->getLastFileId();
45 $this->assertEqual($last_fid_prior, $last_fid, 'File was not saved when uploaded with an invalid form token.');
47 // Submit a new file, without using the Upload button.
48 $last_fid_prior = $this->getLastFileId();
49 $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
50 $this->drupalPostForm($path, $edit, t('Save'));
51 $last_fid = $this->getLastFileId();
52 $this->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');
53 $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [$last_fid])]), 'Submit handler has correct file info.');
55 // Submit no new input, but with a default file.
56 $this->drupalPostForm($path . '/' . $last_fid, [], t('Save'));
57 $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [$last_fid])]), 'Empty submission did not change an existing file.');
59 // Now, test the Upload and Remove buttons, with and without Ajax.
60 foreach ([FALSE, TRUE] as $ajax) {
61 // Upload, then Submit.
62 $last_fid_prior = $this->getLastFileId();
63 $this->drupalGet($path);
64 $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
66 $this->drupalPostAjaxForm(NULL, $edit, $input_base_name . '_upload_button');
69 $this->drupalPostForm(NULL, $edit, t('Upload'));
71 $last_fid = $this->getLastFileId();
72 $this->assertTrue($last_fid > $last_fid_prior, 'New file got uploaded.');
73 $this->drupalPostForm(NULL, [], t('Save'));
74 $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [$last_fid])]), 'Submit handler has correct file info.');
76 // Remove, then Submit.
77 $remove_button_title = $multiple ? t('Remove selected') : t('Remove');
80 $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
81 $remove_edit = [$selected_checkbox => '1'];
83 $this->drupalGet($path . '/' . $last_fid);
85 $this->drupalPostAjaxForm(NULL, $remove_edit, $input_base_name . '_remove_button');
88 $this->drupalPostForm(NULL, $remove_edit, $remove_button_title);
90 $this->drupalPostForm(NULL, [], t('Save'));
91 $this->assertRaw(t('The file ids are %fids.', ['%fids' => '']), 'Submission after file removal was successful.');
93 // Upload, then Remove, then Submit.
94 $this->drupalGet($path);
95 $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
97 $this->drupalPostAjaxForm(NULL, $edit, $input_base_name . '_upload_button');
100 $this->drupalPostForm(NULL, $edit, t('Upload'));
104 $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this->getLastFileId() . '][selected]';
105 $remove_edit = [$selected_checkbox => '1'];
108 $this->drupalPostAjaxForm(NULL, $remove_edit, $input_base_name . '_remove_button');
111 $this->drupalPostForm(NULL, $remove_edit, $remove_button_title);
114 $this->drupalPostForm(NULL, [], t('Save'));
115 $this->assertRaw(t('The file ids are %fids.', ['%fids' => '']), 'Submission after file upload and removal was successful.');
121 // The multiple file upload has additional conditions that need checking.
122 $path = 'file/test/1/1/1';
123 $edit = ['files[nested_file][]' => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
126 $this->drupalGet($path);
128 // Add a single file to the upload field.
129 $this->drupalPostForm(NULL, $edit, t('Upload'));
130 $fid_list[] = $this->getLastFileId();
131 $this->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[0] . '][selected]"]', NULL, 'First file successfully uploaded to multiple file element.');
133 // Add another file to the same upload field.
134 $this->drupalPostForm(NULL, $edit, t('Upload'));
135 $fid_list[] = $this->getLastFileId();
136 $this->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[1] . '][selected]"]', NULL, 'Second file successfully uploaded to multiple file element.');
138 // Save the entire form.
139 $this->drupalPostForm(NULL, [], t('Save'));
140 $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', $fid_list)]), 'Two files saved into a single multiple file element.');
142 // Delete only the first file.
144 'nested[file][file_' . $fid_list[0] . '][selected]' => '1',
146 $this->drupalPostForm($path . '/' . implode(',', $fid_list), $edit, t('Remove selected'));
148 // Check that the first file has been deleted but not the second.
149 $this->assertNoFieldByXpath('//input[@name="nested[file][file_' . $fid_list[0] . '][selected]"]', NULL, 'An individual file can be deleted from a multiple file element.');
150 $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.');
154 * Ensure that warning is shown if file on the field has been removed.
156 public function testManagedFileRemoved() {
157 $this->drupalGet('file/test/1/0/1');
158 $test_file = $this->getTestFile('text');
159 $file_field_name = 'files[nested_file][]';
161 $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
162 $this->drupalPostForm(NULL, $edit, t('Upload'));
164 $fid = $this->getLastFileId();
165 $file = \Drupal::entityManager()->getStorage('file')->load($fid);
168 $this->drupalPostForm(NULL, $edit, t('Upload'));
169 // We expect the title 'Managed <em>file & butter</em>' which got escaped
170 // via a t() call before.
171 $this->assertRaw('The file referenced by the Managed <em>file & butter</em> field does not exist.');
175 * Ensure a file entity can be saved when the file does not exist on disk.
177 public function testFileRemovedFromDisk() {
178 $this->drupalGet('file/test/1/0/1');
179 $test_file = $this->getTestFile('text');
180 $file_field_name = 'files[nested_file][]';
182 $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
183 $this->drupalPostForm(NULL, $edit, t('Upload'));
184 $this->drupalPostForm(NULL, [], t('Save'));
186 $fid = $this->getLastFileId();
187 /** @var $file \Drupal\file\FileInterface */
188 $file = $this->container->get('entity_type.manager')->getStorage('file')->load($fid);
189 $file->setPermanent();
191 $this->assertTrue(file_unmanaged_delete($file->getFileUri()));
193 $this->assertTrue($file->isPermanent());
198 * Verify that unused permanent files can be used.
200 public function testUnusedPermanentFileValidation() {
202 // Create a permanent file without usages.
203 $file = $this->getTestFile('image');
204 $file->setPermanent();
207 // By default, unused files are no longer marked temporary, and it must be
208 // allowed to reference an unused file.
209 $this->drupalGet('file/test/1/0/1/' . $file->id());
210 $this->drupalPostForm(NULL, [], 'Save');
211 $this->assertNoText('The file used in the Managed file & butter field may not be referenced.');
212 $this->assertText('The file ids are ' . $file->id());
214 // Enable marking unused files as tempory, unused permanent files must not
215 // be referenced now.
216 $this->config('file.settings')
217 ->set('make_unused_managed_files_temporary', TRUE)
219 $this->drupalGet('file/test/1/0/1/' . $file->id());
220 $this->drupalPostForm(NULL, [], 'Save');
221 $this->assertText('The file used in the Managed file & butter field may not be referenced.');
222 $this->assertNoText('The file ids are ' . $file->id());
224 // Make the file temporary, now using it is allowed.
225 $file->setTemporary();
228 $this->drupalGet('file/test/1/0/1/' . $file->id());
229 $this->drupalPostForm(NULL, [], 'Save');
230 $this->assertNoText('The file used in the Managed file & butter field may not be referenced.');
231 $this->assertText('The file ids are ' . $file->id());
233 // Make the file permanent again and add a usage from itself, referencing is
235 $file->setPermanent();
238 /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
239 $file_usage = \Drupal::service('file.usage');
240 $file_usage->add($file, 'file', 'file', $file->id());
242 $this->drupalGet('file/test/1/0/1/' . $file->id());
243 $this->drupalPostForm(NULL, [], 'Save');
244 $this->assertNoText('The file used in the Managed file & butter field may not be referenced.');
245 $this->assertText('The file ids are ' . $file->id());