Commit 03e3d7a3 authored by alexpott's avatar alexpott

Issue #2377747 by mondrake, eiriksm, rpayanm, adci_contributor, mgifford,...

Issue #2377747 by mondrake, eiriksm, rpayanm, adci_contributor, mgifford, oakulm, Truptti, chetan2111, joyceg, alexpott, xjm, yoroy, catch: Incorrect node create validation error when an invalid image is attached to a field
parent 44805b25
......@@ -406,7 +406,7 @@ function file_validate_is_image(FileInterface $file) {
$image = $image_factory->get($file->getFileUri());
if (!$image->isValid()) {
$supported_extensions = $image_factory->getSupportedExtensions();
$errors[] = t('Image type not supported. Allowed types: %types', ['%types' => implode(' ', $supported_extensions)]);
$errors[] = t('The image file is invalid or the image type is not allowed. Allowed types: %types', ['%types' => implode(', ', $supported_extensions)]);
}
return $errors;
......
......@@ -112,6 +112,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$field_settings = $this->getFieldSettings();
// Add image validation.
$element['#upload_validators']['file_validate_is_image'] = [];
// Add upload resolution validation.
if ($field_settings['max_resolution'] || $field_settings['min_resolution']) {
$element['#upload_validators']['file_validate_image_resolution'] = [$field_settings['max_resolution'], $field_settings['min_resolution']];
......
......@@ -8,6 +8,61 @@
* @group image
*/
class ImageFieldValidateTest extends ImageFieldTestBase {
/**
* Test image validity.
*/
public function testValid() {
$file_system = $this->container->get('file_system');
$image_files = $this->drupalGetTestFiles('image');
$field_name = strtolower($this->randomMachineName());
$this->createImageField($field_name, 'article', [], ['file_directory' => 'test-upload']);
$expected_path = 'public://test-upload';
// Create alt text for the image.
$alt = $this->randomMachineName();
// Create a node with a valid image.
$node = $this->uploadNodeImage($image_files[0], $field_name, 'article', $alt);
$this->assertTrue(file_exists($expected_path . '/' . $image_files[0]->filename));
// Remove the image.
$this->drupalPostForm('node/' . $node . '/edit', [], t('Remove'));
$this->drupalPostForm(NULL, [], t('Save'));
// Get invalid image test files from simpletest.
$files = file_scan_directory(drupal_get_path('module', 'simpletest') . '/files', '/invalid-img-.*/');
$invalid_image_files = [];
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 = [
'files[' . $field_name . '_0]' => $file_system->realpath($zero_size_image->uri),
];
$this->drupalPostForm('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 = [
'files[' . $field_name . '_0]' => $file_system->realpath($invalid_image->uri),
];
$this->drupalPostForm('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 = [
'files[' . $field_name . '_0]' => $file_system->realpath($valid_image->uri),
];
$this->drupalPostForm('node/' . $node . '/edit', $edit, t('Upload'));
$this->assertTrue(file_exists($expected_path . '/' . $valid_image->filename));
}
/**
* Test min/max resolution settings.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment