3 namespace Drupal\Tests\language\Functional;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\language\Entity\ConfigurableLanguage;
9 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationContentEntity;
10 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
11 use Drupal\Tests\BrowserTestBase;
12 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
13 use Symfony\Component\HttpFoundation\Request;
14 use Symfony\Component\Routing\Route;
17 * Tests language negotiation with the language negotiator content entity.
21 class LanguageNegotiationContentEntityTest extends BrowserTestBase {
28 public static $modules = ['language', 'language_test', 'entity_test', 'system'];
31 * The entity being used for testing.
33 * @var \Drupal\Core\Entity\ContentEntityInterface
40 protected function setUp() {
43 ConfigurableLanguage::create(['id' => 'es'])->save();
44 ConfigurableLanguage::create(['id' => 'fr'])->save();
46 // In order to reflect the changes for a multilingual site in the container
47 // we have to rebuild it.
48 $this->rebuildContainer();
50 $this->createTranslatableEntity();
52 $user = $this->drupalCreateUser(['view test entity']);
53 $this->drupalLogin($user);
57 * Tests default with content language remaining same as interface language.
59 public function testDefaultConfiguration() {
60 $translation = $this->entity;
61 $this->drupalGet($translation->urlInfo());
62 $last = $this->container->get('state')->get('language_test.language_negotiation_last');
63 $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
64 $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
65 $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()]));
67 $translation = $this->entity->getTranslation('es');
68 $this->drupalGet($translation->urlInfo());
69 $last = $this->container->get('state')->get('language_test.language_negotiation_last');
70 $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
71 $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
72 $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()]));
74 $translation = $this->entity->getTranslation('fr');
75 $this->drupalGet($translation->urlInfo());
76 $last = $this->container->get('state')->get('language_test.language_negotiation_last');
77 $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
78 $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
79 $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()]));
83 * Tests enabling the language negotiator language_content_entity.
85 public function testEnabledLanguageContentNegotiator() {
86 // Define the method language-url with a higher priority than
87 // language-content-entity. This configuration should match the default one,
88 // where the language-content-entity is turned off.
89 $config = $this->config('language.types');
90 $config->set('configurable', [LanguageInterface::TYPE_INTERFACE, LanguageInterface::TYPE_CONTENT]);
91 $config->set('negotiation.language_content.enabled', [
92 LanguageNegotiationUrl::METHOD_ID => 0,
93 LanguageNegotiationContentEntity::METHOD_ID => 1
97 // In order to reflect the changes for a multilingual site in the container
98 // we have to rebuild it.
99 $this->rebuildContainer();
101 // The tests for the default configuration should still pass.
102 $this->testDefaultConfiguration();
104 // Define the method language-content-entity with a higher priority than
106 $config->set('negotiation.language_content.enabled', [
107 LanguageNegotiationContentEntity::METHOD_ID => 0,
108 LanguageNegotiationUrl::METHOD_ID => 1
112 // In order to reflect the changes for a multilingual site in the container
113 // we have to rebuild it.
114 $this->rebuildContainer();
116 // The method language-content-entity should run before language-url and
117 // append query parameter for the content language and prevent language-url
118 // from overwriting the URL.
119 $default_site_langcode = $this->config('system.site')->get('default_langcode');
121 // Now switching to an entity route, so that the URL links are generated
122 // while being on an entity route.
123 $this->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical');
125 $translation = $this->entity;
126 $this->drupalGet($translation->urlInfo());
127 $last = $this->container->get('state')->get('language_test.language_negotiation_last');
128 $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
129 $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
130 $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.');
131 $this->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.');
132 $this->assertTrue($last_content_language == $translation->language()->getId(), 'Content language matches the current entity translation language.');
134 $translation = $this->entity->getTranslation('es');
135 $this->drupalGet($translation->urlInfo());
136 $last = $this->container->get('state')->get('language_test.language_negotiation_last');
137 $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
138 $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
139 $this->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.');
140 $this->assertTrue($last_content_language == $translation->language()->getId(), 'Content language matches the current entity translation language.');
142 $translation = $this->entity->getTranslation('fr');
143 $this->drupalGet($translation->urlInfo());
144 $last = $this->container->get('state')->get('language_test.language_negotiation_last');
145 $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
146 $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
147 $this->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.');
148 $this->assertTrue($last_content_language == $translation->language()->getId(), 'Content language matches the current entity translation language.');
152 * Creates a translated entity.
154 protected function createTranslatableEntity() {
155 $this->entity = EntityTest::create();
156 $this->entity->addTranslation('es', ['name' => 'name spanish']);
157 $this->entity->addTranslation('fr', ['name' => 'name french']);
158 $this->entity->save();
162 * Sets the current request to a specific path with the corresponding route.
164 * @param string $path
165 * The path for which the current request should be created.
166 * @param string $route_name
167 * The route name for which the route object for the request should be
170 protected function setCurrentRequestForRoute($path, $route_name) {
171 $request = Request::create($path);
172 $request->attributes->set(RouteObjectInterface::ROUTE_NAME, $route_name);
173 $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route($path));
174 $this->container->get('request_stack')->push($request);