View source
<?php
namespace Drupal\Tests\file\Functional;
use Drupal\file\Entity\File;
class FileManagedFileElementTest extends FileFieldTestBase {
protected $defaultTheme = 'stark';
public function testManagedFile() {
$this
->drupalGet('file/test');
$field = $this
->assertSession()
->fieldExists("files[nested_file]");
$this
->assertEquals(13, $field
->getAttribute('size'));
$test_file = $this
->getTestFile('text');
foreach ([
0,
1,
] as $tree) {
foreach ([
0,
1,
] as $extended) {
foreach ([
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
->drupalGet($path);
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains("The file ids are .");
$last_fid_prior = $this
->getLastFileId();
$this
->drupalGet($path);
$form_token_field = $this
->assertSession()
->hiddenFieldExists('form_token');
$form_token_field
->setValue('invalid token');
$edit = [
$file_field_name => \Drupal::service('file_system')
->realpath($test_file
->getFileUri()),
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('The form has become outdated.');
$last_fid = $this
->getLastFileId();
$this
->assertEquals($last_fid_prior, $last_fid, 'File was not saved when uploaded with an invalid form token.');
$last_fid_prior = $this
->getLastFileId();
$edit = [
$file_field_name => \Drupal::service('file_system')
->realpath($test_file
->getFileUri()),
];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save');
$last_fid = $this
->getLastFileId();
$this
->assertGreaterThan($last_fid_prior, $last_fid, 'New file got saved.');
$this
->assertSession()
->pageTextContains("The file ids are {$last_fid}.");
$this
->drupalGet($path . '/' . $last_fid);
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains("The file ids are {$last_fid}.");
$last_fid_prior = $this
->getLastFileId();
$this
->drupalGet($path);
$edit = [
$file_field_name => \Drupal::service('file_system')
->realpath($test_file
->getFileUri()),
];
$this
->submitForm($edit, 'Upload');
$last_fid = $this
->getLastFileId();
$this
->assertGreaterThan($last_fid_prior, $last_fid, 'New file got uploaded.');
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains("The file ids are {$last_fid}.");
$remove_button_title = $multiple ? 'Remove selected' : 'Remove';
$remove_edit = [];
if ($multiple) {
$selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $last_fid . '][selected]';
$remove_edit = [
$selected_checkbox => '1',
];
}
$this
->drupalGet($path . '/' . $last_fid);
$this
->submitForm($remove_edit, $remove_button_title);
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains("The file ids are .");
$this
->drupalGet($path);
$edit = [
$file_field_name => \Drupal::service('file_system')
->realpath($test_file
->getFileUri()),
];
$this
->submitForm($edit, 'Upload');
$remove_edit = [];
if ($multiple) {
$selected_checkbox = ($tree ? 'nested[file]' : 'file') . '[file_' . $this
->getLastFileId() . '][selected]';
$remove_edit = [
$selected_checkbox => '1',
];
}
$this
->submitForm($remove_edit, $remove_button_title);
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains("The file ids are .");
}
}
}
$path = 'file/test/1/1/1';
$edit = [
'files[nested_file][]' => \Drupal::service('file_system')
->realpath($test_file
->getFileUri()),
];
$fid_list = [];
$this
->drupalGet($path);
$this
->submitForm($edit, 'Upload');
$fid_list[] = $this
->getLastFileId();
$this
->assertSession()
->fieldExists("nested[file][file_{$fid_list[0]}][selected]");
$this
->submitForm($edit, 'Upload');
$fid_list[] = $this
->getLastFileId();
$this
->assertSession()
->fieldExists("nested[file][file_{$fid_list[1]}][selected]");
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains("The file ids are " . implode(',', $fid_list) . ".");
$edit = [
'nested[file][file_' . $fid_list[0] . '][selected]' => '1',
];
$this
->drupalGet($path . '/' . implode(',', $fid_list));
$this
->submitForm($edit, 'Remove selected');
$this
->assertSession()
->fieldNotExists("nested[file][file_{$fid_list[0]}][selected]");
$this
->assertSession()
->fieldExists("nested[file][file_{$fid_list[1]}][selected]");
}
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::service('file_system')
->realpath($test_file
->getFileUri()),
];
$this
->submitForm($edit, 'Upload');
$fid = $this
->getLastFileId();
$file = \Drupal::entityTypeManager()
->getStorage('file')
->load($fid);
$file
->delete();
$this
->submitForm($edit, 'Upload');
$this
->assertSession()
->responseContains('The file referenced by the Managed <em>file & butter</em> field does not exist.');
}
public function testFileNameTrim() {
file_put_contents('public://.leading-period.txt', $this
->randomString(32));
$last_fid_prior = $this
->getLastFileId();
$this
->drupalGet('file/test/0/0/0');
$this
->submitForm([
'files[file]' => \Drupal::service('file_system')
->realpath('public://.leading-period.txt'),
], 'Save');
$next_fid = $this
->getLastFileId();
$this
->assertGreaterThan($last_fid_prior, $next_fid);
$file = File::load($next_fid);
$this
->assertEquals('leading-period.txt', $file
->getFilename());
}
public function testFileRemovedFromDisk() {
$this
->drupalGet('file/test/1/0/1');
$test_file = $this
->getTestFile('text');
$file_field_name = 'files[nested_file][]';
$edit = [
$file_field_name => \Drupal::service('file_system')
->realpath($test_file
->getFileUri()),
];
$this
->submitForm($edit, 'Upload');
$this
->submitForm([], 'Save');
$fid = $this
->getLastFileId();
$file = $this->container
->get('entity_type.manager')
->getStorage('file')
->load($fid);
$file
->setPermanent();
$file
->save();
$this
->assertTrue(\Drupal::service('file_system')
->delete($file
->getFileUri()));
$file
->save();
$this
->assertTrue($file
->isPermanent());
$file
->delete();
}
public function testUnusedPermanentFileValidation() {
$file = $this
->getTestFile('image');
$file
->setPermanent();
$file
->save();
$this
->drupalGet('file/test/1/0/1/' . $file
->id());
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextNotContains('The file used in the Managed file & butter field may not be referenced.');
$this
->assertSession()
->pageTextContains('The file ids are ' . $file
->id());
$this
->config('file.settings')
->set('make_unused_managed_files_temporary', TRUE)
->save();
$this
->drupalGet('file/test/1/0/1/' . $file
->id());
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('The file used in the Managed file & butter field may not be referenced.');
$this
->assertSession()
->pageTextNotContains('The file ids are ' . $file
->id());
$file
->setTemporary();
$file
->save();
$this
->drupalGet('file/test/1/0/1/' . $file
->id());
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextNotContains('The file used in the Managed file & butter field may not be referenced.');
$this
->assertSession()
->pageTextContains('The file ids are ' . $file
->id());
$file
->setPermanent();
$file
->save();
$file_usage = \Drupal::service('file.usage');
$file_usage
->add($file, 'file', 'file', $file
->id());
$this
->drupalGet('file/test/1/0/1/' . $file
->id());
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextNotContains('The file used in the Managed file & butter field may not be referenced.');
$this
->assertSession()
->pageTextContains('The file ids are ' . $file
->id());
}
}