Chris@0: default: Test that the URL prefix setting gets precedence over Chris@0: * the default language. The browser language preference does not have any Chris@0: * influence. Chris@0: * - URL (path) > browser > default: Test that the URL prefix setting gets Chris@0: * precedence over the browser language preference, which in turn gets Chris@0: * precedence over the default language. Chris@0: * - URL (domain) > default: Tests that the URL domain setting gets precedence Chris@0: * over the default language. Chris@0: * Chris@0: * The paths that are used for each of these, are: Chris@0: * - admin/config: Tests the UI using the precedence rules. Chris@0: * - zh-hans/admin/config: Tests the UI in Chinese. Chris@0: * - blah-blah/admin/config: Tests the 404 page. Chris@0: * Chris@0: * @group language Chris@0: */ Chris@0: class LanguageUILanguageNegotiationTest extends BrowserTestBase { Chris@0: Chris@0: /** Chris@17: * The admin user for testing. Chris@17: * Chris@17: * @var \Drupal\user\Entity\User Chris@17: */ Chris@17: protected $adminUser; Chris@17: Chris@17: /** Chris@0: * Modules to enable. Chris@0: * Chris@0: * We marginally use interface translation functionality here, so need to use Chris@0: * the locale module instead of language only, but the 90% of the test is Chris@0: * about the negotiation process which is solely in language module. Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: public static $modules = ['locale', 'language_test', 'block', 'user', 'content_translation']; Chris@0: Chris@0: protected function setUp() { Chris@0: parent::setUp(); Chris@0: Chris@17: $this->adminUser = $this->drupalCreateUser(['administer languages', 'translate interface', 'access administration pages', 'administer blocks']); Chris@17: $this->drupalLogin($this->adminUser); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests for language switching by URL path. Chris@0: */ Chris@0: public function testUILanguageNegotiation() { Chris@0: // A few languages to switch to. Chris@0: // This one is unknown, should get the default lang version. Chris@0: $langcode_unknown = 'blah-blah'; Chris@0: // For testing browser lang preference. Chris@0: $langcode_browser_fallback = 'vi'; Chris@0: // For testing path prefix. Chris@0: $langcode = 'zh-hans'; Chris@0: // For setting browser language preference to 'vi'. Chris@0: $http_header_browser_fallback = ["Accept-Language" => "$langcode_browser_fallback;q=1"]; Chris@0: // For setting browser language preference to some unknown. Chris@0: $http_header_blah = ["Accept-Language" => "blah;q=1"]; Chris@0: Chris@17: // Create a private file for testing accessible by the admin user. Chris@18: \Drupal::service('file_system')->mkdir($this->privateFilesDirectory . '/test'); Chris@17: $filepath = 'private://test/private-file-test.txt'; Chris@17: $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data."; Chris@17: file_put_contents($filepath, $contents); Chris@17: $file = File::create([ Chris@17: 'uri' => $filepath, Chris@17: 'uid' => $this->adminUser->id(), Chris@17: ]); Chris@17: $file->save(); Chris@17: Chris@0: // Setup the site languages by installing two languages. Chris@0: // Set the default language in order for the translated string to be registered Chris@0: // into database when seen by t(). Without doing this, our target string Chris@0: // is for some reason not found when doing translate search. This might Chris@0: // be some bug. Chris@0: $default_language = \Drupal::languageManager()->getDefaultLanguage(); Chris@0: ConfigurableLanguage::createFromLangcode($langcode_browser_fallback)->save(); Chris@0: $this->config('system.site')->set('default_langcode', $langcode_browser_fallback)->save(); Chris@0: ConfigurableLanguage::createFromLangcode($langcode)->save(); Chris@0: Chris@0: // We will look for this string in the admin/config screen to see if the Chris@0: // corresponding translated string is shown. Chris@0: $default_string = 'Hide descriptions'; Chris@0: Chris@0: // First visit this page to make sure our target string is searchable. Chris@0: $this->drupalGet('admin/config'); Chris@0: Chris@0: // Now the t()'ed string is in db so switch the language back to default. Chris@0: // This will rebuild the container so we need to rebuild the container in Chris@0: // the test environment. Chris@0: $this->config('system.site')->set('default_langcode', $default_language->getId())->save(); Chris@0: $this->config('language.negotiation')->set('url.prefixes.en', '')->save(); Chris@0: $this->rebuildContainer(); Chris@0: Chris@0: // Translate the string. Chris@0: $language_browser_fallback_string = "In $langcode_browser_fallback In $langcode_browser_fallback In $langcode_browser_fallback"; Chris@0: $language_string = "In $langcode In $langcode In $langcode"; Chris@0: // Do a translate search of our target string. Chris@0: $search = [ Chris@0: 'string' => $default_string, Chris@0: 'langcode' => $langcode_browser_fallback, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); Chris@0: $textarea = current($this->xpath('//textarea')); Chris@0: $lid = $textarea->getAttribute('name'); Chris@0: $edit = [ Chris@0: $lid => $language_browser_fallback_string, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations')); Chris@0: Chris@0: $search = [ Chris@0: 'string' => $default_string, Chris@0: 'langcode' => $langcode, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); Chris@0: $textarea = current($this->xpath('//textarea')); Chris@0: $lid = $textarea->getAttribute('name'); Chris@0: $edit = [ Chris@0: $lid => $language_string, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations')); Chris@0: Chris@0: // Configure selected language negotiation to use zh-hans. Chris@0: $edit = ['selected_langcode' => $langcode]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection/selected', $edit, t('Save configuration')); Chris@0: $test = [ Chris@0: 'language_negotiation' => [LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $language_string, Chris@0: 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, Chris@0: 'http_header' => $http_header_browser_fallback, Chris@0: 'message' => 'SELECTED: UI language is switched based on selected language.', Chris@0: ]; Chris@0: $this->doRunTest($test); Chris@0: Chris@0: // An invalid language is selected. Chris@0: $this->config('language.negotiation')->set('selected_langcode', NULL)->save(); Chris@0: $test = [ Chris@0: 'language_negotiation' => [LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $default_string, Chris@0: 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, Chris@0: 'http_header' => $http_header_browser_fallback, Chris@0: 'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.', Chris@0: ]; Chris@0: $this->doRunTest($test); Chris@0: Chris@0: // No selected language is available. Chris@0: $this->config('language.negotiation')->set('selected_langcode', $langcode_unknown)->save(); Chris@0: $test = [ Chris@0: 'language_negotiation' => [LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $default_string, Chris@0: 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, Chris@0: 'http_header' => $http_header_browser_fallback, Chris@0: 'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.', Chris@0: ]; Chris@0: $this->doRunTest($test); Chris@0: Chris@0: $tests = [ Chris@0: // Default, browser preference should have no influence. Chris@0: [ Chris@0: 'language_negotiation' => [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $default_string, Chris@0: 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, Chris@0: 'http_header' => $http_header_browser_fallback, Chris@0: 'message' => 'URL (PATH) > DEFAULT: no language prefix, UI language is default and the browser language preference setting is not used.', Chris@0: ], Chris@0: // Language prefix. Chris@0: [ Chris@0: 'language_negotiation' => [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => "$langcode/admin/config", Chris@0: 'expect' => $language_string, Chris@0: 'expected_method_id' => LanguageNegotiationUrl::METHOD_ID, Chris@0: 'http_header' => $http_header_browser_fallback, Chris@0: 'message' => 'URL (PATH) > DEFAULT: with language prefix, UI language is switched based on path prefix', Chris@0: ], Chris@0: // Default, go by browser preference. Chris@0: [ Chris@0: 'language_negotiation' => [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $language_browser_fallback_string, Chris@0: 'expected_method_id' => LanguageNegotiationBrowser::METHOD_ID, Chris@0: 'http_header' => $http_header_browser_fallback, Chris@0: 'message' => 'URL (PATH) > BROWSER: no language prefix, UI language is determined by browser language preference', Chris@0: ], Chris@0: // Prefix, switch to the language. Chris@0: [ Chris@0: 'language_negotiation' => [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID], Chris@0: 'path' => "$langcode/admin/config", Chris@0: 'expect' => $language_string, Chris@0: 'expected_method_id' => LanguageNegotiationUrl::METHOD_ID, Chris@0: 'http_header' => $http_header_browser_fallback, Chris@0: 'message' => 'URL (PATH) > BROWSER: with language prefix, UI language is based on path prefix', Chris@0: ], Chris@0: // Default, browser language preference is not one of site's lang. Chris@0: [ Chris@0: 'language_negotiation' => [LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $default_string, Chris@0: 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, Chris@0: 'http_header' => $http_header_blah, Chris@0: 'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language', Chris@0: ], Chris@0: ]; Chris@0: Chris@0: foreach ($tests as $test) { Chris@0: $this->doRunTest($test); Chris@0: } Chris@0: Chris@0: // Unknown language prefix should return 404. Chris@0: $definitions = \Drupal::languageManager()->getNegotiator()->getNegotiationMethods(); Chris@0: // Enable only methods, which are either not limited to a specific language Chris@0: // type or are supporting the interface language type. Chris@0: $language_interface_method_definitions = array_filter($definitions, function ($method_definition) { Chris@0: return !isset($method_definition['types']) || (isset($method_definition['types']) && in_array(LanguageInterface::TYPE_INTERFACE, $method_definition['types'])); Chris@0: }); Chris@0: $this->config('language.types') Chris@0: ->set('negotiation.' . LanguageInterface::TYPE_INTERFACE . '.enabled', array_flip(array_keys($language_interface_method_definitions))) Chris@0: ->save(); Chris@0: $this->drupalGet("$langcode_unknown/admin/config", [], $http_header_browser_fallback); Chris@0: $this->assertResponse(404, "Unknown language path prefix should return 404"); Chris@0: Chris@0: // Set preferred langcode for user to NULL. Chris@0: $account = $this->loggedInUser; Chris@0: $account->preferred_langcode = NULL; Chris@0: $account->save(); Chris@0: Chris@0: $test = [ Chris@0: 'language_negotiation' => [LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $default_string, Chris@0: 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, Chris@0: 'http_header' => [], Chris@0: 'message' => 'USER > DEFAULT: no preferred user language setting, the UI language is default', Chris@0: ]; Chris@0: $this->doRunTest($test); Chris@0: Chris@17: // Set preferred langcode for user to default langcode. Chris@17: $account = $this->loggedInUser; Chris@17: $account->preferred_langcode = $default_language->getId(); Chris@17: $account->save(); Chris@17: Chris@17: $test = [ Chris@17: 'language_negotiation' => [LanguageNegotiationUser::METHOD_ID, LanguageNegotiationUrl::METHOD_ID], Chris@17: 'path' => "$langcode/admin/config", Chris@17: 'expect' => $default_string, Chris@17: 'expected_method_id' => LanguageNegotiationUser::METHOD_ID, Chris@17: 'http_header' => [], Chris@17: 'message' => 'USER > URL: User has default language as preferred user language setting, the UI language is default', Chris@17: ]; Chris@17: $this->doRunTest($test); Chris@17: Chris@0: // Set preferred langcode for user to unknown language. Chris@0: $account = $this->loggedInUser; Chris@0: $account->preferred_langcode = $langcode_unknown; Chris@0: $account->save(); Chris@0: Chris@0: $test = [ Chris@0: 'language_negotiation' => [LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $default_string, Chris@0: 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, Chris@0: 'http_header' => [], Chris@0: 'message' => 'USER > DEFAULT: invalid preferred user language setting, the UI language is default', Chris@0: ]; Chris@0: $this->doRunTest($test); Chris@0: Chris@0: // Set preferred langcode for user to non default. Chris@0: $account->preferred_langcode = $langcode; Chris@0: $account->save(); Chris@0: Chris@0: $test = [ Chris@0: 'language_negotiation' => [LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $language_string, Chris@0: 'expected_method_id' => LanguageNegotiationUser::METHOD_ID, Chris@0: 'http_header' => [], Chris@0: 'message' => 'USER > DEFAULT: defined preferred user language setting, the UI language is based on user setting', Chris@0: ]; Chris@0: $this->doRunTest($test); Chris@0: Chris@0: // Set preferred admin langcode for user to NULL. Chris@0: $account->preferred_admin_langcode = NULL; Chris@0: $account->save(); Chris@0: Chris@0: $test = [ Chris@0: 'language_negotiation' => [LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $default_string, Chris@0: 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, Chris@0: 'http_header' => [], Chris@0: 'message' => 'USER ADMIN > DEFAULT: no preferred user admin language setting, the UI language is default', Chris@0: ]; Chris@0: $this->doRunTest($test); Chris@0: Chris@0: // Set preferred admin langcode for user to unknown language. Chris@0: $account->preferred_admin_langcode = $langcode_unknown; Chris@0: $account->save(); Chris@0: Chris@0: $test = [ Chris@0: 'language_negotiation' => [LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $default_string, Chris@0: 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, Chris@0: 'http_header' => [], Chris@0: 'message' => 'USER ADMIN > DEFAULT: invalid preferred user admin language setting, the UI language is default', Chris@0: ]; Chris@0: $this->doRunTest($test); Chris@0: Chris@0: // Set preferred admin langcode for user to non default. Chris@0: $account->preferred_admin_langcode = $langcode; Chris@0: $account->save(); Chris@0: Chris@0: $test = [ Chris@0: 'language_negotiation' => [LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'expect' => $language_string, Chris@0: 'expected_method_id' => LanguageNegotiationUserAdmin::METHOD_ID, Chris@0: 'http_header' => [], Chris@0: 'message' => 'USER ADMIN > DEFAULT: defined preferred user admin language setting, the UI language is based on user setting', Chris@0: ]; Chris@0: $this->doRunTest($test); Chris@0: Chris@0: // Go by session preference. Chris@0: $language_negotiation_session_param = $this->randomMachineName(); Chris@0: $edit = ['language_negotiation_session_param' => $language_negotiation_session_param]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection/session', $edit, t('Save configuration')); Chris@0: $tests = [ Chris@0: [ Chris@0: 'language_negotiation' => [LanguageNegotiationSession::METHOD_ID], Chris@0: 'path' => "admin/config", Chris@0: 'expect' => $default_string, Chris@0: 'expected_method_id' => LanguageNegotiatorInterface::METHOD_ID, Chris@0: 'http_header' => $http_header_browser_fallback, Chris@0: 'message' => 'SESSION > DEFAULT: no language given, the UI language is default', Chris@0: ], Chris@0: [ Chris@0: 'language_negotiation' => [LanguageNegotiationSession::METHOD_ID], Chris@0: 'path' => 'admin/config', Chris@0: 'path_options' => ['query' => [$language_negotiation_session_param => $langcode]], Chris@0: 'expect' => $language_string, Chris@0: 'expected_method_id' => LanguageNegotiationSession::METHOD_ID, Chris@0: 'http_header' => $http_header_browser_fallback, Chris@0: 'message' => 'SESSION > DEFAULT: language given, UI language is determined by session language preference', Chris@0: ], Chris@0: ]; Chris@0: foreach ($tests as $test) { Chris@0: $this->doRunTest($test); Chris@0: } Chris@0: } Chris@0: Chris@0: protected function doRunTest($test) { Chris@0: $test += ['path_options' => []]; Chris@0: if (!empty($test['language_negotiation'])) { Chris@0: $method_weights = array_flip($test['language_negotiation']); Chris@0: $this->container->get('language_negotiator')->saveConfiguration(LanguageInterface::TYPE_INTERFACE, $method_weights); Chris@0: } Chris@0: if (!empty($test['language_negotiation_url_part'])) { Chris@0: $this->config('language.negotiation') Chris@0: ->set('url.source', $test['language_negotiation_url_part']) Chris@0: ->save(); Chris@0: } Chris@0: if (!empty($test['language_test_domain'])) { Chris@0: \Drupal::state()->set('language_test.domain', $test['language_test_domain']); Chris@0: } Chris@0: $this->container->get('language_manager')->reset(); Chris@0: $this->drupalGet($test['path'], $test['path_options'], $test['http_header']); Chris@0: $this->assertText($test['expect'], $test['message']); Chris@0: $this->assertText(t('Language negotiation method: @name', ['@name' => $test['expected_method_id']])); Chris@17: Chris@17: // Get the private file and ensure it is a 200. It is important to Chris@17: // invalidate the router cache to ensure the routing system runs a full Chris@17: // match. Chris@17: Cache::invalidateTags(['route_match']); Chris@17: $this->drupalGet('system/files/test/private-file-test.txt'); Chris@17: $this->assertResponse(200); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test URL language detection when the requested URL has no language. Chris@0: */ Chris@0: public function testUrlLanguageFallback() { Chris@0: // Add the Italian language. Chris@0: $langcode_browser_fallback = 'it'; Chris@0: ConfigurableLanguage::createFromLangcode($langcode_browser_fallback)->save(); Chris@0: $languages = $this->container->get('language_manager')->getLanguages(); Chris@0: Chris@0: // Enable the path prefix for the default language: this way any unprefixed Chris@0: // URL must have a valid fallback value. Chris@0: $edit = ['prefix[en]' => 'en']; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); Chris@0: Chris@0: // Enable browser and URL language detection. Chris@0: $edit = [ Chris@0: 'language_interface[enabled][language-browser]' => TRUE, Chris@0: 'language_interface[enabled][language-url]' => TRUE, Chris@0: 'language_interface[weight][language-browser]' => -8, Chris@0: 'language_interface[weight][language-url]' => -10, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: $this->drupalGet('admin/config/regional/language/detection'); Chris@0: Chris@0: // Enable the language switcher block. Chris@0: $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, ['id' => 'test_language_block']); Chris@0: Chris@0: // Log out, because for anonymous users, the "active" class is set by PHP Chris@0: // (which means we can easily test it here), whereas for authenticated users Chris@0: // it is set by JavaScript. Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Place a site branding block in the header region. Chris@0: $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']); Chris@0: Chris@0: // Access the front page without specifying any valid URL language prefix Chris@0: // and having as browser language preference a non-default language. Chris@0: $http_header = ["Accept-Language" => "$langcode_browser_fallback;q=1"]; Chris@0: $language = new Language(['id' => '']); Chris@0: $this->drupalGet('', ['language' => $language], $http_header); Chris@0: Chris@0: // Check that the language switcher active link matches the given browser Chris@0: // language. Chris@18: $args = [':id' => 'block-test-language-block', ':url' => Url::fromRoute('')->toString() . $langcode_browser_fallback]; Chris@0: $fields = $this->xpath('//div[@id=:id]//a[@class="language-link is-active" and starts-with(@href, :url)]', $args); Chris@0: $this->assertSame($fields[0]->getText(), $languages[$langcode_browser_fallback]->getName(), 'The browser language is the URL active language'); Chris@0: Chris@0: // Check that URLs are rewritten using the given browser language. Chris@0: $fields = $this->xpath('//div[@class="site-name"]/a[@rel="home" and @href=:url]', $args); Chris@0: $this->assertSame($fields[0]->getText(), 'Drupal', 'URLs are rewritten using the browser language.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests URL handling when separate domains are used for multiple languages. Chris@0: */ Chris@0: public function testLanguageDomain() { Chris@0: global $base_url; Chris@0: Chris@0: // Get the current host URI we're running on. Chris@0: $base_url_host = parse_url($base_url, PHP_URL_HOST); Chris@0: Chris@0: // Add the Italian language. Chris@0: ConfigurableLanguage::createFromLangcode('it')->save(); Chris@0: Chris@0: $languages = $this->container->get('language_manager')->getLanguages(); Chris@0: Chris@0: // Enable browser and URL language detection. Chris@0: $edit = [ Chris@0: 'language_interface[enabled][language-url]' => TRUE, Chris@0: 'language_interface[weight][language-url]' => -10, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: Chris@0: // Do not allow blank domain. Chris@0: $edit = [ Chris@0: 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN, Chris@0: 'domain[en]' => '', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); Chris@0: $this->assertText('The domain may not be left blank for English', 'The form does not allow blank domains.'); Chris@0: $this->rebuildContainer(); Chris@0: Chris@0: // Change the domain for the Italian language. Chris@0: $edit = [ Chris@0: 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN, Chris@0: 'domain[en]' => $base_url_host, Chris@0: 'domain[it]' => 'it.example.com', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); Chris@0: $this->assertText('The configuration options have been saved', 'Domain configuration is saved.'); Chris@0: $this->rebuildContainer(); Chris@0: Chris@0: // Try to use an invalid domain. Chris@0: $edit = [ Chris@0: 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN, Chris@0: 'domain[en]' => $base_url_host, Chris@0: 'domain[it]' => 'it.example.com/', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); Chris@0: $this->assertRaw(t('The domain for %language may only contain the domain name, not a trailing slash, protocol and/or port.', ['%language' => 'Italian'])); Chris@0: Chris@0: // Build the link we're going to test. Chris@0: $link = 'it.example.com' . rtrim(base_path(), '/') . '/admin'; Chris@0: Chris@0: // Test URL in another language: http://it.example.com/admin. Chris@0: // Base path gives problems on the testbot, so $correct_link is hard-coded. Chris@0: // @see UrlAlterFunctionalTest::assertUrlOutboundAlter (path.test). Chris@0: $italian_url = Url::fromRoute('system.admin', [], ['language' => $languages['it']])->toString(); Chris@0: $url_scheme = \Drupal::request()->isSecure() ? 'https://' : 'http://'; Chris@0: $correct_link = $url_scheme . $link; Chris@0: $this->assertEqual($italian_url, $correct_link, format_string('The right URL (@url) in accordance with the chosen language', ['@url' => $italian_url])); Chris@0: Chris@0: // Test HTTPS via options. Chris@0: $italian_url = Url::fromRoute('system.admin', [], ['https' => TRUE, 'language' => $languages['it']])->toString(); Chris@0: $correct_link = 'https://' . $link; Chris@0: $this->assertTrue($italian_url == $correct_link, format_string('The right HTTPS URL (via options) (@url) in accordance with the chosen language', ['@url' => $italian_url])); Chris@0: Chris@0: // Test HTTPS via current URL scheme. Chris@0: $request = Request::create('', 'GET', [], [], [], ['HTTPS' => 'on']); Chris@0: $this->container->get('request_stack')->push($request); Chris@0: $italian_url = Url::fromRoute('system.admin', [], ['language' => $languages['it']])->toString(); Chris@0: $correct_link = 'https://' . $link; Chris@0: $this->assertTrue($italian_url == $correct_link, format_string('The right URL (via current URL scheme) (@url) in accordance with the chosen language', ['@url' => $italian_url])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests persistence of negotiation settings for the content language type. Chris@0: */ Chris@0: public function testContentCustomization() { Chris@0: // Customize content language settings from their defaults. Chris@0: $edit = [ Chris@0: 'language_content[configurable]' => TRUE, Chris@0: 'language_content[enabled][language-url]' => FALSE, Chris@0: 'language_content[enabled][language-session]' => TRUE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: Chris@0: // Check if configurability persisted. Chris@0: $config = $this->config('language.types'); Chris@0: $this->assertTrue(in_array('language_interface', $config->get('configurable')), 'Interface language is configurable.'); Chris@0: $this->assertTrue(in_array('language_content', $config->get('configurable')), 'Content language is configurable.'); Chris@0: Chris@0: // Ensure configuration was saved. Chris@0: $this->assertFalse(array_key_exists('language-url', $config->get('negotiation.language_content.enabled')), 'URL negotiation is not enabled for content.'); Chris@0: $this->assertTrue(array_key_exists('language-session', $config->get('negotiation.language_content.enabled')), 'Session negotiation is enabled for content.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests if the language switcher block gets deleted when a language type has been made not configurable. Chris@0: */ Chris@0: public function testDisableLanguageSwitcher() { Chris@0: $block_id = 'test_language_block'; Chris@0: Chris@0: // Enable the language switcher block. Chris@0: $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_CONTENT, ['id' => $block_id]); Chris@0: Chris@0: // Check if the language switcher block has been created. Chris@0: $block = Block::load($block_id); Chris@0: $this->assertTrue($block, 'Language switcher block was created.'); Chris@0: Chris@0: // Make sure language_content is not configurable. Chris@0: $edit = [ Chris@0: 'language_content[configurable]' => FALSE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Check if the language switcher block has been removed. Chris@0: $block = Block::load($block_id); Chris@0: $this->assertFalse($block, 'Language switcher block was removed.'); Chris@0: } Chris@0: Chris@0: }