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

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

namespace Drupal\language\Tests;

10
use Drupal\language\Entity\ConfigurableLanguage;
11 12 13 14 15
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser;
use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin;
16
use Drupal\simpletest\WebTestBase;
17
use Drupal\Core\Language\Language;
18
use Drupal\Core\Language\LanguageInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Drupal\language\LanguageNegotiatorInterface;
21 22

/**
23
 * Tests UI language switching.
24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 * 1. URL (PATH) > DEFAULT
 *    UI Language base on URL prefix, browser language preference has no
 *    influence:
 *      admin/config
 *        UI in site default language
 *      zh-hans/admin/config
 *        UI in Chinese
 *      blah-blah/admin/config
 *        404
 * 2. URL (PATH) > BROWSER > DEFAULT
 *        admin/config
 *          UI in user's browser language preference if the site has that
37
 *          language added, if not, the default language
38 39 40 41 42 43 44 45 46
 *        zh-hans/admin/config
 *          UI in Chinese
 *        blah-blah/admin/config
 *          404
 * 3. URL (DOMAIN) > DEFAULT
 *        http://example.com/admin/config
 *          UI language in site default
 *        http://example.cn/admin/config
 *          UI language in Chinese
47 48
 *
 * @group language
49 50
 */
class LanguageUILanguageNegotiationTest extends WebTestBase {
51 52 53 54 55 56 57 58 59 60

  /**
   * Modules to enable.
   *
   * We marginally use interface translation functionality here, so need to use
   * the locale module instead of language only, but the 90% of the test is
   * about the negotiation process which is solely in language module.
   *
   * @var array
   */
61
  public static $modules = array('locale', 'language_test', 'block', 'user');
62

63
  protected function setUp() {
64
    parent::setUp();
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    $admin_user = $this->drupalCreateUser(array('administer languages', 'translate interface', 'access administration pages', 'administer blocks'));
    $this->drupalLogin($admin_user);
  }

