function FileFieldValidateTestCase::testFileExtension in FileField 6.3
Test the file extension, do additional checks if mimedetect is installed.
File
- tests/
filefield.test, line 494
Class
- FileFieldValidateTestCase
- Test class to check for various validations.
Code
function testFileExtension() {
$type = $this->node_type;
$field = $this->field;
// Setup files for extension checking.
$test_file = $this
->getTestFile('image');
preg_match('/(?<=\\.)[^\\.]*$/', $test_file->filename, $matches);
$extention = current($matches);
$wrong_extension_file = drupal_clone($test_file);
$wrong_extension_file->filename = str_replace(".{$extention}", '.jpg', $test_file->filename);
$wrong_extension_file->filepath = file_directory_path() . '/' . $wrong_extension_file->filename;
$original_path = $test_file->filepath;
file_copy($original_path, $wrong_extension_file->filepath);
$this
->updateFileField($field['field_name'], $type->name, array(), array(
'file_extensions' => '',
));
// Check that the file can be uploaded with no extension checking.
$nid = $this
->uploadNodeFile($test_file, $field, $type->name);
$node = node_load($nid, NULL, TRUE);
$node_file = $node->{$field['field_name']}[0];
$this
->assertFileExists($node_file, t('File exists after uploading a file with no extension checking.'));
$this
->assertFileEntryExists($node_file, t('File entry exists after uploading a file with no extension checking.'));
// Enable extension checking.
$this
->updateFileField($field['field_name'], $type->name, array(), array(
'file_extensions' => "txt png jpg {$extention}",
));
// Check that the file can be uploaded with extension checking.
$nid = $this
->uploadNodeFile($test_file, $field, $type->name);
$node = node_load($nid, NULL, TRUE);
$node_file = $node->{$field['field_name']}[0];
$this
->assertFileExists($node_file, t('File exists after uploading a file with extension checking.'));
$this
->assertFileEntryExists($node_file, t('File entry exists after uploading a file with extension checking.'));
// Check that a mismatched extension cannot be uploaded.
$mimedetect = FALSE;
if (module_exists('mimedetect')) {
$mimedetect = TRUE;
module_load_include('install', 'mimedetect');
if (!extension_loaded('fileinfo')) {
variable_set('mimedetect_enable_file_binary', 1);
}
$requirements = mimedetect_requirements('runtime');
foreach ($requirements as $requirement) {
if (isset($requirement['severity']) && $requirement['severity'] == REQUIREMENT_ERROR) {
$mimedetect = FALSE;
}
}
}
if ($mimedetect) {
$this
->uploadNodeFile($wrong_extension_file, $field, $type->name);
$error_pattern = "/The file contents \\([a-z\\-\\/]+\\) do not match its extension \\([a-z]+\\)\\./";
$this
->assertPattern($error_pattern, t('File prevented from uploading because its extension does not match its content.'));
}
else {
$this
->assertTrue(TRUE, t('Mime type checking and extension spoofing skipped because the mimedetect module is not available.'));
}
// Disable the extension checking.
$this
->updateFileField($field['field_name'], $type->name, array(), array(
'file_extensions' => '',
));
}