Chris@0: drupalGet('file/test');
Chris@0: $this->assertFieldByXpath('//input[@name="files[nested_file]" and @size="13"]', NULL, 'The custom #size attribute is passed to the child upload element.');
Chris@0:
Chris@0: // Perform the tests with all permutations of $form['#tree'],
Chris@0: // $element['#extended'], and $element['#multiple'].
Chris@0: $test_file = $this->getTestFile('text');
Chris@0: foreach ([0, 1] as $tree) {
Chris@0: foreach ([0, 1] as $extended) {
Chris@0: foreach ([0, 1] as $multiple) {
Chris@0: $path = 'file/test/' . $tree . '/' . $extended . '/' . $multiple;
Chris@0: $input_base_name = $tree ? 'nested_file' : 'file';
Chris@0: $file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']';
Chris@0:
Chris@0: // Submit without a file.
Chris@0: $this->drupalPostForm($path, [], t('Save'));
Chris@0: $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [])]), 'Submitted without a file.');
Chris@0:
Chris@0: // Submit with a file, but with an invalid form token. Ensure the file
Chris@0: // was not saved.
Chris@0: $last_fid_prior = $this->getLastFileId();
Chris@0: $edit = [
Chris@0: $file_field_name => drupal_realpath($test_file->getFileUri()),
Chris@0: 'form_token' => 'invalid token',
Chris@0: ];
Chris@0: $this->drupalPostForm($path, $edit, t('Save'));
Chris@0: $this->assertText('The form has become outdated. Copy any unsaved work in the form below');
Chris@0: $last_fid = $this->getLastFileId();
Chris@0: $this->assertEqual($last_fid_prior, $last_fid, 'File was not saved when uploaded with an invalid form token.');
Chris@0:
Chris@0: // Submit a new file, without using the Upload button.
Chris@0: $last_fid_prior = $this->getLastFileId();
Chris@0: $edit = [$file_field_name => drupal_realpath($test_file->getFileUri())];
Chris@0: $this->drupalPostForm($path, $edit, t('Save'));
Chris@0: $last_fid = $this->getLastFileId();
Chris@0: $this->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');
Chris@0: $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [$last_fid])]), 'Submit handler has correct file info.');
Chris@0:
Chris@0: // Submit no new input, but with a default file.
Chris@0: $this->drupalPostForm($path . '/' . $last_fid, [], t('Save'));
Chris@0: $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [$last_fid])]), 'Empty submission did not change an existing file.');
Chris@0:
Chris@0: // Now, test the Upload and Remove buttons, with and without Ajax.
Chris@0: foreach ([FALSE, TRUE] as $ajax) {
Chris@0: // Upload, then Submit.
Chris@0: $last_fid_prior = $this->getLastFileId();
Chris@0: $this->drupalGet($path);
Chris@0: $edit = [$file_field_name => drupal_realpath($test_file->getFileUri())];
Chris@0: if ($ajax) {
Chris@0: $this->drupalPostAjaxForm(NULL, $edit, $input_base_name . '_upload_button');
Chris@0: }
Chris@0: else {
Chris@0: $this->drupalPostForm(NULL, $edit, t('Upload'));
Chris@0: }
Chris@0: $last_fid = $this->getLastFileId();
Chris@0: $this->assertTrue($last_fid > $last_fid_prior, 'New file got uploaded.');
Chris@0: $this->drupalPostForm(NULL, [], t('Save'));
Chris@0: $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', [$last_fid])]), 'Submit handler has correct file info.');
Chris@0:
Chris@0: // Remove, then Submit.
Chris@0: $remove_button_title = $multiple ? t('Remove selected') : t('Remove');
Chris@0: $remove_edit = [];
Chris@0: if ($multiple) {
Chris@0: $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
Chris@0: $remove_edit = [$selected_checkbox => '1'];
Chris@0: }
Chris@0: $this->drupalGet($path . '/' . $last_fid);
Chris@0: if ($ajax) {
Chris@0: $this->drupalPostAjaxForm(NULL, $remove_edit, $input_base_name . '_remove_button');
Chris@0: }
Chris@0: else {
Chris@0: $this->drupalPostForm(NULL, $remove_edit, $remove_button_title);
Chris@0: }
Chris@0: $this->drupalPostForm(NULL, [], t('Save'));
Chris@0: $this->assertRaw(t('The file ids are %fids.', ['%fids' => '']), 'Submission after file removal was successful.');
Chris@0:
Chris@0: // Upload, then Remove, then Submit.
Chris@0: $this->drupalGet($path);
Chris@0: $edit = [$file_field_name => drupal_realpath($test_file->getFileUri())];
Chris@0: if ($ajax) {
Chris@0: $this->drupalPostAjaxForm(NULL, $edit, $input_base_name . '_upload_button');
Chris@0: }
Chris@0: else {
Chris@0: $this->drupalPostForm(NULL, $edit, t('Upload'));
Chris@0: }
Chris@0: $remove_edit = [];
Chris@0: if ($multiple) {
Chris@0: $selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this->getLastFileId() . '][selected]';
Chris@0: $remove_edit = [$selected_checkbox => '1'];
Chris@0: }
Chris@0: if ($ajax) {
Chris@0: $this->drupalPostAjaxForm(NULL, $remove_edit, $input_base_name . '_remove_button');
Chris@0: }
Chris@0: else {
Chris@0: $this->drupalPostForm(NULL, $remove_edit, $remove_button_title);
Chris@0: }
Chris@0:
Chris@0: $this->drupalPostForm(NULL, [], t('Save'));
Chris@0: $this->assertRaw(t('The file ids are %fids.', ['%fids' => '']), 'Submission after file upload and removal was successful.');
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: // The multiple file upload has additional conditions that need checking.
Chris@0: $path = 'file/test/1/1/1';
Chris@0: $edit = ['files[nested_file][]' => drupal_realpath($test_file->getFileUri())];
Chris@0: $fid_list = [];
Chris@0:
Chris@0: $this->drupalGet($path);
Chris@0:
Chris@0: // Add a single file to the upload field.
Chris@0: $this->drupalPostForm(NULL, $edit, t('Upload'));
Chris@0: $fid_list[] = $this->getLastFileId();
Chris@0: $this->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[0] . '][selected]"]', NULL, 'First file successfully uploaded to multiple file element.');
Chris@0:
Chris@0: // Add another file to the same upload field.
Chris@0: $this->drupalPostForm(NULL, $edit, t('Upload'));
Chris@0: $fid_list[] = $this->getLastFileId();
Chris@0: $this->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[1] . '][selected]"]', NULL, 'Second file successfully uploaded to multiple file element.');
Chris@0:
Chris@0: // Save the entire form.
Chris@0: $this->drupalPostForm(NULL, [], t('Save'));
Chris@0: $this->assertRaw(t('The file ids are %fids.', ['%fids' => implode(',', $fid_list)]), 'Two files saved into a single multiple file element.');
Chris@0:
Chris@0: // Delete only the first file.
Chris@0: $edit = [
Chris@0: 'nested[file][file_' . $fid_list[0] . '][selected]' => '1',
Chris@0: ];
Chris@0: $this->drupalPostForm($path . '/' . implode(',', $fid_list), $edit, t('Remove selected'));
Chris@0:
Chris@0: // Check that the first file has been deleted but not the second.
Chris@0: $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: $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: }
Chris@0:
Chris@0: /**
Chris@0: * Ensure that warning is shown if file on the field has been removed.
Chris@0: */
Chris@0: public function testManagedFileRemoved() {
Chris@0: $this->drupalGet('file/test/1/0/1');
Chris@0: $test_file = $this->getTestFile('text');
Chris@0: $file_field_name = 'files[nested_file][]';
Chris@0:
Chris@0: $edit = [$file_field_name => drupal_realpath($test_file->getFileUri())];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Upload'));
Chris@0:
Chris@0: $fid = $this->getLastFileId();
Chris@0: $file = \Drupal::entityManager()->getStorage('file')->load($fid);
Chris@0: $file->delete();
Chris@0:
Chris@0: $this->drupalPostForm(NULL, $edit, t('Upload'));
Chris@0: // We expect the title 'Managed file & butter' which got escaped
Chris@0: // via a t() call before.
Chris@0: $this->assertRaw('The file referenced by the Managed file & butter field does not exist.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Ensure a file entity can be saved when the file does not exist on disk.
Chris@0: */
Chris@0: public function testFileRemovedFromDisk() {
Chris@0: $this->drupalGet('file/test/1/0/1');
Chris@0: $test_file = $this->getTestFile('text');
Chris@0: $file_field_name = 'files[nested_file][]';
Chris@0:
Chris@0: $edit = [$file_field_name => drupal_realpath($test_file->getFileUri())];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Upload'));
Chris@0: $this->drupalPostForm(NULL, [], t('Save'));
Chris@0:
Chris@0: $fid = $this->getLastFileId();
Chris@0: /** @var $file \Drupal\file\FileInterface */
Chris@0: $file = $this->container->get('entity_type.manager')->getStorage('file')->load($fid);
Chris@0: $file->setPermanent();
Chris@0: $file->save();
Chris@0: $this->assertTrue(file_unmanaged_delete($file->getFileUri()));
Chris@0: $file->save();
Chris@0: $this->assertTrue($file->isPermanent());
Chris@0: $file->delete();
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Verify that unused permanent files can be used.
Chris@0: */
Chris@0: public function testUnusedPermanentFileValidation() {
Chris@0:
Chris@0: // Create a permanent file without usages.
Chris@0: $file = $this->getTestFile('image');
Chris@0: $file->setPermanent();
Chris@0: $file->save();
Chris@0:
Chris@0: // By default, unused files are no longer marked temporary, and it must be
Chris@0: // allowed to reference an unused file.
Chris@0: $this->drupalGet('file/test/1/0/1/' . $file->id());
Chris@0: $this->drupalPostForm(NULL, [], 'Save');
Chris@0: $this->assertNoText('The file used in the Managed file & butter field may not be referenced.');
Chris@0: $this->assertText('The file ids are ' . $file->id());
Chris@0:
Chris@0: // Enable marking unused files as tempory, unused permanent files must not
Chris@0: // be referenced now.
Chris@0: $this->config('file.settings')
Chris@0: ->set('make_unused_managed_files_temporary', TRUE)
Chris@0: ->save();
Chris@0: $this->drupalGet('file/test/1/0/1/' . $file->id());
Chris@0: $this->drupalPostForm(NULL, [], 'Save');
Chris@0: $this->assertText('The file used in the Managed file & butter field may not be referenced.');
Chris@0: $this->assertNoText('The file ids are ' . $file->id());
Chris@0:
Chris@0: // Make the file temporary, now using it is allowed.
Chris@0: $file->setTemporary();
Chris@0: $file->save();
Chris@0:
Chris@0: $this->drupalGet('file/test/1/0/1/' . $file->id());
Chris@0: $this->drupalPostForm(NULL, [], 'Save');
Chris@0: $this->assertNoText('The file used in the Managed file & butter field may not be referenced.');
Chris@0: $this->assertText('The file ids are ' . $file->id());
Chris@0:
Chris@0: // Make the file permanent again and add a usage from itself, referencing is
Chris@0: // still allowed.
Chris@0: $file->setPermanent();
Chris@0: $file->save();
Chris@0:
Chris@0: /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
Chris@0: $file_usage = \Drupal::service('file.usage');
Chris@0: $file_usage->add($file, 'file', 'file', $file->id());
Chris@0:
Chris@0: $this->drupalGet('file/test/1/0/1/' . $file->id());
Chris@0: $this->drupalPostForm(NULL, [], 'Save');
Chris@0: $this->assertNoText('The file used in the Managed file & butter field may not be referenced.');
Chris@0: $this->assertText('The file ids are ' . $file->id());
Chris@0: }
Chris@0:
Chris@0: }