Version 1
[yaffs-website] / web / core / modules / language / tests / src / Functional / LanguageNegotiationContentEntityTest.php
1 <?php
2
3 namespace Drupal\Tests\language\Functional;
4
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;
15
16 /**
17  * Tests language negotiation with the language negotiator content entity.
18  *
19  * @group language
20  */
21 class LanguageNegotiationContentEntityTest extends BrowserTestBase {
22
23   /**
24    * Modules to enable.
25    *
26    * @var array
27    */
28   public static $modules = ['language', 'language_test', 'entity_test', 'system'];
29
30   /**
31    * The entity being used for testing.
32    *
33    * @var \Drupal\Core\Entity\ContentEntityInterface
34    */
35   protected $entity;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42
43     ConfigurableLanguage::create(['id' => 'es'])->save();
44     ConfigurableLanguage::create(['id' => 'fr'])->save();
45
46     // In order to reflect the changes for a multilingual site in the container
47     // we have to rebuild it.
48     $this->rebuildContainer();
49
50     $this->createTranslatableEntity();
51
52     $user = $this->drupalCreateUser(['view test entity']);
53     $this->drupalLogin($user);
54   }
55
56   /**
57    * Tests default with content language remaining same as interface language.
58    */
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()]));
66
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()]));
73
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()]));
80   }
81
82   /**
83    * Tests enabling the language negotiator language_content_entity.
84    */
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
94     ]);
95     $config->save();
96
97     // In order to reflect the changes for a multilingual site in the container
98     // we have to rebuild it.
99     $this->rebuildContainer();
100
101     // The tests for the default configuration should still pass.
102     $this->testDefaultConfiguration();
103
104     // Define the method language-content-entity with a higher priority than
105     // language-url.
106     $config->set('negotiation.language_content.enabled', [
107       LanguageNegotiationContentEntity::METHOD_ID => 0,
108       LanguageNegotiationUrl::METHOD_ID => 1
109     ]);
110     $config->save();
111
112     // In order to reflect the changes for a multilingual site in the container
113     // we have to rebuild it.
114     $this->rebuildContainer();
115
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');
120
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');
124
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.');
133
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.');
141
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.');
149   }
150
151   /**
152    * Creates a translated entity.
153    */
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();
159   }
160
161   /**
162    * Sets the current request to a specific path with the corresponding route.
163    *
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
168    *   created.
169    */
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);
175   }
176
177 }