Problem/Motivation

We've seen about half a dozen regressions in EditorImageDialog over the past year. Currently, we still have e.g. #2387011: EditorImageDialog passes incorrect arguments to file_validate_image_resolution open. Let's prevent such regressions once and for all by writing a proper integration test.

Steps to reproduce

Proposed resolution

Remaining tasks

Is this still a valid issue or have the tests been written since this was opened.

User interface changes

API changes

Data model changes

Release notes snippet

CommentFileSizeAuthor
#10 2395377-10.patch5.71 KBaneek
#2 2395377-1-do-not-test.patch3.67 KBaneek

Comments

aneek’s picture

@Wim Leers,

I was trying to make a test case that would validate the image's max dimension and resize it while upload. This piece of test can be useful to #2387011: EditorImageDialog passes incorrect arguments to file_validate_image_resolution issue. Need some of your help on this. The test case I've written so far is as following.
File: core/modules/editor/src/Tests/EditorImageDialogTest.php

/**
 * @file
 * Contains \Drupal\editor\Tests\EditorImageDialogTest.
 */


namespace Drupal\editor\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests Image Editor functionalities.
 *
 * @group editor
 */
class EditorImageDialogTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('filter', 'editor', 'editor_test', 'node');

  /**
   * An authenticated user to login.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  /**
   * An image file path for uploading.
   */
  protected $image;

  /**
   * Test case setup.
   */
  protected function setUp() {
    parent::setUp();

    // Add text formats.
    $full_html_format = entity_create('filter_format', array(
      'format' => 'full_html',
      'name' => 'Full HTML',
      'weight' => 1,
      'filters' => array(),
    ));
    $full_html_format->save();


    $this->adminUser = $this->drupalCreateUser(array(
      'use text format full_html'
    ));
  }

  /**
   * Tests the validations for image max resolution.
   */
  public function testMaxImageResolution() {
    // Set image resolution variables.
    $max_resolution = 100;

    // Only associate a text editor with the "Full HTML" text format.
    $editor = entity_create('editor', array(
      'format' => 'full_html',
      'editor' => 'unicorn',
      'image_upload' => array(
        'status' => FALSE,
        'scheme' => file_default_scheme(),
        'directory' => 'inline-images',
        'max_size' => '',
        'max_dimensions' => ['width' => $max_resolution, 'height' => $max_resolution],
      )
    ));
    $editor->save();
    // Login the user.
    $this->drupalLogin($this->adminUser);
    // Editor dialog page.
    $this->drupalGet('editor/dialog/image/full_html');

    // Get images test images based on the given resolution.
    $suitable_image = FALSE;
    $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) {
        $suitable_image = $image;
      }
      if ($suitable_image) {
        break;
      }
    }
    // Construct the form submit.
    $edit = array(
      'attributes[src]' => drupal_realpath($suitable_image->uri),
      'attributes[alt]' => 'Test Image file',
    );
    $this->drupalPostForm(NULL, $edit, t('Save'));
  }
}

I am not able to see the message in the editor page "editor/dialog/image/full_html" as The image was resized to fit within the maximum allowed dimensions of 100x100 pixels. Since there one issue with the EditorImageDialog::buildForm() to take wrong array parameters as for image resolution validation, I have also fixed that one.

