c5df74dadea2d8362829ab76a611c7136ea2b05e
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / TermTranslationFieldViewTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\taxonomy\Tests\TaxonomyTranslationTestTrait;
7
8 /**
9  * Tests the translation of taxonomy terms field on nodes.
10  *
11  * @group taxonomy
12  */
13 class TermTranslationFieldViewTest extends TaxonomyTestBase {
14
15   use TaxonomyTranslationTestTrait;
16
17   /**
18    * The term that should be translated.
19    *
20    * @var \Drupal\taxonomy\Entity\Term
21    */
22   protected $term;
23
24   /**
25    * The tag in the source language.
26    *
27    * @var string
28    */
29   protected $baseTagName = 'OriginalTagName';
30
31   /**
32    * The translated value for the tag.
33    *
34    * @var string
35    */
36   protected $translatedTagName = 'TranslatedTagName';
37
38   /**
39    * Modules to enable.
40    *
41    * @var array
42    */
43   public static $modules = ['language', 'content_translation', 'taxonomy'];
44
45   protected function setUp() {
46     parent::setUp();
47     $this->setupLanguages();
48     $this->vocabulary = $this->createVocabulary();
49     $this->enableTranslation();
50     $this->setUpTerm();
51     $this->setUpTermReferenceField();
52     $this->setUpNode();
53   }
54
55   /**
56    * Tests if the translated taxonomy term is displayed.
57    */
58   public function testTranslatedTaxonomyTermReferenceDisplay() {
59     $path = 'node/' . $this->node->id();
60     $translation_path = $this->translateToLangcode . '/' . $path;
61
62     $this->drupalGet($path);
63     $this->assertNoText($this->translatedTagName);
64     $this->assertText($this->baseTagName);
65     $this->drupalGet($translation_path);
66     $this->assertText($this->translatedTagName);
67     $this->assertNoText($this->baseTagName);
68   }
69
70   /**
71    * Creates a test subject node, with translation.
72    */
73   protected function setUpNode() {
74     /** @var \Drupal\node\Entity\Node $node */
75     $node = Node::create([
76       'title' => $this->randomMachineName(),
77       'type' => 'article',
78       'description' => [[
79         'value' => $this->randomMachineName(),
80         'format' => 'basic_html'
81       ]],
82       $this->termFieldName => [['target_id' => $this->term->id()]],
83       'langcode' => $this->baseLangcode,
84     ]);
85     $node->save();
86     $node->addTranslation($this->translateToLangcode, $node->toArray());
87     $node->save();
88     $this->node = $node;
89   }
90
91   /**
92    * Creates a test subject term, with translation.
93    */
94   protected function setUpTerm() {
95     $this->term = $this->createTerm($this->vocabulary, [
96       'name' => $this->baseTagName,
97       'langcode' => $this->baseLangcode,
98     ]);
99
100     $this->term->addTranslation($this->translateToLangcode, [
101       'name' => $this->translatedTagName,
102     ]);
103     $this->term->save();
104   }
105
106 }