  /**
   * Tests for language switching by URL path.
   */
  function testUILanguageNegotiation() {
    // A few languages to switch to.
    // This one is unknown, should get the default lang version.
    $langcode_unknown = 'blah-blah';
    // For testing browser lang preference.
    $langcode_browser_fallback = 'vi';
    // For testing path prefix.
    $langcode = 'zh-hans';
    // For setting browser language preference to 'vi'.
    $http_header_browser_fallback = array("Accept-Language: $langcode_browser_fallback;q=1");
    // For setting browser language preference to some unknown.
    $http_header_blah = array("Accept-Language: blah;q=1");

    // Setup the site languages by installing two languages.
87 88 89 90
    // Set the default language in order for the translated string to be registered
    // into database when seen by t(). Without doing this, our target string
    // is for some reason not found when doing translate search. This might
    // be some bug.
91
    $default_language = \Drupal::languageManager()->getDefaultLanguage();
92 93
    ConfigurableLanguage::createFromLangcode($langcode_browser_fallback)->save();
    \Drupal::config('system.site')->set('langcode', $langcode_browser_fallback)->save();
94
    ConfigurableLanguage::createFromLangcode($langcode)->save();
95 96 97

    // We will look for this string in the admin/config screen to see if the
    // corresponding translated string is shown.
98
    $default_string = 'Hide descriptions';
99 100 101

    // First visit this page to make sure our target string is searchable.
    $this->drupalGet('admin/config');
102

103
    // Now the t()'ed string is in db so switch the language back to default.
104 105
    // This will rebuild the container so we need to rebuild the container in
    // the test environment.
106
    \Drupal::config('system.site')->set('langcode', $default_language->getId())->save();
107 108
    \Drupal::config('language.negotiation')->set('url.prefixes.en', '')->save();
    $this->rebuildContainer();
109 110 111 112 113

    // Translate the string.
    $language_browser_fallback_string = "In $langcode_browser_fallback In $langcode_browser_fallback In $langcode_browser_fallback";
    $language_string = "In $langcode In $langcode In $langcode";
    // Do a translate search of our target string.
114 115 116 117
    $search = array(
      'string' => $default_string,
      'langcode' => $langcode_browser_fallback,
    );
118
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
119 120 121 122 123
    $textarea = current($this->xpath('//textarea'));
    $lid = (string) $textarea[0]['name'];
    $edit = array(
      $lid => $language_browser_fallback_string,
    );
124
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
125 126 127 128 129

    $search = array(
      'string' => $default_string,
      'langcode' => $langcode,
    );
130
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
131 132
    $textarea = current($this->xpath('//textarea'));
    $lid = (string) $textarea[0]['name'];
133
    $edit = array(
134
      $lid => $language_string,
135
    );
136
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
137

138 139
    // Configure selected language negotiation to use zh-hans.
    $edit = array('selected_langcode' => $langcode);
140
    $this->drupalPostForm('admin/config/regional/language/detection/selected', $edit, t('Save configuration'));
141
    $test = array(
142
      'language_negotiation' => array(LanguageNegotiationSelected::METHOD_ID),
143 144
      'path' => 'admin/config',
      'expect' => $language_string,
145
      'expected_method_id' => LanguageNegotiationSelected::METHOD_ID,
146 147 148 149 150 151
      'http_header' => $http_header_browser_fallback,
      'message' => 'SELECTED: UI language is switched based on selected language.',
    );
    $this->runTest($test);

    // An invalid language is selected.
152
    \Drupal::config('language.negotiation')->set('selected_langcode', NULL)->save();
153
    $test = array(
154
      'language_negotiation' => array(LanguageNegotiationSelected::METHOD_ID),
155 156
      'path' => 'admin/config',
      'expect' => $default_string,
157
      'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
158 159 160 161 162 163
      'http_header' => $http_header_browser_fallback,
      'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.',
    );
    $this->runTest($test);

    // No selected language is available.
164
    \Drupal::config('language.negotiation')->set('selected_langcode', $langcode_unknown)->save();
165
    $test = array(
166
      'language_negotiation' => array(LanguageNegotiationSelected::METHOD_ID),
167 168
      'path' => 'admin/config',
      'expect' => $default_string,
169
      'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
170 171 172 173 174
      'http_header' => $http_header_browser_fallback,
      'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.',
    );
    $this->runTest($test);

175 176 177
    $tests = array(
      // Default, browser preference should have no influence.
      array(
178
        'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationSelected::METHOD_ID),
179 180
        'path' => 'admin/config',
        'expect' => $default_string,
181
        'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
182 183 184 185 186
        'http_header' => $http_header_browser_fallback,
        'message' => 'URL (PATH) > DEFAULT: no language prefix, UI language is default and the browser language preference setting is not used.',
      ),
      // Language prefix.
      array(
187
        'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationSelected::METHOD_ID),
188 189
        'path' => "$langcode/admin/config",
        'expect' => $language_string,
190
        'expected_method_id' => LanguageNegotiationUrl::METHOD_ID,
191 192 193 194 195
        'http_header' => $http_header_browser_fallback,
        'message' => 'URL (PATH) > DEFAULT: with language prefix, UI language is switched based on path prefix',
      ),
      // Default, go by browser preference.
      array(
196
        'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID),
197 198
        'path' => 'admin/config',
        'expect' => $language_browser_fallback_string,
199
        'expected_method_id' => LanguageNegotiationBrowser::METHOD_ID,
200 201 202 203 204
        'http_header' => $http_header_browser_fallback,
        'message' => 'URL (PATH) > BROWSER: no language prefix, UI language is determined by browser language preference',
      ),
      // Prefix, switch to the language.
      array(
205
        'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID),
206 207
        'path' => "$langcode/admin/config",
        'expect' => $language_string,
208
        'expected_method_id' => LanguageNegotiationUrl::METHOD_ID,
209
        'http_header' => $http_header_browser_fallback,
210
        'message' => 'URL (PATH) > BROWSER: with language prefix, UI language is based on path prefix',
211 212 213
      ),
      // Default, browser language preference is not one of site's lang.
      array(
214
        'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID),
215 216
        'path' => 'admin/config',
        'expect' => $default_string,
217
        'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
218 219 220 221 222 223 224 225 226 227
        'http_header' => $http_header_blah,
        'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language',
      ),
    );

    foreach ($tests as $test) {
      $this->runTest($test);
    }

    // Unknown language prefix should return 404.
228
    $definitions = \Drupal::languageManager()->getNegotiator()->getNegotiationMethods();
229
    \Drupal::config('language.types')
230
      ->set('negotiation.' . LanguageInterface::TYPE_INTERFACE . '.enabled', array_flip(array_keys($definitions)))
231
      ->save();
232 233 234
    $this->drupalGet("$langcode_unknown/admin/config", array(), $http_header_browser_fallback);
    $this->assertResponse(404, "Unknown language path prefix should return 404");

235 236 237 238 239
    // Set preferred langcode for user to NULL.
    $account = $this->loggedInUser;
    $account->preferred_langcode = NULL;
    $account->save();

240
    $test = array(
241
      'language_negotiation' => array(LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID),
242 243
      'path' => 'admin/config',
      'expect' => $default_string,
244
      'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
245 246
      'http_header' => array(),
      'message' => 'USER > DEFAULT: no preferred user language setting, the UI language is default',
247
    );
248
    $this->runTest($test);
249 250 251 252 253 254

    // Set preferred langcode for user to unknown language.
    $account = $this->loggedInUser;
    $account->preferred_langcode = $langcode_unknown;
    $account->save();

255
    $test = array(
256
      'language_negotiation' => array(LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID),
257 258
      'path' => 'admin/config',
      'expect' => $default_string,
259
      'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
260 261
      'http_header' => array(),
      'message' => 'USER > DEFAULT: invalid preferred user language setting, the UI language is default',
262
    );
263
    $this->runTest($test);
264 265 266 267 268

    // Set preferred langcode for user to non default.
    $account->preferred_langcode = $langcode;
    $account->save();

269
    $test = array(
270
      'language_negotiation' => array(LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID),
271 272
      'path' => 'admin/config',
      'expect' => $language_string,
273
      'expected_method_id' => LanguageNegotiationUser::METHOD_ID,
274 275
      'http_header' => array(),
      'message' => 'USER > DEFAULT: defined prefereed user language setting, the UI language is based on user setting',
276
    );
277
    $this->runTest($test);
278 279 280 281 282

    // Set preferred admin langcode for user to NULL.
    $account->preferred_admin_langcode = NULL;
    $account->save();

283
    $test = array(
284
      'language_negotiation' => array(LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID),
285 286
      'path' => 'admin/config',
      'expect' => $default_string,
287
      'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
288 289
      'http_header' => array(),
      'message' => 'USER ADMIN > DEFAULT: no preferred user admin language setting, the UI language is default',
290
    );
291
    $this->runTest($test);
292 293 294 295 296

    // Set preferred admin langcode for user to unknown language.
    $account->preferred_admin_langcode = $langcode_unknown;
    $account->save();

297
    $test = array(
298
      'language_negotiation' => array(LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID),
299 300
      'path' => 'admin/config',
      'expect' => $default_string,
301
      'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
302 303
      'http_header' => array(),
      'message' => 'USER ADMIN > DEFAULT: invalid preferred user admin language setting, the UI language is default',
304
    );
305
    $this->runTest($test);
306 307 308 309 310

    // Set preferred admin langcode for user to non default.
    $account->preferred_admin_langcode = $langcode;
    $account->save();

311
    $test = array(
312
      'language_negotiation' => array(LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID),
313 314
      'path' => 'admin/config',
      'expect' => $language_string,
315
      'expected_method_id' => LanguageNegotiationUserAdmin::METHOD_ID,
316 317
      'http_header' => array(),
      'message' => 'USER ADMIN > DEFAULT: defined prefereed user admin language setting, the UI language is based on user setting',
318
    );
319
    $this->runTest($test);
320 321 322 323 324
  }

