View source
<?php
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
class TermLanguageTest extends TaxonomyTestBase {
public static $modules = [
'language',
];
protected $defaultTheme = 'stark';
protected $vocabulary;
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'administer taxonomy',
]));
$this->vocabulary = $this
->createVocabulary();
foreach ([
'aa',
'bb',
'cc',
] as $language_code) {
ConfigurableLanguage::create([
'id' => $language_code,
'label' => $this
->randomMachineName(),
])
->save();
}
}
public function testTermLanguage() {
$edit = [
'default_language[language_alterable]' => TRUE,
];
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary
->id(), $edit, t('Save'));
$this
->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
->id() . '/add');
$this
->assertField('edit-langcode-0-value', t('The language selector field was found on the page.'));
$edit = [
'name[0][value]' => $this
->randomMachineName(),
'langcode[0][value]' => 'aa',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
$term = reset($terms);
$this
->assertEqual($term
->language()
->getId(), $edit['langcode[0][value]'], 'The term contains the correct langcode.');
$this
->drupalGet('taxonomy/term/' . $term
->id() . '/edit');
$this
->assertOptionSelected('edit-langcode-0-value', $edit['langcode[0][value]'], 'The term language was correctly selected.');
$edit['langcode[0][value]'] = 'bb';
$this
->drupalPostForm('taxonomy/term/' . $term
->id() . '/edit', $edit, t('Save'));
$this
->drupalGet('taxonomy/term/' . $term
->id() . '/edit');
$this
->assertOptionSelected('edit-langcode-0-value', $edit['langcode[0][value]'], 'The term language was correctly selected.');
}
public function testDefaultTermLanguage() {
$edit = [
'default_language[langcode]' => 'bb',
'default_language[language_alterable]' => TRUE,
];
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary
->id(), $edit, t('Save'));
$this
->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
->id() . '/add');
$this
->assertOptionSelected('edit-langcode-0-value', 'bb', 'The expected langcode was selected.');
$edit = [
'default_language[langcode]' => 'current_interface',
'default_language[language_alterable]' => TRUE,
];
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary
->id(), $edit, t('Save'));
$this
->drupalGet('aa/admin/structure/taxonomy/manage/' . $this->vocabulary
->id() . '/add');
$this
->assertOptionSelected('edit-langcode-0-value', 'aa', "The expected langcode, 'aa', was selected.");
$this
->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary
->id() . '/add');
$this
->assertOptionSelected('edit-langcode-0-value', 'bb', "The expected langcode, 'bb', was selected.");
$this
->config('system.site')
->set('default_langcode', 'cc')
->save();
$edit = [
'default_language[langcode]' => LanguageInterface::LANGCODE_SITE_DEFAULT,
'default_language[language_alterable]' => TRUE,
];
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary
->id(), $edit, t('Save'));
$this
->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
->id() . '/add');
$this
->assertOptionSelected('edit-langcode-0-value', 'cc', "The expected langcode, 'cc', was selected.");
}
public function testTermTranslatedOnOverviewPage() {
$edit = [
'default_language[language_alterable]' => TRUE,
];
$this
->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary
->id(), $edit, t('Save'));
$this
->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
->id() . '/add');
$edit = [
'name[0][value]' => $this
->randomMachineName(),
'langcode[0][value]' => 'aa',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
$term = reset($terms);
$translated_title = $this
->randomMachineName();
$term
->addTranslation('bb', [
'name' => $translated_title,
]);
$term
->save();
$this
->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary
->id() . '/overview');
$this
->assertPattern('|<a[^>]*>' . $translated_title . '</a>|');
}
}