TermTranslationTest.php 4.02 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\taxonomy\Tests;

5
use Drupal\Core\Url;
6 7 8 9 10 11 12
use Drupal\system\Tests\Menu\AssertBreadcrumbTrait;

/**
 * Tests for proper breadcrumb translation.
 *
 * @group taxonomy
 */
13
class TermTranslationTest extends TaxonomyTestBase {
14 15 16 17 18 19 20 21 22

  use AssertBreadcrumbTrait;
  use TaxonomyTranslationTestTrait;

  /**
   * Term to translated term mapping.
   *
   * @var array
   */
23
  protected $termTranslationMap = [
24 25 26
    'one' => 'translatedOne',
    'two' => 'translatedTwo',
    'three' => 'translatedThree',
27
  ];
28 29 30 31 32 33

  /**
   * Created terms.
   *
   * @var \Drupal\taxonomy\Entity\Term[]
   */
34
  protected $terms = [];
35 36 37 38

  /**
   * {@inheritdoc}
   */
39
  public static $modules = ['taxonomy', 'language', 'content_translation'];
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->setupLanguages();
    $this->vocabulary = $this->createVocabulary();
    $this->enableTranslation();
    $this->setUpTerms();
    $this->setUpTermReferenceField();
  }

  /**
   * Test translated breadcrumbs.
   */
  public function testTranslatedBreadcrumbs() {
    // 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 63 64 65 66
    }
    // The last item will not be in the breadcrumb.
    array_pop($breadcrumb);

    // Check the breadcrumb on the leaf term page.
    $term = $this->getLeafTerm();
67 68 69
    $this->assertBreadcrumb($term->urlInfo(), $breadcrumb, $term->label());

    $languages = \Drupal::languageManager()->getLanguages();
70 71

    // Construct the expected translated breadcrumb.
72
    $breadcrumb = [Url::fromRoute('<front>', [], ['language' => $languages[$this->translateToLangcode]])->toString() => 'Home'];
73 74
    foreach ($this->terms as $term) {
      $translated = $term->getTranslation($this->translateToLangcode);
75 76
      $url = $translated->url('canonical', ['language' => $languages[$this->translateToLangcode]]);
      $breadcrumb[$url] = $translated->label();
77 78 79 80 81 82
    }
    array_pop($breadcrumb);

    // Check for the translated breadcrumb on the translated leaf term page.
    $term = $this->getLeafTerm();
    $translated = $term->getTranslation($this->translateToLangcode);
83
    $this->assertBreadcrumb($translated->urlInfo('canonical', ['language' => $languages[$this->translateToLangcode]]), $breadcrumb, $translated->label());
84 85 86 87 88 89

  }

  /**
   * Test translation of terms are showed in the node.
   */
90
  public function testTermsTranslation() {
91 92 93 94

    // Set the display of the term reference field on the article content type
    // to "Check boxes/radio buttons".
    entity_get_form_display('node', 'article', 'default')
95
      ->setComponent($this->termFieldName, [
96
        'type' => 'options_buttons',
97
      ])
98 99 100 101 102 103 104 105 106 107 108 109 110 111
      ->save();
    $this->drupalLogin($this->drupalCreateUser(['create article content']));

    // Test terms are listed.
    $this->drupalget('node/add/article');
    $this->assertText('one');
    $this->assertText('two');
    $this->assertText('three');

    // Test terms translated are listed.
    $this->drupalget('hu/node/add/article');
    $this->assertText('translatedOne');
    $this->assertText('translatedTwo');
    $this->assertText('translatedThree');
112 113 114 115 116 117 118 119 120
  }

  /**
   * Setup translated terms in a hierarchy.
   */
  protected function setUpTerms() {
    $parent_vid = 0;
    foreach ($this->termTranslationMap as $name => $translation) {

121
      $term = $this->createTerm($this->vocabulary, [
122 123 124
        'name' => $name,
        'langcode' => $this->baseLangcode,
        'parent' => $parent_vid,
125
      ]);
126

127
      $term->addTranslation($this->translateToLangcode, [
128
        'name' => $translation,
129
      ]);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
      $term->save();

      // Each term is nested under the last.
      $parent_vid = $term->id();

      $this->terms[] = $term;
    }
  }

  /**
   * Get the final (leaf) term in the hierarchy.
   *
   * @return \Drupal\taxonomy\Entity\Term
   *   The final term in the hierarchy.
   */
  protected function getLeafTerm() {
    return $this->terms[count($this->termTranslationMap) - 1];
  }

}