annotate core/modules/taxonomy/tests/src/Functional/TermLanguageTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\taxonomy\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\Language\LanguageInterface;
Chris@0 6 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests the language functionality for the taxonomy terms.
Chris@0 10 *
Chris@0 11 * @group taxonomy
Chris@0 12 */
Chris@0 13 class TermLanguageTest extends TaxonomyTestBase {
Chris@0 14
Chris@0 15 public static $modules = ['language'];
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Vocabulary for testing.
Chris@0 19 *
Chris@0 20 * @var \Drupal\taxonomy\VocabularyInterface
Chris@0 21 */
Chris@0 22 protected $vocabulary;
Chris@0 23
Chris@0 24 protected function setUp() {
Chris@0 25 parent::setUp();
Chris@0 26
Chris@0 27 // Create an administrative user.
Chris@0 28 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
Chris@0 29
Chris@0 30 // Create a vocabulary to which the terms will be assigned.
Chris@0 31 $this->vocabulary = $this->createVocabulary();
Chris@0 32
Chris@0 33 // Add some custom languages.
Chris@0 34 foreach (['aa', 'bb', 'cc'] as $language_code) {
Chris@0 35 ConfigurableLanguage::create([
Chris@0 36 'id' => $language_code,
Chris@0 37 'label' => $this->randomMachineName(),
Chris@0 38 ])->save();
Chris@0 39 }
Chris@0 40 }
Chris@0 41
Chris@0 42 public function testTermLanguage() {
Chris@0 43 // Configure the vocabulary to not hide the language selector.
Chris@0 44 $edit = [
Chris@0 45 'default_language[language_alterable]' => TRUE,
Chris@0 46 ];
Chris@0 47 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
Chris@0 48
Chris@0 49 // Add a term.
Chris@0 50 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
Chris@0 51 // Check that we have the language selector.
Chris@0 52 $this->assertField('edit-langcode-0-value', t('The language selector field was found on the page.'));
Chris@0 53 // Submit the term.
Chris@0 54 $edit = [
Chris@0 55 'name[0][value]' => $this->randomMachineName(),
Chris@0 56 'langcode[0][value]' => 'aa',
Chris@0 57 ];
Chris@0 58 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 59 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
Chris@0 60 $term = reset($terms);
Chris@0 61 $this->assertEqual($term->language()->getId(), $edit['langcode[0][value]'], 'The term contains the correct langcode.');
Chris@0 62
Chris@0 63 // Check if on the edit page the language is correct.
Chris@0 64 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
Chris@0 65 $this->assertOptionSelected('edit-langcode-0-value', $edit['langcode[0][value]'], 'The term language was correctly selected.');
Chris@0 66
Chris@0 67 // Change the language of the term.
Chris@0 68 $edit['langcode[0][value]'] = 'bb';
Chris@0 69 $this->drupalPostForm('taxonomy/term/' . $term->id() . '/edit', $edit, t('Save'));
Chris@0 70
Chris@0 71 // Check again that on the edit page the language is correct.
Chris@0 72 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
Chris@0 73 $this->assertOptionSelected('edit-langcode-0-value', $edit['langcode[0][value]'], 'The term language was correctly selected.');
Chris@0 74 }
Chris@0 75
Chris@0 76 public function testDefaultTermLanguage() {
Chris@0 77 // Configure the vocabulary to not hide the language selector, and make the
Chris@0 78 // default language of the terms fixed.
Chris@0 79 $edit = [
Chris@0 80 'default_language[langcode]' => 'bb',
Chris@0 81 'default_language[language_alterable]' => TRUE,
Chris@0 82 ];
Chris@0 83 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
Chris@0 84 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
Chris@0 85 $this->assertOptionSelected('edit-langcode-0-value', 'bb', 'The expected langcode was selected.');
Chris@0 86
Chris@0 87 // Make the default language of the terms to be the current interface.
Chris@0 88 $edit = [
Chris@0 89 'default_language[langcode]' => 'current_interface',
Chris@0 90 'default_language[language_alterable]' => TRUE,
Chris@0 91 ];
Chris@0 92 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
Chris@0 93 $this->drupalGet('aa/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
Chris@0 94 $this->assertOptionSelected('edit-langcode-0-value', 'aa', "The expected langcode, 'aa', was selected.");
Chris@0 95 $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
Chris@0 96 $this->assertOptionSelected('edit-langcode-0-value', 'bb', "The expected langcode, 'bb', was selected.");
Chris@0 97
Chris@0 98 // Change the default language of the site and check if the default terms
Chris@0 99 // language is still correctly selected.
Chris@0 100 $this->config('system.site')->set('default_langcode', 'cc')->save();
Chris@0 101 $edit = [
Chris@0 102 'default_language[langcode]' => LanguageInterface::LANGCODE_SITE_DEFAULT,
Chris@0 103 'default_language[language_alterable]' => TRUE,
Chris@0 104 ];
Chris@0 105 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
Chris@0 106 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
Chris@0 107 $this->assertOptionSelected('edit-langcode-0-value', 'cc', "The expected langcode, 'cc', was selected.");
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * Tests that translated terms are displayed correctly on the term overview.
Chris@0 112 */
Chris@0 113 public function testTermTranslatedOnOverviewPage() {
Chris@0 114 // Configure the vocabulary to not hide the language selector.
Chris@0 115 $edit = [
Chris@0 116 'default_language[language_alterable]' => TRUE,
Chris@0 117 ];
Chris@0 118 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
Chris@0 119
Chris@0 120 // Add a term.
Chris@0 121 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
Chris@0 122 // Submit the term.
Chris@0 123 $edit = [
Chris@0 124 'name[0][value]' => $this->randomMachineName(),
Chris@0 125 'langcode[0][value]' => 'aa',
Chris@0 126 ];
Chris@0 127 $this->drupalPostForm(NULL, $edit, t('Save'));
Chris@0 128 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
Chris@0 129 $term = reset($terms);
Chris@0 130
Chris@0 131 // Add a translation for that term.
Chris@0 132 $translated_title = $this->randomMachineName();
Chris@0 133 $term->addTranslation('bb', [
Chris@0 134 'name' => $translated_title,
Chris@0 135 ]);
Chris@0 136 $term->save();
Chris@0 137
Chris@0 138 // Overview page in the other language shows the translated term
Chris@0 139 $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
Chris@0 140 $this->assertPattern('|<a[^>]*>' . $translated_title . '</a>|', 'The term language is correct');
Chris@0 141 }
Chris@0 142
Chris@0 143 }