View source
<?php
namespace Drupal\Tests\language\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
class LanguageBrowserDetectionAcceptLanguageTest extends BrowserTestBase {
public static $modules = [
'language',
'locale',
'content_translation',
'system_test',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$admin = $this
->drupalCreateUser([], NULL, TRUE);
$this
->drupalLogin($admin);
ConfigurableLanguage::createFromLangcode('fr')
->save();
$this
->drupalPostForm('/admin/config/regional/language/detection', [
'language_interface[enabled][language-url]' => TRUE,
'language_interface[enabled][language-browser]' => TRUE,
'language_interface[enabled][language-selected]' => TRUE,
], 'Save settings');
$this
->drupalPostForm('/admin/config/regional/language/detection/url', [
'prefix[en]' => 'en',
'prefix[fr]' => 'fr',
], 'Save configuration');
$this
->drupalPostForm('/admin/config/regional/language/detection/browser', [
'new_mapping[browser_langcode]' => 'fr',
'new_mapping[drupal_langcode]' => 'fr',
], 'Save configuration');
$this
->drupalPostForm('/admin/config/regional/language/detection/browser', [
'new_mapping[browser_langcode]' => 'en',
'new_mapping[drupal_langcode]' => 'en',
], 'Save configuration');
$this
->drupalPostForm('/admin/config/regional/language/detection/selected', [
'edit-selected-langcode' => 'en',
], 'Save configuration');
$this
->drupalLogout();
}
public function testAcceptLanguageEmptyDefault() {
$this
->drupalGet('/en/system-test/echo/language test', [], [
'Accept-Language' => 'en',
]);
$this
->assertSession()
->responseHeaderEquals('Content-Language', 'en');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->drupalGet('/fr/system-test/echo/language test', [], [
'Accept-Language' => 'en',
]);
$this
->assertSession()
->responseHeaderEquals('Content-Language', 'fr');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->drupalGet('/system-test/echo/language test', [], [
'Accept-Language' => 'en',
]);
$this
->assertSession()
->responseHeaderEquals('Content-Language', 'en');
$this
->assertNull($this
->drupalGetHeader('X-Drupal-Cache'));
$this
->drupalGet('/system-test/echo/language test', [], [
'Accept-Language' => 'en-UK,en',
]);
$this
->assertSession()
->responseHeaderEquals('Content-Language', 'en');
$this
->assertNull($this
->drupalGetHeader('X-Drupal-Cache'));
$this
->drupalGet('/system-test/echo/language test', [], [
'Accept-Language' => 'fr-FR,fr',
]);
$this
->assertSession()
->responseHeaderEquals('Content-Language', 'fr');
$this
->assertNull($this
->drupalGetHeader('X-Drupal-Cache'));
$this
->drupalGet('/system-test/echo/language test', [], [
'Accept-Language' => NULL,
]);
$this
->assertSession()
->responseHeaderEquals('Content-Language', 'en');
$this
->assertNull($this
->drupalGetHeader('X-Drupal-Cache'));
$this
->drupalGet('/system-test/echo/language test', [], [
'Accept-Language' => 'fr-FR,fr',
]);
$this
->assertSession()
->responseHeaderEquals('Content-Language', 'fr');
$this
->assertNull($this
->drupalGetHeader('X-Drupal-Cache'));
$this
->drupalGet('/system-test/echo/language test', [], [
'Accept-Language' => 'en-UK,en',
]);
$this
->assertSession()
->responseHeaderEquals('Content-Language', 'en');
$this
->assertNull($this
->drupalGetHeader('X-Drupal-Cache'));
$this
->drupalGet('/en/system-test/echo/language test', [], [
'Accept-Language' => 'en',
]);
$this
->assertSession()
->responseHeaderEquals('Content-Language', 'en');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$this
->drupalGet('/fr/system-test/echo/language test', [], [
'Accept-Language' => 'en',
]);
$this
->assertSession()
->responseHeaderEquals('Content-Language', 'fr');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
}
}