summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/language/tests/src/Functional/ConfigurableLanguageManagerTest.php
blob: b29adce28b5a7967c8a9ec514d9670ed49e85812 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php

declare(strict_types=1);

namespace Drupal\Tests\language\Functional;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\WaitTerminateTestTrait;

// cspell:ignore funciona

/**
 * Tests Language Negotiation.
 *
 * Uses different negotiators for content and interface.
 *
 * @group language
 */
class ConfigurableLanguageManagerTest extends BrowserTestBase {

  use WaitTerminateTestTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'language',
    'content_translation',
    'node',
    'locale',
    'block',
    'system',
    'user',
  ];

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

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

    // The \Drupal\locale\LocaleTranslation service clears caches after the
    // response is flushed to the client. We use WaitTerminateTestTrait to wait
    // for Drupal to perform its termination work before continuing.
    $this->setWaitForTerminate();

    /** @var \Drupal\user\UserInterface $user */
    $user = $this->createUser([], '', TRUE);
    $this->drupalLogin($user);
    ConfigurableLanguage::createFromLangcode('es')->save();

    // Create a page node type and make it translatable.
    NodeType::create([
      'type' => 'page',
      'name' => 'Page',
    ])->save();

    $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page');
    $config->setDefaultLangcode('en')
      ->setLanguageAlterable(TRUE)
      ->save();

    // Create a Node with title 'English' and translate it to Spanish.
    $node = Node::create([
      'type' => 'page',
      'title' => 'English',
    ]);
    $node->save();
    $node->addTranslation('es', ['title' => 'Español']);
    $node->save();

    // Enable both language_interface and language_content language negotiation.
    \Drupal::getContainer()->get('language_negotiator')->updateConfiguration([
      'language_interface',
      'language_content',
    ]);

    // Set the preferred language of the user for admin pages to English.
    $user->set('preferred_admin_langcode', 'en')->save();

    // Place a Block with a translatable string on the page.
    $this->placeBlock('system_powered_by_block', ['region' => 'content']);

    // Load the Spanish Node page once, to register the translatable string.
    $this->drupalGet('/es/node/1');

    // Translate the Powered by string.
    /** @var \Drupal\locale\StringStorageInterface $string_storage */
    $string_storage = \Drupal::getContainer()->get('locale.storage');
    $source = $string_storage->findString(['source' => 'Powered by <a href=":poweredby">Drupal</a>']);
    $string_storage->createTranslation([
      'lid' => $source->lid,
      'language' => 'es',
      'translation' => 'Funciona con ...',
    ])->save();
    // Invalidate caches so that the new translation will be used.
    Cache::invalidateTags(['rendered', 'locale']);
  }

  /**
   * Tests translation with URL and Preferred Admin Language negotiators.
   *
   * The interface language uses the preferred language for admin pages of the
   * user and after that the URL. The Content uses just the URL.
   */
  public function testUrlContentTranslationWithPreferredAdminLanguage(): void {
    $assert_session = $this->assertSession();
    // Set the interface language to use the preferred administration language
    // and then the URL.
    /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
    $language_negotiator = \Drupal::getContainer()->get('language_negotiator');
    $language_negotiator->saveConfiguration('language_interface', [
      'language-user-admin' => 1,
      'language-url' => 2,
      'language-selected' => 3,
    ]);
    // Set Content Language Negotiator to use just the URL.
    $language_negotiator->saveConfiguration('language_content', [
      'language-url' => 4,
      'language-selected' => 5,
    ]);

    // See if the full view of the node in english is present and the
    // string in the Powered By Block is in English.
    $this->drupalGet('/node/1');
    $assert_session->pageTextContains('English');
    $assert_session->pageTextContains('Powered by');

    // Load the spanish node page again and see if both the node and the string
    // are translated.
    $this->drupalGet('/es/node/1');
    $assert_session->pageTextContains('Español');
    $assert_session->pageTextContains('Funciona con');
    $assert_session->pageTextNotContains('Powered by');

    // Check if the Powered by string is shown in English on an
    // administration page, and the node content is shown in Spanish.
    $this->drupalGet('/es/node/1/edit');
    $assert_session->pageTextContains('Español');
    $assert_session->pageTextContains('Powered by');
    $assert_session->pageTextNotContains('Funciona con');
  }

  /**
   * Tests translation with URL and Session Language Negotiators.
   */
  public function testUrlContentTranslationWithSessionLanguage(): void {
    $assert_session = $this->assertSession();
    /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
    $language_negotiator = \Drupal::getContainer()->get('language_negotiator');
    // Set Interface Language Negotiator to Session.
    $language_negotiator->saveConfiguration('language_interface', [
      'language-session' => 1,
      'language-url' => 2,
      'language-selected' => 3,
    ]);

    // Set Content Language Negotiator to URL.
    $language_negotiator->saveConfiguration('language_content', [
      'language-url' => 4,
      'language-selected' => 5,
    ]);

    // See if the full view of the node in english is present and the
    // string in the Powered By Block is in English.
    $this->drupalGet('/node/1');
    $assert_session->pageTextContains('English');
    $assert_session->pageTextContains('Powered by');

    // The language session variable has not been set yet, so
    // The string should be in Spanish.
    $this->drupalGet('/es/node/1');
    $assert_session->pageTextContains('Español');
    $assert_session->pageTextNotContains('Powered by');
    $assert_session->pageTextContains('Funciona con');

    // Set the session language to Spanish but load the English node page.
    $this->drupalGet('/node/1', ['query' => ['language' => 'es']]);
    $assert_session->pageTextContains('English');
    $assert_session->pageTextNotContains('Español');
    $assert_session->pageTextContains('Funciona con');
    $assert_session->pageTextNotContains('Powered by');

    // Set the session language to English but load the node page in Spanish.
    $this->drupalGet('/es/node/1', ['query' => ['language' => 'en']]);
    $assert_session->pageTextNotContains('English');
    $assert_session->pageTextContains('Español');
    $assert_session->pageTextNotContains('Funciona con');
    $assert_session->pageTextContains('Powered by');
  }

  /**
   * Tests translation of the user profile edit form.
   *
   * The user profile edit form is a special case when used with the preferred
   * admin language negotiator because of the recursive way that the negotiator
   * is called.
   */
  public function testUserProfileTranslationWithPreferredAdminLanguage(): void {
    $assert_session = $this->assertSession();
    // Set the interface language to use the preferred administration language.
    /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
    $language_negotiator = \Drupal::getContainer()->get('language_negotiator');
    $language_negotiator->saveConfiguration('language_interface', [
      'language-user-admin' => 1,
      'language-selected' => 2,
    ]);

    // Create a field on the user entity.
    $field_name = $this->randomMachineName();
    $label = $this->randomMachineName();
    $field_label_en = "English $label";
    $field_label_es = "Español $label";

    $field_storage = FieldStorageConfig::create([
      'field_name' => $field_name,
      'entity_type' => 'user',
      'type' => 'string',
    ]);
    $field_storage->save();

    $instance = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => 'user',
      'label' => $field_label_en,
    ]);
    $instance->save();

    // Add a Spanish translation.
    \Drupal::languageManager()
      ->getLanguageConfigOverride('es', "field.field.user.user.$field_name")
      ->set('label', $field_label_es)
      ->save();

    // Add the new field to the edit form.
    EntityFormDisplay::create([
      'targetEntityType' => 'user',
      'bundle' => 'user',
      'mode' => 'default',
      'status' => TRUE,
    ])
      ->setComponent($field_name, [
        'type' => 'string_textfield',
      ])
      ->save();

    $user_id = \Drupal::currentUser()->id();
    $this->drupalGet("/user/$user_id/edit");
    // Admin language choice is "No preference" so we should get the default.
    $assert_session->pageTextContains($field_label_en);
    $assert_session->pageTextNotContains($field_label_es);

    // Set admin language to Spanish.
    $this->submitForm(['edit-preferred-admin-langcode' => 'es'], 'edit-submit');
    $assert_session->pageTextContains($field_label_es);
    $assert_session->pageTextNotContains($field_label_en);

    // Set admin language to English.
    $this->submitForm(['edit-preferred-admin-langcode' => 'en'], 'edit-submit');
    $assert_session->pageTextContains($field_label_en);
    $assert_session->pageTextNotContains($field_label_es);
  }

}