summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/language/tests/src/Functional/LanguageNegotiationContentEntityTest.php
blob: 3db65f083fd4ab11131df46e7fb5b4af8b4aadfe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php

declare(strict_types=1);

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\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\Routing\Route;

/**
 * Tests language negotiation with the language negotiator content entity.
 *
 * @group language
 */
class LanguageNegotiationContentEntityTest extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'language',
    'language_test',
    'entity_test',
    'system',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * The entity being used for testing.
   *
   * @var \Drupal\Core\Entity\ContentEntityInterface
   */
  protected $entity;

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    ConfigurableLanguage::createFromLangcode('es')->save();
    ConfigurableLanguage::createFromLangcode('fr')->save();

    // In order to reflect the changes for a multilingual site in the container
    // we have to rebuild it.
    $this->rebuildContainer();

    $this->createTranslatableEntity();

    $user = $this->drupalCreateUser(['view test entity']);
    $this->drupalLogin($user);
  }

  /**
   * Tests default with content language remaining same as interface language.
   */
  public function testDefaultConfiguration(): void {
    $translation = $this->entity;
    $this->drupalGet($translation->toUrl());
    $last = \Drupal::keyValue('language_test')->get('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 = \Drupal::keyValue('language_test')->get('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 = \Drupal::keyValue('language_test')->get('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);
  }

  /**
   * Tests enabling the language negotiator language_content_entity.
   */
  public function testEnabledLanguageContentNegotiator(): void {
    // Define the method language-url with a higher priority than
    // language-content-entity. This configuration should match the default one,
    // where the language-content-entity is turned off.
    $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();

    // In order to reflect the changes for a multilingual site in the container
    // we have to rebuild it.
    $this->rebuildContainer();

    // The tests for the default configuration should still pass.
    $this->testDefaultConfiguration();

    // Define the method language-content-entity with a higher priority than
    // language-url.
    $config->set('negotiation.language_content.enabled', [
      LanguageNegotiationContentEntity::METHOD_ID => 0,
      LanguageNegotiationUrl::METHOD_ID => 1,
    ]);
    $config->save();

    // In order to reflect the changes for a multilingual site in the container
    // we have to rebuild it.
    $this->rebuildContainer();

    // The method language-content-entity should run before language-url and
    // append query parameter for the content language and prevent language-url
    // from overwriting the URL.
    $default_site_langcode = $this->config('system.site')->get('default_langcode');

    // Now switching to an entity route, so that the URL links are generated
    // while being on an entity route.
    $this->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical');

    $translation = $this->entity;
    $this->drupalGet($translation->toUrl());
    $last = \Drupal::keyValue('language_test')->get('language_negotiation_last');
    $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
    $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
    // Check that interface language and content language are the same as the
    // default translation language of the entity.
    $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 = \Drupal::keyValue('language_test')->get('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 = \Drupal::keyValue('language_test')->get('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.');
  }

  /**
   * Creates a translated entity.
   */
  protected function createTranslatableEntity(): void {
    $this->entity = EntityTest::create();
    $this->entity->addTranslation('es', ['name' => 'name spanish']);
    $this->entity->addTranslation('fr', ['name' => 'name french']);
    $this->entity->save();
  }

  /**
   * Sets the current request to a specific path with the corresponding route.
   *
   * @param string $path
   *   The path for which the current request should be created.
   * @param string $route_name
   *   The route name for which the route object for the request should be
   *   created.
   */
  protected function setCurrentRequestForRoute($path, $route_name): void {
    $request = Request::create($path);
    $request->attributes->set(RouteObjectInterface::ROUTE_NAME, $route_name);
    $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route($path));
    $request->setSession(new Session(new MockArraySessionStorage()));
    $this->container->get('request_stack')->push($request);
  }

}