View source
<?php
namespace Drupal\Tests\language\Functional;
use Drupal\Component\Render\FormattableMarkup;
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 Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
class LanguageNegotiationContentEntityTest extends BrowserTestBase {
public static $modules = [
'language',
'language_test',
'entity_test',
'system',
];
protected $defaultTheme = 'stark';
protected $entity;
protected function setUp() {
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
->assertTrue($last_interface_language == $last_content_language && $last_content_language == $translation
->language()
->getId(), new FormattableMarkup('Interface language %interface_language and Content language %content_language are the same as the translation language %translation_language of the entity.', [
'%interface_language' => $last_interface_language,
'%content_language' => $last_content_language,
'%translation_language' => $translation
->language()
->getId(),
]));
$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
->assertTrue($last_interface_language == $last_content_language && $last_content_language == $translation
->language()
->getId(), new FormattableMarkup('Interface language %interface_language and Content language %content_language are the same as the translation language %translation_language of the entity.', [
'%interface_language' => $last_interface_language,
'%content_language' => $last_content_language,
'%translation_language' => $translation
->language()
->getId(),
]));
$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
->assertTrue($last_interface_language == $last_content_language && $last_content_language == $translation
->language()
->getId(), new FormattableMarkup('Interface language %interface_language and Content language %content_language are the same as the translation language %translation_language of the entity.', [
'%interface_language' => $last_interface_language,
'%content_language' => $last_content_language,
'%translation_language' => $translation
->language()
->getId(),
]));
}
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
->assertTrue($last_interface_language == $default_site_langcode && $last_interface_language == $last_content_language && $last_content_language == $translation
->language()
->getId(), 'Interface language and Content language are the same as the default translation language of the entity.');
$this
->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.');
$this
->assertTrue($last_content_language == $translation
->language()
->getId(), 'Content language matches the current entity translation 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
->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.');
$this
->assertTrue($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
->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.');
$this
->assertTrue($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);
}
}