  protected function runTest($test) {
    if (!empty($test['language_negotiation'])) {
      $method_weights = array_flip($test['language_negotiation']);
325
      $this->container->get('language_negotiator')->saveConfiguration(LanguageInterface::TYPE_INTERFACE, $method_weights);
326 327
    }
    if (!empty($test['language_negotiation_url_part'])) {
328
      \Drupal::config('language.negotiation')
329 330
        ->set('url.source', $test['language_negotiation_url_part'])
        ->save();
331 332
    }
    if (!empty($test['language_test_domain'])) {
333
      \Drupal::state()->set('language_test.domain', $test['language_test_domain']);
334
    }
335
    $this->container->get('language_manager')->reset();
336 337 338 339 340 341 342 343 344 345 346
    $this->drupalGet($test['path'], array(), $test['http_header']);
    $this->assertText($test['expect'], $test['message']);
    $this->assertText(t('Language negotiation method: @name', array('@name' => $test['expected_method_id'])));
  }

  /**
   * Test URL language detection when the requested URL has no language.
   */
  function testUrlLanguageFallback() {
    // Add the Italian language.
    $langcode_browser_fallback = 'it';
347
    ConfigurableLanguage::createFromLangcode($langcode_browser_fallback)->save();
348
    $languages = $this->container->get('language_manager')->getLanguages();
349 350 351 352

    // Enable the path prefix for the default language: this way any unprefixed
    // URL must have a valid fallback value.
    $edit = array('prefix[en]' => 'en');
353
    $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
354 355 356 357 358 359 360 361

    // Enable browser and URL language detection.
    $edit = array(
      'language_interface[enabled][language-browser]' => TRUE,
      'language_interface[enabled][language-url]' => TRUE,
      'language_interface[weight][language-browser]' => -8,
      'language_interface[weight][language-url]' => -10,
    );
362
    $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
363 364 365
    $this->drupalGet('admin/config/regional/language/detection');

    // Enable the language switcher block.
366
    $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, array('id' => 'test_language_block'));
367

368 369 370 371 372
    // Log out, because for anonymous users, the "active" class is set by PHP
    // (which means we can easily test it here), whereas for authenticated users
    // it is set by JavaScript.
    $this->drupalLogout();

373 374 375
    // Access the front page without specifying any valid URL language prefix
    // and having as browser language preference a non-default language.
    $http_header = array("Accept-Language: $langcode_browser_fallback;q=1");
376
    $language = new Language(array('id' => ''));
377 378 379 380
    $this->drupalGet('', array('language' => $language), $http_header);

