VocabularyLanguageTest.php 5.49 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\taxonomy\Tests\VocabularyLanguageTest.
 */

namespace Drupal\taxonomy\Tests;

10 11
use Drupal\Core\Language\Language;

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/**
 * Tests for the language feature on vocabularies.
 */
class VocabularyLanguageTest extends TaxonomyTestBase {

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

  public static function getInfo() {
    return array(
      'name' => 'Vocabulary language',
      'description' => 'Tests the language functionality for vocabularies.',
      'group' => 'Taxonomy',
    );
  }

  function setUp() {
    parent::setUp();

    // Create an administrative user.
    $this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
    $this->drupalLogin($this->admin_user);

34
    // Add some custom languages.
35
    $language = new Language(array(
36 37
      'langcode' => 'aa',
      'name' => $this->randomName(),
38
    ));
39 40
    language_save($language);

41
    $language = new Language(array(
42 43
      'langcode' => 'bb',
      'name' => $this->randomName(),
44
    ));
45
    language_save($language);
46
  }
47

48 49 50 51
  /**
   * Tests language settings for vocabularies.
   */
  function testVocabularyLanguage() {
52
    $this->drupalGet('admin/structure/taxonomy/add');
53

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    // Check that we have the language selector available.
    $this->assertField('edit-langcode', t('The language selector field was found on the page'));

    // Create the vocabulary.
    $machine_name = drupal_strtolower($this->randomName());
    $edit['name'] = $this->randomName();
    $edit['description'] = $this->randomName();
    $edit['langcode'] = 'aa';
    $edit['machine_name'] = $machine_name;
    $this->drupalPost(NULL, $edit, t('Save'));

    // Check the language on the edit page.
    $this->drupalGet('admin/structure/taxonomy/' . $machine_name . '/edit');
    $this->assertOptionSelected('edit-langcode', $edit['langcode'], t('The vocabulary language was correctly selected.'));

    // Change the language and save again.
    $edit['langcode'] = 'bb';
    $this->drupalPost(NULL, $edit, t('Save'));

    // Check again the language on the edit page.
    $this->drupalGet('admin/structure/taxonomy/' . $machine_name . '/edit');
    $this->assertOptionSelected('edit-langcode', $edit['langcode'], t('The vocabulary language was correctly selected.'));
  }
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 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

  /**
   * Tests term language settings for vocabulary terms are saved and updated.
   */
  function testVocabularyDefaultLanguageForTerms() {
    // Add a new vocabulary and check that the default language settings are for
    // the terms are saved.
    $edit = array(
      'name' => $this->randomName(),
      'machine_name' => drupal_strtolower($this->randomName()),
      'default_language[langcode]' => 'bb',
      'default_language[language_hidden]' => FALSE,
    );
    $machine_name = $edit['machine_name'];
    $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save'));

    // Check that the vocabulary was actually created.
    $this->drupalGet('admin/structure/taxonomy/' . $edit['machine_name'] . '/edit');
    $this->assertResponse(200, 'The vocabulary has been created.');

    // Check that the language settings were saved.
    $language_settings = language_get_default_configuration('vocabulary', $edit['machine_name']);
    $this->assertEqual($language_settings['langcode'], 'bb');
    $this->assertEqual($language_settings['language_hidden'], FALSE);

    // Check that the correct options are selected in the interface.
    $this->assertOptionSelected('edit-default-language-langcode', 'bb', 'The correct default language for the terms of this vocabulary is selected.');
    $this->assertNoFieldChecked('edit-default-language-language-hidden', 'Hide language selection option is not checked.');

    // Edit the vocabulary and check that the new settings are updated.
    $edit = array(
      'default_language[langcode]' => 'aa',
      'default_language[language_hidden]' => TRUE,
    );
    $this->drupalPost('admin/structure/taxonomy/' . $machine_name . '/edit', $edit, t('Save'));

    // And check again the settings and also the interface.
    $language_settings = language_get_default_configuration('vocabulary', $machine_name);
    $this->assertEqual($language_settings['langcode'], 'aa');
    $this->assertEqual($language_settings['language_hidden'], TRUE);

    $this->drupalGet('admin/structure/taxonomy/' . $machine_name . '/edit');
    $this->assertOptionSelected('edit-default-language-langcode', 'aa', 'The correct default language for the terms of this vocabulary is selected.');
    $this->assertFieldChecked('edit-default-language-language-hidden', 'Hide language selection option is not checked.');

    // Check that, if the machine name of the vocabulary is changed, then the
    // settings are applied on the new machine name.
     $edit = array(
      'machine_name' => $machine_name . '_new',
      'default_language[langcode]' => 'authors_default',
      'default_language[language_hidden]' => TRUE,
    );
    $new_machine_name = $edit['machine_name'];
    $this->drupalPost('admin/structure/taxonomy/' . $machine_name . '/edit', $edit, t('Save'));

    // Check that the old settings are empty.
    $old_settings = config('language.settings')->get(language_get_default_configuration_settings_key('vocabulary', $machine_name));
    $this->assertNull($old_settings, 'The old vocabulary settings were deleted.');
    // Check that we have the new settings.
    $new_settings = language_get_default_configuration('vocabulary', $new_machine_name);
    $this->assertEqual($new_settings['langcode'], 'authors_default');
    $this->assertEqual($new_settings['language_hidden'], TRUE);
  }
140
}