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

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

namespace Drupal\language\Tests;

use Drupal\simpletest\WebTestBase;
11
use Drupal\Core\Language\Language;
12
use Symfony\Component\HttpFoundation\Request;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

/**
 * Test UI language negotiation
 *
 * 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
 *          language enabled, if not, the default language
 *        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
 */
class LanguageUILanguageNegotiationTest extends WebTestBase {
41
42
43
44
45
46
47
48
49
50
51
52

  /**
   * 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
   */
  public static $modules = array('locale', 'language_test', 'block');

53
54
55
  public static function getInfo() {
    return array(
      'name' => 'UI language negotiation',
56
      'description' => 'Test UI language switching by URL path prefix and domain.',
57
58
59
60
61
      'group' => 'Language',
    );
  }

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

64
65
66
    $this->request = Request::create('http://example.com/');
    $this->container->set('request', $this->request);

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    require_once DRUPAL_ROOT . '/core/includes/language.inc';
    $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");

    // This domain should switch the UI to Chinese.
    $language_domain = 'example.cn';

    // Setup the site languages by installing two languages.
92
    $language = new Language(array(
93
      'langcode' => $langcode_browser_fallback,
94
    ));
95
    language_save($language);
96
    $language = new Language(array(
97
      'langcode' => $langcode,
98
    ));
99
100
101
102
103
104
105
106
107
108
109
110
    language_save($language);

    // We will look for this string in the admin/config screen to see if the
    // corresponding translated string is shown.
    $default_string = 'Configure languages for content and the user interface';

    // 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.
    drupal_static_reset('language_list');
    $languages = language_list();
111
    variable_set('language_default', (array) $languages['vi']);
112
113
114
115
116
117
118
119
120
    // First visit this page to make sure our target string is searchable.
    $this->drupalGet('admin/config');
    // Now the t()'ed string is in db so switch the language back to default.
    variable_del('language_default');

    // 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.
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    $search = array(
      'string' => $default_string,
      'langcode' => $langcode_browser_fallback,
    );
    $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
    $textarea = current($this->xpath('//textarea'));
    $lid = (string) $textarea[0]['name'];
    $edit = array(
      $lid => $language_browser_fallback_string,
    );
    $this->drupalPost('admin/config/regional/translate/translate', $edit, t('Save translations'));

    $search = array(
      'string' => $default_string,
      'langcode' => $langcode,
    );
    $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
    $textarea = current($this->xpath('//textarea'));
    $lid = (string) $textarea[0]['name'];
140
    $edit = array(
141
      $lid => $language_string,
142
    );
143
    $this->drupalPost('admin/config/regional/translate/translate', $edit, t('Save translations'));
144
145
146
147

    // Configure URL language rewrite.
    variable_set('language_negotiation_url_type', LANGUAGE_TYPE_INTERFACE);

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    // Configure selected language negotiation to use zh-hans.
    $edit = array('selected_langcode' => $langcode);
    $this->drupalPost('admin/config/regional/language/detection/selected', $edit, t('Save configuration'));
    $test = array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_SELECTED),
      'path' => 'admin/config',
      'expect' => $language_string,
      'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED,
      'http_header' => $http_header_browser_fallback,
      'message' => 'SELECTED: UI language is switched based on selected language.',
    );
    $this->runTest($test);

    // An invalid language is selected.
    config('language.negotiation')->set('selected_langcode', NULL)->save();
    $test = array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_SELECTED),
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED,
      '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.
    config('language.negotiation')->set('selected_langcode', $langcode_unknown)->save();
    $test = array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_SELECTED),
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED,
      'http_header' => $http_header_browser_fallback,
      'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.',
    );
    $this->runTest($test);

