UserAdminLanguageTest.php in Drupal 10
File
core/modules/user/tests/src/Functional/UserAdminLanguageTest.php
View source
<?php
namespace Drupal\Tests\user\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
class UserAdminLanguageTest extends BrowserTestBase {
protected $adminUser;
protected $regularUser;
protected static $modules = [
'user',
'language',
'language_test',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
]);
$this->regularUser = $this
->drupalCreateUser();
}
public function testUserAdminLanguageConfigurationNotAvailableWithOnlyOneLanguage() {
$this
->drupalLogin($this->adminUser);
$this
->setLanguageNegotiation();
$path = 'user/' . $this->adminUser
->id() . '/edit';
$this
->drupalGet($path);
$this
->assertSession()
->fieldNotExists('edit-preferred-admin-langcode');
}
public function testUserAdminLanguageConfigurationAvailableWithAdminLanguageNegotiation() {
$this
->drupalLogin($this->adminUser);
$this
->addCustomLanguage();
$path = 'user/' . $this->adminUser
->id() . '/edit';
$this
->drupalGet($path);
$this
->assertSession()
->fieldNotExists('edit-preferred-admin-langcode');
$this
->setLanguageNegotiation();
$this
->drupalGet($path);
$this
->assertSession()
->fieldExists('edit-preferred-admin-langcode');
}
public function testUserAdminLanguageConfigurationAvailableIfAdminLanguageNegotiationIsEnabled() {
$this
->drupalLogin($this->adminUser);
$this
->addCustomLanguage();
$this
->setLanguageNegotiation();
$path = 'user/' . $this->adminUser
->id() . '/edit';
$this
->drupalGet($path);
$this
->assertSession()
->fieldExists('edit-preferred-admin-langcode');
$editor = $this
->drupalCreateUser([
'view the administration theme',
]);
$this
->drupalLogin($editor);
$path = 'user/' . $editor
->id() . '/edit';
$this
->drupalGet($path);
$this
->assertSession()
->fieldExists('edit-preferred-admin-langcode');
$this
->drupalLogin($this->regularUser);
$path = 'user/' . $this->regularUser
->id() . '/edit';
$this
->drupalGet($path);
$this
->assertSession()
->fieldNotExists('edit-preferred-admin-langcode');
}
public function testActualNegotiation() {
$this
->drupalLogin($this->adminUser);
$this
->addCustomLanguage();
$this
->setLanguageNegotiation();
$path = 'user/' . $this->adminUser
->id() . '/edit';
$this
->drupalGet($path);
$this
->assertSession()
->pageTextContains('Language negotiation method: language-default');
$this
->drupalGet('xx/' . $path);
$this
->assertSession()
->pageTextContains('Language negotiation method: language-url');
$edit = [];
$edit['preferred_admin_langcode'] = 'xx';
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save');
$this
->drupalGet($path);
$this
->assertSession()
->pageTextContains('Language negotiation method: language-user-admin');
$this
->drupalGet('xx/' . $path);
$this
->assertSession()
->pageTextContains('Language negotiation method: language-url');
$this
->setLanguageNegotiation(TRUE);
$this
->drupalGet($path);
$this
->assertSession()
->pageTextContains('Language negotiation method: language-user-admin');
$this
->drupalGet('xx/' . $path);
$this
->assertSession()
->pageTextContains('Language negotiation method: language-user-admin');
$edit = [];
$edit['preferred_admin_langcode'] = '';
$this
->drupalGet($path);
$this
->submitForm($edit, 'Save');
$this
->drupalGet($path);
$this
->assertSession()
->pageTextContains('Language negotiation method: language-default');
$this
->drupalGet('xx/' . $path);
$this
->assertSession()
->pageTextContains('Language negotiation method: language-url');
}
public function setLanguageNegotiation($admin_first = FALSE) {
$edit = [
'language_interface[enabled][language-user-admin]' => TRUE,
'language_interface[enabled][language-url]' => TRUE,
'language_interface[weight][language-user-admin]' => $admin_first ? -12 : -8,
'language_interface[weight][language-url]' => -10,
];
$this
->drupalGet('admin/config/regional/language/detection');
$this
->submitForm($edit, 'Save settings');
}
public function addCustomLanguage() {
$langcode = 'xx';
$name = $this
->randomMachineName(16);
$edit = [
'predefined_langcode' => 'custom',
'langcode' => $langcode,
'label' => $name,
'direction' => LanguageInterface::DIRECTION_LTR,
];
$this
->drupalGet('admin/config/regional/language/add');
$this
->submitForm($edit, 'Add custom language');
}
}