Skip to content
Snippets Groups Projects
Verified Commit fd71fedd authored by Drew Webber's avatar Drew Webber
Browse files

Issue #2345695 by poker10, formatC'vt, frodri, quietone, mgifford, mondrake,...

Issue #2345695 by poker10, formatC'vt, frodri, quietone, mgifford, mondrake, thirdender, james.williams: Users are able to upload 0-byte images
parent dcf3c140
No related branches found
No related tags found
1 merge request!7330Issue #3306390 by poker10, catch, Fabianx, pwolanin, rvtraveller: [D7]...
Pipeline #59371 failed
+7
......@@ -1862,7 +1862,7 @@ function file_validate_is_image(stdClass $file) {
$info = image_get_info($file->uri);
if (!$info || empty($info['extension'])) {
$errors[] = t('Only JPEG, PNG and GIF images are allowed.');
$errors[] = t('The image file is invalid or the image type is not allowed. Allowed types: %allowed_types', array('%allowed_types' => t('Only JPEG, PNG and GIF images are allowed.')));
}
return $errors;
......
......@@ -347,6 +347,7 @@ function image_field_widget_form(&$form, &$form_state, $field, $instance, $langc
$extensions = isset($elements[$delta]['#upload_validators']['file_validate_extensions'][0]) ? $elements[$delta]['#upload_validators']['file_validate_extensions'][0] : implode(' ', $supported_extensions);
$extensions = array_intersect(explode(' ', $extensions), $supported_extensions);
$elements[$delta]['#upload_validators']['file_validate_extensions'][0] = implode(' ', $extensions);
$elements[$delta]['#upload_validators']['file_validate_is_image'] = array();
// Add all extra functionality provided by the image widget.
$elements[$delta]['#process'][] = 'image_field_widget_process';
......
......@@ -1254,6 +1254,56 @@ class ImageFieldValidateTestCase extends ImageFieldTestCase {
);
}
/**
* Test image validity.
*/
public function testValid() {
$image_files = $this->drupalGetTestFiles('image');
$field_name = strtolower($this->randomName());
$this->createImageField($field_name, 'article', array(), array('file_directory' => 'test-upload'));
$expected_path = 'public://test-upload';
// Create a node with a valid image.
$node = $this->uploadNodeImage($image_files[0], $field_name, 'article');
$this->assertTrue(file_exists($expected_path . '/' . $image_files[0]->filename));
// Remove the image.
$this->drupalPost('node/' . $node . '/edit', array(), t('Remove'));
$this->drupalPost(NULL, array(), t('Save'));
// Get invalid image test files from simpletest.
$files = file_scan_directory(drupal_get_path('module', 'simpletest') . '/files', '/invalid-img-.*/');
$invalid_image_files = array();
foreach ($files as $file) {
$invalid_image_files[$file->filename] = $file;
}
// Try uploading a zero-byte image.
$zero_size_image = $invalid_image_files['invalid-img-zero-size.png'];
$edit = array(
'files[' . $field_name . '_' . LANGUAGE_NONE . '_0]' => drupal_realpath($zero_size_image->uri),
);
$this->drupalPost('node/' . $node . '/edit', $edit, t('Upload'));
$this->assertFalse(file_exists($expected_path . '/' . $zero_size_image->filename));
// Try uploading an invalid image.
$invalid_image = $invalid_image_files['invalid-img-test.png'];
$edit = array(
'files[' . $field_name . '_' . LANGUAGE_NONE . '_0]' => drupal_realpath($invalid_image->uri),
);
$this->drupalPost('node/' . $node . '/edit', $edit, t('Upload'));
$this->assertFalse(file_exists($expected_path . '/' . $invalid_image->filename));
// Upload a valid image again.
$valid_image = $image_files[0];
$edit = array(
'files[' . $field_name . '_' . LANGUAGE_NONE . '_0]' => drupal_realpath($valid_image->uri),
);
$this->drupalPost('node/' . $node . '/edit', $edit, t('Upload'));
$this->assertTrue(file_exists($expected_path . '/' . $valid_image->filename));
}
/**
* Test min/max resolution settings.
*/
......
invalid image file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment