comparison core/modules/taxonomy/src/Tests/TermTranslationTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\taxonomy\Tests;
4
5 use Drupal\Core\Url;
6 use Drupal\system\Tests\Menu\AssertBreadcrumbTrait;
7
8 /**
9 * Tests for proper breadcrumb translation.
10 *
11 * @group taxonomy
12 */
13 class TermTranslationTest extends TaxonomyTestBase {
14
15 use AssertBreadcrumbTrait;
16 use TaxonomyTranslationTestTrait;
17
18 /**
19 * Term to translated term mapping.
20 *
21 * @var array
22 */
23 protected $termTranslationMap = [
24 'one' => 'translatedOne',
25 'two' => 'translatedTwo',
26 'three' => 'translatedThree',
27 ];
28
29 /**
30 * Created terms.
31 *
32 * @var \Drupal\taxonomy\Entity\Term[]
33 */
34 protected $terms = [];
35
36 /**
37 * {@inheritdoc}
38 */
39 public static $modules = ['taxonomy', 'language', 'content_translation'];
40
41 /**
42 * {@inheritdoc}
43 */
44 protected function setUp() {
45 parent::setUp();
46 $this->setupLanguages();
47 $this->vocabulary = $this->createVocabulary();
48 $this->enableTranslation();
49 $this->setUpTerms();
50 $this->setUpTermReferenceField();
51 }
52
53 /**
54 * Test translated breadcrumbs.
55 */
56 public function testTranslatedBreadcrumbs() {
57 // Ensure non-translated breadcrumb is correct.
58 $breadcrumb = [Url::fromRoute('<front>')->toString() => 'Home'];
59 foreach ($this->terms as $term) {
60 $breadcrumb[$term->url()] = $term->label();
61 }
62 // The last item will not be in the breadcrumb.
63 array_pop($breadcrumb);
64
65 // Check the breadcrumb on the leaf term page.
66 $term = $this->getLeafTerm();
67 $this->assertBreadcrumb($term->urlInfo(), $breadcrumb, $term->label());
68
69 $languages = \Drupal::languageManager()->getLanguages();
70
71 // Construct the expected translated breadcrumb.
72 $breadcrumb = [Url::fromRoute('<front>', [], ['language' => $languages[$this->translateToLangcode]])->toString() => 'Home'];
73 foreach ($this->terms as $term) {
74 $translated = $term->getTranslation($this->translateToLangcode);
75 $url = $translated->url('canonical', ['language' => $languages[$this->translateToLangcode]]);
76 $breadcrumb[$url] = $translated->label();
77 }
78 array_pop($breadcrumb);
79
80 // Check for the translated breadcrumb on the translated leaf term page.
81 $term = $this->getLeafTerm();
82 $translated = $term->getTranslation($this->translateToLangcode);
83 $this->assertBreadcrumb($translated->urlInfo('canonical', ['language' => $languages[$this->translateToLangcode]]), $breadcrumb, $translated->label());
84
85 }
86
87 /**
88 * Test translation of terms are showed in the node.
89 */
90 public function testTermsTranslation() {
91
92 // Set the display of the term reference field on the article content type
93 // to "Check boxes/radio buttons".
94 entity_get_form_display('node', 'article', 'default')
95 ->setComponent($this->termFieldName, [
96 'type' => 'options_buttons',
97 ])
98 ->save();
99 $this->drupalLogin($this->drupalCreateUser(['create article content']));
100
101 // Test terms are listed.
102 $this->drupalget('node/add/article');
103 $this->assertText('one');
104 $this->assertText('two');
105 $this->assertText('three');
106
107 // Test terms translated are listed.
108 $this->drupalget('hu/node/add/article');
109 $this->assertText('translatedOne');
110 $this->assertText('translatedTwo');
111 $this->assertText('translatedThree');
112 }
113
114 /**
115 * Setup translated terms in a hierarchy.
116 */
117 protected function setUpTerms() {
118 $parent_vid = 0;
119 foreach ($this->termTranslationMap as $name => $translation) {
120
121 $term = $this->createTerm($this->vocabulary, [
122 'name' => $name,
123 'langcode' => $this->baseLangcode,
124 'parent' => $parent_vid,
125 ]);
126
127 $term->addTranslation($this->translateToLangcode, [
128 'name' => $translation,
129 ]);
130 $term->save();
131
132 // Each term is nested under the last.
133 $parent_vid = $term->id();
134
135 $this->terms[] = $term;
136 }
137 }
138
139 /**
140 * Get the final (leaf) term in the hierarchy.
141 *
142 * @return \Drupal\taxonomy\Entity\Term
143 * The final term in the hierarchy.
144 */
145 protected function getLeafTerm() {
146 return $this->terms[count($this->termTranslationMap) - 1];
147 }
148
149 }