In core/modules/editor/src/Form/EditorImageDialog.php,

The setting of maximum dimension of inline-image do not obtain correctly.

When set the dimension limit of inline-image to be 400x300 for basic html at "admin/config/content/formats/manage/basic_html", those setting value cannot obtain due incorrect array name.

In the image upload settings object,
it should be
$image_upload['max_dimensions']['width']
$image_upload['max_dimensions']['height']

BUT
it try to look for
$image_upload['dimensions']['max_width']
$image_upload['dimensions']['max_height']

In a result, the dimensions limit would always be ZERO, that make system do NOT reform the inline images.

CommentFileSizeAuthor
#48 2628656-48.patch7.48 KBthpoul
#48 interdiff-2628656-46-48.txt1.28 KBthpoul
#46 2628656-46.patch7.34 KBthpoul
#46 interdiff-2628656-41-46.txt1.93 KBthpoul
#41 2628656-41.patch6.94 KBthpoul
#41 interdiff-2628656-33-41.txt3.79 KBthpoul
#33 2628656-33.patch6.35 KBthpoul
#33 interdiff-2628656-32-33.txt2.37 KBthpoul
#32 2628656-32.patch6.05 KBthpoul
#32 interdiff-2628656-31-32.txt1.36 KBthpoul
#31 interdiff-2628656-29-31.txt1.8 KBaerozeppelin
#31 2628656-31.patch6.02 KBaerozeppelin
#29 2628656-29.patch6.07 KBthpoul
#29 interdiff-2628656-27-29.txt6.09 KBthpoul
#27 2628656-27.patch6.53 KBthpoul
#27 interdiff-2628656-25-27.txt5.86 KBthpoul
#25 2628656-25.patch6.75 KBthpoul
#25 interdiff-2628656-21-25.txt4.45 KBthpoul
#21 2628656-21.patch7.32 KBthpoul
#21 interdiff-2628656-16-21.txt13.18 KBthpoul
#20 2628656-20-test-only.patch6.42 KBthpoul
#16 2628656-16.patch9.17 KBMatt_five
#13 2628656-13.patch8.83 KBMatt_five
#8 EditorImageDimensionTest.txt7.39 KBMatt_five
#7 EditorUploadImageRescaleTest.txt7.36 KBMatt_five
#7 2628656-2.patch1007 bytesMatt_five
#2 2628656-1.patch862 bytesMatt_five
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Matt_five created an issue. See original summary.

Matt_five’s picture

Patch applied.

Please check.

Wim Leers’s picture

Issue tags: -editor, -inline image +Needs tests

Thank you!

This still needs test coverage.

Matt_five’s picture

Hi,

I don't know much about test coverage.
Do I need to submit a unit test of my patch according to "https://api.drupal.org/api/drupal/core!core.api.php/group/testing/8" ?

cilefen’s picture

@Matt_five Yes, or a WebTest if that is appropriate. The test code should be added to subsequent patches.

Wim Leers’s picture

Status: Needs review » Needs work
Matt_five’s picture

(EditorUploadImageRescaleTest.txt is wrong version, please refer to comment #8 for the correct file)

Hello,

I had created a new patch and WebTest.
Please remind that the WebTest is using the "standard" profile, it need longer time to run.

Since I cannot upload my test file "EditorImageDimensionTest.php" due to file type restriction, I rename the file to "EditorImageDimensionTest.txt". Please advice and review.

Moreover, I would like to talk other issue related to this fix.

1. Duplicate remind/error/warning message was shown when image just uploaded
---------------------------------------------------------
When the image cannot pass the validation (e.g. file extension restriction, file size limitation) or trigger scale down process, it would print the message twice. I search in Core issue queue and found #2346893 (https://www.drupal.org/node/2346893). I think the problem in the Editor image dialog form would be the issue related to #2346893. Therefore, I would not cover duplicate message issue in this patch.

2. Integration test for EditorImageDialog
---------------------------------------------------------
I found the issue #2395377 (https://www.drupal.org/node/2395377). I support that directly visit 'editor/dialog/image/basic_html/' would be one of the way to test the dialog form for specific functionality.

3. Invalid argument for file_validate_image_resolution
---------------------------------------------------------
Related to #2387011 (https://www.drupal.org/node/2387011)

When using new fields to transform "$image_upload['dimensions']['max_width'] and "$image_upload['dimensions']['max_height']" to String value, it would generate string with 'x' (e.g. "123x456") which would be processed by file_validate_image_resolution at file.module (line 423).

This patch:

      $max_width = (string) $image_upload['max_dimensions']['width'];
      $max_height = (string) $image_upload['max_dimensions']['height'];
      $max_dimensions = $max_width . 'x' . $max_height;

file_validate_image_resolution at file.module (line 423):

  list($width, $height) = explode('x', $maximum_dimensions);

Thank you.

Matt_five’s picture

FileSize
7.39 KB

Previous "EditorUploadImageRescaleTest.txt" is wrong version.

Matt_five’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
Matt_five’s picture

Issue tags: +Needs tests
Wim Leers’s picture

Status: Needs review » Needs work

Can you please git add /path/to/file your newly written test, so that the new test is part of the patch? :)

