diff --git a/core/modules/user/lib/Drupal/user/AccountFormController.php b/core/modules/user/lib/Drupal/user/AccountFormController.php index 46c272c..56a2658 100644 --- a/core/modules/user/lib/Drupal/user/AccountFormController.php +++ b/core/modules/user/lib/Drupal/user/AccountFormController.php @@ -204,10 +204,12 @@ public function form(array $form, array &$form_state) { // if one of the detection and selection methods uses it. if (Drupal::moduleHandler()->moduleExists('language')) { $show_admin_language = FALSE; - foreach (language_types_info() as $type_key => $language_type) { - $negotiation_settings = variable_get("language_negotiation_{$type_key}", array()); - if ($show_admin_language = isset($negotiation_settings[LANGUAGE_NEGOTIATION_USER_ADMIN])) { - break; + if (language_multilingual()) { + foreach (language_types_info() as $type_key => $language_type) { + $negotiation_settings = variable_get("language_negotiation_{$type_key}", array()); + if ($show_admin_language = isset($negotiation_settings[LANGUAGE_NEGOTIATION_USER_ADMIN])) { + break; + } } } $form['language']['preferred_admin_langcode'] = array( diff --git a/core/modules/user/lib/Drupal/user/Tests/UserAdminLanguageTest.php b/core/modules/user/lib/Drupal/user/Tests/UserAdminLanguageTest.php index 06a03d2..b674028 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserAdminLanguageTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserAdminLanguageTest.php @@ -29,30 +29,77 @@ public static function getInfo() { ); } + protected function setUp() { + parent::setUp(); + // User to add and remove language. + $this->admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); + // User to check non-admin access. + $this->regular_user = $this->drupalCreateUser(); + } + /** - * Test if user can change their administration pages language. + * Tests that the admin language configuration is not available if the site + * is not multilingual. */ - function testUserAdminLanguageConfiguration() { - global $base_url; + function testUserAdminLanguageConfigurationNotAvailableWithOnlyOneLanguage() { + $this->drupalLogin($this->admin_user); + $this->setLanguageNegotiation(); + $path = 'user/' . $this->admin_user->uid . '/edit'; + $this->drupalGet($path); + // Ensure administration pages language settings widget is not available. + $this->assertNoFieldById('edit-preferred-admin-langcode', '', 'Administration pages language selector not available.'); + } - // User to add and remove language. - $super_admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); - // User to check non-admin access. - $non_admin_user = $this->drupalCreateUser('access administration pages'); - $non_admin_user->name = 'non-admin user'; - // User to change their administration pages language. - $admin_user = $this->drupalCreateUser('access administration pages'); - $admin_user->name = 'admin user'; + /** + * Tests that the admin language configuration is not available if the admin + * language negotiation is not enabled. + */ + function testUserAdminLanguageConfigurationNotAvailableWithoutAdminLanguageNegotiation() { + $this->drupalLogin($this->admin_user); + $path = 'user/' . $this->admin_user->uid . '/edit'; + $this->drupalGet($path); + // Ensure administration pages language settings widget is not available. + $this->assertNoFieldById('edit-preferred-admin-langcode', '', 'Administration pages language selector not available.'); + } + + /** + * Tests that the admin language configuration is available for admins in + * multilingual sites, but not for users without the right permissions. + */ + function testUserAdminLanguageConfigurationAvailableIfAdminLanguageNegotiationIsEnabled() { + $this->drupalLogin($this->admin_user); + $this->addCustomLanguage(); + $this->setLanguageNegotiation(); + $path = 'user/' . $this->admin_user->uid . '/edit'; + $this->drupalGet($path); + // Ensure administration pages language settings widget is available for admin. + $this->assertFieldById('edit-preferred-admin-langcode', 'en', 'Administration pages language selector available for admins.'); + + // Ensure administration pages language settings widget is not available + // for regular users. + $this->drupalLogin($this->regular_user); + $path = 'user/' . $this->regular_user->uid . '/edit'; + $this->drupalGet($path); + $this->assertNoFieldById('edit-preferred-admin-langcode', '', 'Administration pages language selector not available for regular user.'); + } - // Login as users and edit account settings. - $check_users = array($non_admin_user, $admin_user); - foreach ($check_users as $check_user) { - testUserAdminLanguageNoSetting($check_user, 'before language'); - } - - // Add custom language. - $this->drupalLogin($super_admin_user); - // Code for the language. + /** + * Helper meethod for setting the language user admin negotiation. + */ + function setLanguageNegotiation() { + $edit = array( + 'language_interface[enabled][language-user-admin]' => TRUE, + 'language_interface[enabled][language-url]' => TRUE, + 'language_interface[weight][language-user-admin]' => -8, + 'language_interface[weight][language-url]' => -10, + ); + $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings')); + } + + /** + * Helper meethod for adding a custom language. + */ + function addCustomLanguage() { $langcode = 'xx'; // The English name for the language. $name = $this->randomName(16); @@ -63,42 +110,5 @@ function testUserAdminLanguageConfiguration() { 'direction' => '0', ); $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language')); - $this->drupalLogout(); - - // Login as non-admin user and edit account settings. - testUserAdminLanguageNoSetting($non_admin_user, 'after language'); - - // Login as admin user and edit account settings. - $this->drupalLogin($admin_user); - $path = 'user/' . $admin_user->uid . '/edit'; - $this->drupalGet($path); - // Ensure administration pages language settings widget is available. - $this->assertFieldById('edit-preferred-admin-langcode', '', 'Administration pages language selector available.'); - // Ensure custom language is present. - $this->assertText($name, 'Administration pages language present on form.'); - // Switch to our custom language. - $edit = array( - 'preferred_admin_langcode' => $langcode, - ); - $this->drupalPost($path, $edit, t('Save')); - // Ensure form was submitted successfully. - $this->assertText(t('The changes have been saved.'), 'Changes were saved.'); - // Check if language was changed. - $this->assertOptionSelected('edit-preferred-admin-langcode', $langcode, 'Administration pages language successfully updated.'); - - $this->drupalLogout(); - } - - /** - * Helper function to check if administration pages language setting is not - * available. - */ - function testUserAdminLanguageNoSetting($check_user, $context) { - $context = $check_user->name . ' '. $context; - $this->drupalLogin($check_user); - $path = 'user/' . $check_user->uid . '/edit'; - $this->drupalGet($path); - // Ensure administration pages language settings widget is not available. - $this->assertNoFieldById('edit-preferred-admin-langcode', '', 'Administration pages language selector not available (' . $context . ').'); } }