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