Matt_five’s picture

The new test file is included in the update patch.

Thanks.

aerozeppelin’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 13: 2628656-13.patch, failed testing.

Matt_five’s picture

Please find the updated patch.

Matt_five’s picture

Status: Needs work » Needs review
Matt_five’s picture

Hi,
would anyone have advise for the issue ?

Matt_five’s picture

Assigned: Matt_five » Unassigned
thpoul’s picture

thpoul’s picture

Version: 8.0.0 » 8.0.x-dev
FileSize
13.18 KB
7.32 KB

Trying to move this forward, hopefully in the right direction :)

The last submitted patch, 20: 2628656-20-test-only.patch, failed testing.

The last submitted patch, 20: 2628656-20-test-only.patch, failed testing.

Wim Leers’s picture

Status: Needs review » Needs work

Looking great! :) Only small remarks, to improve long-term maintainability of this, but overall this looks ready.

  1. +++ b/core/modules/editor/src/Tests/EditorUploadImageRescaleTest.php
    @@ -0,0 +1,164 @@
    +    $same_dimensions = ($uploaded_image_file_width === $image_file_width) && ($uploaded_image_file_height === $image_file_height);
    +    $this->assertTrue($same_dimensions, 'Uploaded image had the same dimensions');
    

    I'd make this into two asserts:

    $this->assertEqual($image_file_width, $uploaded_image_file_width);
    $this->assertEqual($image_file_height, $uploaded_image_file_height);
    
  2. +++ b/core/modules/editor/src/Tests/EditorUploadImageRescaleTest.php
    @@ -0,0 +1,164 @@
    +    //Upload the image
    ...
    +    //Check result: Image dimension would be the same.
    

    For cases 1 and 3, these steps aren't spelled out separately. I think we also shouldn't here.

  3. +++ b/core/modules/editor/src/Tests/EditorUploadImageRescaleTest.php
    @@ -0,0 +1,164 @@
    +    $edit = [];
    +    $edit['files[fid]'] = drupal_realpath($test_image->uri);
    +    $this->drupalPostForm('editor/dialog/image/basic_html/', $edit, t('Upload'));
    +    list($uploaded_image_file_width, $uploaded_image_file_height) = $this->getUploadedImageDimensions($image_basename);
    

    I'd create a helper for these 4 lines, because you repeat them 3 times (once for every case).

  4. +++ b/core/modules/editor/src/Tests/EditorUploadImageRescaleTest.php
    @@ -0,0 +1,164 @@
    +    $image_file_width = (int) $image_file->getWidth();
    +    $image_file_height = (int) $image_file->getHeight();
    +    $path_parts = pathinfo($image_file->getSource());
    +    $image_basename = $path_parts['basename'];
    ...
    +    $uploaded_image_file_width = (int) $uploaded_image_file->getWidth();
    +    $uploaded_image_file_height = (int) $uploaded_image_file->getHeight();
    

    Let's move these into the return statement itself directly. No need to first assign these to helper variables.

  5. +++ b/core/modules/editor/src/Tests/EditorUploadImageRescaleTest.php
    @@ -0,0 +1,164 @@
    +    return [
    +        $uploaded_image_file_width,
    +        $uploaded_image_file_height,
    +    ];
    

    Nit: 4 spaces of indentation, should be 2.

thpoul’s picture

Status: Needs work » Needs review
FileSize
4.45 KB
6.75 KB

Thank you for the review Wim! Here is the updated patch.

Wim Leers’s picture

Status: Needs review » Needs work

