PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/web/core/modules/taxonomy/tests/src/Functional/TermTranslationUITest.php

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