View source
<?php
namespace Drupal\Tests\language_access\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language_access\Plugin\LanguageNegotiation\LanguageAccessNegotiationBrowser;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Symfony\Component\HttpFoundation\Request;
class LanguageAccessBrowserTest extends KernelTestBase {
use UserCreationTrait;
protected static $modules = [
'language_access',
'language',
'system',
'user',
];
protected $languageNegotiator;
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'user',
]);
$this
->installSchema('system', [
'sequences',
]);
$this
->installEntitySchema('user');
$this
->createUser();
$this->languageNegotiator = $this->container
->get('language_negotiator');
ConfigurableLanguage::createFromLangcode('nl')
->save();
ConfigurableLanguage::createFromLangcode('fr')
->save();
}
public function testBrowserLanguageNegotiation(string $expected, array $user_permissions, $http_accept_language) {
$this->languageNegotiator
->setCurrentUser($this
->createUser($user_permissions));
$browser_negotiation_method = $this->languageNegotiator
->getNegotiationMethodInstance('language-browser');
$this
->assertInstanceOf(LanguageAccessNegotiationBrowser::class, $browser_negotiation_method);
$request = new Request([], [], [], [], [], [
'HTTP_ACCEPT_LANGUAGE' => $http_accept_language,
]);
$this
->assertEquals($expected, $browser_negotiation_method
->getLangcode($request));
}
public function browserLanguageNegotiationDataProvider() {
return [
[
'nl',
[
'access language nl',
],
'nl-BE,fr-FR;q=0.5',
],
[
'fr',
[
'access language fr',
],
'nl-BE,fr-FR;q=0.5',
],
[
'nl',
[
'access language fr',
'access language nl',
],
'nl-BE,fr-FR;q=0.5',
],
];
}
}