TermLanguageTest.php 5.74 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\taxonomy\Functional;
4

5
use Drupal\Core\Language\LanguageInterface;
6
use Drupal\language\Entity\ConfigurableLanguage;
7

8
/**
9 10 11
 * Tests the language functionality for the taxonomy terms.
 *
 * @group taxonomy
12 13 14
 */
class TermLanguageTest extends TaxonomyTestBase {

15
  public static $modules = ['language'];
16

17 18 19 20 21 22 23
  /**
   * Vocabulary for testing.
   *
   * @var \Drupal\taxonomy\VocabularyInterface
   */
  protected $vocabulary;

24
  protected function setUp() {
25 26 27
    parent::setUp();

    // Create an administrative user.
28
    $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
29 30 31

    // Create a vocabulary to which the terms will be assigned.
    $this->vocabulary = $this->createVocabulary();
32 33

    // Add some custom languages.
34 35
    foreach (['aa', 'bb', 'cc'] as $language_code) {
      ConfigurableLanguage::create([
36
        'id' => $language_code,
37
        'label' => $this->randomMachineName(),
38
      ])->save();
39
    }
40 41
  }

42
  public function testTermLanguage() {
43
    // Configure the vocabulary to not hide the language selector.
44
    $edit = [
45
      'default_language[language_alterable]' => TRUE,
46
    ];
47
    $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
48 49

    // Add a term.
50
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
51
    // Check that we have the language selector.
52
    $this->assertField('edit-langcode-0-value', t('The language selector field was found on the page.'));
53
    // Submit the term.
54
    $edit = [
55
      'name[0][value]' => $this->randomMachineName(),
56
      'langcode[0][value]' => 'aa',
57
    ];
58
    $this->drupalPostForm(NULL, $edit, t('Save'));
59
    $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
60
    $term = reset($terms);
61
    $this->assertEqual($term->language()->getId(), $edit['langcode[0][value]'], 'The term contains the correct langcode.');
62 63

    // Check if on the edit page the language is correct.
64
    $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
65
    $this->assertOptionSelected('edit-langcode-0-value', $edit['langcode[0][value]'], 'The term language was correctly selected.');
66 67

    // Change the language of the term.
68
    $edit['langcode[0][value]'] = 'bb';
69
    $this->drupalPostForm('taxonomy/term/' . $term->id() . '/edit', $edit, t('Save'));
70 71

    // Check again that on the edit page the language is correct.
72
    $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
73
    $this->assertOptionSelected('edit-langcode-0-value', $edit['langcode[0][value]'], 'The term language was correctly selected.');
74
  }
75

76
  public function testDefaultTermLanguage() {
77 78
    // Configure the vocabulary to not hide the language selector, and make the
    // default language of the terms fixed.
79
    $edit = [
80
      'default_language[langcode]' => 'bb',
81
      'default_language[language_alterable]' => TRUE,
82
    ];
83
    $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
84
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
85
    $this->assertOptionSelected('edit-langcode-0-value', 'bb', 'The expected langcode was selected.');
86 87

    // Make the default language of the terms to be the current interface.
88
    $edit = [
89
      'default_language[langcode]' => 'current_interface',
90
      'default_language[language_alterable]' => TRUE,
91
    ];
92
    $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
93
    $this->drupalGet('aa/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
94
    $this->assertOptionSelected('edit-langcode-0-value', 'aa', "The expected langcode, 'aa', was selected.");
95
    $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
96
    $this->assertOptionSelected('edit-langcode-0-value', 'bb', "The expected langcode, 'bb', was selected.");
97 98 99

    // Change the default language of the site and check if the default terms
    // language is still correctly selected.
100
    $this->config('system.site')->set('default_langcode', 'cc')->save();
101
    $edit = [
102
      'default_language[langcode]' => LanguageInterface::LANGCODE_SITE_DEFAULT,
103
      'default_language[language_alterable]' => TRUE,
104
    ];
105
    $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
106
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
107
    $this->assertOptionSelected('edit-langcode-0-value', 'cc', "The expected langcode, 'cc', was selected.");
108
  }
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  /**
   * Tests that translated terms are displayed correctly on the term overview.
   */
  public function testTermTranslatedOnOverviewPage() {
    // Configure the vocabulary to not hide the language selector.
    $edit = [
      'default_language[language_alterable]' => TRUE,
    ];
    $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));

    // Add a term.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
    // Submit the term.
    $edit = [
      'name[0][value]' => $this->randomMachineName(),
      'langcode[0][value]' => 'aa',
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
    $term = reset($terms);

    // Add a translation for that term.
    $translated_title = $this->randomMachineName();
    $term->addTranslation('bb', [
      'name' => $translated_title,
    ]);
    $term->save();

    // Overview page in the other language shows the translated term
    $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
    $this->assertPattern('|<a[^>]*>' . $translated_title . '</a>|', 'The term language is correct');
  }

143
}