f48532e8d8e0019bd6f6f249c1d40918037306c6
[yaffs-website] / web / core / modules / file / src / Tests / FileManagedFileElementTest.php
1 <?php
2
3 namespace Drupal\file\Tests;
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    * Tests the managed_file element type.
15    */
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.');
20
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 . ']';
30
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.');
34
35           // Submit with a file, but with an invalid form token. Ensure the file
36           // was not saved.
37           $last_fid_prior = $this->getLastFileId();
38           $edit = [
39             $file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri()),
40             'form_token' => 'invalid token',
41           ];
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.');
46
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.');
54
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.');
58
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())];
65             if ($ajax) {
66               $this->drupalPostAjaxForm(NULL, $edit, $input_base_name . '_upload_button');
67             }
68             else {
69               $this->drupalPostForm(NULL, $edit, t('Upload'));
70             }
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.');
75
76             // Remove, then Submit.
77             $remove_button_title = $multiple ? t('Remove selected') : t('Remove');
78             $remove_edit = [];
79             if ($multiple) {
80               $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
81               $remove_edit = [$selected_checkbox => '1'];
82             }
83             $this->drupalGet($path . '/' . $last_fid);
84             if ($ajax) {
85               $this->drupalPostAjaxForm(NULL, $remove_edit, $input_base_name . '_remove_button');
86             }
87             else {
88               $this->drupalPostForm(NULL, $remove_edit, $remove_button_title);
89             }
90             $this->drupalPostForm(NULL, [], t('Save'));
91             $this->assertRaw(t('The file ids are %fids.', ['%fids' => '']), 'Submission after file removal was successful.');
92
93             // Upload, then Remove, then Submit.
94             $this->drupalGet($path);
95             $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
96             if ($ajax) {
97               $this->drupalPostAjaxForm(NULL, $edit, $input_base_name . '_upload_button');
98             }
99             else {
100               $this->drupalPostForm(NULL, $edit, t('Upload'));
101             }
102             $remove_edit = [];
103             if ($multiple) {
104               $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this->getLastFileId() . '][selected]';
105               $remove_edit = [$selected_checkbox => '1'];
106             }
107             if ($ajax) {
108               $this->drupalPostAjaxForm(NULL, $remove_edit, $input_base_name . '_remove_button');
109             }
110             else {
111               $this->drupalPostForm(NULL, $remove_edit, $remove_button_title);
112             }
113
114             $this->drupalPostForm(NULL, [], t('Save'));
115             $this->assertRaw(t('The file ids are %fids.', ['%fids' => '']), 'Submission after file upload and removal was successful.');
116           }
117         }
118       }
119     }
120
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())];
124     $fid_list = [];
125
126     $this->drupalGet($path);
127
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.');
132
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.');
137
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.');
141
142     // Delete only the first file.
143     $edit = [
144       'nested[file][file_' . $fid_list[0] . '][selected]' => '1',
145     ];
146     $this->drupalPostForm($path . '/' . implode(',', $fid_list), $edit, t('Remove selected'));
147
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.');
151   }
152
153   /**
154    * Ensure that warning is shown if file on the field has been removed.
155    */
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][]';
160
161     $edit = [$file_field_name => \Drupal::service('file_system')->realpath($test_file->getFileUri())];
162     $this->drupalPostForm(NULL, $edit, t('Upload'));
163
164     $fid = $this->getLastFileId();
165     $file = \Drupal::entityManager()->getStorage('file')->load($fid);
166     $file->delete();
167
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 &amp; butter</em> field does not exist.');
172   }
173
174   /**
175    * Ensure a file entity can be saved when the file does not exist on disk.
176    */
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][]';
181
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'));
185
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();
190     $file->save();
191     $this->assertTrue(file_unmanaged_delete($file->getFileUri()));
192     $file->save();
193     $this->assertTrue($file->isPermanent());
194     $file->delete();
195   }
196
197   /**
198    * Verify that unused permanent files can be used.
199    */
200   public function testUnusedPermanentFileValidation() {
201
202     // Create a permanent file without usages.
203     $file = $this->getTestFile('image');
204     $file->setPermanent();
205     $file->save();
206
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 &amp; butter field may not be referenced.');
212     $this->assertText('The file ids are ' . $file->id());
213
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)
218       ->save();
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 &amp; butter field may not be referenced.');
222     $this->assertNoText('The file ids are ' . $file->id());
223
224     // Make the file temporary, now using it is allowed.
225     $file->setTemporary();
226     $file->save();
227
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 &amp; butter field may not be referenced.');
231     $this->assertText('The file ids are ' . $file->id());
232
233     // Make the file permanent again and add a usage from itself, referencing is
234     // still allowed.
235     $file->setPermanent();
236     $file->save();
237
238     /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
239     $file_usage = \Drupal::service('file.usage');
240     $file_usage->add($file, 'file', 'file', $file->id());
241
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 &amp; butter field may not be referenced.');
245     $this->assertText('The file ids are ' . $file->id());
246   }
247
248 }