db backup prior to drupal security update
[yaffs-website] / web / core / modules / system / src / Tests / Form / LanguageSelectElementTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Form;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\simpletest\WebTestBase;
9
10 /**
11  * Tests that the language select form element prints and submits the right
12  * options.
13  *
14  * @group Form
15  */
16 class LanguageSelectElementTest extends WebTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['form_test', 'language'];
24
25   /**
26    * Tests that the options printed by the language select element are correct.
27    */
28   public function testLanguageSelectElementOptions() {
29     // Add some languages.
30     ConfigurableLanguage::create([
31       'id' => 'aaa',
32       'label' => $this->randomMachineName(),
33     ])->save();
34
35     ConfigurableLanguage::create([
36       'id' => 'bbb',
37       'label' => $this->randomMachineName(),
38     ])->save();
39
40     \Drupal::languageManager()->reset();
41
42     $this->drupalGet('form-test/language_select');
43     // Check that the language fields were rendered on the page.
44     $ids = [
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
49     ];
50     foreach ($ids as $id => $flags) {
51       $this->assertField($id, format_string('The @id field was found on the page.', ['@id' => $id]));
52       $options = [];
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();
57       }
58       $this->_testLanguageSelectElementOptions($id, $options);
59     }
60
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']);
64   }
65
66   /**
67    * Tests the case when the language select elements should not be printed.
68    *
69    * This happens when the language module is disabled.
70    */
71   public function testHiddenLanguageSelectElement() {
72     // Disable the language module, so that the language select field will not
73     // be rendered.
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]));
80     }
81
82     // Check that the submitted values were the default values of the language
83     // field elements.
84     $edit = [];
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');
92   }
93
94   /**
95    * Helper function to check the options of a language select form element.
96    *
97    * @param string $id
98    *   The id of the language select element to check.
99    *
100    * @param array $options
101    *   An array with options to compare with.
102    */
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 . "']");
107     $count = 0;
108     foreach ($elements[0]->option as $option) {
109       $count++;
110       $option_title = current($options);
111       $this->assertEqual((string) $option, $option_title);
112       next($options);
113     }
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]));
115   }
116
117 }