public function LanguageConfigurationTest::testLanguageConfiguration in Drupal 9
Same name and namespace in other branches
- 8 core/modules/language/tests/src/Functional/LanguageConfigurationTest.php \Drupal\Tests\language\Functional\LanguageConfigurationTest::testLanguageConfiguration()
Functional tests for adding, editing and deleting languages.
File
- core/modules/ language/ tests/ src/ Functional/ LanguageConfigurationTest.php, line 32 
Class
- LanguageConfigurationTest
- Adds and configures languages to check negotiation changes.
Namespace
Drupal\Tests\language\FunctionalCode
public function testLanguageConfiguration() {
  // Ensure the after installing the language module the weight of the English
  // language is still 0.
  $this
    ->assertEquals(0, ConfigurableLanguage::load('en')
    ->getWeight(), 'The English language has a weight of 0.');
  // User to add and remove language.
  $admin_user = $this
    ->drupalCreateUser([
    'administer languages',
    'access administration pages',
  ]);
  $this
    ->drupalLogin($admin_user);
  // Check if the Default English language has no path prefix.
  $this
    ->drupalGet('admin/config/regional/language/detection/url');
  $this
    ->assertSession()
    ->fieldValueEquals("prefix[en]", '');
  // Check that Add language is a primary button.
  $this
    ->drupalGet('admin/config/regional/language/add');
  $button = $this
    ->assertSession()
    ->buttonExists('Add language');
  $this
    ->assertTrue($button
    ->hasClass("button--primary"));
  // Add predefined language.
  $edit = [
    'predefined_langcode' => 'fr',
  ];
  $this
    ->submitForm($edit, 'Add language');
  $this
    ->assertSession()
    ->pageTextContains('French');
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.configurable_language.collection'));
  // Langcode for Languages is always 'en'.
  $language = $this
    ->config('language.entity.fr')
    ->get();
  $this
    ->assertEquals('en', $language['langcode']);
  // Check if the Default English language has no path prefix.
  $this
    ->drupalGet('admin/config/regional/language/detection/url');
  $this
    ->assertSession()
    ->fieldValueEquals("prefix[en]", '');
  // Check if French has a path prefix.
  $this
    ->drupalGet('admin/config/regional/language/detection/url');
  $this
    ->assertSession()
    ->fieldValueEquals("prefix[fr]", 'fr');
  // Check if we can change the default language.
  $this
    ->drupalGet('admin/config/regional/language');
  $this
    ->assertSession()
    ->checkboxChecked('edit-site-default-language-en');
  // Change the default language.
  $edit = [
    'site_default_language' => 'fr',
  ];
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->rebuildContainer();
  $this
    ->assertSession()
    ->checkboxChecked('edit-site-default-language-fr');
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.configurable_language.collection', [], [
    'langcode' => 'fr',
  ]));
  // Check if a valid language prefix is added after changing the default
  // language.
  $this
    ->drupalGet('admin/config/regional/language/detection/url');
  $this
    ->assertSession()
    ->fieldValueEquals("prefix[en]", 'en');
  // Check if French still has a path prefix.
  $this
    ->drupalGet('admin/config/regional/language/detection/url');
  $this
    ->assertSession()
    ->fieldValueEquals("prefix[fr]", 'fr');
  // Check that prefix can be changed.
  $edit = [
    'prefix[fr]' => 'french',
  ];
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->assertSession()
    ->fieldValueEquals("prefix[fr]", 'french');
  // Check that the prefix can be removed.
  $edit = [
    'prefix[fr]' => '',
  ];
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->assertSession()
    ->pageTextNotContains('The prefix may only be left blank for the selected detection fallback language.');
  // Change default negotiation language.
  $this
    ->config('language.negotiation')
    ->set('selected_langcode', 'fr')
    ->save();
  // Check that the prefix of a language that is not the negotiation one
  // cannot be changed to empty string.
  $edit = [
    'prefix[en]' => '',
  ];
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->assertSession()
    ->pageTextContains('The prefix may only be left blank for the selected detection fallback language.');
  // Check that prefix cannot be changed to contain a slash.
  $edit = [
    'prefix[en]' => 'foo/bar',
  ];
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->assertSession()
    ->pageTextContains('The prefix may not contain a slash.');
  // Remove English language and add a new Language to check if langcode of
  // Language entity is 'en'.
  $this
    ->drupalGet('admin/config/regional/language/delete/en');
  $this
    ->submitForm([], 'Delete');
  $this
    ->rebuildContainer();
  $this
    ->assertSession()
    ->pageTextContains("The English (en) language has been removed.");
  // Ensure that French language has a weight of 1 after being created through
  // the UI.
  $french = ConfigurableLanguage::load('fr');
  $this
    ->assertEquals(1, $french
    ->getWeight(), 'The French language has a weight of 1.');
  // Ensure that French language can now have a weight of 0.
  $french
    ->setWeight(0)
    ->save();
  $this
    ->assertEquals(0, $french
    ->getWeight(), 'The French language has a weight of 0.');
  // Ensure that new languages created through the API get a weight of 0.
  $afrikaans = ConfigurableLanguage::createFromLangcode('af');
  $afrikaans
    ->save();
  $this
    ->assertEquals(0, $afrikaans
    ->getWeight(), 'The Afrikaans language has a weight of 0.');
  // Ensure that a new language can be created with any weight.
  $arabic = ConfigurableLanguage::createFromLangcode('ar');
  $arabic
    ->setWeight(4)
    ->save();
  $this
    ->assertEquals(4, $arabic
    ->getWeight(), 'The Arabic language has a weight of 0.');
  $edit = [
    'predefined_langcode' => 'de',
  ];
  $this
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, 'Add language');
  $language = $this
    ->config('language.entity.de')
    ->get();
  $this
    ->assertEquals('fr', $language['langcode']);
  // Ensure that German language has a weight of 5 after being created through
  // the UI.
  $french = ConfigurableLanguage::load('de');
  $this
    ->assertEquals(5, $french
    ->getWeight(), 'The German language has a weight of 5.');
}