| 1 file.test | public FileFieldValidateTestCase::testMultipleFileValidate() |
Tests file extension validation when uploading multiple files at once.
File
- core/
modules/ file/ tests/ file.test, line 1630 - Tests for file.module.
Class
- FileFieldValidateTestCase
- Tests various validations.
Code
public function testMultipleFileValidate() {
$type_name = 'post';
$field_name = strtolower($this->randomName());
$this->createFileField($field_name, $type_name, array('cardinality' => FIELD_CARDINALITY_UNLIMITED));
$txt_file1 = $this->getTestFile('text');
// 128KB, distinct from txt_file1.
$txt_file2 = $this->getTestFile('text', 131072);
$image_file = $this->getTestFile('image');
// Restrict uploads to text files only.
$this->updateFileField($field_name, $type_name, array('file_extensions' => 'txt'));
// Upload two valid txt files and one invalid image file simultaneously.
$nid = $this->uploadNodeFile(array($txt_file1, $txt_file2, $image_file), $field_name, $type_name);
// The image file should be rejected with an extension error.
$error_message = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => 'txt'));
$this->assertRaw($error_message, 'Extension error shown when invalid file uploaded alongside valid files.');
// The node should have been saved with only the two valid txt files.
$node = node_load($nid, NULL, TRUE);
$this->assertEqual(2, count($node->{$field_name}[LANGUAGE_NONE]), 'Node saved with both valid txt files.');
foreach ($node->{$field_name}[LANGUAGE_NONE] as $delta => $item) {
$node_file = file_load($item['fid']);
$this->assertFileExists($node_file, format_string('Txt file @delta exists on disk.', array('@delta' => $delta)));
$this->assertFileEntryExists($node_file, format_string('Txt file @delta entry exists in the database.', array('@delta' => $delta)));
$this->assertEqual(FILE_STATUS_PERMANENT, $node_file->status, format_string('Txt file @delta is marked permanent.', array('@delta' => $delta)));
}
// Test max file size validation with multiple simultaneous uploads.
// Set a 500 KB size cap while keeping the txt-only extension restriction.
$this->updateFileField($field_name, $type_name, array('max_filesize' => '500K'));
// 128 KB, under the 500 KB limit.
$small_txt_file = $this->getTestFile('text', 131072);
// 1.2 MB, exceeds 500 KB.
$large_txt_file = $this->getTestFile('text', 1310720);
// Upload both txt files simultaneously.
$nid = $this->uploadNodeFile(array($small_txt_file, $large_txt_file), $field_name, $type_name);
// The oversized file should be rejected.
$size_error = t('The file is %filesize exceeding the maximum file size of %maxsize.', array(
'%filesize' => format_size($large_txt_file->filesize),
'%maxsize' => format_size(parse_size('500K')),
));
$this->assertRaw($size_error, 'Size error shown when oversized txt file uploaded alongside a valid txt file.');
// The node should be saved with only the small, valid file.
$node = node_load($nid, NULL, TRUE);
$this->assertEqual(1, count($node->{$field_name}[LANGUAGE_NONE]), 'Node saved with only the small txt file.');
$saved_file = file_load($node->{$field_name}[LANGUAGE_NONE][0]['fid']);
$this->assertFileExists($saved_file, 'Small txt file exists on disk after uploading alongside an oversized file.');
$this->assertFileEntryExists($saved_file, 'Small txt file entry exists in the database after uploading alongside an oversized file.');
$this->assertEqual(FILE_STATUS_PERMANENT, $saved_file->status, 'Small txt file is marked permanent.');
// Remove our file field.
field_delete_field($field_name);
}