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

/**
 * @file
 * Definition of Drupal\language\Tests\LanguageConfigurationTest.
 */

namespace Drupal\language\Tests;

10
use Drupal\Core\Language\LanguageInterface;
11 12 13 14 15 16
use Drupal\simpletest\WebTestBase;

/**
 * Functional tests for language configuration's effect on negotiation setup.
 */
class LanguageConfigurationTest extends WebTestBase {
17 18 19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('language');

25 26 27 28 29 30 31 32 33 34 35 36 37 38
  public static function getInfo() {
    return array(
      'name' => 'Language negotiation autoconfiguration',
      'description' => 'Adds and configures languages to check negotiation changes.',
      'group' => 'Language',
    );
  }

  /**
   * Functional tests for adding, editing and deleting languages.
   */
  function testLanguageConfiguration() {

    // User to add and remove language.
39
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer site configuration'));
40 41 42 43
    $this->drupalLogin($admin_user);

    // Check if the Default English language has no path prefix.
    $this->drupalGet('admin/config/regional/language/detection/url');
44
    $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', 'Default English has no path prefix.');
45 46 47 48 49

    // Add predefined language.
    $edit = array(
      'predefined_langcode' => 'fr',
    );
50
    $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
51
    $this->assertText('French');
52
    $this->assertEqual($this->getUrl(), url('admin/config/regional/language', array('absolute' => TRUE)), 'Correct page redirection.');
53 54 55
    // Langcode for Languages is always 'en'.
    $language = $this->container->get('config.factory')->get('language.entity.fr')->get();
    $this->assertEqual($language['langcode'], 'en');
56 57 58

    // Check if the Default English language has no path prefix.
    $this->drupalGet('admin/config/regional/language/detection/url');
59
    $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', 'Default English has no path prefix.');
60 61
    // Check if French has a path prefix.
    $this->drupalGet('admin/config/regional/language/detection/url');
62
    $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French has a path prefix.');
63 64

    // Check if we can change the default language.
65 66 67
    $this->drupalGet('admin/config/regional/settings');
    $this->assertOptionSelected('edit-site-default-language', 'en', 'English is the default language.');

68 69
    // Change the default language.
    $edit = array(
70
      'site_default_language' => 'fr',
71
    );
72
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
73
    $this->assertOptionSelected('edit-site-default-language', 'fr', 'Default language updated.');
74
    $this->assertEqual($this->getUrl(), url('fr/admin/config/regional/settings', array('absolute' => TRUE)), 'Correct page redirection.');
75

76
    // Check if a valid language prefix is added after changing the default
77 78
    // language.
    $this->drupalGet('admin/config/regional/language/detection/url');
79
    $this->assertFieldByXPath('//input[@name="prefix[en]"]', 'en', 'A valid path prefix has been added to the previous default language.');
80 81
    // Check if French still has a path prefix.
    $this->drupalGet('admin/config/regional/language/detection/url');
82 83 84 85 86 87
    $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French still has a path prefix.');

    // Check that prefix can be changed.
    $edit = array(
      'prefix[fr]' => 'french',
    );
88
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
89 90
    $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'french', 'French path prefix has changed.');

91
    // Check that prefix of non default language cannot be changed to
92 93 94 95
    // empty string.
    $edit = array(
      'prefix[en]' => '',
    );
96
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
97 98 99 100 101 102
    $this->assertText(t('The prefix may only be left blank for the default language.'), 'English prefix cannot be changed to empty string.');

    //  Check that prefix cannot be changed to contain a slash.
    $edit = array(
      'prefix[en]' => 'foo/bar',
    );
103
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
104
    $this->assertText(t('The prefix may not contain a slash.'), 'English prefix cannot be changed to contain a slash.');
105 106 107

    // Remove English language and add a new Language to check if langcode of
    // Language entity is 'en'.
108 109
    $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete'));
    $this->assertRaw(t('The %language (%langcode) language has been removed.', array('%language' => 'English', '%langcode' => 'en')));
110 111 112
    $edit = array(
      'predefined_langcode' => 'de',
    );
113
    $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
114 115
    $language = $this->container->get('config.factory')->get('language.entity.de')->get();
    $this->assertEqual($language['langcode'], 'en');
116
  }
117 118 119 120 121 122 123 124 125 126 127 128 129 130

  /**
   * Functional tests for setting system language weight on adding, editing and deleting languages.
   */
  function testLanguageConfigurationWeight() {
    // User to add and remove language.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
    $this->drupalLogin($admin_user);
    $this->checkConfigurableLanguageWeight();

    // Add predefined language.
    $edit = array(
      'predefined_langcode' => 'fr',
    );
131
    $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
132 133 134 135 136 137
    $this->checkConfigurableLanguageWeight('after adding new language');

    // Re-ordering languages.
    $edit = array(
      'languages[en][weight]' => $this->getHighestConfigurableLanguageWeight() + 1,
    );
138
    $this->drupalPostForm('admin/config/regional/language', $edit, 'Save configuration');
139 140 141 142 143 144
    $this->checkConfigurableLanguageWeight('after re-ordering');

    // Remove predefined language.
    $edit = array(
      'confirm' => 1,
    );
145
    $this->drupalPostForm('admin/config/regional/language/delete/fr', $edit, 'Delete');
146 147 148 149 150 151 152 153 154 155 156 157 158
    $this->checkConfigurableLanguageWeight('after deleting a language');
  }

  /**
   * Validates system languages are ordered after configurable languages.
   *
   * @param string $state
   *   (optional) A string for customizing assert messages, containing the
   *   description of the state of the check, for example: 'after re-ordering'.
   *   Defaults to 'by default'.
   */
  protected function checkConfigurableLanguageWeight($state = 'by default') {
    // Reset language list.
159
    \Drupal::languageManager()->reset();
160 161
    $max_configurable_language_weight = $this->getHighestConfigurableLanguageWeight();
    $replacements = array('@event' => $state);
162
    foreach (\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_LOCKED) as $locked_language) {
163 164 165 166 167 168 169 170 171 172 173 174
      $replacements['%language'] = $locked_language->name;
      $this->assertTrue($locked_language->weight > $max_configurable_language_weight, format_string('System language %language has higher weight than configurable languages @event', $replacements));
    }
  }

  /**
   * Helper to get maximum weight of configurable (unlocked) languages.
   *
   * @return int
   *   Maximum weight of configurable languages.
   */
  protected function getHighestConfigurableLanguageWeight(){
175 176 177 178 179 180 181 182 183 184
    $max_weight = 0;

    $languages = entity_load_multiple('language_entity', NULL, TRUE);
    foreach ($languages as $language) {
      if (!$language->locked && $language->weight > $max_weight) {
        $max_weight = $language->weight;
      }
    }

    return $max_weight;
185 186
  }

187
}