function LanguageBrowserDetectionAcceptLanguageTest::testAcceptLanguageEmptyDefault

Same name in other branches
  1. 9 core/modules/language/tests/src/Functional/LanguageBrowserDetectionAcceptLanguageTest.php \Drupal\Tests\language\Functional\LanguageBrowserDetectionAcceptLanguageTest::testAcceptLanguageEmptyDefault()
  2. 8.9.x core/modules/language/tests/src/Functional/LanguageBrowserDetectionAcceptLanguageTest.php \Drupal\Tests\language\Functional\LanguageBrowserDetectionAcceptLanguageTest::testAcceptLanguageEmptyDefault()
  3. 10 core/modules/language/tests/src/Functional/LanguageBrowserDetectionAcceptLanguageTest.php \Drupal\Tests\language\Functional\LanguageBrowserDetectionAcceptLanguageTest::testAcceptLanguageEmptyDefault()

Tests with browsers with and without Accept-Language header.

File

core/modules/language/tests/src/Functional/LanguageBrowserDetectionAcceptLanguageTest.php, line 77

Class

LanguageBrowserDetectionAcceptLanguageTest
Tests browser language detection with different accept-language headers.

Namespace

Drupal\Tests\language\Functional

Code

public function testAcceptLanguageEmptyDefault() : void {
    // Check correct headers.
    $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->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'UNCACHEABLE (response policy)');
    // Check with UK browser.
    $this->drupalGet('/system-test/echo/language test', [], [
        'Accept-Language' => 'en-UK,en',
    ]);
    $this->assertSession()
        ->responseHeaderEquals('Content-Language', 'en');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'UNCACHEABLE (response policy)');
    // Check with french browser.
    $this->drupalGet('/system-test/echo/language test', [], [
        'Accept-Language' => 'fr-FR,fr',
    ]);
    $this->assertSession()
        ->responseHeaderEquals('Content-Language', 'fr');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'UNCACHEABLE (response policy)');
    // Check with browser without language settings - should return fallback language.
    $this->drupalGet('/system-test/echo/language test', [], [
        'Accept-Language' => '',
    ]);
    $this->assertSession()
        ->responseHeaderEquals('Content-Language', 'en');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'UNCACHEABLE (response policy)');
    // Check with french browser again.
    $this->drupalGet('/system-test/echo/language test', [], [
        'Accept-Language' => 'fr-FR,fr',
    ]);
    $this->assertSession()
        ->responseHeaderEquals('Content-Language', 'fr');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'UNCACHEABLE (response policy)');
    // Check with UK browser.
    $this->drupalGet('/system-test/echo/language test', [], [
        'Accept-Language' => 'en-UK,en',
    ]);
    $this->assertSession()
        ->responseHeaderEquals('Content-Language', 'en');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache', 'UNCACHEABLE (response policy)');
    // Check if prefixed URLs are still cached.
    $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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.