View source
<?php
namespace Drupal\Tests\lingotek\Functional\Form;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
class LanguageFormTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'lingotek',
'lingotek_test',
];
public function setUp() : void {
parent::setUp();
$admin_user = $this
->drupalCreateUser([
'administer languages',
'access administration pages',
]);
$this
->drupalLogin($admin_user);
}
public function testAddingLanguage() {
$this
->config('locale.settings')
->set('translation.import_enabled', TRUE)
->save();
$this
->drupalGet('admin/config/regional/language/add');
$edit = [
'predefined_langcode' => 'de',
];
$this
->drupalPostForm(NULL, $edit, 'Add language');
$this
->clickLink('Edit', 1);
$this
->assertFieldByName('lingotek_locale', 'de-DE', 'The Lingotek locale is set correctly.');
}
public function testEditingLanguage() {
ConfigurableLanguage::createFromLangcode('de')
->save();
$this
->drupalGet('/admin/config/regional/language');
$this
->clickLink('Edit', 1);
$this
->assertFieldByName('lingotek_locale', 'de-DE', 'The Lingotek locale is set correctly.');
$this
->drupalPostForm(NULL, [
'name' => 'German (Germany)',
], 'Save language');
$this
->assertText('German (Germany)');
}
public function testEditingLanguageWith401() {
\Drupal::state()
->set('lingotek.locales_error', TRUE);
ConfigurableLanguage::createFromLangcode('de')
->save();
$this
->drupalGet('/admin/config/regional/language');
$this
->clickLink('Edit', 1);
$this
->assertFieldByName('lingotek_locale', 'de-DE', 'The Lingotek locale is set correctly.');
$this
->drupalPostForm(NULL, [
'name' => 'German (Germany)',
], 'Save language');
$this
->assertText('German (Germany)');
$this
->assertText("The Lingotek locale has not been validated.");
}
public function testAddingCustomLanguage() {
$this
->drupalGet('admin/config/regional/language/add');
$this
->assertField('lingotek_locale', 'There is a field for adding the Lingotek locale.');
$this
->assertFieldByName('lingotek_locale', '', 'The Lingotek locale is empty by default.');
$this
->getSession()
->getPage()
->hasUncheckedField('lingotek_disabled');
$edit = [
'predefined_langcode' => 'custom',
'langcode' => 'es-DE',
'label' => 'Spanish (Germany)',
'direction' => 'ltr',
'lingotek_locale' => 'es-ES',
];
$this
->drupalPostForm(NULL, $edit, 'Add custom language');
$this
->assertText('The language Spanish (Germany) has been created and can now be used.');
$language = ConfigurableLanguage::load('es-DE');
$this
->assertEqual('es_ES', $language
->getThirdPartySetting('lingotek', 'locale'), 'The Lingotek locale has been saved successfully.');
$locale_mapper = \Drupal::service('lingotek.language_locale_mapper');
$this
->assertEqual('es_ES', $locale_mapper
->getLocaleForLangcode('es-DE'), 'The language locale mapper correctly guesses the locale.');
$this
->assertEqual('es-DE', $locale_mapper
->getConfigurableLanguageForLocale('es_ES')
->getId(), 'The language locale mapper correctly guesses the langcode.');
}
public function testDisablingCustomLanguage() {
$this
->testAddingCustomLanguage();
$this
->drupalGet('/admin/config/regional/language');
$this
->clickLink('Edit', 1);
$this
->getSession()
->getPage()
->hasUncheckedField('lingotek_disabled');
$edit = [
'lingotek_disabled' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Save language');
$language = ConfigurableLanguage::load('es-DE');
$this
->assertTrue($language
->getThirdPartySetting('lingotek', 'disabled'), 'The Lingotek locale has been disabled successfully.');
$this
->drupalGet('/admin/config/regional/language');
$this
->clickLink('Edit', 1);
$this
->getSession()
->getPage()
->hasCheckedField('lingotek_disabled');
}
public function testEnablingCustomLanguage() {
ConfigurableLanguage::create([
'id' => 'de-at',
'label' => 'German (AT)',
])
->setThirdPartySetting('lingotek', 'disabled', TRUE)
->save();
$this
->drupalGet('/admin/config/regional/language');
$this
->clickLink('Edit', 1);
$this
->getSession()
->getPage()
->hasCheckedField('lingotek_disabled');
$edit = [
'lingotek_disabled' => FALSE,
];
$this
->drupalPostForm(NULL, $edit, 'Save language');
$language = ConfigurableLanguage::load('de-at');
$this
->assertFalse($language
->getThirdPartySetting('lingotek', 'disabled'), 'The Lingotek locale has been enabled successfully.');
$this
->drupalGet('/admin/config/regional/language');
$this
->clickLink('Edit', 1);
$this
->getSession()
->getPage()
->hasUncheckedField('lingotek_disabled');
}
public function testEditingCustomLanguage() {
ConfigurableLanguage::create([
'id' => 'de-at',
'label' => 'German (AT)',
])
->save();
$this
->drupalGet('/admin/config/regional/language');
$this
->clickLink('Edit', 1);
$this
->assertFieldByName('lingotek_locale', 'de-AT', 'The Lingotek locale is set to the right language.');
$edit = [
'lingotek_locale' => 'de-DE',
];
$this
->drupalPostForm(NULL, $edit, 'Save language');
$this
->clickLink('Edit', 1);
$this
->assertFieldByName('lingotek_locale', 'de-DE', 'The Lingotek locale is set to the right language after editing.');
}
public function testEditingCustomLanguageWithWrongLocale() {
ConfigurableLanguage::create([
'id' => 'de-at',
'label' => 'German (AT)',
])
->save();
$this
->drupalGet('/admin/config/regional/language');
$this
->clickLink('Edit', 1);
$this
->assertFieldByName('lingotek_locale', 'de-AT', 'The Lingotek locale is set to the right language.');
$edit = [
'lingotek_locale' => 'de-IN',
];
$this
->drupalPostForm(NULL, $edit, 'Save language');
$this
->assertText('The Lingotek locale de-IN does not exist.');
}
public function testEditingCustomLanguageWithUnderscoredLocale() {
ConfigurableLanguage::create([
'id' => 'de-at',
'label' => 'German (AT)',
])
->save();
$this
->drupalGet('/admin/config/regional/language');
$this
->clickLink('Edit', 1);
$this
->assertFieldByName('lingotek_locale', 'de-AT', 'The Lingotek locale is set to the right language.');
$edit = [
'lingotek_locale' => 'de_AT',
];
$this
->drupalPostForm(NULL, $edit, 'Save language');
$this
->clickLink('Edit', 1);
$this
->assertFieldByName('lingotek_locale', 'de-AT', 'The Lingotek locale is set to the right language.');
}
public function testAutocompleteLocale() {
$page = $this
->getSession()
->getPage();
ConfigurableLanguage::createFromLangcode('de')
->save();
$this
->drupalGet('/admin/config/regional/language');
$this
->clickLink('Edit', 1);
$this
->assertRaw('core/misc/autocomplete.js');
$this
->assertFieldByName('lingotek_locale', 'de-DE', 'The Lingotek locale is set correctly.');
$result = $this
->xpath('//input[@name="lingotek_locale" and contains(@data-autocomplete-path, "admin/lingotek/supported-locales-autocomplete")]');
$target_url = $this
->getAbsoluteUrl($result[0]
->getAttribute('data-autocomplete-path'));
$this
->drupalGet($target_url, [
'query' => [
'q' => 'de',
],
]);
$result = json_decode($page
->getContent(), TRUE);
$this
->assertEquals($result[0]['value'], 'de-AT');
$this
->assertEquals($result[0]['label'], 'German (Austria) (de-AT) [matched: Code: <em class="placeholder">de-AT</em>]');
$this
->assertEquals($result[1]['value'], 'de-DE');
$this
->assertEquals($result[1]['label'], 'German (Germany) (de-DE) [matched: Code: <em class="placeholder">de-DE</em>]');
}
}