Chris@17: adminUser = $this->drupalCreateUser(['administer languages', 'access administration pages']); Chris@17: // User to check non-admin access. Chris@17: $this->regularUser = $this->drupalCreateUser(); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests that admin language is not configurable in single language sites. Chris@17: */ Chris@17: public function testUserAdminLanguageConfigurationNotAvailableWithOnlyOneLanguage() { Chris@17: $this->drupalLogin($this->adminUser); Chris@17: $this->setLanguageNegotiation(); Chris@17: $path = 'user/' . $this->adminUser->id() . '/edit'; Chris@17: $this->drupalGet($path); Chris@17: // Ensure administration pages language settings widget is not available. Chris@17: $this->assertNoFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector not available.'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests that admin language negotiation is configurable only if enabled. Chris@17: */ Chris@17: public function testUserAdminLanguageConfigurationAvailableWithAdminLanguageNegotiation() { Chris@17: $this->drupalLogin($this->adminUser); Chris@17: $this->addCustomLanguage(); Chris@17: $path = 'user/' . $this->adminUser->id() . '/edit'; Chris@17: Chris@17: // Checks with user administration pages language negotiation disabled. Chris@17: $this->drupalGet($path); Chris@17: // Ensure administration pages language settings widget is not available. Chris@17: $this->assertNoFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector not available.'); Chris@17: Chris@17: // Checks with user administration pages language negotiation enabled. Chris@17: $this->setLanguageNegotiation(); Chris@17: $this->drupalGet($path); Chris@17: // Ensure administration pages language settings widget is available. Chris@17: $this->assertFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector is available.'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests that the admin language is configurable only for administrators. Chris@17: * Chris@17: * If a user has the permission "access administration pages", they should Chris@17: * be able to see the setting to pick the language they want those pages in. Chris@17: * Chris@17: * If a user does not have that permission, it would confusing for them to Chris@17: * have a setting for pages they cannot access, so they should not be able to Chris@17: * set a language for those pages. Chris@17: */ Chris@17: public function testUserAdminLanguageConfigurationAvailableIfAdminLanguageNegotiationIsEnabled() { Chris@17: $this->drupalLogin($this->adminUser); Chris@17: // Adds a new language, because with only one language, setting won't show. Chris@17: $this->addCustomLanguage(); Chris@17: $this->setLanguageNegotiation(); Chris@17: $path = 'user/' . $this->adminUser->id() . '/edit'; Chris@17: $this->drupalGet($path); Chris@17: // Ensure administration pages language setting is visible for admin. Chris@17: $this->assertFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector available for admins.'); Chris@17: Chris@17: // Ensure administration pages language setting is hidden for non-admins. Chris@17: $this->drupalLogin($this->regularUser); Chris@17: $path = 'user/' . $this->regularUser->id() . '/edit'; Chris@17: $this->drupalGet($path); Chris@17: $this->assertNoFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector not available for regular user.'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Tests the actual language negotiation. Chris@17: */ Chris@17: public function testActualNegotiation() { Chris@17: $this->drupalLogin($this->adminUser); Chris@17: $this->addCustomLanguage(); Chris@17: $this->setLanguageNegotiation(); Chris@17: Chris@17: // Even though we have admin language negotiation, so long as the user has Chris@17: // no preference set, negotiation will fall back further. Chris@17: $path = 'user/' . $this->adminUser->id() . '/edit'; Chris@17: $this->drupalGet($path); Chris@17: $this->assertText('Language negotiation method: language-default'); Chris@17: $this->drupalGet('xx/' . $path); Chris@17: $this->assertText('Language negotiation method: language-url'); Chris@17: Chris@17: // Set a preferred language code for the user. Chris@17: $edit = []; Chris@17: $edit['preferred_admin_langcode'] = 'xx'; Chris@17: $this->drupalPostForm($path, $edit, t('Save')); Chris@17: Chris@17: // Test negotiation with the URL method first. The admin method will only Chris@17: // be used if the URL method did not match. Chris@17: $this->drupalGet($path); Chris@17: $this->assertText('Language negotiation method: language-user-admin'); Chris@17: $this->drupalGet('xx/' . $path); Chris@17: $this->assertText('Language negotiation method: language-url'); Chris@17: Chris@17: // Test negotiation with the admin language method first. The admin method Chris@17: // will be used at all times. Chris@17: $this->setLanguageNegotiation(TRUE); Chris@17: $this->drupalGet($path); Chris@17: $this->assertText('Language negotiation method: language-user-admin'); Chris@17: $this->drupalGet('xx/' . $path); Chris@17: $this->assertText('Language negotiation method: language-user-admin'); Chris@17: Chris@17: // Unset the preferred language code for the user. Chris@17: $edit = []; Chris@17: $edit['preferred_admin_langcode'] = ''; Chris@17: $this->drupalPostForm($path, $edit, t('Save')); Chris@17: $this->drupalGet($path); Chris@17: $this->assertText('Language negotiation method: language-default'); Chris@17: $this->drupalGet('xx/' . $path); Chris@17: $this->assertText('Language negotiation method: language-url'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Sets the User interface negotiation detection method. Chris@17: * Chris@17: * Enables the "Account preference for administration pages" language Chris@17: * detection method for the User interface language negotiation type. Chris@17: * Chris@17: * @param bool $admin_first Chris@17: * Whether the admin negotiation should be first. Chris@17: */ Chris@17: public function setLanguageNegotiation($admin_first = FALSE) { Chris@17: $edit = [ Chris@17: 'language_interface[enabled][language-user-admin]' => TRUE, Chris@17: 'language_interface[enabled][language-url]' => TRUE, Chris@17: 'language_interface[weight][language-user-admin]' => ($admin_first ? -12 : -8), Chris@17: 'language_interface[weight][language-url]' => -10, Chris@17: ]; Chris@17: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Helper method for adding a custom language. Chris@17: */ Chris@17: public function addCustomLanguage() { Chris@17: $langcode = 'xx'; Chris@17: // The English name for the language. Chris@17: $name = $this->randomMachineName(16); Chris@17: $edit = [ Chris@17: 'predefined_langcode' => 'custom', Chris@17: 'langcode' => $langcode, Chris@17: 'label' => $name, Chris@17: 'direction' => LanguageInterface::DIRECTION_LTR, Chris@17: ]; Chris@17: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); Chris@17: } Chris@17: Chris@17: }