1<?php
2
3namespace Drupal\Tests\taxonomy\Functional;
4
5use Drupal\Core\Database\Database;
6use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
7use Drupal\Core\Language\LanguageInterface;
8use Drupal\taxonomy\Entity\Vocabulary;
9
10/**
11 * Tests the Term Translation UI.
12 *
13 * @group taxonomy
14 */
15class TermTranslationUITest extends ContentTranslationUITestBase {
16
17  /**
18   * The vocabulary used for creating terms.
19   *
20   * @var \Drupal\taxonomy\VocabularyInterface
21   */
22  protected $vocabulary;
23
24  /**
25   * Modules to enable.
26   *
27   * @var array
28   */
29  public static $modules = ['language', 'content_translation', 'taxonomy'];
30
31  /**
32   * {@inheritdoc}
33   */
34  protected $defaultTheme = 'classy';
35
36  protected function setUp() {
37    $this->entityTypeId = 'taxonomy_term';
38    $this->bundle = 'tags';
39    parent::setUp();
40  }
41
42  /**
43   * {@inheritdoc}
44   */
45  protected function setupBundle() {
46    parent::setupBundle();
47
48    // Create a vocabulary.
49    $this->vocabulary = Vocabulary::create([
50      'name' => $this->bundle,
51      'description' => $this->randomMachineName(),
52      'vid' => $this->bundle,
53      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
54      'weight' => mt_rand(0, 10),
55    ]);
56    $this->vocabulary->save();
57  }
58
59  /**
60   * {@inheritdoc}
61   */
62  protected function getTranslatorPermissions() {
63    return array_merge(parent::getTranslatorPermissions(), ['administer taxonomy']);
64  }
65
66  /**
67   * {@inheritdoc}
68   */
69  protected function getNewEntityValues($langcode) {
70    return ['name' => $this->randomMachineName()] + parent::getNewEntityValues($langcode);
71  }
72
73  /**
74   * Returns an edit array containing the values to be posted.
75   */
76  protected function getEditValues($values, $langcode, $new = FALSE) {
77    $edit = parent::getEditValues($values, $langcode, $new);
78
79    // To be able to post values for the configurable base fields (name,
80    // description) have to be suffixed with [0][value].
81    foreach ($edit as $property => $value) {
82      foreach (['name', 'description'] as $key) {
83        if ($property == $key) {
84          $edit[$key . '[0][value]'] = $value;
85          unset($edit[$property]);
86        }
87      }
88    }
89    return $edit;
90  }
91
92  /**
93   * {@inheritdoc}
94   */
95  public function testTranslationUI() {
96    parent::testTranslationUI();
97
98    // Make sure that no row was inserted for taxonomy vocabularies which do
99    // not have translations enabled.
100    $rows = Database::getConnection()->query('SELECT tid, count(tid) AS count FROM {taxonomy_term_field_data} WHERE vid <> :vid GROUP BY tid', [':vid' => $this->bundle])->fetchAll();
101    foreach ($rows as $row) {
102      $this->assertTrue($row->count < 2, 'Term does not have translations.');
103    }
104  }
105
106  /**
107   * Tests translate link on vocabulary term list.
108   */
109  public function testTranslateLinkVocabularyAdminPage() {
110    $this->drupalLogin($this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), ['access administration pages', 'administer taxonomy'])));
111
112    $values = [
113      'name' => $this->randomMachineName(),
114    ];
115    $translatable_tid = $this->createEntity($values, $this->langcodes[0], $this->vocabulary->id());
116
117    // Create an untranslatable vocabulary.
118    $untranslatable_vocabulary = Vocabulary::create([
119      'name' => 'untranslatable_voc',
120      'description' => $this->randomMachineName(),
121      'vid' => 'untranslatable_voc',
122      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
123      'weight' => mt_rand(0, 10),
124    ]);
125    $untranslatable_vocabulary->save();
126
127    $values = [
128      'name' => $this->randomMachineName(),
129    ];
130    $untranslatable_tid = $this->createEntity($values, $this->langcodes[0], $untranslatable_vocabulary->id());
131
132    // Verify translation links.
133    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
134    $this->assertSession()->statusCodeEquals(200);
135    $this->assertLinkByHref('term/' . $translatable_tid . '/translations', 0, 'The translations link exists for a translatable vocabulary.');
136    $this->assertLinkByHref('term/' . $translatable_tid . '/edit', 0, 'The edit link exists for a translatable vocabulary.');
137
138    $this->drupalGet('admin/structure/taxonomy/manage/' . $untranslatable_vocabulary->id() . '/overview');
139    $this->assertSession()->statusCodeEquals(200);
140    $this->assertLinkByHref('term/' . $untranslatable_tid . '/edit');
141    $this->assertNoLinkByHref('term/' . $untranslatable_tid . '/translations');
142  }
143
144  /**
145   * {@inheritdoc}
146   */
147  protected function doTestTranslationEdit() {
148    $storage = $this->container->get('entity_type.manager')
149      ->getStorage($this->entityTypeId);
150    $storage->resetCache([$this->entityId]);
151    $entity = $storage->load($this->entityId);
152    $languages = $this->container->get('language_manager')->getLanguages();
153
154    foreach ($this->langcodes as $langcode) {
155      // We only want to test the title for non-english translations.
156      if ($langcode != 'en') {
157        $options = ['language' => $languages[$langcode]];
158        $url = $entity->toUrl('edit-form', $options);
159        $this->drupalGet($url);
160
161        $title = t('@title [%language translation]', [
162          '@title' => $entity->getTranslation($langcode)->label(),
163          '%language' => $languages[$langcode]->getName(),
164        ]);
165        $this->assertRaw($title);
166      }
167    }
168  }
169
170  /**
171   * {@inheritdoc}
172   */
173  protected function doTestPublishedStatus() {
174    $storage = $this->container->get('entity_type.manager')
175      ->getStorage($this->entityTypeId);
176    $storage->resetCache([$this->entityId]);
177    $entity = $storage->load($this->entityId);
178    $languages = $this->container->get('language_manager')->getLanguages();
179
180    $statuses = [
181      TRUE,
182      FALSE,
183    ];
184
185    foreach ($statuses as $index => $value) {
186      // (Un)publish the term translations and check that the translation
187      // statuses are (un)published accordingly.
188      foreach ($this->langcodes as $langcode) {
189        $options = ['language' => $languages[$langcode]];
190        $url = $entity->toUrl('edit-form', $options);
191        $this->drupalPostForm($url, ['status[value]' => $value], t('Save'), $options);
192      }
193      $storage->resetCache([$this->entityId]);
194      $entity = $storage->load($this->entityId);
195      foreach ($this->langcodes as $langcode) {
196        // The term is created as unpublished thus we switch to the published
197        // status first.
198        $status = !$index;
199        $translation = $entity->getTranslation($langcode);
200        $this->assertEquals($status, $this->manager->getTranslationMetadata($translation)->isPublished(), 'The translation has been correctly unpublished.');
201      }
202    }
203  }
204
205}
206