View source
<?php
namespace Drupal\file\Tests;
class FileManagedFileElementTest extends FileFieldTestBase {
function testManagedFile() {
$this
->drupalGet('file/test');
$this
->assertFieldByXpath('//input[@name="files[nested_file]" and @size="13"]', NULL, 'The custom #size attribute is passed to the child upload element.');
$test_file = $this
->getTestFile('text');
foreach (array(
0,
1,
) as $tree) {
foreach (array(
0,
1,
) as $extended) {
foreach (array(
0,
1,
) as $multiple) {
$path = 'file/test/' . $tree . '/' . $extended . '/' . $multiple;
$input_base_name = $tree ? 'nested_file' : 'file';
$file_field_name = $multiple ? 'files[' . $input_base_name . '][]' : 'files[' . $input_base_name . ']';
$this
->drupalPostForm($path, array(), t('Save'));
$this
->assertRaw(t('The file ids are %fids.', array(
'%fids' => implode(',', array()),
)), 'Submitted without a file.');
$last_fid_prior = $this
->getLastFileId();
$edit = [
$file_field_name => drupal_realpath($test_file
->getFileUri()),
'form_token' => 'invalid token',
];
$this
->drupalPostForm($path, $edit, t('Save'));
$this
->assertText('The form has become outdated. Copy any unsaved work in the form below');
$last_fid = $this
->getLastFileId();
$this
->assertEqual($last_fid_prior, $last_fid, 'File was not saved when uploaded with an invalid form token.');
$last_fid_prior = $this
->getLastFileId();
$edit = array(
$file_field_name => drupal_realpath($test_file
->getFileUri()),
);
$this
->drupalPostForm($path, $edit, t('Save'));
$last_fid = $this
->getLastFileId();
$this
->assertTrue($last_fid > $last_fid_prior, 'New file got saved.');
$this
->assertRaw(t('The file ids are %fids.', array(
'%fids' => implode(',', array(
$last_fid,
)),
)), 'Submit handler has correct file info.');
$this
->drupalPostForm($path . '/' . $last_fid, array(), t('Save'));
$this
->assertRaw(t('The file ids are %fids.', array(
'%fids' => implode(',', array(
$last_fid,
)),
)), 'Empty submission did not change an existing file.');
foreach (array(
FALSE,
TRUE,
) as $ajax) {
$last_fid_prior = $this
->getLastFileId();
$this
->drupalGet($path);
$edit = array(
$file_field_name => drupal_realpath($test_file
->getFileUri()),
);
if ($ajax) {
$this
->drupalPostAjaxForm(NULL, $edit, $input_base_name . '_upload_button');
}
else {
$this
->drupalPostForm(NULL, $edit, t('Upload'));
}
$last_fid = $this
->getLastFileId();
$this
->assertTrue($last_fid > $last_fid_prior, 'New file got uploaded.');
$this
->drupalPostForm(NULL, array(), t('Save'));
$this
->assertRaw(t('The file ids are %fids.', array(
'%fids' => implode(',', array(
$last_fid,
)),
)), 'Submit handler has correct file info.');
$remove_button_title = $multiple ? t('Remove selected') : t('Remove');
$remove_edit = array();
if ($multiple) {
$selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
$remove_edit = array(
$selected_checkbox => '1',
);
}
$this
->drupalGet($path . '/' . $last_fid);
if ($ajax) {
$this
->drupalPostAjaxForm(NULL, $remove_edit, $input_base_name . '_remove_button');
}
else {
$this
->drupalPostForm(NULL, $remove_edit, $remove_button_title);
}
$this
->drupalPostForm(NULL, array(), t('Save'));
$this
->assertRaw(t('The file ids are %fids.', array(
'%fids' => '',
)), 'Submission after file removal was successful.');
$this
->drupalGet($path);
$edit = array(
$file_field_name => drupal_realpath($test_file
->getFileUri()),
);
if ($ajax) {
$this
->drupalPostAjaxForm(NULL, $edit, $input_base_name . '_upload_button');
}
else {
$this
->drupalPostForm(NULL, $edit, t('Upload'));
}
$remove_edit = array();
if ($multiple) {
$selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this
->getLastFileId() . '][selected]';
$remove_edit = array(
$selected_checkbox => '1',
);
}
if ($ajax) {
$this
->drupalPostAjaxForm(NULL, $remove_edit, $input_base_name . '_remove_button');
}
else {
$this
->drupalPostForm(NULL, $remove_edit, $remove_button_title);
}
$this
->drupalPostForm(NULL, array(), t('Save'));
$this
->assertRaw(t('The file ids are %fids.', array(
'%fids' => '',
)), 'Submission after file upload and removal was successful.');
}
}
}
}
$path = 'file/test/1/1/1';
$edit = array(
'files[nested_file][]' => drupal_realpath($test_file
->getFileUri()),
);
$fid_list = array();
$this
->drupalGet($path);
$this
->drupalPostForm(NULL, $edit, t('Upload'));
$fid_list[] = $this
->getLastFileId();
$this
->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[0] . '][selected]"]', NULL, 'First file successfully uploaded to multiple file element.');
$this
->drupalPostForm(NULL, $edit, t('Upload'));
$fid_list[] = $this
->getLastFileId();
$this
->assertFieldByXpath('//input[@name="nested[file][file_' . $fid_list[1] . '][selected]"]', NULL, 'Second file successfully uploaded to multiple file element.');
$this
->drupalPostForm(NULL, array(), t('Save'));
$this
->assertRaw(t('The file ids are %fids.', array(
'%fids' => implode(',', $fid_list),
)), 'Two files saved into a single multiple file element.');
$edit = array(
'nested[file][file_' . $fid_list[0] . '][selected]' => '1',
);
$this
->drupalPostForm($path . '/' . implode(',', $fid_list), $edit, t('Remove selected'));
$this
->assertNoFieldByXpath('//input[@name="nested[file][file_' . $fid_list[0] . '][selected]"]', NULL, 'An individual file can be deleted from a multiple file element.');
$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.');
}
public function testManagedFileRemoved() {
$this
->drupalGet('file/test/1/0/1');
$test_file = $this
->getTestFile('text');
$file_field_name = 'files[nested_file][]';
$edit = [
$file_field_name => drupal_realpath($test_file
->getFileUri()),
];
$this
->drupalPostForm(NULL, $edit, t('Upload'));
$fid = $this
->getLastFileId();
$file = \Drupal::entityManager()
->getStorage('file')
->load($fid);
$file
->delete();
$this
->drupalPostForm(NULL, $edit, t('Upload'));
$this
->assertRaw('The file referenced by the Managed <em>file & butter</em> field does not exist.');
}
}