3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\taxonomy\Entity\Vocabulary;
10 * Tests the Term Translation UI.
14 class TermTranslationUITest extends ContentTranslationUITestBase {
17 * The vocabulary used for creating terms.
19 * @var \Drupal\taxonomy\VocabularyInterface
21 protected $vocabulary;
28 public static $modules = ['language', 'content_translation', 'taxonomy'];
30 protected function setUp() {
31 $this->entityTypeId = 'taxonomy_term';
32 $this->bundle = 'tags';
39 protected function setupBundle() {
40 parent::setupBundle();
42 // Create a vocabulary.
43 $this->vocabulary = Vocabulary::create([
44 'name' => $this->bundle,
45 'description' => $this->randomMachineName(),
46 'vid' => $this->bundle,
47 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
48 'weight' => mt_rand(0, 10),
50 $this->vocabulary->save();
56 protected function getTranslatorPermissions() {
57 return array_merge(parent::getTranslatorPermissions(), ['administer taxonomy']);
63 protected function getNewEntityValues($langcode) {
64 return ['name' => $this->randomMachineName()] + parent::getNewEntityValues($langcode);
68 * Returns an edit array containing the values to be posted.
70 protected function getEditValues($values, $langcode, $new = FALSE) {
71 $edit = parent::getEditValues($values, $langcode, $new);
73 // To be able to post values for the configurable base fields (name,
74 // description) have to be suffixed with [0][value].
75 foreach ($edit as $property => $value) {
76 foreach (['name', 'description'] as $key) {
77 if ($property == $key) {
78 $edit[$key . '[0][value]'] = $value;
79 unset($edit[$property]);
89 public function testTranslationUI() {
90 parent::testTranslationUI();
92 // Make sure that no row was inserted for taxonomy vocabularies which do
93 // not have translations enabled.
94 $rows = db_query('SELECT tid, count(tid) AS count FROM {taxonomy_term_field_data} WHERE vid <> :vid GROUP BY tid', [':vid' => $this->bundle])->fetchAll();
95 foreach ($rows as $row) {
96 $this->assertTrue($row->count < 2, 'Term does not have translations.');
101 * Tests translate link on vocabulary term list.
103 public function testTranslateLinkVocabularyAdminPage() {
104 $this->drupalLogin($this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), ['access administration pages', 'administer taxonomy'])));
107 'name' => $this->randomMachineName(),
109 $translatable_tid = $this->createEntity($values, $this->langcodes[0], $this->vocabulary->id());
111 // Create an untranslatable vocabulary.
112 $untranslatable_vocabulary = Vocabulary::create([
113 'name' => 'untranslatable_voc',
114 'description' => $this->randomMachineName(),
115 'vid' => 'untranslatable_voc',
116 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
117 'weight' => mt_rand(0, 10),
119 $untranslatable_vocabulary->save();
122 'name' => $this->randomMachineName(),
124 $untranslatable_tid = $this->createEntity($values, $this->langcodes[0], $untranslatable_vocabulary->id());
126 // Verify translation links.
127 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
128 $this->assertResponse(200, 'The translatable vocabulary page was found.');
129 $this->assertLinkByHref('term/' . $translatable_tid . '/translations', 0, 'The translations link exists for a translatable vocabulary.');
130 $this->assertLinkByHref('term/' . $translatable_tid . '/edit', 0, 'The edit link exists for a translatable vocabulary.');
132 $this->drupalGet('admin/structure/taxonomy/manage/' . $untranslatable_vocabulary->id() . '/overview');
133 $this->assertResponse(200);
134 $this->assertLinkByHref('term/' . $untranslatable_tid . '/edit');
135 $this->assertNoLinkByHref('term/' . $untranslatable_tid . '/translations');
141 protected function doTestTranslationEdit() {
142 $storage = $this->container->get('entity_type.manager')
143 ->getStorage($this->entityTypeId);
144 $storage->resetCache([$this->entityId]);
145 $entity = $storage->load($this->entityId);
146 $languages = $this->container->get('language_manager')->getLanguages();
148 foreach ($this->langcodes as $langcode) {
149 // We only want to test the title for non-english translations.
150 if ($langcode != 'en') {
151 $options = ['language' => $languages[$langcode]];
152 $url = $entity->urlInfo('edit-form', $options);
153 $this->drupalGet($url);
155 $title = t('@title [%language translation]', [
156 '@title' => $entity->getTranslation($langcode)->label(),
157 '%language' => $languages[$langcode]->getName(),
159 $this->assertRaw($title);