I think this test would benefit from some further refactoring, it'd help maintainability.

  1. +++ b/core/modules/editor/src/Tests/EditorUploadImageRescaleTest.php
    @@ -0,0 +1,154 @@
    + * Contains \Drupal\editor\Tests\EditorUploadImageRescaleTest.
    ...
    + * Test for image rescale function for Editor inline image.
    ...
    +    // image should not rescale.
    ...
    +    // rescale.
    ...
    +    $rescale_result = ($uploaded_image_file_width <= $max_width) && ($uploaded_image_file_height <= $max_height);
    ...
    +    // not rescale.
    

    This is not "rescaling", it's just "scaling".

    (It's easy for me to make that same mistake, because in Dutch it's "herschalen", literally "re-scale". It seems it's something similar in Greek — your native tongue.)

    But look at file_validate_image_resolution()'s documentation, it clearly says "scale".

    And it's ScaleImageEffect, not RescaleImageEffect.

  2. +++ b/core/modules/editor/src/Tests/EditorUploadImageRescaleTest.php
    @@ -0,0 +1,154 @@
    +    $this->drupalGet('editor/dialog/image/basic_html/');
    +    $this->postImage($test_image->uri);
    +    list($uploaded_image_file_width, $uploaded_image_file_height) = $this->getUploadedImageDimensions($image_basename);
    ...
    +    $this->drupalGet('editor/dialog/image/basic_html/');
    +    $this->postImage($test_image->uri);
    +    list($uploaded_image_file_width, $uploaded_image_file_height) = $this->getUploadedImageDimensions($image_basename);
    ...
    +    $this->drupalGet('editor/dialog/image/basic_html/');
    +    $this->postImage($test_image->uri);
    +    list($uploaded_image_file_width, $uploaded_image_file_height) = $this->getUploadedImageDimensions($image_basename);
    

    This drupalGet() call can be moved into the helper.

    And getUploadedImageDimensions() can AFAICT be merged into it as well.

    So then you can have an uploadImage() helper method that returns the dimensions of the uploaded image. Makes sense, right? :)

thpoul’s picture

Status: Needs work » Needs review
FileSize
5.86 KB
6.53 KB

Hope I got this right :) Thank you once again!

