You are here

public function LanguageCustomLanguageConfigurationTest::testLanguageConfiguration in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/language/tests/src/Functional/LanguageCustomLanguageConfigurationTest.php \Drupal\Tests\language\Functional\LanguageCustomLanguageConfigurationTest::testLanguageConfiguration()
  2. 10 core/modules/language/tests/src/Functional/LanguageCustomLanguageConfigurationTest.php \Drupal\Tests\language\Functional\LanguageCustomLanguageConfigurationTest::testLanguageConfiguration()

Functional tests for adding, editing and deleting languages.

File

core/modules/language/tests/src/Functional/LanguageCustomLanguageConfigurationTest.php, line 32

Class

LanguageCustomLanguageConfigurationTest
Adds and configures custom languages.

Namespace

Drupal\Tests\language\Functional

Code

public function testLanguageConfiguration() {

  // Create user with permissions to add and remove languages.
  $admin_user = $this
    ->drupalCreateUser([
    'administer languages',
    'access administration pages',
  ]);
  $this
    ->drupalLogin($admin_user);

  // Add custom language.
  $edit = [
    'predefined_langcode' => 'custom',
  ];
  $this
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, 'Add custom language');

  // Test validation on missing values.
  $this
    ->assertSession()
    ->pageTextContains('Language code field is required.');
  $this
    ->assertSession()
    ->pageTextContains('Language name field is required.');
  $empty_language = new Language();
  $this
    ->assertSession()
    ->checkboxChecked('edit-direction-' . $empty_language
    ->getDirection());
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('language.add'));

  // Test validation of invalid values.
  $edit = [
    'predefined_langcode' => 'custom',
    'langcode' => 'white space',
    'label' => '<strong>evil markup</strong>',
    'direction' => LanguageInterface::DIRECTION_LTR,
  ];
  $this
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, 'Add custom language');
  $this
    ->assertSession()
    ->pageTextContains("Language code must be a valid language tag as defined by the W3C.");
  $this
    ->assertSession()
    ->linkExists("defined by the W3C");
  $this
    ->assertSession()
    ->linkByHrefExists("http://www.w3.org/International/articles/language-tags/");
  $this
    ->assertSession()
    ->pageTextContains("Language name cannot contain any markup.");
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('language.add'));

  // Test adding a custom language with a numeric region code.
  $edit = [
    'predefined_langcode' => 'custom',
    'langcode' => 'es-419',
    'label' => 'Latin American Spanish',
    'direction' => LanguageInterface::DIRECTION_LTR,
  ];
  $this
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, 'Add custom language');
  $this
    ->assertSession()
    ->pageTextContains("The language {$edit['label']} has been created and can now be used.");
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.configurable_language.collection'));

  // Test validation of existing language values.
  $edit = [
    'predefined_langcode' => 'custom',
    'langcode' => 'de',
    'label' => 'German',
    'direction' => LanguageInterface::DIRECTION_LTR,
  ];

  // Add the language the first time.
  $this
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, 'Add custom language');
  $this
    ->assertSession()
    ->pageTextContains("The language {$edit['label']} has been created and can now be used.");
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.configurable_language.collection'));

  // Add the language a second time and confirm that this is not allowed.
  $this
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, 'Add custom language');
  $this
    ->assertSession()
    ->pageTextContains("The language {$edit['label']} ({$edit['langcode']}) already exists.");
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('language.add'));
}