diff --git a/core/modules/editor/src/Form/EditorImageDialog.php b/core/modules/editor/src/Form/EditorImageDialog.php
index a493dea..9b200fa 100644
--- a/core/modules/editor/src/Form/EditorImageDialog.php
+++ b/core/modules/editor/src/Form/EditorImageDialog.php
@@ -52,8 +52,8 @@ public function buildForm(array $form, FormStateInterface $form_state, FilterFor
 
     // Construct strings to use in the upload validators.
     $image_upload = $editor->getImageUploadSettings();
-    if (!empty($image_upload['dimensions'])) {
-      $max_dimensions = $image_upload['dimensions']['max_width'] . '×' . $image_upload['dimensions']['max_height'];
+    if (!empty($image_upload['max_dimensions'])) {
+      $max_dimensions = $image_upload['max_dimensions']['width'] . 'x' . $image_upload['max_dimensions']['height'];
     }
     else {
       $max_dimensions = 0;

Am I missing something in the test case?

aneek’s picture

StatusFileSize
new3.67 KB

Uploading a patch that might help someone to check for the issues I was referring to in comment #1.
FYI not for bot review as of now.

aneek’s picture

Status: Active » Needs work
berdir’s picture

Status: Needs work » Needs review

You can run the test anyway, it will happen sooner or later anyway.

aneek’s picture

@Berdir,

I did locally but this should give an message "The image was resized to fit within the maximum allowed dimensions of 100x100 pixels" but it's not.

I think there may be coding issue with the test that I've written. It will be good if you or anyone can have a look after applying it locally.

catch’s picture

Category: Bug report » Task
wim leers’s picture

aneek’s picture

@Wim Leers, great!! I will have a go on this.

aneek’s picture

Status: Needs review » Needs work
aneek’s picture

StatusFileSize
new5.71 KB

@WimLeers, Uploading a basic test file. This does the following test validations,

  1. Tests form validation for the image editor form.
  2. Validates the editor object with pre loaded value.

Please review this and let me know what more can I add and improve in this patch.

Thanks!

aneek’s picture

Status: Needs work » Needs review
wim leers’s picture

Status: Needs review » Needs work

#10: Thank you so much for working on this! :) :)

  1. +++ b/core/modules/editor/src/Tests/EditorImageDialogIntegrationTest.php
    @@ -0,0 +1,203 @@
    + * Tests Image Editor behaviour.
    

    It's not an "image editor", it's an image dialog for a text editor.

    So: Tests image dialog behavior. would be better.

  2. +++ b/core/modules/editor/src/Tests/EditorImageDialogIntegrationTest.php
    @@ -0,0 +1,203 @@
    +   * Defines the maximum dimension for a image while uploading.
    

    Nit: s/a/an/

  3. +++ b/core/modules/editor/src/Tests/EditorImageDialogIntegrationTest.php
    @@ -0,0 +1,203 @@
    +   * Set up test case.
    

    Nit: {@inheritdoc}

  4. +++ b/core/modules/editor/src/Tests/EditorImageDialogIntegrationTest.php
    @@ -0,0 +1,203 @@
    +    // Setup 'filtered_html' filter format.
    ...
    +    // Setup 'full_html' filter format.
    

    Nit: s/Setup/Set up/

  5. +++ b/core/modules/editor/src/Tests/EditorImageDialogIntegrationTest.php
    @@ -0,0 +1,203 @@
    +    $images = $this->drupalGetTestFiles('image');
    

    Oh, WOW! I had no idea this existed :D Thanks :)

  6. +++ b/core/modules/editor/src/Tests/EditorImageDialogIntegrationTest.php
    @@ -0,0 +1,203 @@
    +    // Check for the error messages.
    +    $this->assertRaw('form-item--error-message', 'Error message class appears.');
    

    Shouldn't we also check which error message appears?

  7. +++ b/core/modules/editor/src/Tests/EditorImageDialogIntegrationTest.php
    @@ -0,0 +1,203 @@
    +  public function testLoadEditorObject() {
    

    YES! What this method is testing is exactly what we very much need.

    <3 <3

  8. +++ b/core/modules/editor/src/Tests/EditorImageDialogIntegrationTest.php
    @@ -0,0 +1,203 @@
    +    // Create an editor object.
    

    ", as POSTed by the text editor's JavaScript."

  9. +++ b/core/modules/editor/src/Tests/EditorImageDialogIntegrationTest.php
    @@ -0,0 +1,203 @@
    +    $form_state = (new FormState())
    +      ->setRequestMethod('POST')
    +      ->setUserInput($input)
    +      ->addBuildInfo('args', [$this->fullHtml]);
    +
    +    $form_builder = \Drupal::formBuilder();
    +    $form_object = new EditorImageDialog(\Drupal::entityManager()->getStorage('file'));
    +    $form_id = $form_builder->getFormId($form_object, $form_state);
    +    $form = $form_builder->retrieveForm($form_id, $form_state);
    +    $form_builder->prepareForm($form_id, $form, $form_state);
    +    $form_builder->processForm($form_id, $form, $form_state);
    +
    +    // Get the form values.
    +    $attributes = $form_state->getValue('attributes');
    +    // Assert the loaded variables.
    +    $this->assertEqual($this->uploadImage, $attributes['src']);
    +    $this->assertEqual('foo', $attributes['alt']);
    +    $this->assertEqual('left', $attributes['data-align']);
    +    $this->assertTrue($attributes['hasCaption']);
    

    Hrm… but this is just like \Drupal\editor\Tests\EditorImageDialogTest::testEditorImageDialog(). It's not an integration test.

    This should use a POST request (drupalPost()) and then verify the form that Drupal responds with matches our expectations.

    And do that for various editor objects being posted.

    Does that make sense?

aneek’s picture

@WimLeers, thanks for reviewing the patch. I have some queries from #13. Though I'll ping you asking for those in IRC but I am also writing my queries here as well.

  • #13.6: So what do you suggest on this? Should we check like the following:
    $this->assertText('Image field is required');
    $this->assertText('Alternative text is required.')
    

    Or you have any better ideas?

  • #13.9:
    Well yes, this is like EditorImageDialogTest::testEditorImageDialog() and I've tested the loading of the editor with pre-loaded values. I've tried with drupalPost() but might have misses something. So, where should the value will be posted? Is it going to be on drupalPost('editor/dialog/image/full_html') or on a node creation page? I'll discuss with you on this point in IRC. Right now I can't be online :(

Please let me know your thoughts. Thanks!

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.

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

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

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

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

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

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

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

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

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

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

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

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

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

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Issue summary: View changes
Status: Needs work » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

This issue was discussed at a Bug Smash Initiative group triage meeting.

There has been no activity here for 7 years. Is this still valid? Perhaps, the test needed here was added in #2628656: Max dimensions do not apply to Editor Inline image or another issue?

Changing to PMNMI to determine if this still needs to be done before someone spends time on a patch,

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Since there hasn't been a follow up to #27 going to close for now.

If still valid please reopen updating IS with what is still needed.

Thanks everyone!