TermTranslationFieldViewTest.php 2.47 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\taxonomy\Functional;
4

5 6
use Drupal\node\Entity\Node;

7 8 9 10 11 12 13
/**
 * Tests the translation of taxonomy terms field on nodes.
 *
 * @group taxonomy
 */
class TermTranslationFieldViewTest extends TaxonomyTestBase {

14
  use TaxonomyTranslationTestTrait;
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

  /**
   * The term that should be translated.
   *
   * @var \Drupal\taxonomy\Entity\Term
   */
  protected $term;

  /**
   * The tag in the source language.
   *
   * @var string
   */
  protected $baseTagName = 'OriginalTagName';

  /**
   * The translated value for the tag.
   *
   * @var string
   */
  protected $translatedTagName = 'TranslatedTagName';

  /**
   * Modules to enable.
   *
   * @var array
   */
42
  public static $modules = ['language', 'content_translation', 'taxonomy'];
43 44 45 46 47 48 49

  protected function setUp() {
    parent::setUp();
    $this->setupLanguages();
    $this->vocabulary = $this->createVocabulary();
    $this->enableTranslation();
    $this->setUpTerm();
50
    $this->setUpTermReferenceField();
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    $this->setUpNode();
  }

  /**
   * Tests if the translated taxonomy term is displayed.
   */
  public function testTranslatedTaxonomyTermReferenceDisplay() {
    $path = 'node/' . $this->node->id();
    $translation_path = $this->translateToLangcode . '/' . $path;

    $this->drupalGet($path);
    $this->assertNoText($this->translatedTagName);
    $this->assertText($this->baseTagName);
    $this->drupalGet($translation_path);
    $this->assertText($this->translatedTagName);
    $this->assertNoText($this->baseTagName);
  }

  /**
   * Creates a test subject node, with translation.
   */
  protected function setUpNode() {
    /** @var \Drupal\node\Entity\Node $node */
74
    $node = Node::create([
75 76
      'title' => $this->randomMachineName(),
      'type' => 'article',
77
      'description' => [[
78
        'value' => $this->randomMachineName(),
79 80
        'format' => 'basic_html'
      ]],
81
      $this->termFieldName => [['target_id' => $this->term->id()]],
82
      'langcode' => $this->baseLangcode,
83
    ]);
84
    $node->save();
85
    $node->addTranslation($this->translateToLangcode, $node->toArray());
86 87 88 89 90 91 92 93
    $node->save();
    $this->node = $node;
  }

  /**
   * Creates a test subject term, with translation.
   */
  protected function setUpTerm() {
94
    $this->term = $this->createTerm($this->vocabulary, [
95 96
      'name' => $this->baseTagName,
      'langcode' => $this->baseLangcode,
97
    ]);
98

99
    $this->term->addTranslation($this->translateToLangcode, [
100
      'name' => $this->translatedTagName,
101
    ]);
102 103 104 105
    $this->term->save();
  }

}