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