function LanguageSelectElementTest::testLanguageSelectElementOptions

Same name in other branches
  1. 8.9.x core/modules/system/tests/src/Functional/Form/LanguageSelectElementTest.php \Drupal\Tests\system\Functional\Form\LanguageSelectElementTest::testLanguageSelectElementOptions()
  2. 10 core/modules/system/tests/src/Functional/Form/LanguageSelectElementTest.php \Drupal\Tests\system\Functional\Form\LanguageSelectElementTest::testLanguageSelectElementOptions()
  3. 11.x core/modules/system/tests/src/Functional/Form/LanguageSelectElementTest.php \Drupal\Tests\system\Functional\Form\LanguageSelectElementTest::testLanguageSelectElementOptions()

Tests that the options printed by the language select element are correct.

File

core/modules/system/tests/src/Functional/Form/LanguageSelectElementTest.php, line 32

Class

LanguageSelectElementTest
Tests the language selection form element.

Namespace

Drupal\Tests\system\Functional\Form

Code

public function testLanguageSelectElementOptions() {
    // Add some languages.
    ConfigurableLanguage::create([
        'id' => 'aaa',
        'label' => $this->randomMachineName(),
    ])
        ->save();
    ConfigurableLanguage::create([
        'id' => 'bbb',
        'label' => $this->randomMachineName(),
    ])
        ->save();
    \Drupal::languageManager()->reset();
    $this->drupalGet('form-test/language_select');
    // Check that the language fields were rendered on the page.
    $ids = [
        'edit-languages-all' => LanguageInterface::STATE_ALL,
        'edit-languages-configurable' => LanguageInterface::STATE_CONFIGURABLE,
        'edit-languages-locked' => LanguageInterface::STATE_LOCKED,
        'edit-languages-config-and-locked' => LanguageInterface::STATE_CONFIGURABLE | LanguageInterface::STATE_LOCKED,
    ];
    foreach ($ids as $id => $flags) {
        $this->assertSession()
            ->fieldExists($id);
        $options = [];
        
        /** @var \Drupal\Core\Language\LanguageManagerInterface $language_manager */
        $language_manager = $this->container
            ->get('language_manager');
        foreach ($language_manager->getLanguages($flags) as $langcode => $language) {
            $options[$langcode] = $language->isLocked() ? "- {$language->getName()} -" : $language->getName();
        }
        $this->_testLanguageSelectElementOptions($id, $options);
    }
    // Test that the #options were not altered by #languages.
    $this->assertSession()
        ->fieldExists('edit-language-custom-options');
    $this->_testLanguageSelectElementOptions('edit-language-custom-options', [
        'opt1' => 'First option',
        'opt2' => 'Second option',
        'opt3' => 'Third option',
    ]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.