comparison core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\language\Functional; 3 namespace Drupal\Tests\language\Functional;
4 4
5 use Drupal\Core\Cache\Cache;
5 use Drupal\Core\Url; 6 use Drupal\Core\Url;
7 use Drupal\file\Entity\File;
6 use Drupal\language\Entity\ConfigurableLanguage; 8 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser; 9 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser;
8 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected; 10 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
9 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession; 11 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession;
10 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; 12 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
38 * @group language 40 * @group language
39 */ 41 */
40 class LanguageUILanguageNegotiationTest extends BrowserTestBase { 42 class LanguageUILanguageNegotiationTest extends BrowserTestBase {
41 43
42 /** 44 /**
45 * The admin user for testing.
46 *
47 * @var \Drupal\user\Entity\User
48 */
49 protected $adminUser;
50
51 /**
43 * Modules to enable. 52 * Modules to enable.
44 * 53 *
45 * We marginally use interface translation functionality here, so need to use 54 * We marginally use interface translation functionality here, so need to use
46 * the locale module instead of language only, but the 90% of the test is 55 * the locale module instead of language only, but the 90% of the test is
47 * about the negotiation process which is solely in language module. 56 * about the negotiation process which is solely in language module.
51 public static $modules = ['locale', 'language_test', 'block', 'user', 'content_translation']; 60 public static $modules = ['locale', 'language_test', 'block', 'user', 'content_translation'];
52 61
53 protected function setUp() { 62 protected function setUp() {
54 parent::setUp(); 63 parent::setUp();
55 64
56 $admin_user = $this->drupalCreateUser(['administer languages', 'translate interface', 'access administration pages', 'administer blocks']); 65 $this->adminUser = $this->drupalCreateUser(['administer languages', 'translate interface', 'access administration pages', 'administer blocks']);
57 $this->drupalLogin($admin_user); 66 $this->drupalLogin($this->adminUser);
58 } 67 }
59 68
60 /** 69 /**
61 * Tests for language switching by URL path. 70 * Tests for language switching by URL path.
62 */ 71 */
71 // For setting browser language preference to 'vi'. 80 // For setting browser language preference to 'vi'.
72 $http_header_browser_fallback = ["Accept-Language" => "$langcode_browser_fallback;q=1"]; 81 $http_header_browser_fallback = ["Accept-Language" => "$langcode_browser_fallback;q=1"];
73 // For setting browser language preference to some unknown. 82 // For setting browser language preference to some unknown.
74 $http_header_blah = ["Accept-Language" => "blah;q=1"]; 83 $http_header_blah = ["Accept-Language" => "blah;q=1"];
75 84
85 // Create a private file for testing accessible by the admin user.
86 drupal_mkdir($this->privateFilesDirectory . '/test');
87 $filepath = 'private://test/private-file-test.txt';
88 $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data.";
89 file_put_contents($filepath, $contents);
90 $file = File::create([
91 'uri' => $filepath,
92 'uid' => $this->adminUser->id(),
93 ]);
94 $file->save();
95
76 // Setup the site languages by installing two languages. 96 // Setup the site languages by installing two languages.
77 // Set the default language in order for the translated string to be registered 97 // Set the default language in order for the translated string to be registered
78 // into database when seen by t(). Without doing this, our target string 98 // into database when seen by t(). Without doing this, our target string
79 // is for some reason not found when doing translate search. This might 99 // is for some reason not found when doing translate search. This might
80 // be some bug. 100 // be some bug.
237 'path' => 'admin/config', 257 'path' => 'admin/config',
238 'expect' => $default_string, 258 'expect' => $default_string,
239 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, 259 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID,
240 'http_header' => [], 260 'http_header' => [],
241 'message' => 'USER > DEFAULT: no preferred user language setting, the UI language is default', 261 'message' => 'USER > DEFAULT: no preferred user language setting, the UI language is default',
262 ];
263 $this->doRunTest($test);
264
265 // Set preferred langcode for user to default langcode.
266 $account = $this->loggedInUser;
267 $account->preferred_langcode = $default_language->getId();
268 $account->save();
269
270 $test = [
271 'language_negotiation' => [LanguageNegotiationUser::METHOD_ID, LanguageNegotiationUrl::METHOD_ID],
272 'path' => "$langcode/admin/config",
273 'expect' => $default_string,
274 'expected_method_id' => LanguageNegotiationUser::METHOD_ID,
275 'http_header' => [],
276 'message' => 'USER > URL: User has default language as preferred user language setting, the UI language is default',
242 ]; 277 ];
243 $this->doRunTest($test); 278 $this->doRunTest($test);
244 279
245 // Set preferred langcode for user to unknown language. 280 // Set preferred langcode for user to unknown language.
246 $account = $this->loggedInUser; 281 $account = $this->loggedInUser;
357 } 392 }
358 $this->container->get('language_manager')->reset(); 393 $this->container->get('language_manager')->reset();
359 $this->drupalGet($test['path'], $test['path_options'], $test['http_header']); 394 $this->drupalGet($test['path'], $test['path_options'], $test['http_header']);
360 $this->assertText($test['expect'], $test['message']); 395 $this->assertText($test['expect'], $test['message']);
361 $this->assertText(t('Language negotiation method: @name', ['@name' => $test['expected_method_id']])); 396 $this->assertText(t('Language negotiation method: @name', ['@name' => $test['expected_method_id']]));
397
398 // Get the private file and ensure it is a 200. It is important to
399 // invalidate the router cache to ensure the routing system runs a full
400 // match.
401 Cache::invalidateTags(['route_match']);
402 $this->drupalGet('system/files/test/private-file-test.txt');
403 $this->assertResponse(200);
362 } 404 }
363 405
364 /** 406 /**
365 * Test URL language detection when the requested URL has no language. 407 * Test URL language detection when the requested URL has no language.
366 */ 408 */