185
186
187
    $tests = array(
      // Default, browser preference should have no influence.
      array(
188
        'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_SELECTED),
189
190
        'path' => 'admin/config',
        'expect' => $default_string,
191
        'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED,
192
193
194
195
196
        '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(
197
        'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_SELECTED),
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
        'path' => "$langcode/admin/config",
        'expect' => $language_string,
        'expected_method_id' => LANGUAGE_NEGOTIATION_URL,
        '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(
        'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER),
        'path' => 'admin/config',
        'expect' => $language_browser_fallback_string,
        'expected_method_id' => LANGUAGE_NEGOTIATION_BROWSER,
        '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(
        'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER),
        'path' => "$langcode/admin/config",
        'expect' => $language_string,
        'expected_method_id' => LANGUAGE_NEGOTIATION_URL,
        'http_header' => $http_header_browser_fallback,
        'message' => 'URL (PATH) > BROWSER: with langage prefix, UI language is based on path prefix',
      ),
      // Default, browser language preference is not one of site's lang.
      array(
224
        'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER, LANGUAGE_NEGOTIATION_SELECTED),
225
226
        'path' => 'admin/config',
        'expect' => $default_string,
227
        'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED,
228
229
230
231
232
233
234
235
236
237
238
239
240
241
        '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.
    variable_set('language_negotiation_' . LANGUAGE_TYPE_INTERFACE, language_language_negotiation_info());
    $this->drupalGet("$langcode_unknown/admin/config", array(), $http_header_browser_fallback);
    $this->assertResponse(404, "Unknown language path prefix should return 404");

242
243
244
245
246
    // Set preferred langcode for user to NULL.
    $account = $this->loggedInUser;
    $account->preferred_langcode = NULL;
    $account->save();

247
248
249
250
251
252
253
    $test = array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER, LANGUAGE_NEGOTIATION_SELECTED),
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED,
      'http_header' => array(),
      'message' => 'USER > DEFAULT: no preferred user language setting, the UI language is default',
254
    );
255
    $this->runTest($test);
256
257
258
259
260
261

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

262
263
264
265
266
267
268
    $test = array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER, LANGUAGE_NEGOTIATION_SELECTED),
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED,
      'http_header' => array(),
      'message' => 'USER > DEFAULT: invalid preferred user language setting, the UI language is default',
269
    );
270
    $this->runTest($test);
271
272
273
274
275

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

276
277
278
279
280
281
282
    $test = array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER, LANGUAGE_NEGOTIATION_SELECTED),
      'path' => 'admin/config',
      'expect' => $language_string,
      'expected_method_id' => LANGUAGE_NEGOTIATION_USER,
      'http_header' => array(),
      'message' => 'USER > DEFAULT: defined prefereed user language setting, the UI language is based on user setting',
283
    );
284
    $this->runTest($test);
285
286
287
288
289

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

290
291
292
293
294
295
296
    $test = array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER_ADMIN, LANGUAGE_NEGOTIATION_SELECTED),
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED,
      'http_header' => array(),
      'message' => 'USER ADMIN > DEFAULT: no preferred user admin language setting, the UI language is default',
297
    );
298
    $this->runTest($test);
299
300
301
302
303

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

304
305
306
307
308
309
310
    $test = array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER_ADMIN, LANGUAGE_NEGOTIATION_SELECTED),
      'path' => 'admin/config',
      'expect' => $default_string,
      'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED,
      'http_header' => array(),
      'message' => 'USER ADMIN > DEFAULT: invalid preferred user admin language setting, the UI language is default',
311
    );
312
    $this->runTest($test);
313
314
315
316
317

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

318
319
320
321
322
323
324
    $test = array(
      'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER_ADMIN, LANGUAGE_NEGOTIATION_SELECTED),
      'path' => 'admin/config',
      'expect' => $language_string,
      'expected_method_id' => LANGUAGE_NEGOTIATION_USER_ADMIN,
      'http_header' => array(),
      'message' => 'USER ADMIN > DEFAULT: defined prefereed user admin language setting, the UI language is based on user setting',
325
    );
326
    $this->runTest($test);
327

