View source
<?php
namespace Drupal\Tests\language\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUI;
use Drupal\Tests\BrowserTestBase;
class LanguageNegotiationInfoTest extends BrowserTestBase {
public static $modules = [
'language',
'content_translation',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
'view the administration theme',
'administer modules',
]);
$this
->drupalLogin($admin_user);
$this
->drupalPostForm('admin/config/regional/language/add', [
'predefined_langcode' => 'it',
], t('Add language'));
}
protected function languageManager() {
return $this->container
->get('language_manager');
}
protected function stateSet(array $values) {
$this->container
->get('state')
->setMultiple($values);
$this
->refreshVariables();
$this->container
->get('language_negotiator')
->purgeConfiguration();
}
public function testInfoAlterations() {
$this
->stateSet([
'language_test.language_types' => TRUE,
'language_test.language_negotiation_info' => TRUE,
'language_test.content_language_type' => TRUE,
]);
$this->container
->get('module_installer')
->install([
'language_test',
]);
$this
->resetAll();
$this
->checkFixedLanguageTypes();
$type = LanguageInterface::TYPE_CONTENT;
$language_types = $this
->languageManager()
->getLanguageTypes();
$this
->assertContains($type, $language_types, 'Content language type is configurable.');
$test_type = 'test_language_type';
$interface_method_id = LanguageNegotiationUI::METHOD_ID;
$test_method_id = 'test_language_negotiation_method';
$form_field = $type . '[enabled][' . $interface_method_id . ']';
$edit = [
$form_field => TRUE,
$type . '[enabled][' . $test_method_id . ']' => TRUE,
$test_type . '[enabled][' . $test_method_id . ']' => TRUE,
$test_type . '[configurable]' => TRUE,
];
$this
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$this
->stateSet([
'language_test.language_negotiation_info_alter' => TRUE,
]);
$negotiation = $this
->config('language.types')
->get('negotiation.' . $type . '.enabled');
$this
->assertFalse(isset($negotiation[$interface_method_id]), 'Interface language negotiation method removed from the stored settings.');
$this
->drupalGet('admin/config/regional/language/detection');
$this
->assertNoFieldByName($form_field, NULL, 'Interface language negotiation method unavailable.');
foreach ($this
->languageManager()
->getLanguageTypes() as $type) {
$form_field = $type . '[enabled][test_language_negotiation_method_ts]';
if ($type == $test_type) {
$this
->assertFieldByName($form_field, NULL, new FormattableMarkup('Type-specific test language negotiation method available for %type.', [
'%type' => $type,
]));
}
else {
$this
->assertNoFieldByName($form_field, NULL, new FormattableMarkup('Type-specific test language negotiation method unavailable for %type.', [
'%type' => $type,
]));
}
}
$this
->drupalGet('');
$last = $this->container
->get('state')
->get('language_test.language_negotiation_last');
foreach ($this
->languageManager()
->getDefinedLanguageTypes() as $type) {
$langcode = $last[$type];
$value = $type == LanguageInterface::TYPE_CONTENT || strpos($type, 'test') !== FALSE ? 'it' : 'en';
$this
->assertEqual($langcode, $value, new FormattableMarkup('The negotiated language for %type is %language', [
'%type' => $type,
'%language' => $value,
]));
}
$this->container
->get('module_installer')
->uninstall([
'language_test',
]);
$this
->rebuildContainer();
foreach ($this
->languageManager()
->getDefinedLanguageTypes() as $type) {
$this
->assertStringNotContainsString('test', $type, new FormattableMarkup('The %type language is still available', [
'%type' => $type,
]));
}
$this
->checkFixedLanguageTypes();
$negotiation = $this
->config('language.types')
->get('negotiation.' . $type . '.enabled');
$this
->assertFalse(isset($negotiation[$test_method_id]), 'The disabled test language negotiation method is not part of the content language negotiation settings.');
$this
->assertNoRaw(t('Test language detection'), 'No test language type configuration available.');
$this
->assertNoRaw(t('This is a test language negotiation method'), 'No test language negotiation method available.');
}
protected function checkFixedLanguageTypes() {
$configurable = $this
->languageManager()
->getLanguageTypes();
foreach ($this
->languageManager()
->getDefinedLanguageTypesInfo() as $type => $info) {
if (!in_array($type, $configurable) && isset($info['fixed'])) {
$negotiation = $this
->config('language.types')
->get('negotiation.' . $type . '.enabled');
$equal = array_keys($negotiation) === array_values($info['fixed']);
$this
->assertTrue($equal, new FormattableMarkup('language negotiation for %type is properly set up', [
'%type' => $type,
]));
}
}
}
public function testConfigLangTypeAlterations() {
$test_type = LanguageInterface::TYPE_CONTENT;
$this
->assertFalse($this
->isLanguageTypeConfigurable($test_type), 'Language type is not configurable.');
$edit = [
$test_type . '[configurable]' => TRUE,
];
$this
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$this
->assertTrue($this
->isLanguageTypeConfigurable($test_type), 'Language type is now configurable.');
$this
->drupalPostForm('admin/modules', [
'modules[test_module][enable]' => 1,
], t('Install'));
$this
->assertTrue($this
->isLanguageTypeConfigurable($test_type), 'Language type is still configurable.');
$this
->drupalPostForm('admin/modules/uninstall', [
'uninstall[test_module]' => 1,
], t('Uninstall'));
$this
->assertTrue($this
->isLanguageTypeConfigurable($test_type), 'Language type is still configurable.');
}
protected function isLanguageTypeConfigurable($type) {
$configurable_types = $this
->config('language.types')
->get('configurable');
return in_array($type, $configurable_types);
}
}