View source
<?php
namespace Drupal\Tests\file\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\TestTools\PhpUnitCompatibility\RunnerVersion;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\Tests\Traits\ExpectDeprecationTrait;
class FileFieldValidateTest extends FileFieldTestBase {
use ExpectDeprecationTrait;
protected $defaultTheme = 'stark';
public function testRequired() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$type_name = 'article';
$field_name = strtolower($this
->randomMachineName());
$storage = $this
->createFileField($field_name, 'node', $type_name, [], [
'required' => '1',
]);
$field = FieldConfig::loadByName('node', $type_name, $field_name);
$test_file = $this
->getTestFile('text');
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName();
$this
->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
$this
->assertRaw(t('@title field is required.', [
'@title' => $field
->getLabel(),
]), 'Node save failed when required file field was empty.');
$nid = $this
->uploadNodeFile($test_file, $field_name, $type_name);
$this
->assertTrue($nid !== FALSE, new FormattableMarkup('uploadNodeFile(@test_file, @field_name, @type_name) succeeded', [
'@test_file' => $test_file
->getFileUri(),
'@field_name' => $field_name,
'@type_name' => $type_name,
]));
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this
->assertFileExists($node_file
->getFileUri());
$this
->assertFileEntryExists($node_file, 'File entry exists after uploading to the required field.');
$storage
->delete();
$this
->createFileField($field_name, 'node', $type_name, [
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
], [
'required' => '1',
]);
$edit = [];
$edit['title[0][value]'] = $this
->randomMachineName();
$this
->drupalPostForm('node/add/' . $type_name, $edit, t('Save'));
$this
->assertRaw(t('@title field is required.', [
'@title' => $field
->getLabel(),
]), 'Node save failed when required multiple value file field was empty.');
$nid = $this
->uploadNodeFile($test_file, $field_name, $type_name);
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this
->assertFileExists($node_file
->getFileUri());
$this
->assertFileEntryExists($node_file, 'File entry exists after uploading to the required multiple value field.');
}
public function testFileMaxSize() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$type_name = 'article';
$field_name = strtolower($this
->randomMachineName());
$this
->createFileField($field_name, 'node', $type_name, [], [
'required' => '1',
]);
$small_file = $this
->getTestFile('text', 131072);
$large_file = $this
->getTestFile('text', 1310720);
$sizes = [
'1M' => 1048576,
'1024K' => 1048576,
'1048576' => 1048576,
];
foreach ($sizes as $max_filesize => $file_limit) {
$this
->updateFileField($field_name, $type_name, [
'max_filesize' => $max_filesize,
]);
$nid = $this
->uploadNodeFile($small_file, $field_name, $type_name);
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this
->assertFileExists($node_file
->getFileUri());
$this
->assertFileEntryExists($node_file, new FormattableMarkup('File entry exists after uploading a file (%filesize) under the max limit (%maxsize).', [
'%filesize' => format_size($small_file
->getSize()),
'%maxsize' => $max_filesize,
]));
$this
->uploadNodeFile($large_file, $field_name, $type_name);
$error_message = t('The file is %filesize exceeding the maximum file size of %maxsize.', [
'%filesize' => format_size($large_file
->getSize()),
'%maxsize' => format_size($file_limit),
]);
$this
->assertRaw($error_message, new FormattableMarkup('Node save failed when file (%filesize) exceeded the max upload size (%maxsize).', [
'%filesize' => format_size($large_file
->getSize()),
'%maxsize' => $max_filesize,
]));
}
$this
->updateFileField($field_name, $type_name, [
'max_filesize' => '',
]);
$nid = $this
->uploadNodeFile($large_file, $field_name, $type_name);
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this
->assertFileExists($node_file
->getFileUri());
$this
->assertFileEntryExists($node_file, new FormattableMarkup('File entry exists after uploading a file (%filesize) with no max limit.', [
'%filesize' => format_size($large_file
->getSize()),
]));
}
public function testFileExtension() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$type_name = 'article';
$field_name = strtolower($this
->randomMachineName());
$this
->createFileField($field_name, 'node', $type_name);
$test_file = $this
->getTestFile('image');
list(, $test_file_extension) = explode('.', $test_file
->getFilename());
$this
->updateFileField($field_name, $type_name, [
'file_extensions' => '',
]);
$nid = $this
->uploadNodeFile($test_file, $field_name, $type_name);
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this
->assertFileExists($node_file
->getFileUri());
$this
->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
$this
->updateFileField($field_name, $type_name, [
'file_extensions' => 'txt',
]);
$this
->uploadNodeFile($test_file, $field_name, $type_name);
$error_message = t('Only files with the following extensions are allowed: %files-allowed.', [
'%files-allowed' => 'txt',
]);
$this
->assertRaw($error_message, 'Node save failed when file uploaded with the wrong extension.');
$this
->updateFileField($field_name, $type_name, [
'file_extensions' => "txt {$test_file_extension}",
]);
$nid = $this
->uploadNodeFile($test_file, $field_name, $type_name);
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this
->assertFileExists($node_file
->getFileUri());
$this
->assertFileEntryExists($node_file, 'File entry exists after uploading a file with extension checking.');
}
public function testFileRemoval() {
$node_storage = $this->container
->get('entity_type.manager')
->getStorage('node');
$type_name = 'article';
$field_name = 'file_test';
$this
->createFileField($field_name, 'node', $type_name);
$test_file = $this
->getTestFile('image');
$this
->updateFileField($field_name, $type_name, [
'file_extensions' => '',
]);
$nid = $this
->uploadNodeFile($test_file, $field_name, $type_name);
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this
->assertFileExists($node_file
->getFileUri());
$this
->assertFileEntryExists($node_file, 'File entry exists after uploading a file with no extension checking.');
$this
->updateFileField($field_name, $type_name, [
'file_extensions' => 'txt',
]);
$this
->removeNodeFile($nid);
$this
->assertNoText('Only files with the following extensions are allowed: txt.');
$this
->assertText('Article ' . $node
->getTitle() . ' has been updated.');
}
public function testAssertFileExistsDeprecation() {
if (RunnerVersion::getMajor() == 6) {
$this
->addExpectedDeprecationMessage('Passing a File entity as $file argument to FileFieldTestBase::assertFileExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326');
$this
->addExpectedDeprecationMessage('Passing a File entity as $file argument to FileFieldTestBase::assertFileNotExists is deprecated in drupal:8.8.0. It will be removed from drupal:9.0.0. Instead, pass the File entity URI via File::getFileUri(). See https://www.drupal.org/node/3057326');
}
else {
$this
->markTestSkipped('This test does not work in PHPUnit 7+ since assertFileExists only accepts string arguments for $file');
}
$node_storage = $this->container
->get('entity.manager')
->getStorage('node');
$type_name = 'article';
$field_name = 'file_test';
$this
->createFileField($field_name, 'node', $type_name);
$test_file = $this
->getTestFile('image');
$this
->updateFileField($field_name, $type_name, [
'file_extensions' => '',
]);
$nid = $this
->uploadNodeFile($test_file, $field_name, $type_name);
$node_storage
->resetCache([
$nid,
]);
$node = $node_storage
->load($nid);
$node_file = File::load($node->{$field_name}->target_id);
$this
->assertFileExists($node_file);
unlink($node_file
->getFileUri());
$this
->assertFileNotExists($node_file);
}
}