From ade9d780aedf0c0b1171edbb0341c5835bb41737 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 14 Mar 2017 09:20:01 +0000
Subject: [PATCH] =?UTF-8?q?Issue=20#2828092=20by=20dmsmidt,=20gaurav.kapoo?=
 =?UTF-8?q?r,=20s.d.sirois,=20andrewmacpherson,=20G=C3=A1bor=20Hojtsy,=20t?=
 =?UTF-8?q?stoeckler,=20jules.,=20Wim=20Leers,=20SKAUGHT,=20marcvangend,?=
 =?UTF-8?q?=20yoroy,=20catch:=20Inline=20Form=20Errors=20not=20compatible?=
 =?UTF-8?q?=20with=20Quick=20Edit?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../src/FormErrorHandler.php                  |  7 ++
 .../FormErrorHandlerQuickEditTest.php         | 92 +++++++++++++++++++
 .../tests/src/Unit/FormErrorHandlerTest.php   | 35 +++++++
 3 files changed, 134 insertions(+)
 create mode 100644 core/modules/inline_form_errors/tests/src/FunctionalJavascript/FormErrorHandlerQuickEditTest.php

diff --git a/core/modules/inline_form_errors/src/FormErrorHandler.php b/core/modules/inline_form_errors/src/FormErrorHandler.php
index 8e0f1b820c07..2b892e99dd68 100644
--- a/core/modules/inline_form_errors/src/FormErrorHandler.php
+++ b/core/modules/inline_form_errors/src/FormErrorHandler.php
@@ -53,6 +53,13 @@ public function __construct(TranslationInterface $string_translation, LinkGenera
    *   The current state of the form.
    */
   protected function displayErrorMessages(array $form, FormStateInterface $form_state) {
+    // Use the original error display for Quick Edit forms, because in this case
+    // the errors are already near the form element.
+    if ($form['#form_id'] === 'quickedit_field_form') {
+      parent::displayErrorMessages($form, $form_state);
+      return;
+    }
+
     $error_links = [];
     $errors = $form_state->getErrors();
     // Loop through all form errors and check if we need to display a link.
diff --git a/core/modules/inline_form_errors/tests/src/FunctionalJavascript/FormErrorHandlerQuickEditTest.php b/core/modules/inline_form_errors/tests/src/FunctionalJavascript/FormErrorHandlerQuickEditTest.php
new file mode 100644
index 000000000000..b956cccbd0bd
--- /dev/null
+++ b/core/modules/inline_form_errors/tests/src/FunctionalJavascript/FormErrorHandlerQuickEditTest.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace Drupal\Tests\inline_form_errors\FunctionalJavascript;
+
+use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
+use Drupal\node\Entity\NodeType;
+
+/**
+ * Tests Inline Form Errors compatibility with Quick Edit.
+ *
+ * @group inline_form_errors
+ */
+class FormErrorHandlerQuickEditTest extends JavascriptTestBase {
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = [
+    'quickedit',
+    'node',
+    'inline_form_errors',
+  ];
+
+  /**
+   * An editor user with permissions to access the in-place editor.
+   *
+   * @var \Drupal\user\UserInterface
+   */
+  protected $editorUser;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+
+    // Create a page node type for testing.
+    NodeType::create(['type' => 'page', 'name' => 'page'])->save();
+
+    // Create a user with the permission to use in-place editing.
+    $permissions = [
+      'access content',
+      'create page content',
+      'edit any page content',
+      'access contextual links',
+      'access in-place editing',
+    ];
+    $this->editorUser = $this->drupalCreateUser($permissions);
+    $this->drupalLogin($this->editorUser);
+  }
+
+  /**
+   * Tests that the inline form errors are not visible for Quick Edit forms.
+   */
+  public function testDisabledInlineFormErrors() {
+    $session = $this->getSession();
+    $web_assert = $this->assertSession();
+
+    // Create a page node.
+    $node = $this->drupalCreateNode();
+
+    // Visit the node page.
+    $this->drupalGet('node/' . $node->id());
+
+    // Wait until the quick edit link is available.
+    $web_assert->waitForElement('css', '.quickedit > a');
+
+    // Activate the quick editing mode.
+    $session->executeScript("jQuery('article.node').find('.quickedit > a').click()");
+
+    $web_assert->waitForElement('css', '.quickedit-toolbar');
+
+    // Clear the title field. Trigger a 'keyup' to be able to save the changes.
+    $session->executeScript("jQuery('.field--name-title').text('').trigger('keyup')");
+
+    // Try to save the changes.
+    $save_button = $web_assert->waitForElement('css', '.action-save.quickedit-button');
+    $save_button->click();
+
+    // Wait until the form submission is complete.
+    $web_assert->assertWaitOnAjaxRequest();
+
+    // Assert that no error summary from Inline Form Errors is shown.
+    $web_assert->elementTextNotContains('css', '.quickedit-validation-errors', '1 error has been found');
+
+    // Assert that the required title error is shown.
+    $web_assert->elementTextContains('css', '.quickedit-validation-errors', 'Title field is required.');
+  }
+
+}
diff --git a/core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php b/core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php
index 8053a751d698..acd255ba2715 100644
--- a/core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php
+++ b/core/modules/inline_form_errors/tests/src/Unit/FormErrorHandlerTest.php
@@ -50,6 +50,7 @@ public function testDisplayErrorMessagesInline() {
 
     $form = [
       '#parents' => [],
+      '#form_id' => 'test_form',
       '#array_parents' => [],
     ];
     $form['test1'] = [
@@ -122,6 +123,7 @@ public function testSetElementErrorsFromFormState() {
 
     $form = [
       '#parents' => [],
+      '#form_id' => 'test_form',
       '#array_parents' => [],
     ];
     $form['test'] = [
@@ -137,4 +139,37 @@ public function testSetElementErrorsFromFormState() {
     $this->assertSame('invalid', $form['test']['#errors']);
   }
 
+  /**
+   * Test that Quick Edit forms show non-inline errors.
+   *
+   * @covers ::handleFormErrors
+   * @covers ::displayErrorMessages
+   */
+  public function testDisplayErrorMessagesNotInlineQuickEdit() {
+    $form_error_handler = $this->getMockBuilder(FormErrorHandler::class)
+      ->setConstructorArgs([$this->getStringTranslationStub(), $this->getMock(LinkGeneratorInterface::class), $this->getMock(RendererInterface::class)])
+      ->setMethods(['drupalSetMessage'])
+      ->getMock();
+
+    $form_error_handler->expects($this->at(0))
+      ->method('drupalSetMessage')
+      ->with('invalid', 'error');
+
+    $form = [
+      '#parents' => [],
+      '#form_id' => 'quickedit_field_form',
+      '#array_parents' => [],
+    ];
+    $form['test'] = [
+      '#type' => 'textfield',
+      '#title' => 'Test',
+      '#parents' => ['test'],
+      '#id' => 'edit-test',
+      '#array_parents' => ['test']
+    ];
+    $form_state = new FormState();
+    $form_state->setErrorByName('test', 'invalid');
+    $form_error_handler->handleFormErrors($form, $form_state);
+  }
+
 }
-- 
GitLab