You are here

public function LanguageLocaleListTest::testLanguageLocaleList in Drupal 9

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

Tests adding, editing, and deleting languages.

File

core/modules/language/tests/src/Functional/LanguageLocaleListTest.php, line 39

Class

LanguageLocaleListTest
Adds a new language with translations and tests language list order.

Namespace

Drupal\Tests\language\Functional

Code

public function testLanguageLocaleList() {

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

  // Add predefined language.
  $edit = [
    'predefined_langcode' => 'fr',
  ];
  $this
    ->drupalGet('admin/config/regional/language/add');
  $this
    ->submitForm($edit, 'Add language');
  $this
    ->assertSession()
    ->pageTextContains('The language French has been created and can now be used');
  $this
    ->assertSession()
    ->addressEquals(Url::fromRoute('entity.configurable_language.collection'));
  $this
    ->rebuildContainer();

  // Translate Spanish language to French (Espagnol).
  $source = $this->storage
    ->createString([
    'source' => 'Spanish',
    'context' => '',
  ])
    ->save();
  $this->storage
    ->createTranslation([
    'lid' => $source->lid,
    'language' => 'fr',
    'translation' => 'Espagnol',
  ])
    ->save();

  // Get language list displayed in select list.
  $this
    ->drupalGet('fr/admin/config/regional/language/add');
  $options = $this
    ->assertSession()
    ->selectExists('edit-predefined-langcode')
    ->findAll('css', 'option');
  $options = array_map(function ($item) {
    return $item
      ->getText();
  }, $options);

  // Remove the 'Custom language...' option form the end.
  array_pop($options);

  // Order language list.
  $options_ordered = $options;
  natcasesort($options_ordered);

  // Check the language list displayed is ordered.
  $this
    ->assertSame($options, $options_ordered, 'Language list is ordered.');
}