From c2bdd141b48ab560cc5c930d7f746b9c8f3feddc Mon Sep 17 00:00:00 2001
From: nod_ <nod_@598310.no-reply.drupal.org>
Date: Mon, 21 Oct 2024 16:25:08 +0200
Subject: [PATCH] Issue #3473999 by mondrake, smustgrave, catch: Ajax-enabled
 image effect forms do not update to the latest ajax processed configuration

(cherry picked from commit 4bfa1e9987e929e274b1612159affdc16271535e)
---
 .../image/src/Form/ImageEffectFormBase.php    |  5 +++-
 .../ImageAdminStylesTest.php                  | 26 +++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/core/modules/image/src/Form/ImageEffectFormBase.php b/core/modules/image/src/Form/ImageEffectFormBase.php
index 1e9cd77c898d..511e4c125e25 100644
--- a/core/modules/image/src/Form/ImageEffectFormBase.php
+++ b/core/modules/image/src/Form/ImageEffectFormBase.php
@@ -122,7 +122,10 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
     $this->imageEffect->submitConfigurationForm($form['data'], SubformState::createForSubform($form['data'], $form, $form_state));
 
     $this->imageEffect->setWeight($form_state->getValue('weight'));
-    if (!$this->imageEffect->getUuid()) {
+    if ($uuid = $this->imageEffect->getUuid()) {
+      $this->imageStyle->getEffect($uuid)->setConfiguration($this->imageEffect->getConfiguration());
+    }
+    else {
       $this->imageStyle->addImageEffect($this->imageEffect->getConfiguration());
     }
     $this->imageStyle->save();
diff --git a/core/modules/image/tests/src/FunctionalJavascript/ImageAdminStylesTest.php b/core/modules/image/tests/src/FunctionalJavascript/ImageAdminStylesTest.php
index faea9dd8d4b3..2da7f3edd9b1 100644
--- a/core/modules/image/tests/src/FunctionalJavascript/ImageAdminStylesTest.php
+++ b/core/modules/image/tests/src/FunctionalJavascript/ImageAdminStylesTest.php
@@ -57,6 +57,7 @@ public function testAjaxEnabledEffectForm(): void {
     foreach ($style->getEffects() as $uuid => $effect) {
       $effect_path = $admin_path . '/manage/' . $style_name . '/effects/' . $uuid;
       $this->drupalGet($effect_path);
+      $this->assertSession()->fieldValueEquals('data[test_parameter]', '100');
       $page->findField('data[test_parameter]')->setValue(111);
       $ajax_value = $page->find('css', '#ajax-value')->getText();
       $this->assertSame('Ajax value bar', $ajax_value);
@@ -67,7 +68,32 @@ public function testAjaxEnabledEffectForm(): void {
       }));
       $page->pressButton('Update effect');
       $assert->statusMessageContains('The image effect was successfully applied.', 'status');
+      $this->drupalGet($effect_path);
+      $this->assertSession()->fieldValueEquals('data[test_parameter]', '111');
     }
+
+    // Edit the 1st effect, multiple AJAX calls before updating.
+    $style = ImageStyle::load($style_name);
+    $uuid = array_values($style->getEffects()->getInstanceIds())[0];
+    $this->drupalGet($admin_path . '/manage/' . $style_name . '/effects/' . $uuid);
+    $this->assertSession()->fieldValueEquals('data[test_parameter]', '111');
+    $field = $page->findField('data[test_parameter]');
+    $field->setValue(200);
+    $page->pressButton('Ajax refresh');
+    $this->assertSession()->assertExpectedAjaxRequest(1);
+    $field->setValue(300);
+    $page->pressButton('Ajax refresh');
+    $this->assertSession()->assertExpectedAjaxRequest(2);
+    $field->setValue(400);
+    $page->pressButton('Ajax refresh');
+    $this->assertSession()->assertExpectedAjaxRequest(3);
+    $page->pressButton('Update effect');
+    $this->assertSession()->statusMessageContains('The image effect was successfully applied.', 'status');
+    $style = ImageStyle::load($style_name);
+    $effectConfiguration = $style->getEffect($uuid)->getConfiguration();
+    $this->assertSame(400, $effectConfiguration['data']['test_parameter']);
+    $this->drupalGet($admin_path . '/manage/' . $style_name . '/effects/' . $uuid);
+    $this->assertSession()->fieldValueEquals('data[test_parameter]', '400');
   }
 
 }
-- 
GitLab