3 namespace Drupal\Tests\language\Functional;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUI;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests alterations to language types/negotiation info.
14 class LanguageNegotiationInfoTest extends BrowserTestBase {
21 public static $modules = ['language', 'content_translation'];
26 protected function setUp() {
28 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'view the administration theme', 'administer modules']);
29 $this->drupalLogin($admin_user);
30 $this->drupalPostForm('admin/config/regional/language/add', ['predefined_langcode' => 'it'], t('Add language'));
34 * Returns the configurable language manager.
36 * @return \Drupal\language\ConfigurableLanguageManager
38 protected function languageManager() {
39 return $this->container->get('language_manager');
43 * Sets state flags for language_test module.
45 * Ensures to correctly update data both in the child site and the test runner
48 * @param array $values
49 * The key/value pairs to set in state.
51 protected function stateSet(array $values) {
52 // Set the new state values.
53 $this->container->get('state')->setMultiple($values);
54 // Refresh in-memory static state/config caches and static variables.
55 $this->refreshVariables();
56 // Refresh/rewrite language negotiation configuration, in order to pick up
57 // the manipulations performed by language_test module's info alter hooks.
58 $this->container->get('language_negotiator')->purgeConfiguration();
62 * Tests alterations to language types/negotiation info.
64 public function testInfoAlterations() {
66 // Enable language_test type info.
67 'language_test.language_types' => TRUE,
68 // Enable language_test negotiation info (not altered yet).
69 'language_test.language_negotiation_info' => TRUE,
70 // Alter LanguageInterface::TYPE_CONTENT to be configurable.
71 'language_test.content_language_type' => TRUE,
73 $this->container->get('module_installer')->install(['language_test']);
76 // Check that fixed language types are properly configured without the need
77 // of saving the language negotiation settings.
78 $this->checkFixedLanguageTypes();
80 $type = LanguageInterface::TYPE_CONTENT;
81 $language_types = $this->languageManager()->getLanguageTypes();
82 $this->assertTrue(in_array($type, $language_types), 'Content language type is configurable.');
84 // Enable some core and custom language negotiation methods. The test
85 // language type is supposed to be configurable.
86 $test_type = 'test_language_type';
87 $interface_method_id = LanguageNegotiationUI::METHOD_ID;
88 $test_method_id = 'test_language_negotiation_method';
89 $form_field = $type . '[enabled][' . $interface_method_id . ']';
92 $type . '[enabled][' . $test_method_id . ']' => TRUE,
93 $test_type . '[enabled][' . $test_method_id . ']' => TRUE,
94 $test_type . '[configurable]' => TRUE,
96 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
98 // Alter language negotiation info to remove interface language negotiation
101 'language_test.language_negotiation_info_alter' => TRUE,
104 $negotiation = $this->config('language.types')->get('negotiation.' . $type . '.enabled');
105 $this->assertFalse(isset($negotiation[$interface_method_id]), 'Interface language negotiation method removed from the stored settings.');
107 $this->drupalGet('admin/config/regional/language/detection');
108 $this->assertNoFieldByName($form_field, NULL, 'Interface language negotiation method unavailable.');
110 // Check that type-specific language negotiation methods can be assigned
111 // only to the corresponding language types.
112 foreach ($this->languageManager()->getLanguageTypes() as $type) {
113 $form_field = $type . '[enabled][test_language_negotiation_method_ts]';
114 if ($type == $test_type) {
115 $this->assertFieldByName($form_field, NULL, format_string('Type-specific test language negotiation method available for %type.', ['%type' => $type]));
118 $this->assertNoFieldByName($form_field, NULL, format_string('Type-specific test language negotiation method unavailable for %type.', ['%type' => $type]));
122 // Check language negotiation results.
123 $this->drupalGet('');
124 $last = $this->container->get('state')->get('language_test.language_negotiation_last');
125 foreach ($this->languageManager()->getDefinedLanguageTypes() as $type) {
126 $langcode = $last[$type];
127 $value = $type == LanguageInterface::TYPE_CONTENT || strpos($type, 'test') !== FALSE ? 'it' : 'en';
128 $this->assertEqual($langcode, $value, format_string('The negotiated language for %type is %language', ['%type' => $type, '%language' => $value]));
131 // Uninstall language_test and check that everything is set back to the
133 $this->container->get('module_installer')->uninstall(['language_test']);
134 $this->rebuildContainer();
136 // Check that only the core language types are available.
137 foreach ($this->languageManager()->getDefinedLanguageTypes() as $type) {
138 $this->assertTrue(strpos($type, 'test') === FALSE, format_string('The %type language is still available', ['%type' => $type]));
141 // Check that fixed language types are properly configured, even those
142 // previously set to configurable.
143 $this->checkFixedLanguageTypes();
145 // Check that unavailable language negotiation methods are not present in
146 // the negotiation settings.
147 $negotiation = $this->config('language.types')->get('negotiation.' . $type . '.enabled');
148 $this->assertFalse(isset($negotiation[$test_method_id]), 'The disabled test language negotiation method is not part of the content language negotiation settings.');
150 // Check that configuration page presents the correct options and settings.
151 $this->assertNoRaw(t('Test language detection'), 'No test language type configuration available.');
152 $this->assertNoRaw(t('This is a test language negotiation method'), 'No test language negotiation method available.');
156 * Check that language negotiation for fixed types matches the stored one.
158 protected function checkFixedLanguageTypes() {
159 $configurable = $this->languageManager()->getLanguageTypes();
160 foreach ($this->languageManager()->getDefinedLanguageTypesInfo() as $type => $info) {
161 if (!in_array($type, $configurable) && isset($info['fixed'])) {
162 $negotiation = $this->config('language.types')->get('negotiation.' . $type . '.enabled');
163 $equal = array_keys($negotiation) === array_values($info['fixed']);
164 $this->assertTrue($equal, format_string('language negotiation for %type is properly set up', ['%type' => $type]));
170 * Tests altering config of configurable language types.
172 public function testConfigLangTypeAlterations() {
173 // Default of config.
174 $test_type = LanguageInterface::TYPE_CONTENT;
175 $this->assertFalse($this->isLanguageTypeConfigurable($test_type), 'Language type is not configurable.');
178 $edit = [$test_type . '[configurable]' => TRUE];
179 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
180 $this->assertTrue($this->isLanguageTypeConfigurable($test_type), 'Language type is now configurable.');
182 // After installing another module, the config should be the same.
183 $this->drupalPostForm('admin/modules', ['modules[test_module][enable]' => 1], t('Install'));
184 $this->assertTrue($this->isLanguageTypeConfigurable($test_type), 'Language type is still configurable.');
186 // After uninstalling the other module, the config should be the same.
187 $this->drupalPostForm('admin/modules/uninstall', ['uninstall[test_module]' => 1], t('Uninstall'));
188 $this->assertTrue($this->isLanguageTypeConfigurable($test_type), 'Language type is still configurable.');
192 * Checks whether the given language type is configurable.
194 * @param string $type
198 * TRUE if the specified language type is configurable, FALSE otherwise.
200 protected function isLanguageTypeConfigurable($type) {
201 $configurable_types = $this->config('language.types')->get('configurable');
202 return in_array($type, $configurable_types);