TermLanguageTest.php 4.52 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\taxonomy\Tests;

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 15 16
 */
class TermLanguageTest extends TaxonomyTestBase {

  public static $modules = array('language');

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 34

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

  function testTermLanguage() {
43 44
    // Configure the vocabulary to not hide the language selector.
    $edit = array(
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 54
    // Submit the term.
    $edit = array(
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 77 78 79 80

  function testDefaultTermLanguage() {
    // Configure the vocabulary to not hide the language selector, and make the
    // default language of the terms fixed.
    $edit = array(
      '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 88 89

    // Make the default language of the terms to be the current interface.
    $edit = array(
      '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 = array(
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
}