From 981921d8234e20a5a4115ad6030a13294f9bf540 Mon Sep 17 00:00:00 2001
From: nod_ <nod_@598310.no-reply.drupal.org>
Date: Fri, 20 Oct 2023 15:54:39 +0200
Subject: [PATCH] Issue #3181647 by ultimike, guiu.rocafort.ferrer, tobiasb,
 ilgnerfagundes, prudloff, jurgenhaas, guzmanb, maxime.rffd, hexaki,
 samuel_orhan, rauch, guiu.rocafort.ferrer, mullzk, binnythomas: Add
 autocomplete attributes on login form and password reset form

---
 core/modules/user/src/Form/UserLoginForm.php          |  4 ++++
 core/modules/user/src/Form/UserPasswordForm.php       |  1 +
 .../user/tests/src/Functional/UserLoginTest.php       | 11 +++++++++++
 .../tests/src/Functional/UserPasswordResetTest.php    |  9 +++++++++
 4 files changed, 25 insertions(+)

diff --git a/core/modules/user/src/Form/UserLoginForm.php b/core/modules/user/src/Form/UserLoginForm.php
index cdbcfa7c4e7b..e4f027381e12 100644
--- a/core/modules/user/src/Form/UserLoginForm.php
+++ b/core/modules/user/src/Form/UserLoginForm.php
@@ -115,6 +115,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
         'autocapitalize' => 'none',
         'spellcheck' => 'false',
         'autofocus' => 'autofocus',
+        'autocomplete' => 'username',
       ],
     ];
 
@@ -123,6 +124,9 @@ public function buildForm(array $form, FormStateInterface $form_state) {
       '#title' => $this->t('Password'),
       '#size' => 60,
       '#required' => TRUE,
+      '#attributes' => [
+        'autocomplete' => 'current-password',
+      ],
     ];
 
     $form['actions'] = ['#type' => 'actions'];
diff --git a/core/modules/user/src/Form/UserPasswordForm.php b/core/modules/user/src/Form/UserPasswordForm.php
index 11407bce73ca..db5efc6180bf 100644
--- a/core/modules/user/src/Form/UserPasswordForm.php
+++ b/core/modules/user/src/Form/UserPasswordForm.php
@@ -120,6 +120,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
         'autocapitalize' => 'off',
         'spellcheck' => 'false',
         'autofocus' => 'autofocus',
+        'autocomplete' => 'username',
       ],
     ];
     // Allow logged in users to request this also.
diff --git a/core/modules/user/tests/src/Functional/UserLoginTest.php b/core/modules/user/tests/src/Functional/UserLoginTest.php
index de192a168f2b..fe339e896ce5 100644
--- a/core/modules/user/tests/src/Functional/UserLoginTest.php
+++ b/core/modules/user/tests/src/Functional/UserLoginTest.php
@@ -343,4 +343,15 @@ public function resetUserPassword($user) {
     $this->submitForm([], 'Log in');
   }
 
+  /**
+   * Tests that user login form has the autocomplete attributes.
+   */
+  public function testAutocompleteHtmlAttributes() {
+    $this->drupalGet('user/login');
+    $name_field = $this->getSession()->getPage()->findField('name');
+    $pass_field = $this->getSession()->getPage()->findField('pass');
+    $this->assertEquals('username', $name_field->getAttribute('autocomplete'));
+    $this->assertEquals('current-password', $pass_field->getAttribute('autocomplete'));
+  }
+
 }
diff --git a/core/modules/user/tests/src/Functional/UserPasswordResetTest.php b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php
index ab202ee7ea8d..442a0cb8c6da 100644
--- a/core/modules/user/tests/src/Functional/UserPasswordResetTest.php
+++ b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php
@@ -642,4 +642,13 @@ public function testResetImpersonation() {
     $this->assertSession()->pageTextContains('You have tried to use a one-time login link that has either been used or is no longer valid. Request a new one using the form below.');
   }
 
+  /**
+   * Test the autocomplete attribute is present.
+   */
+  public function testResetFormHasAutocompleteAttribute() {
+    $this->drupalGet('user/password');
+    $field = $this->getSession()->getPage()->findField('name');
+    $this->assertEquals('username', $field->getAttribute('autocomplete'));
+  }
+
 }
-- 
GitLab