Skip to content
Snippets Groups Projects
Commit 03e3d7a3 authored by Alex Pott's avatar Alex Pott
Browse files

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
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -406,7 +406,7 @@ function file_validate_is_image(FileInterface $file) { ...@@ -406,7 +406,7 @@ function file_validate_is_image(FileInterface $file) {
$image = $image_factory->get($file->getFileUri()); $image = $image_factory->get($file->getFileUri());
if (!$image->isValid()) { if (!$image->isValid()) {
$supported_extensions = $image_factory->getSupportedExtensions(); $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; return $errors;
......
...@@ -112,6 +112,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ...@@ -112,6 +112,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
$field_settings = $this->getFieldSettings(); $field_settings = $this->getFieldSettings();
// Add image validation.
$element['#upload_validators']['file_validate_is_image'] = [];
// Add upload resolution validation. // Add upload resolution validation.
if ($field_settings['max_resolution'] || $field_settings['min_resolution']) { if ($field_settings['max_resolution'] || $field_settings['min_resolution']) {
$element['#upload_validators']['file_validate_image_resolution'] = [$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 @@ ...@@ -8,6 +8,61 @@
* @group image * @group image
*/ */
class ImageFieldValidateTest extends ImageFieldTestBase { 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. * 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