1<?php 2 3namespace Drupal\Tests\taxonomy\Functional; 4 5use Drupal\Core\Language\LanguageInterface; 6use Drupal\language\Entity\ConfigurableLanguage; 7 8/** 9 * Tests the language functionality for the taxonomy terms. 10 * 11 * @group taxonomy 12 */ 13class TermLanguageTest extends TaxonomyTestBase { 14 15 protected static $modules = ['language']; 16 17 /** 18 * {@inheritdoc} 19 */ 20 protected $defaultTheme = 'stark'; 21 22 /** 23 * Vocabulary for testing. 24 * 25 * @var \Drupal\taxonomy\VocabularyInterface 26 */ 27 protected $vocabulary; 28 29 protected function setUp(): void { 30 parent::setUp(); 31 32 // Create an administrative user. 33 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy'])); 34 35 // Create a vocabulary to which the terms will be assigned. 36 $this->vocabulary = $this->createVocabulary(); 37 38 // Add some custom languages. 39 foreach (['aa', 'bb', 'cc'] as $language_code) { 40 ConfigurableLanguage::create([ 41 'id' => $language_code, 42 'label' => $this->randomMachineName(), 43 ])->save(); 44 } 45 } 46 47 public function testTermLanguage() { 48 // Configure the vocabulary to not hide the language selector. 49 $edit = [ 50 'default_language[language_alterable]' => TRUE, 51 ]; 52 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id()); 53 $this->submitForm($edit, 'Save'); 54 55 // Add a term. 56 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); 57 // Check that we have the language selector. 58 $this->assertSession()->fieldExists('edit-langcode-0-value'); 59 // Submit the term. 60 $edit = [ 61 'name[0][value]' => $this->randomMachineName(), 62 'langcode[0][value]' => 'aa', 63 ]; 64 $this->submitForm($edit, 'Save'); 65 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']); 66 $term = reset($terms); 67 $this->assertEquals($edit['langcode[0][value]'], $term->language()->getId(), 'The term contains the correct langcode.'); 68 69 // Check if on the edit page the language is correct. 70 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit'); 71 $this->assertTrue($this->assertSession()->optionExists('edit-langcode-0-value', $edit['langcode[0][value]'])->isSelected()); 72 73 // Change the language of the term. 74 $edit['langcode[0][value]'] = 'bb'; 75 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit'); 76 $this->submitForm($edit, 'Save'); 77 78 // Check again that on the edit page the language is correct. 79 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit'); 80 $this->assertTrue($this->assertSession()->optionExists('edit-langcode-0-value', $edit['langcode[0][value]'])->isSelected()); 81 } 82 83 public function testDefaultTermLanguage() { 84 // Configure the vocabulary to not hide the language selector, and make the 85 // default language of the terms fixed. 86 $edit = [ 87 'default_language[langcode]' => 'bb', 88 'default_language[language_alterable]' => TRUE, 89 ]; 90 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id()); 91 $this->submitForm($edit, 'Save'); 92 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); 93 $this->assertTrue($this->assertSession()->optionExists('edit-langcode-0-value', 'bb')->isSelected()); 94 95 // Make the default language of the terms to be the current interface. 96 $edit = [ 97 'default_language[langcode]' => 'current_interface', 98 'default_language[language_alterable]' => TRUE, 99 ]; 100 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id()); 101 $this->submitForm($edit, 'Save'); 102 $this->drupalGet('aa/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); 103 $this->assertTrue($this->assertSession()->optionExists('edit-langcode-0-value', 'aa')->isSelected()); 104 $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); 105 $this->assertTrue($this->assertSession()->optionExists('edit-langcode-0-value', 'bb')->isSelected()); 106 107 // Change the default language of the site and check if the default terms 108 // language is still correctly selected. 109 $this->config('system.site')->set('default_langcode', 'cc')->save(); 110 $edit = [ 111 'default_language[langcode]' => LanguageInterface::LANGCODE_SITE_DEFAULT, 112 'default_language[language_alterable]' => TRUE, 113 ]; 114 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id()); 115 $this->submitForm($edit, 'Save'); 116 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); 117 $this->assertTrue($this->assertSession()->optionExists('edit-langcode-0-value', 'cc')->isSelected()); 118 } 119 120 /** 121 * Tests that translated terms are displayed correctly on the term overview. 122 */ 123 public function testTermTranslatedOnOverviewPage() { 124 // Configure the vocabulary to not hide the language selector. 125 $edit = [ 126 'default_language[language_alterable]' => TRUE, 127 ]; 128 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id()); 129 $this->submitForm($edit, 'Save'); 130 131 // Add a term. 132 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add'); 133 // Submit the term. 134 $edit = [ 135 'name[0][value]' => $this->randomMachineName(), 136 'langcode[0][value]' => 'aa', 137 ]; 138 $this->submitForm($edit, 'Save'); 139 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']); 140 $term = reset($terms); 141 142 // Add a translation for that term. 143 $translated_title = $this->randomMachineName(); 144 $term->addTranslation('bb', [ 145 'name' => $translated_title, 146 ]); 147 $term->save(); 148 149 // Overview page in the other language shows the translated term 150 $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview'); 151 $this->assertSession()->responseMatches('|<a[^>]*>' . $translated_title . '</a>|'); 152 } 153 154} 155