328
329
330
331
332
333
334
335
336
    // Setup for domain negotiation, first configure the language to have domain
    // URL.
    $edit = array("domain[$langcode]" => $language_domain);
    $this->drupalPost("admin/config/regional/language/detection/url", $edit, t('Save configuration'));
    // Set the site to use domain language negotiation.

    $tests = array(
      // Default domain, browser preference should have no influence.
      array(
337
        'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_SELECTED),
338
339
340
        'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN,
        'path' => 'admin/config',
        'expect' => $default_string,
341
        'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED,
342
343
344
345
        'http_header' => $http_header_browser_fallback,
        'message' => 'URL (DOMAIN) > DEFAULT: default domain should get default language',
      ),
      // Language domain specific URL, we set the $_SERVER['HTTP_HOST'] in
346
      // \Drupal\language_test\LanguageTestManager to simulate this.
347
      array(
348
        'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_SELECTED),
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
        'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN,
        'language_test_domain' => $language_domain . ':88',
        'path' => 'admin/config',
        'expect' => $language_string,
        'expected_method_id' => LANGUAGE_NEGOTIATION_URL,
        'http_header' => $http_header_browser_fallback,
        'message' => 'URL (DOMAIN) > DEFAULT: domain example.cn should switch to Chinese',
      ),
    );

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

  protected function runTest($test) {
    if (!empty($test['language_negotiation'])) {
      $method_weights = array_flip($test['language_negotiation']);
      language_negotiation_set(LANGUAGE_TYPE_INTERFACE, $method_weights);
    }
    if (!empty($test['language_negotiation_url_part'])) {
370
371
372
      config('language.negotiation')
        ->set('url.source', $test['language_negotiation_url_part'])
        ->save();
373
374
    }
    if (!empty($test['language_test_domain'])) {
375
      state()->set('language_test.domain', $test['language_test_domain']);
376
    }
377
    $this->container->get('language_manager')->reset();
378
379
380
381
382
383
384
385
386
387
388
    $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';
389
    $language = new Language(array(
390
      'langcode' => $langcode_browser_fallback,
391
    ));
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
    language_save($language);
    $languages = language_list();

    // Enable the path prefix for the default language: this way any unprefixed
    // URL must have a valid fallback value.
    $edit = array('prefix[en]' => 'en');
    $this->drupalPost('admin/config/regional/language/detection/url', $edit, t('Save configuration'));

    // 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,
    );
    $this->drupalPost('admin/config/regional/language/detection', $edit, t('Save settings'));
    $this->drupalGet('admin/config/regional/language/detection');

    // Enable the language switcher block.
411
    $this->drupalPlaceBlock('language_block:' . LANGUAGE_TYPE_INTERFACE, array('machine_name' => 'test_language_block'));
412
413
414
415

    // 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");
416
    $language = new Language(array('langcode' => ''));
417
418
419
420
    $this->drupalGet('', array('language' => $language), $http_header);

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

    // Check that URLs are rewritten using the given browser language.
    $fields = $this->xpath('//p[@id="site-name"]/strong/a[@rel="home" and @href=:url]', $args);
427
    $this->assertTrue($fields[0] == 'Drupal', 'URLs are rewritten using the browser language.');
428
429
430
431
432
433
434
435
  }

  /**
   * Tests url() when separate domains are used for multiple languages.
   */
  function testLanguageDomain() {
    // Add the Italian language.
    $langcode = 'it';
436
    $language = new Language(array(
437
      'langcode' => $langcode,
438
    ));
439
440
441
442
443
444
445
446
447
448
449
450
    language_save($language);
    $languages = language_list();

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

    // Change the domain for the Italian language.
    $edit = array(
451
      'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN,
452
453
454
455
456
457
458
459
460
461
462
      'domain[it]' => 'it.example.com',
    );
    $this->drupalPost('admin/config/regional/language/detection/url', $edit, t('Save configuration'));

    // Build the link we're going to test.
    $link = 'it.example.com/admin';

    // 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).
    $italian_url = url('admin', array('language' => $languages['it'], 'script' => ''));
463
    $url_scheme = $this->request->isSecure() ? 'https://' : 'http://';
464
    $correct_link = $url_scheme . $link;
465
    $this->assertTrue($italian_url == $correct_link, format_string('The url() function returns the right URL (@url) in accordance with the chosen language', array('@url' => $italian_url)));
466

467
    // Test HTTPS via options.
468
    $this->settingsSet('mixed_mode_sessions', TRUE);
469
470
    $italian_url = url('admin', array('https' => TRUE, 'language' => $languages['it'], 'script' => ''));
    $correct_link = 'https://' . $link;
471
    $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)));
472
    $this->settingsSet('mixed_mode_sessions', FALSE);
473

474
    // Test HTTPS via current URL scheme.
475
476
    $temp_https = $this->request->server->get('HTTPS');
    $this->request->server->set('HTTPS', 'on');
477
478
    $italian_url = url('admin', array('language' => $languages['it'], 'script' => ''));
    $correct_link = 'https://' . $link;
479
    $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)));
480
    $this->request->server->set('HTTPS', $temp_https);
481
482
  }
}