From fbcd92404dbe0c13ee84ffdae92fdd888e216aec Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 19 Jul 2016 15:59:05 +0100 Subject: [PATCH] Issue #2644838 by thpoul, Matt_five, swarad07, Wim Leers: Add test coverage for the editor's max dimension setting, and fix its message when one dimension is unrestricted --- .../editor/config/schema/editor.schema.yml | 2 + .../src/Tests/EditorUploadImageScaleTest.php | 59 ++++++++++++- core/modules/file/file.module | 12 ++- .../src/Tests/ImageFieldValidateTest.php | 84 +++++++++++++++---- 4 files changed, 136 insertions(+), 21 deletions(-) diff --git a/core/modules/editor/config/schema/editor.schema.yml b/core/modules/editor/config/schema/editor.schema.yml index 80a01f9820e..f68cb82056a 100644 --- a/core/modules/editor/config/schema/editor.schema.yml +++ b/core/modules/editor/config/schema/editor.schema.yml @@ -34,7 +34,9 @@ editor.editor.*: mapping: width: type: integer + nullable: true label: 'Maximum width' height: type: integer + nullable: true label: 'Maximum height' diff --git a/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php index ac6e46b0891..5ac88c70fa5 100644 --- a/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php @@ -50,8 +50,8 @@ class EditorUploadImageScaleTest extends WebTestBase { 'directory' => 'inline-images', 'max_size' => '', 'max_dimensions' => [ - 'width' => '', - 'height' => '' + 'width' => NULL, + 'height' => NULL ], ] ])->save(); @@ -71,9 +71,10 @@ class EditorUploadImageScaleTest extends WebTestBase { // Case 1: no max dimensions set: uploaded image not scaled. $test_image = $testing_image_list[0]; list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri); - $max_width = ''; - $max_height = ''; + $max_width = NULL; + $max_height = NULL; $this->setMaxDimensions($max_width, $max_height); + $this->assertSavedMaxDimensions($max_width, $max_height); list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri); $this->assertEqual($uploaded_image_file_width, $image_file_width); $this->assertEqual($uploaded_image_file_height, $image_file_height); @@ -85,6 +86,7 @@ class EditorUploadImageScaleTest extends WebTestBase { $max_width = $image_file_width - 5; $max_height = $image_file_height; $this->setMaxDimensions($max_width, $max_height); + $this->assertSavedMaxDimensions($max_width, $max_height); list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri); $this->assertEqual($uploaded_image_file_width, $max_width); $this->assertEqual($uploaded_image_file_height, $uploaded_image_file_height * ($uploaded_image_file_width / $max_width)); @@ -96,6 +98,7 @@ class EditorUploadImageScaleTest extends WebTestBase { $max_width = $image_file_width; $max_height = $image_file_height - 5; $this->setMaxDimensions($max_width, $max_height); + $this->assertSavedMaxDimensions($max_width, $max_height); list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri); $this->assertEqual($uploaded_image_file_width, $uploaded_image_file_width * ($uploaded_image_file_height / $max_height)); $this->assertEqual($uploaded_image_file_height, $max_height); @@ -107,10 +110,37 @@ class EditorUploadImageScaleTest extends WebTestBase { $max_width = $image_file_width + 5; $max_height = $image_file_height + 5; $this->setMaxDimensions($max_width, $max_height); + $this->assertSavedMaxDimensions($max_width, $max_height); list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri); $this->assertEqual($uploaded_image_file_width, $image_file_width); $this->assertEqual($uploaded_image_file_height, $image_file_height); $this->assertNoRaw(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', ['%dimensions' => $max_width . 'x' . $max_height])); + + // Case 5: only max width dimension was provided and it was smaller than + // uploaded image: image scaled down. + $test_image = $testing_image_list[4]; + list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri); + $max_width = $image_file_width - 5; + $max_height = NULL; + $this->setMaxDimensions($max_width, $max_height); + $this->assertSavedMaxDimensions($max_width, $max_height); + list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri); + $this->assertEqual($uploaded_image_file_width, $max_width); + $this->assertEqual($uploaded_image_file_height, $uploaded_image_file_height * ($uploaded_image_file_width / $max_width)); + $this->assertRaw(t('The image was resized to fit within the maximum allowed width of %width pixels.', ['%width' => $max_width])); + + // Case 6: only max height dimension was provided and it was smaller than + // uploaded image: image scaled down. + $test_image = $testing_image_list[5]; + list($image_file_width, $image_file_height) = $this->getTestImageInfo($test_image->uri); + $max_width = NULL; + $max_height = $image_file_height - 5; + $this->setMaxDimensions($max_width, $max_height); + $this->assertSavedMaxDimensions($max_width, $max_height); + list($uploaded_image_file_width, $uploaded_image_file_height) = $this->uploadImage($test_image->uri); + $this->assertEqual($uploaded_image_file_width, $uploaded_image_file_width * ($uploaded_image_file_height / $max_height)); + $this->assertEqual($uploaded_image_file_height, $max_height); + $this->assertRaw(t('The image was resized to fit within the maximum allowed height of %height pixels.', ['%height' => $max_height])); } /** @@ -169,4 +199,25 @@ class EditorUploadImageScaleTest extends WebTestBase { ]; } + /** + * Asserts whether the saved maximum dimensions equal the ones provided. + * + * @param string $width + * The expected width of the uploaded image. + * @param string $height + * The expected height of the uploaded image. + * + * @return bool + */ + protected function assertSavedMaxDimensions($width, $height) { + $image_upload_settings = Editor::load('basic_html')->getImageUploadSettings(); + $expected = [ + 'width' => $image_upload_settings['max_dimensions']['width'], + 'height' => $image_upload_settings['max_dimensions']['height'], + ]; + $same_width = $this->assertEqual($width, $expected['width'], 'Actual width of "' . $width . '" equals the expected width of "' . $expected['width'] . '"'); + $same_height = $this->assertEqual($height, $expected['height'], 'Actual height of "' . $height . '" equals the expected width of "' . $expected['height'] . '"'); + return $same_width && $same_height; + } + } diff --git a/core/modules/file/file.module b/core/modules/file/file.module index d69c149ff0d..2c8914726e5 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -442,8 +442,16 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions // Try to resize the image to fit the dimensions. if ($image->scale($width, $height)) { $image->save(); - $file->filesize = $image->getFileSize(); - drupal_set_message(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions))); + if (!empty($width) && !empty($height)) { + $message = t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', array('%dimensions' => $maximum_dimensions)); + } + elseif (empty($width)) { + $message = t('The image was resized to fit within the maximum allowed height of %height pixels.', array('%height' => $height)); + } + elseif (empty($height)) { + $message = t('The image was resized to fit within the maximum allowed width of %width pixels.', array('%width' => $width)); + } + drupal_set_message($message); } else { $errors[] = t('The image exceeds the maximum allowed dimensions and an attempt to resize it failed.'); diff --git a/core/modules/image/src/Tests/ImageFieldValidateTest.php b/core/modules/image/src/Tests/ImageFieldValidateTest.php index 2532524126b..5f0bf213fae 100644 --- a/core/modules/image/src/Tests/ImageFieldValidateTest.php +++ b/core/modules/image/src/Tests/ImageFieldValidateTest.php @@ -12,15 +12,43 @@ class ImageFieldValidateTest extends ImageFieldTestBase { * Test min/max resolution settings. */ function testResolution() { - $field_name = strtolower($this->randomMachineName()); - $min_resolution = 50; - $max_resolution = 100; - $field_settings = array( - 'max_resolution' => $max_resolution . 'x' . $max_resolution, - 'min_resolution' => $min_resolution . 'x' . $min_resolution, - 'alt_field' => 0, - ); - $this->createImageField($field_name, 'article', array(), $field_settings); + $field_names = [ + 0 => strtolower($this->randomMachineName()), + 1 => strtolower($this->randomMachineName()), + 2 => strtolower($this->randomMachineName()), + ]; + $min_resolution = [ + 'width' => 50, + 'height' => 50 + ]; + $max_resolution = [ + 'width' => 100, + 'height' => 100 + ]; + $no_height_min_resolution = [ + 'width' => 50, + 'height' => NULL + ]; + $no_height_max_resolution = [ + 'width' => 100, + 'height' => NULL + ]; + $no_width_min_resolution = [ + 'width' => NULL, + 'height' => 50 + ]; + $no_width_max_resolution = [ + 'width' => NULL, + 'height' => 100 + ]; + $field_settings = [ + 0 => $this->getFieldSettings($min_resolution, $max_resolution), + 1 => $this->getFieldSettings($no_height_min_resolution, $no_height_max_resolution), + 2 => $this->getFieldSettings($no_width_min_resolution, $no_width_max_resolution), + ]; + $this->createImageField($field_names[0], 'article', [], $field_settings[0]); + $this->createImageField($field_names[1], 'article', [], $field_settings[1]); + $this->createImageField($field_names[2], 'article', [], $field_settings[2]); // We want a test image that is too small, and a test image that is too // big, so cycle through test image files until we have what we need. @@ -29,21 +57,29 @@ class ImageFieldValidateTest extends ImageFieldTestBase { $image_factory = $this->container->get('image.factory'); foreach ($this->drupalGetTestFiles('image') as $image) { $image_file = $image_factory->get($image->uri); - if ($image_file->getWidth() > $max_resolution) { + if ($image_file->getWidth() > $max_resolution['width']) { $image_that_is_too_big = $image; } - if ($image_file->getWidth() < $min_resolution) { + if ($image_file->getWidth() < $min_resolution['width']) { $image_that_is_too_small = $image; } if ($image_that_is_too_small && $image_that_is_too_big) { break; } } - $this->uploadNodeImage($image_that_is_too_small, $field_name, 'article'); - $this->assertRaw(t('The specified file %name could not be uploaded.', array('%name' => $image_that_is_too_small->filename))); - $this->assertRaw(t('The image is too small; the minimum dimensions are %dimensions pixels.', array('%dimensions' => '50x50'))); - $this->uploadNodeImage($image_that_is_too_big, $field_name, 'article'); + $this->uploadNodeImage($image_that_is_too_small, $field_names[0], 'article'); + $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); + $this->assertRaw(t('The image is too small; the minimum dimensions are %dimensions pixels.', ['%dimensions' => '50x50'])); + $this->uploadNodeImage($image_that_is_too_big, $field_names[0], 'article'); $this->assertText(t('The image was resized to fit within the maximum allowed dimensions of 100x100 pixels.')); + $this->uploadNodeImage($image_that_is_too_small, $field_names[1], 'article'); + $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); + $this->uploadNodeImage($image_that_is_too_big, $field_names[1], 'article'); + $this->assertText(t('The image was resized to fit within the maximum allowed width of 100 pixels.')); + $this->uploadNodeImage($image_that_is_too_small, $field_names[2], 'article'); + $this->assertRaw(t('The specified file %name could not be uploaded.', ['%name' => $image_that_is_too_small->filename])); + $this->uploadNodeImage($image_that_is_too_big, $field_names[2], 'article'); + $this->assertText(t('The image was resized to fit within the maximum allowed height of 100 pixels.')); } /** @@ -102,4 +138,22 @@ class ImageFieldValidateTest extends ImageFieldTestBase { $this->assertNoText(t('Title field is required.')); } + /** + * Returns field settings. + * + * @param int[] $min_resolution + * The minimum width and height resolution setting. + * @param int[] $max_resolution + * The maximum width and height resolution setting. + * + * @return array + */ + protected function getFieldSettings($min_resolution, $max_resolution) { + return [ + 'max_resolution' => $max_resolution['width'] . 'x' . $max_resolution['height'], + 'min_resolution' => $min_resolution['width'] . 'x' . $min_resolution['height'], + 'alt_field' => 0, + ]; + } + }