View source
<?php
namespace Drupal\Tests\language\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationContentEntity;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
class LanguageNegotiationContentEntityTest extends BrowserTestBase {
protected static $modules = [
'language',
'language_test',
'entity_test',
'system',
];
protected $defaultTheme = 'stark';
protected $entity;
protected function setUp() : void {
parent::setUp();
ConfigurableLanguage::create([
'id' => 'es',
])
->save();
ConfigurableLanguage::create([
'id' => 'fr',
])
->save();
$this
->rebuildContainer();
$this
->createTranslatableEntity();
$user = $this
->drupalCreateUser([
'view test entity',
]);
$this
->drupalLogin($user);
}
public function testDefaultConfiguration() {
$translation = $this->entity;
$this
->drupalGet($translation
->toUrl());
$last = $this->container
->get('state')
->get('language_test.language_negotiation_last');
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
$last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
$this
->assertSame($last_content_language, $last_interface_language);
$this
->assertSame($translation
->language()
->getId(), $last_content_language);
$translation = $this->entity
->getTranslation('es');
$this
->drupalGet($translation
->toUrl());
$last = $this->container
->get('state')
->get('language_test.language_negotiation_last');
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
$last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
$this
->assertSame($last_content_language, $last_interface_language);
$this
->assertSame($translation
->language()
->getId(), $last_content_language);
$translation = $this->entity
->getTranslation('fr');
$this
->drupalGet($translation
->toUrl());
$last = $this->container
->get('state')
->get('language_test.language_negotiation_last');
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
$last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
$this
->assertSame($last_content_language, $last_interface_language);
$this
->assertSame($translation
->language()
->getId(), $last_content_language);
}
public function testEnabledLanguageContentNegotiator() {
$config = $this
->config('language.types');
$config
->set('configurable', [
LanguageInterface::TYPE_INTERFACE,
LanguageInterface::TYPE_CONTENT,
]);
$config
->set('negotiation.language_content.enabled', [
LanguageNegotiationUrl::METHOD_ID => 0,
LanguageNegotiationContentEntity::METHOD_ID => 1,
]);
$config
->save();
$this
->rebuildContainer();
$this
->testDefaultConfiguration();
$config
->set('negotiation.language_content.enabled', [
LanguageNegotiationContentEntity::METHOD_ID => 0,
LanguageNegotiationUrl::METHOD_ID => 1,
]);
$config
->save();
$this
->rebuildContainer();
$default_site_langcode = $this
->config('system.site')
->get('default_langcode');
$this
->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical');
$translation = $this->entity;
$this
->drupalGet($translation
->toUrl());
$last = $this->container
->get('state')
->get('language_test.language_negotiation_last');
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
$last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
$this
->assertSame($default_site_langcode, $last_interface_language);
$this
->assertSame($last_content_language, $last_interface_language);
$this
->assertSame($translation
->language()
->getId(), $last_content_language);
$translation = $this->entity
->getTranslation('es');
$this
->drupalGet($translation
->toUrl());
$last = $this->container
->get('state')
->get('language_test.language_negotiation_last');
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
$last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
$this
->assertSame($last_interface_language, $default_site_langcode, 'Interface language did not change from the default site language.');
$this
->assertSame($last_content_language, $translation
->language()
->getId(), 'Content language matches the current entity translation language.');
$translation = $this->entity
->getTranslation('fr');
$this
->drupalGet($translation
->toUrl());
$last = $this->container
->get('state')
->get('language_test.language_negotiation_last');
$last_content_language = $last[LanguageInterface::TYPE_CONTENT];
$last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
$this
->assertSame($last_interface_language, $default_site_langcode, 'Interface language did not change from the default site language.');
$this
->assertSame($last_content_language, $translation
->language()
->getId(), 'Content language matches the current entity translation language.');
}
protected function createTranslatableEntity() {
$this->entity = EntityTest::create();
$this->entity
->addTranslation('es', [
'name' => 'name spanish',
]);
$this->entity
->addTranslation('fr', [
'name' => 'name french',
]);
$this->entity
->save();
}
protected function setCurrentRequestForRoute($path, $route_name) {
$request = Request::create($path);
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, $route_name);
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route($path));
$this->container
->get('request_stack')
->push($request);
}
}