Test URL language detection when the requested URL has no language.
function testUrlLanguageFallback() {
// Add the Italian language.
$langcode_browser_fallback = 'it';
$language = new Language(array(
'langcode' => $langcode_browser_fallback,
));
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.
$edit = array(
'blocks[language_language_interface][region]' => 'sidebar_first',
);
$this
->drupalPost('admin/structure/block', $edit, t('Save blocks'));
// 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",
);
$language = new Language(array(
'langcode' => '',
));
$this
->drupalGet('', array(
'language' => $language,
), $http_header);
// Check that the language switcher active link matches the given browser
// language.
$args = array(
':url' => base_path() . $GLOBALS['script_path'] . $langcode_browser_fallback,
);
$fields = $this
->xpath('//div[@id="block-language-language-interface"]//a[@class="language-link active" and starts-with(@href, :url)]', $args);
$this
->assertTrue($fields[0] == $languages[$langcode_browser_fallback]->name, 'The browser language is the URL active language');
// 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);
$this
->assertTrue($fields[0] == 'Drupal', 'URLs are rewritten using the browser language.');
}