annotate core/modules/user/tests/src/Functional/UserAdminLanguageTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@17 1 <?php
Chris@17 2
Chris@17 3 namespace Drupal\Tests\user\Functional;
Chris@17 4
Chris@17 5 use Drupal\Core\Language\LanguageInterface;
Chris@17 6 use Drupal\Tests\BrowserTestBase;
Chris@17 7
Chris@17 8 /**
Chris@17 9 * Tests users' ability to change their own administration language.
Chris@17 10 *
Chris@17 11 * @group user
Chris@17 12 */
Chris@17 13 class UserAdminLanguageTest extends BrowserTestBase {
Chris@17 14
Chris@17 15 /**
Chris@17 16 * A user with permission to access admin pages and administer languages.
Chris@17 17 *
Chris@17 18 * @var \Drupal\user\UserInterface
Chris@17 19 */
Chris@17 20 protected $adminUser;
Chris@17 21
Chris@17 22 /**
Chris@17 23 * A non-administrator user for this test.
Chris@17 24 *
Chris@17 25 * @var \Drupal\user\UserInterface
Chris@17 26 */
Chris@17 27 protected $regularUser;
Chris@17 28
Chris@17 29 /**
Chris@17 30 * Modules to enable.
Chris@17 31 *
Chris@17 32 * @var array
Chris@17 33 */
Chris@17 34 public static $modules = ['user', 'language', 'language_test'];
Chris@17 35
Chris@17 36 protected function setUp() {
Chris@17 37 parent::setUp();
Chris@17 38 // User to add and remove language.
Chris@17 39 $this->adminUser = $this->drupalCreateUser(['administer languages', 'access administration pages']);
Chris@17 40 // User to check non-admin access.
Chris@17 41 $this->regularUser = $this->drupalCreateUser();
Chris@17 42 }
Chris@17 43
Chris@17 44 /**
Chris@17 45 * Tests that admin language is not configurable in single language sites.
Chris@17 46 */
Chris@17 47 public function testUserAdminLanguageConfigurationNotAvailableWithOnlyOneLanguage() {
Chris@17 48 $this->drupalLogin($this->adminUser);
Chris@17 49 $this->setLanguageNegotiation();
Chris@17 50 $path = 'user/' . $this->adminUser->id() . '/edit';
Chris@17 51 $this->drupalGet($path);
Chris@17 52 // Ensure administration pages language settings widget is not available.
Chris@17 53 $this->assertNoFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector not available.');
Chris@17 54 }
Chris@17 55
Chris@17 56 /**
Chris@17 57 * Tests that admin language negotiation is configurable only if enabled.
Chris@17 58 */
Chris@17 59 public function testUserAdminLanguageConfigurationAvailableWithAdminLanguageNegotiation() {
Chris@17 60 $this->drupalLogin($this->adminUser);
Chris@17 61 $this->addCustomLanguage();
Chris@17 62 $path = 'user/' . $this->adminUser->id() . '/edit';
Chris@17 63
Chris@17 64 // Checks with user administration pages language negotiation disabled.
Chris@17 65 $this->drupalGet($path);
Chris@17 66 // Ensure administration pages language settings widget is not available.
Chris@17 67 $this->assertNoFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector not available.');
Chris@17 68
Chris@17 69 // Checks with user administration pages language negotiation enabled.
Chris@17 70 $this->setLanguageNegotiation();
Chris@17 71 $this->drupalGet($path);
Chris@17 72 // Ensure administration pages language settings widget is available.
Chris@17 73 $this->assertFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector is available.');
Chris@17 74 }
Chris@17 75
Chris@17 76 /**
Chris@17 77 * Tests that the admin language is configurable only for administrators.
Chris@17 78 *
Chris@17 79 * If a user has the permission "access administration pages", they should
Chris@17 80 * be able to see the setting to pick the language they want those pages in.
Chris@17 81 *
Chris@17 82 * If a user does not have that permission, it would confusing for them to
Chris@17 83 * have a setting for pages they cannot access, so they should not be able to
Chris@17 84 * set a language for those pages.
Chris@17 85 */
Chris@17 86 public function testUserAdminLanguageConfigurationAvailableIfAdminLanguageNegotiationIsEnabled() {
Chris@17 87 $this->drupalLogin($this->adminUser);
Chris@17 88 // Adds a new language, because with only one language, setting won't show.
Chris@17 89 $this->addCustomLanguage();
Chris@17 90 $this->setLanguageNegotiation();
Chris@17 91 $path = 'user/' . $this->adminUser->id() . '/edit';
Chris@17 92 $this->drupalGet($path);
Chris@17 93 // Ensure administration pages language setting is visible for admin.
Chris@17 94 $this->assertFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector available for admins.');
Chris@17 95
Chris@17 96 // Ensure administration pages language setting is hidden for non-admins.
Chris@17 97 $this->drupalLogin($this->regularUser);
Chris@17 98 $path = 'user/' . $this->regularUser->id() . '/edit';
Chris@17 99 $this->drupalGet($path);
Chris@17 100 $this->assertNoFieldByXPath($this->constructFieldXpath('id', 'edit-preferred-admin-langcode'), NULL, 'Administration pages language selector not available for regular user.');
Chris@17 101 }
Chris@17 102
Chris@17 103 /**
Chris@17 104 * Tests the actual language negotiation.
Chris@17 105 */
Chris@17 106 public function testActualNegotiation() {
Chris@17 107 $this->drupalLogin($this->adminUser);
Chris@17 108 $this->addCustomLanguage();
Chris@17 109 $this->setLanguageNegotiation();
Chris@17 110
Chris@17 111 // Even though we have admin language negotiation, so long as the user has
Chris@17 112 // no preference set, negotiation will fall back further.
Chris@17 113 $path = 'user/' . $this->adminUser->id() . '/edit';
Chris@17 114 $this->drupalGet($path);
Chris@17 115 $this->assertText('Language negotiation method: language-default');
Chris@17 116 $this->drupalGet('xx/' . $path);
Chris@17 117 $this->assertText('Language negotiation method: language-url');
Chris@17 118
Chris@17 119 // Set a preferred language code for the user.
Chris@17 120 $edit = [];
Chris@17 121 $edit['preferred_admin_langcode'] = 'xx';
Chris@17 122 $this->drupalPostForm($path, $edit, t('Save'));
Chris@17 123
Chris@17 124 // Test negotiation with the URL method first. The admin method will only
Chris@17 125 // be used if the URL method did not match.
Chris@17 126 $this->drupalGet($path);
Chris@17 127 $this->assertText('Language negotiation method: language-user-admin');
Chris@17 128 $this->drupalGet('xx/' . $path);
Chris@17 129 $this->assertText('Language negotiation method: language-url');
Chris@17 130
Chris@17 131 // Test negotiation with the admin language method first. The admin method
Chris@17 132 // will be used at all times.
Chris@17 133 $this->setLanguageNegotiation(TRUE);
Chris@17 134 $this->drupalGet($path);
Chris@17 135 $this->assertText('Language negotiation method: language-user-admin');
Chris@17 136 $this->drupalGet('xx/' . $path);
Chris@17 137 $this->assertText('Language negotiation method: language-user-admin');
Chris@17 138
Chris@17 139 // Unset the preferred language code for the user.
Chris@17 140 $edit = [];
Chris@17 141 $edit['preferred_admin_langcode'] = '';
Chris@17 142 $this->drupalPostForm($path, $edit, t('Save'));
Chris@17 143 $this->drupalGet($path);
Chris@17 144 $this->assertText('Language negotiation method: language-default');
Chris@17 145 $this->drupalGet('xx/' . $path);
Chris@17 146 $this->assertText('Language negotiation method: language-url');
Chris@17 147 }
Chris@17 148
Chris@17 149 /**
Chris@17 150 * Sets the User interface negotiation detection method.
Chris@17 151 *
Chris@17 152 * Enables the "Account preference for administration pages" language
Chris@17 153 * detection method for the User interface language negotiation type.
Chris@17 154 *
Chris@17 155 * @param bool $admin_first
Chris@17 156 * Whether the admin negotiation should be first.
Chris@17 157 */
Chris@17 158 public function setLanguageNegotiation($admin_first = FALSE) {
Chris@17 159 $edit = [
Chris@17 160 'language_interface[enabled][language-user-admin]' => TRUE,
Chris@17 161 'language_interface[enabled][language-url]' => TRUE,
Chris@17 162 'language_interface[weight][language-user-admin]' => ($admin_first ? -12 : -8),
Chris@17 163 'language_interface[weight][language-url]' => -10,
Chris@17 164 ];
Chris@17 165 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
Chris@17 166 }
Chris@17 167
Chris@17 168 /**
Chris@17 169 * Helper method for adding a custom language.
Chris@17 170 */
Chris@17 171 public function addCustomLanguage() {
Chris@17 172 $langcode = 'xx';
Chris@17 173 // The English name for the language.
Chris@17 174 $name = $this->randomMachineName(16);
Chris@17 175 $edit = [
Chris@17 176 'predefined_langcode' => 'custom',
Chris@17 177 'langcode' => $langcode,
Chris@17 178 'label' => $name,
Chris@17 179 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@17 180 ];
Chris@17 181 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@17 182 }
Chris@17 183
Chris@17 184 }