From 123f19afe92332eb1e67740cf606f8bd2403afc6 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 31 Aug 2020 09:51:48 +0100
Subject: [PATCH] Issue #3005641 by ridhimaabrol24, matsbla, tanubansal,
 jungle, msankhala, dww, quietone, hchonov: Exception is thrown on changing
 "Site language" setting of a user if user account is translated

(cherry picked from commit 95af12b524256f8577d1cb09c460329e58a3ed33)
---
 core/modules/user/src/AccountForm.php         |  7 ++-
 .../tests/src/Functional/UserEditTest.php     | 49 +++++++++++++++++++
 2 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/core/modules/user/src/AccountForm.php b/core/modules/user/src/AccountForm.php
index 8e32dad8b179..893fdd251b00 100644
--- a/core/modules/user/src/AccountForm.php
+++ b/core/modules/user/src/AccountForm.php
@@ -272,8 +272,11 @@ public function form(array $form, FormStateInterface $form_state) {
     // separately, assume that the user profile data is in the user's preferred
     // language. This entity builder provides that synchronization. For
     // use-cases where this synchronization is not desired, a module can alter
-    // or remove this item.
-    $form['#entity_builders']['sync_user_langcode'] = '::syncUserLangcode';
+    // or remove this item. Sync user langcode only when a user registers and
+    // not when a user is updated or translated.
+    if ($register) {
+      $form['#entity_builders']['sync_user_langcode'] = '::syncUserLangcode';
+    }
 
     $system_date_config = \Drupal::config('system.date');
     $form['timezone'] = [
diff --git a/core/modules/user/tests/src/Functional/UserEditTest.php b/core/modules/user/tests/src/Functional/UserEditTest.php
index 6a71927a96cc..d06df78f3aaf 100644
--- a/core/modules/user/tests/src/Functional/UserEditTest.php
+++ b/core/modules/user/tests/src/Functional/UserEditTest.php
@@ -174,4 +174,53 @@ public function testUserWellKnownChangePasswordAnon() {
     $this->assertSession()->statusCodeEquals(403);
   }
 
+  /**
+   * Tests that a user is able to change site language.
+   */
+  public function testUserChangeSiteLanguage() {
+    // Install these modules here as these aren't needed for other test methods.
+    \Drupal::service('module_installer')->install([
+      'content_translation',
+      'language',
+    ]);
+    // Create and login as an admin user to add a new language and enable
+    // translation for user accounts.
+    $adminUser = $this->drupalCreateUser([
+      'administer account settings',
+      'administer languages',
+      'administer content translation',
+      'administer users',
+      'translate any entity',
+    ]);
+    $this->drupalLogin($adminUser);
+
+    // Add a new language into the system.
+    $edit = [
+      'predefined_langcode' => 'fr',
+    ];
+    $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
+    $this->assertSession()->pageTextContains('French');
+
+    // Enable translation for user accounts.
+    $edit = [
+      'language[content_translation]' => 1,
+    ];
+    $this->drupalPostForm('admin/config/people/accounts', $edit, 'Save configuration');
+    $this->assertSession()->pageTextContains('The configuration options have been saved.');
+
+    // Create a regular user for whom translation will be enabled.
+    $webUser = $this->drupalCreateUser();
+
+    // Create a translation for a regular user account.
+    $this->drupalPostForm('user/' . $webUser->id() . '/translations/add/en/fr', [], 'Save');
+    $this->assertSession()->pageTextContains('The changes have been saved.');
+
+    // Update the site language of the user account.
+    $edit = [
+      'preferred_langcode' => 'fr',
+    ];
+    $this->drupalPostForm('user/' . $webUser->id() . '/edit', $edit, 'Save');
+    $this->assertSession()->statusCodeEquals(200);
+  }
+
 }
-- 
GitLab