Wim Leers’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,151 @@
    + * Test for image scale function for Editor inline image.
    

    Tests scaling of uploaded images.

  2. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,151 @@
    +   * Tests scale of inline image.
    

    s/scale/scaling/

  3. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,151 @@
    +    // Case 1: Editor "image_upload" has no "max_dimensions" set. The uploaded
    +    // image should not scale.
    +    $test_image = $testing_image_list[0];
    ...
    +    // Case 2: Editor "image_upload" has "max_dimensions" with both width and
    +    // height being lower than the uploaded image. The uploaded image should
    +    // scale.
    ...
    +    // Case 3: Editor "image_upload" has "max_dimensions" with both width and
    +    // height being higher than the uploaded image. The uploaded image should
    +    // not scale.
    

    I think this can be made much simpler and clearer.

    We're testing the same editor every time. So no need to call that out.

    // Case 1: no max dimensions set: uploaded image not scaled.

    // Case 2: max dimensions smaller than uploaded image: image scaled down.

    // Case 3: max dimensions greater than uploaded image: image not scaled.

  4. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,151 @@
    +    $this->assertEqual($uploaded_image_file_width, $image_file_width);
    +    $this->assertEqual($uploaded_image_file_height, $image_file_height);
    

    This is great. It gives precise, actionable information in the test output.

  5. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,151 @@
    +    $scale_result = ($uploaded_image_file_width <= $max_width) && ($uploaded_image_file_height <= $max_height);
    +    $this->assertTrue($scale_result, 'Uploaded image had been scaled');
    ...
    +    $same_dimensions = ($uploaded_image_file_width == $image_file_width) && ($uploaded_image_file_height == $image_file_height);
    +    $this->assertTrue($same_dimensions, 'Uploaded image had the same dimensions');
    

    These are both imprecise (because <= is used) and does not give useful output. Please make this like the previously cited example (in the previous point).

  6. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,151 @@
    +    $this->assertRaw(t('The image was resized to fit within the maximum allowed dimensions of %dimensions pixels.', ['%dimensions' => $max_width . 'x' . $max_height]));
    

    Perhaps $this->assertNoRaw('The image was resized') for cases 1 and 3?

  7. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,151 @@
    +  protected function uploadImage($uri, $image_basename) {
    

    $image_basename can be calculated from $uri. So there's no need to pass this.

  8. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,151 @@
    +      $path_parts['basename'],
    

    Then it also becomes unnecessary to return this. (See previous point.)

  9. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,151 @@
    +    $this->postImage($uri);
    

    This is the only place where this is being called. So you can remove that helper method and just move its body in here.

  10. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,151 @@
    +    return [
    +        (int) $uploaded_image_file->getWidth(),
    +        (int) $uploaded_image_file->getHeight(),
    +    ];
    

    4 spaces of indentation instead of 2.

thpoul’s picture

Status: Needs work » Needs review
FileSize
6.09 KB
6.07 KB

Thank you Wim. I made the proposed changes and also some nitpicks while I was reading the code again. I didn't add comments yet to the helper functions since I'm waiting to see what the review of #2644838-34: Add test coverage for the editor's max dimension setting, and fix its message when one dimension is unrestricted will be and will apply the same strategy here.

Wim Leers’s picture

Status: Needs review » Needs work
  1. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,140 @@
    +        'scheme' => file_default_scheme(),
    

    You can totally hardcode this to public.

  2. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,140 @@
    +   * Tests scaling of inline image.
    

    This would have to be plural: images. But let's just copy the text from the class docblock.

  3. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,140 @@
    +    $this->assertNotEqual($uploaded_image_file_width, $max_width);
    +    $this->assertNotEqual($uploaded_image_file_height, $max_height);
    

    Why are you not able to use a precise expected width & height?

  4. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,140 @@
    +    $edit = [
    +        'files[fid]' => drupal_realpath($uri),
    +    ];
    

    4 spaces of indentation instead of 2.

aerozeppelin’s picture

Status: Needs work » Needs review
FileSize
6.02 KB
1.8 KB

Updates as per #30.

thpoul’s picture

The problem I see after the comment of #33-3, is that the test was passing just by luck. The image is not rescaling for some reason.

Fail: Value 40 is equal to value 35.
Fail: Value 20 is equal to value 15.

The problem started from #29. From the following interdiff:

+++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
@@ -130,21 +124,16 @@ protected function setMaxDimensions($width, $height) {
-    $this->postImage($uri);
-    $image_factory = $this->container->get('image.factory');
-    $uploaded_image_url = 'public://inline-images/' . $image_basename;
-    $uploaded_image_file = $image_factory->get($uploaded_image_url);
+    $this->drupalPostForm('editor/dialog/image/basic_html', $edit, t('Upload'));
+    $uploaded_image_file = $this->container->get('image.factory')->get($uri);

Changing $uploaded_image_file = $this->container->get('image.factory')->get($uri); with

$image_factory = $this->container->get('image.factory');
$image_file = $image_factory->get($uri);
$path_parts = pathinfo($image_file->getSource());
$uploaded_image_url = 'public://inline-images/' . $path_parts['basename'];
$uploaded_image_file = $image_factory->get($uploaded_image_url);

fixes allows the image to scale which confuses me a lot :/

Note: this patch will fail, I'm working on one that will pass.

thpoul’s picture

Test passes, but I'm not happy with testing only the width. Waiting for @Wim Leers's suggestions

edit: I forgot a line commented out, but this patch still needs work so I won't post an other patch while waiting for a review.

The last submitted patch, 32: 2628656-32.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Closed #2710843: Ckeditor Inline Images don't respect maximum dimensions as a duplicate of this.

I was waiting to review this one because I think it'll make sense to have the tests here in the same *Test.php file as #2644838: Add test coverage for the editor's max dimension setting, and fix its message when one dimension is unrestricted. So I'd like #2644838 to land first (it's been RTBC for a week).

Review coming soon. Assigning to myself so I don't forget.

Matt_five’s picture

support and thanks

Wim Leers’s picture

Wim Leers’s picture

Status: Needs review » Needs work

This is a review based on what we just learned at #2644838-51: Add test coverage for the editor's max dimension setting, and fix its message when one dimension is unrestricted.

  1. +++ b/core/modules/editor/src/Form/EditorImageDialog.php
    @@ -90,8 +90,8 @@ public function buildForm(array $form, FormStateInterface $form_state, FilterFor
    +    if (!empty($image_upload['max_dimensions']['width']) && !empty($image_upload['max_dimensions']['height'])) {
    

    This check is over-zealous. It's not necessary to have both specified, it's sufficient to have either specified.

    This && should be ||.

    (See #2644838-51: Add test coverage for the editor's max dimension setting, and fix its message when one dimension is unrestricted.)

  2. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -0,0 +1,145 @@
    +    // Case 1: no max dimensions set: uploaded image not scaled.
    ...
    +    // Case 2: max width smaller than uploaded image: image scaled down.
    ...
    +    // Case 3: max dimensions greater than uploaded image: image not scaled.
    

    Oh, this actually provides test coverage for points #2644838-51: Add test coverage for the editor's max dimension setting, and fix its message when one dimension is unrestricted.3.1 and #2644838-51: Add test coverage for the editor's max dimension setting, and fix its message when one dimension is unrestricted .3.2!

    What's missing, is test coverage for point #2644838-51: Add test coverage for the editor's max dimension setting, and fix its message when one dimension is unrestricted.3.3, which would then also have test coverage for the above remark.

Wim Leers’s picture

#32:

Because $uri is the image to upload: public://image-test-transparent-indexed.gif, and 'public://inline-images/' . $path_parts['basename']; is the uploaded image. That's a key difference!

So all you need here is:

$uploaded_image_file = $this->container->get('image.factory')->get('public://inline-images/' . basename($uri));

#33:

+++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
@@ -84,15 +84,15 @@ public function testEditorUploadImageScale() {
-    $this->assertEqual($uploaded_image_file_height, $max_height);
+    $this->assertTrue($uploaded_image_file_height < $max_height);

Do this instead:

$this->assertEqual($uploaded_image_file_height, $uploaded_image_file_height * ($uploaded_image_file_width / $max_width));

That calculates the scaled height, matching the scaled change in width. (The scale operation preserves the aspect ratio, and this respects that too.)

That's it :)

Test passes, but I'm not happy with testing only the width. Waiting for @Wim Leers's suggestions

Just duplicate what you're doing in case 2, but this time do $max_height = $image_file_height - 5;.

thpoul’s picture

Status: Needs work » Needs review
FileSize
3.79 KB
6.94 KB

Thank you Wim for your review. Here is the patch!

Status: Needs review » Needs work

The last submitted patch, 41: 2628656-41.patch, failed testing.

thpoul’s picture

Status: Needs work » Needs review

Weird test failure. timplunkett issued a retest and it passed. Switching back to NR

Wim Leers’s picture

+++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
@@ -0,0 +1,151 @@
+ * Tests scaling of uploaded images.
...
+   * Tests scaling of inline images.

Nit: inconsistent.

+++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
@@ -0,0 +1,151 @@
+  protected function getTestImageInfo($uri) {
...
+  protected function setMaxDimensions($width, $height) {
...
+  protected function uploadImage($uri) {

These 3 helpers need documentation, much like we did in the related issue. Otherwise committers will mark this NW for that reason.

That's it! Then this is RTBC :)

Wim Leers’s picture

Status: Needs review » Needs work
thpoul’s picture

Status: Needs work » Needs review
FileSize
1.93 KB
7.34 KB

Here it is :)

Wim Leers’s picture

Status: Needs review » Needs work

Sorry, you were a bit too fast I'm afraid :P

  1. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -118,6 +113,14 @@ public function testEditorUploadImageScale() {
    +   * @return array
    
    @@ -135,6 +146,14 @@ protected function setMaxDimensions($width, $height) {
    +   * @return array
    

    These are undefined.

  2. +++ b/core/modules/editor/src/Tests/EditorUploadImageScaleTest.php
    @@ -126,6 +129,14 @@ protected function getTestImageInfo($uri) {
    +   *   The URI of the image.
    ...
    +   *   The URI of the image.
    

    These are wrong.

thpoul’s picture

Status: Needs work » Needs review
FileSize
1.28 KB
7.48 KB

Oops!

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed f4a1d0c and pushed to 8.1.x and 8.2.x. Thanks!

  • alexpott committed e65accd on 8.2.x
    Issue #2628656 by thpoul, Matt_five, aerozeppelin, Wim Leers: Max...

  • alexpott committed f4a1d0c on 8.1.x
    Issue #2628656 by thpoul, Matt_five, aerozeppelin, Wim Leers: Max...
Wim Leers’s picture

So glad this is finally fixed!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.