LanguageConfigurationTest.php 7.29 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
use Drupal\simpletest\WebTestBase;

/**
14 15 16
 * Adds and configures languages to check negotiation changes.
 *
 * @group language
17 18
 */
class LanguageConfigurationTest extends WebTestBase {
19 20 21 22 23 24 25 26

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

27 28 29 30 31 32
  /**
   * Functional tests for adding, editing and deleting languages.
   */
  function testLanguageConfiguration() {

    // User to add and remove language.
33
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer site configuration'));
34 35 36 37
    $this->drupalLogin($admin_user);

    // Check if the Default English language has no path prefix.
    $this->drupalGet('admin/config/regional/language/detection/url');
38
    $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', 'Default English has no path prefix.');
39 40 41 42 43

    // Add predefined language.
    $edit = array(
      'predefined_langcode' => 'fr',
    );
44
    $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
45
    $this->assertText('French');
46
    $this->assertUrl(\Drupal::url('language.admin_overview', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
47 48 49
    // Langcode for Languages is always 'en'.
    $language = $this->container->get('config.factory')->get('language.entity.fr')->get();
    $this->assertEqual($language['langcode'], 'en');
50 51 52

    // Check if the Default English language has no path prefix.
    $this->drupalGet('admin/config/regional/language/detection/url');
53
    $this->assertFieldByXPath('//input[@name="prefix[en]"]', '', 'Default English has no path prefix.');
54 55
    // Check if French has a path prefix.
    $this->drupalGet('admin/config/regional/language/detection/url');
56
    $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French has a path prefix.');
57 58

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

62 63
    // Change the default language.
    $edit = array(
64
      'site_default_language' => 'fr',
65
    );
66
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
67
    $this->rebuildContainer();
68
    $this->assertOptionSelected('edit-site-default-language', 'fr', 'Default language updated.');
69
    $this->assertUrl(\Drupal::url('system.regional_settings', [], ['absolute' => TRUE, 'langcode' => 'fr']), [], 'Correct page redirection.');
70

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

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

86
    // Check that prefix of non default language cannot be changed to
87 88 89 90
    // empty string.
    $edit = array(
      'prefix[en]' => '',
    );
91
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
92 93 94 95 96 97
    $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',
    );
98
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
99
    $this->assertText(t('The prefix may not contain a slash.'), 'English prefix cannot be changed to contain a slash.');
100 101 102

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

  /**
   * 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',
    );
127
    $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
128 129 130 131 132 133
    $this->checkConfigurableLanguageWeight('after adding new language');

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

    // Remove predefined language.
    $edit = array(
      'confirm' => 1,
    );
141
    $this->drupalPostForm('admin/config/regional/language/delete/fr', $edit, 'Delete');
142 143 144 145 146 147 148 149 150 151 152 153 154
    $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.
155
    \Drupal::languageManager()->reset();
156 157
    $max_configurable_language_weight = $this->getHighestConfigurableLanguageWeight();
    $replacements = array('@event' => $state);
158
    foreach (\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_LOCKED) as $locked_language) {
159 160 161 162 163 164 165 166 167 168 169 170
      $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(){
171 172
    $max_weight = 0;

173
    $languages = entity_load_multiple('configurable_language', NULL, TRUE);
174
    foreach ($languages as $language) {
175
      if (!$language->isLocked() && $language->weight > $max_weight) {
176 177 178 179 180
        $max_weight = $language->weight;
      }
    }

    return $max_weight;
181 182
  }

183
}