3 namespace Drupal\Tests\file\Functional;
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 {
15 * Tests the managed_file element type.
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.');
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 . ']';
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.');
36 // Submit with a file, but with an invalid form token. Ensure the file
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');
43 $file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri()),
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.');
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.');
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.');
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.');
72 // Remove, then Submit.
73 $remove_button_title = $multiple ? t('Remove selected') : t('Remove');
76 $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
77 $remove_edit = [$selected_checkbox => '1'];
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.');
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'));
90 $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this->getLastFileId() . '][selected]';
91 $remove_edit = [$selected_checkbox => '1'];
93 $this->drupalPostForm(NULL, $remove_edit, $remove_button_title);
95 $this->drupalPostForm(NULL, [], t('Save'));
96 $this->assertRaw(t('The file ids are %fids.', ['%fids' => '']), 'Submission after file upload and removal was successful.');
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())];
106 $this->drupalGet($path);
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.');
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.');
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.');
122 // Delete only the first file.
124 'nested[file][file_' . $fid_list[0] . '][selected]' => '1',
126 $this->drupalPostForm($path . '/' . implode(',', $fid_list), $edit, t('Remove selected'));
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.');
134 * Ensure that warning is shown if file on the field has been removed.
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][]';
141 $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
142 $this->drupalPostForm(NULL, $edit, t('Upload'));
144 $fid = $this->getLastFileId();
145 $file = \Drupal::entityManager()->getStorage('file')->load($fid);
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 & butter</em> field does not exist.');
155 * Ensure a file entity can be saved when the file does not exist on disk.
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][]';
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'));
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();
171 $this->assertTrue(file_unmanaged_delete($file->getFileUri()));
173 $this->assertTrue($file->isPermanent());
178 * Verify that unused permanent files can be used.
180 public function testUnusedPermanentFileValidation() {
182 // Create a permanent file without usages.
183 $file = $this->getTestFile('image');
184 $file->setPermanent();
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 & butter field may not be referenced.');
192 $this->assertText('The file ids are ' . $file->id());
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)
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 & butter field may not be referenced.');
202 $this->assertNoText('The file ids are ' . $file->id());
204 // Make the file temporary, now using it is allowed.
205 $file->setTemporary();
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 & butter field may not be referenced.');
211 $this->assertText('The file ids are ' . $file->id());
213 // Make the file permanent again and add a usage from itself, referencing is
215 $file->setPermanent();
218 /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
219 $file_usage = \Drupal::service('file.usage');
220 $file_usage->add($file, 'file', 'file', $file->id());
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 & butter field may not be referenced.');
225 $this->assertText('The file ids are ' . $file->id());