3 namespace Drupal\system\Tests\Form;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\simpletest\WebTestBase;
11 * Tests that the language select form element prints and submits the right
16 class LanguageSelectElementTest extends WebTestBase {
23 public static $modules = ['form_test', 'language'];
26 * Tests that the options printed by the language select element are correct.
28 public function testLanguageSelectElementOptions() {
29 // Add some languages.
30 ConfigurableLanguage::create([
32 'label' => $this->randomMachineName(),
35 ConfigurableLanguage::create([
37 'label' => $this->randomMachineName(),
40 \Drupal::languageManager()->reset();
42 $this->drupalGet('form-test/language_select');
43 // Check that the language fields were rendered on the page.
45 'edit-languages-all' => LanguageInterface::STATE_ALL,
46 'edit-languages-configurable' => LanguageInterface::STATE_CONFIGURABLE,
47 'edit-languages-locked' => LanguageInterface::STATE_LOCKED,
48 'edit-languages-config-and-locked' => LanguageInterface::STATE_CONFIGURABLE | LanguageInterface::STATE_LOCKED
50 foreach ($ids as $id => $flags) {
51 $this->assertField($id, format_string('The @id field was found on the page.', ['@id' => $id]));
53 /* @var $language_manager \Drupal\Core\Language\LanguageManagerInterface */
54 $language_manager = $this->container->get('language_manager');
55 foreach ($language_manager->getLanguages($flags) as $langcode => $language) {
56 $options[$langcode] = $language->isLocked() ? t('- @name -', ['@name' => $language->getName()]) : $language->getName();
58 $this->_testLanguageSelectElementOptions($id, $options);
61 // Test that the #options were not altered by #languages.
62 $this->assertField('edit-language-custom-options', format_string('The @id field was found on the page.', ['@id' => 'edit-language-custom-options']));
63 $this->_testLanguageSelectElementOptions('edit-language-custom-options', ['opt1' => 'First option', 'opt2' => 'Second option', 'opt3' => 'Third option']);
67 * Tests the case when the language select elements should not be printed.
69 * This happens when the language module is disabled.
71 public function testHiddenLanguageSelectElement() {
72 // Disable the language module, so that the language select field will not
74 $this->container->get('module_installer')->uninstall(['language']);
75 $this->drupalGet('form-test/language_select');
76 // Check that the language fields were rendered on the page.
77 $ids = ['edit-languages-all', 'edit-languages-configurable', 'edit-languages-locked', 'edit-languages-config-and-locked'];
78 foreach ($ids as $id) {
79 $this->assertNoField($id, format_string('The @id field was not found on the page.', ['@id' => $id]));
82 // Check that the submitted values were the default values of the language
85 $this->drupalPostForm(NULL, $edit, t('Submit'));
86 $values = Json::decode($this->getRawContent());
87 $this->assertEqual($values['languages_all'], 'xx');
88 $this->assertEqual($values['languages_configurable'], 'en');
89 $this->assertEqual($values['languages_locked'], LanguageInterface::LANGCODE_NOT_SPECIFIED);
90 $this->assertEqual($values['languages_config_and_locked'], 'dummy_value');
91 $this->assertEqual($values['language_custom_options'], 'opt2');
95 * Helper function to check the options of a language select form element.
98 * The id of the language select element to check.
100 * @param array $options
101 * An array with options to compare with.
103 protected function _testLanguageSelectElementOptions($id, $options) {
104 // Check that the options in the language field are exactly the same,
105 // including the order, as the languages sent as a parameter.
106 $elements = $this->xpath("//select[@id='" . $id . "']");
108 foreach ($elements[0]->option as $option) {
110 $option_title = current($options);
111 $this->assertEqual((string) $option, $option_title);
114 $this->assertEqual($count, count($options), format_string('The number of languages and the number of options shown by the language element are the same: @languages languages, @number options', ['@languages' => count($options), '@number' => $count]));