    // Check that the language switcher active link matches the given browser
    // language.
381
    $args = array(':id' => 'block-test-language-block', ':url' => base_path() . $GLOBALS['script_path'] . $langcode_browser_fallback);
382
    $fields = $this->xpath('//div[@id=:id]//a[@class="language-link active" and starts-with(@href, :url)]', $args);
383
    $this->assertTrue($fields[0] == $languages[$langcode_browser_fallback]->getName(), 'The browser language is the URL active language');
384 385

    // Check that URLs are rewritten using the given browser language.
386
    $fields = $this->xpath('//strong[@class="site-name"]/a[@rel="home" and @href=:url]', $args);
387
    $this->assertTrue($fields[0] == 'Drupal', 'URLs are rewritten using the browser language.');
388 389 390
  }

  /**
391
   * Tests _url() when separate domains are used for multiple languages.
392 393
   */
  function testLanguageDomain() {
394 395 396 397 398
    global $base_url;

    // Get the current host URI we're running on.
    $base_url_host = parse_url($base_url, PHP_URL_HOST);

399
    // Add the Italian language.
400 401
    ConfigurableLanguage::createFromLangcode('it')->save();

402
    $languages = $this->container->get('language_manager')->getLanguages();
403 404 405 406 407 408

    // Enable browser and URL language detection.
    $edit = array(
      'language_interface[enabled][language-url]' => TRUE,
      'language_interface[weight][language-url]' => -10,
    );
409
    $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
410

411 412 413 414 415 416 417 418 419
    // Do not allow blank domain.
    $edit = array(
      'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
      'domain[en]' => '',
    );
    $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
    $this->assertText('The domain may not be left blank for English', 'The form does not allow blank domains.');
    $this->rebuildContainer();

420 421
    // Change the domain for the Italian language.
    $edit = array(
422
      'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
423
      'domain[en]' => $base_url_host,
424 425
      'domain[it]' => 'it.example.com',
    );
426
    $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
427
    $this->assertText('The configuration options have been saved', 'Domain configuration is saved.');
428
    $this->rebuildContainer();
429 430

    // Build the link we're going to test.
431
    $link = 'it.example.com' . rtrim(base_path(), '/') . '/admin';
432 433 434 435

    // Test URL in another language: http://it.example.com/admin.
    // Base path gives problems on the testbot, so $correct_link is hard-coded.
    // @see UrlAlterFunctionalTest::assertUrlOutboundAlter (path.test).
436
    $italian_url = _url('admin', array('language' => $languages['it'], 'script' => ''));
437
    $url_scheme = \Drupal::request()->isSecure() ? 'https://' : 'http://';
438
    $correct_link = $url_scheme . $link;
439
    $this->assertEqual($italian_url, $correct_link, format_string('The _url() function returns the right URL (@url) in accordance with the chosen language', array('@url' => $italian_url)));
440

441
    // Test HTTPS via options.
442
    $this->settingsSet('mixed_mode_sessions', TRUE);
443 444
    $this->rebuildContainer();

445
    $italian_url = _url('admin', array('https' => TRUE, 'language' => $languages['it'], 'script' => ''));
446
    $correct_link = 'https://' . $link;
447
    $this->assertTrue($italian_url == $correct_link, format_string('The _url() function returns the right HTTPS URL (via options) (@url) in accordance with the chosen language', array('@url' => $italian_url)));
448
    $this->settingsSet('mixed_mode_sessions', FALSE);
449

450
    // Test HTTPS via current URL scheme.
451
    $request = Request::create('', 'GET', array(), array(), array(), array('HTTPS' => 'on'));
452 453
    $this->container->get('request_stack')->push($request);
    $generator = $this->container->get('url_generator');
454
    $italian_url = _url('admin', array('language' => $languages['it'], 'script' => ''));
455
    $correct_link = 'https://' . $link;
456
    $this->assertTrue($italian_url == $correct_link, format_string('The _url() function returns the right URL (via current URL scheme) (@url) in accordance with the chosen language', array('@url' => $italian_url)));
457 458
  }
}