Commit 52e5d45e authored by alexpott's avatar alexpott

Issue #2403229 by robertdbailey, andypost, penyaskito, Gábor Hojtsy:...

Issue #2403229 by robertdbailey, andypost, penyaskito, Gábor Hojtsy: language.negotiation configuration can have overrides bleed in
parent 3a0002fc
<?php
/**
* @file
* Contains \Drupal\config\Tests\LanguageNegotiationFormOverrideTest.
*/
namespace Drupal\config\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests language-negotiation overrides are not on language-negotiation form.
*
* @group config
* @see \Drupal\Core\Form\ConfigFormBase
*/
class LanguageNegotiationFormOverrideTest extends WebTestBase {
public static $modules = array('language', 'locale');
/**
* Tests that overrides do not affect language-negotiation form values.
*/
public function testFormWithOverride() {
$this->drupalLogin($this->rootUser);
$overridden_value_en = 'whatever';
$overridden_value_es = 'loquesea';
// Set up an override.
$settings['config']['language.negotiation']['url']['prefixes'] = (object) array(
'value' => array('en' => $overridden_value_en, 'es' => $overridden_value_es),
'required' => TRUE,
);
$this->writeSettings($settings);
// Add predefined language.
$edit = array(
'predefined_langcode' => 'es',
);
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
// Overridden string for language-negotiation should not exist in the form.
$this->drupalGet('admin/config/regional/language/detection/url');
// The language-negotiation form should be found.
$this->assertText('Path prefix configuration', 'Language-negotiation form found for English.');
// The English override should not be found.
$this->assertNoFieldByName('prefix[en]', $overridden_value_en, 'Language-negotiation config override not found in English.');
// Now check the Spanish version of the page for the same thing.
$this->drupalGet($overridden_value_es . '/admin/config/regional/language/detection/url');
// The language-negotiation form should be found.
$this->assertText('Path prefix configuration', 'Language-negotiation form found for Spanish using the overridden prefix.');
// The Spanish override should not be found.
$this->assertNoFieldByName('prefix[es]', $overridden_value_es, 'Language-negotiation config override not found in Spanish.');
}
}
......@@ -284,6 +284,9 @@ function language_get_default_langcode($entity_type, $bundle) {
/**
* Reads language prefixes and uses the langcode if no prefix is set.
*
* @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
* Use \Drupal::config('language.negotiation')->get('url.prefixes') instead.
*/
function language_negotiation_url_prefixes() {
return \Drupal::config('language.negotiation')->get('url.prefixes');
......@@ -293,7 +296,8 @@ function language_negotiation_url_prefixes() {
* Update the list of prefixes from the installed languages.
*/
function language_negotiation_url_prefixes_update() {
$prefixes = language_negotiation_url_prefixes();
$config = \Drupal::configFactory()->getEditable('language.negotiation');
$prefixes = $config->get('url.prefixes');
foreach (\Drupal::languageManager()->getLanguages() as $language) {
// The prefix for this language should be updated if it's not assigned yet
// or the prefix is set to the empty string.
......@@ -304,38 +308,19 @@ function language_negotiation_url_prefixes_update() {
}
// Otherwise we keep the configured prefix.
}
language_negotiation_url_prefixes_save($prefixes);
}
/**
* Saves language prefix settings.
*/
function language_negotiation_url_prefixes_save(array $prefixes) {
// @todo https://www.drupal.org/node/2403229 $prefixes can contain
// configuration overrides.
\Drupal::configFactory()->getEditable('language.negotiation')
->set('url.prefixes', $prefixes)
->save();
$config->set('url.prefixes', $prefixes)->save();
}
/**
* Reads language domains.
*
* @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
* Use \Drupal::config('language.negotiation')->get('url.domains') instead.
*/
function language_negotiation_url_domains() {
return \Drupal::config('language.negotiation')->get('url.domains');
}
/**
* Saves the language domain settings.
*/
function language_negotiation_url_domains_save(array $domains) {
// @todo https://www.drupal.org/node/2403229 $domains can contain
// configuration overrides.
\Drupal::configFactory()->getEditable('language.negotiation')
->set('url.domains', $domains)
->save();
}
/**
* Implements hook_modules_installed().
*/
......@@ -376,9 +361,10 @@ function language_configurable_language_insert(ConfigurableLanguageInterface $la
}
// Add language to the list of language domains.
$domains = language_negotiation_url_domains();
$config = \Drupal::configFactory()->getEditable('language.negotiation');
$domains = $config->get('url.domains');
$domains[$language->id()] = '';
language_negotiation_url_domains_save($domains);
$config->set('url.domains', $domains)->save();
}
/**
......@@ -386,14 +372,16 @@ function language_configurable_language_insert(ConfigurableLanguageInterface $la
*/
function language_configurable_language_delete(ConfigurableLanguageInterface $language) {
// Remove language from language prefix list.
$prefixes = language_negotiation_url_prefixes();
$config = \Drupal::configFactory()->getEditable('language.negotiation');
$prefixes = $config->get('url.prefixes');
unset($prefixes[$language->id()]);
language_negotiation_url_prefixes_save($prefixes);
$config->set('url.prefixes', $prefixes)->save();
// Remove language from language domain list.
$domains = language_negotiation_url_domains();
$config = \Drupal::configFactory()->getEditable('language.negotiation');
$domains = $config->get('url.domains');
unset($domains[$language->id()]);
language_negotiation_url_domains_save($domains);
$config->set('url.domains', $domains)->save();
}
/**
......
......@@ -111,8 +111,8 @@ public function buildForm(array $form, FormStateInterface $form_state) {
);
$languages = $this->languageManager->getLanguages();
$prefixes = language_negotiation_url_prefixes();
$domains = language_negotiation_url_domains();
$prefixes = $config->get('url.prefixes');
$domains = $config->get('url.domains');
foreach ($languages as $langcode => $language) {
$t_args = array('%language' => $language->getName(), '%langcode' => $language->getId());
$form['prefix'][$langcode] = array(
......@@ -211,12 +211,11 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
// Save selected format (prefix or domain).
$this->config('language.negotiation')
->set('url.source', $form_state->getValue('language_negotiation_url_part'))
// Save new domain and prefix values.
->set('url.prefixes', $form_state->getValue('prefix'))
->set('url.domains', $form_state->getValue('domain'))
->save();
// Save new domain and prefix values.
language_negotiation_url_prefixes_save($form_state->getValue('prefix'));
language_negotiation_url_domains_save($form_state->getValue('domain'));
parent::submitForm($form, $form_state);
}
......
......@@ -67,7 +67,9 @@ public function testSameTypes() {
public function testEntityLanguage() {
$language = ConfigurableLanguage::createFromLangcode('de');
$language->save();
language_negotiation_url_prefixes_save(array('de' => 'de'));
\Drupal::configFactory()->getEditable('language.negotiation')
->set('url.prefixes', array('de' => 'de'))
->save();
// The container must be recreated after adding a new language.
$this->rebuildContainer();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment