Pull merge.
[yaffs-website] / web / core / modules / language / tests / src / Functional / ConfigurableLanguageManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\language\Functional;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\language\Entity\ContentLanguageSettings;
8 use Drupal\node\Entity\Node;
9 use Drupal\node\Entity\NodeType;
10 use Drupal\Tests\BrowserTestBase;
11
12 /**
13  * Tests Language Negotiation.
14  *
15  * Uses different negotiators for content and interface.
16  *
17  * @group language
18  */
19 class ConfigurableLanguageManagerTest extends BrowserTestBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public static $modules = [
25     'language',
26     'content_translation',
27     'node',
28     'locale',
29     'block',
30     'system',
31     'user',
32   ];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39
40     /** @var \Drupal\user\UserInterface $user */
41     $user = $this->createUser([], '', TRUE);
42     $this->drupalLogin($user);
43     ConfigurableLanguage::createFromLangcode('es')->save();
44
45     // Create a page node type and make it translatable.
46     NodeType::create([
47       'type' => 'page',
48       'name' => t('Page'),
49     ])->save();
50
51     $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page');
52     $config->setDefaultLangcode('en')
53       ->setLanguageAlterable(TRUE)
54       ->save();
55
56     // Create a Node with title 'English' and translate it to Spanish.
57     $node = Node::create([
58       'type' => 'page',
59       'title' => 'English',
60     ]);
61     $node->save();
62     $node->addTranslation('es', ['title' => 'Español']);
63     $node->save();
64
65     // Enable both language_interface and language_content language negotiation.
66     \Drupal::getContainer()->get('language_negotiator')->updateConfiguration([
67       'language_interface',
68       'language_content',
69     ]);
70
71     // Set the preferred language of the user for admin pages to English.
72     $user->set('preferred_admin_langcode', 'en')->save();
73
74     // Make sure node edit pages are administration pages.
75     $this->config('node.settings')->set('use_admin_theme', '1')->save();
76     $this->container->get('router.builder')->rebuild();
77
78     // Place a Block with a translatable string on the page.
79     $this->placeBlock('system_powered_by_block', ['region' => 'content']);
80
81     // Load the Spanish Node page once, to register the translatable string.
82     $this->drupalGet('/es/node/1');
83
84     // Translate the Powered by string.
85     /** @var \Drupal\locale\StringStorageInterface $string_storage */
86     $string_storage = \Drupal::getContainer()->get('locale.storage');
87     $source = $string_storage->findString(['source' => 'Powered by <a href=":poweredby">Drupal</a>']);
88     $string_storage->createTranslation([
89       'lid' => $source->lid,
90       'language' => 'es',
91       'translation' => 'Funciona con ...',
92     ])->save();
93     // Invalidate caches so that the new translation will be used.
94     Cache::invalidateTags(['rendered', 'locale']);
95   }
96
97   /**
98    * Test translation with URL and Preferred Admin Language negotiators.
99    *
100    * The interface language uses the preferred language for admin pages of the
101    * user and after that the URL. The Content uses just the URL.
102    */
103   public function testUrlContentTranslationWithPreferredAdminLanguage() {
104     $assert_session = $this->assertSession();
105     // Set the interface language to use the preferred administration language
106     // and then the URL.
107     /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
108     $language_negotiator = \Drupal::getContainer()->get('language_negotiator');
109     $language_negotiator->saveConfiguration('language_interface', [
110       'language-user-admin' => 1,
111       'language-url' => 2,
112       'language-selected' => 3,
113     ]);
114     // Set Content Language Negotiator to use just the URL.
115     $language_negotiator->saveConfiguration('language_content', [
116       'language-url' => 4,
117       'language-selected' => 5,
118     ]);
119
120     // See if the full view of the node in english is present and the
121     // string in the Powered By Block is in English.
122     $this->drupalGet('/node/1');
123     $assert_session->pageTextContains('English');
124     $assert_session->pageTextContains('Powered by');
125
126     // Load the spanish node page again and see if both the node and the string
127     // are translated.
128     $this->drupalGet('/es/node/1');
129     $assert_session->pageTextContains('Español');
130     $assert_session->pageTextContains('Funciona con');
131     $assert_session->pageTextNotContains('Powered by');
132
133     // Check if the Powered by string is shown in English on an
134     // administration page, and the node content is shown in Spanish.
135     $this->drupalGet('/es/node/1/edit');
136     $assert_session->pageTextContains('Español');
137     $assert_session->pageTextContains('Powered by');
138     $assert_session->pageTextNotContains('Funciona con');
139   }
140
141   /**
142    * Test translation with URL and Session Language Negotiators.
143    */
144   public function testUrlContentTranslationWithSessionLanguage() {
145     $assert_session = $this->assertSession();
146     /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
147     $language_negotiator = \Drupal::getContainer()->get('language_negotiator');
148     // Set Interface Language Negotiator to Session.
149     $language_negotiator->saveConfiguration('language_interface', [
150       'language-session' => 1,
151       'language-url' => 2,
152       'language-selected' => 3,
153     ]);
154
155     // Set Content Language Negotiator to URL.
156     $language_negotiator->saveConfiguration('language_content', [
157       'language-url' => 4,
158       'language-selected' => 5,
159     ]);
160
161     // See if the full view of the node in english is present and the
162     // string in the Powered By Block is in English.
163     $this->drupalGet('/node/1');
164     $assert_session->pageTextContains('English');
165     $assert_session->pageTextContains('Powered by');
166
167     // The language session variable has not been set yet, so
168     // The string should be in Spanish.
169     $this->drupalGet('/es/node/1');
170     $assert_session->pageTextContains('Español');
171     $assert_session->pageTextNotContains('Powered by');
172     $assert_session->pageTextContains('Funciona con');
173
174     // Set the session language to Spanish but load the English node page.
175     $this->drupalGet('/node/1', ['query' => ['language' => 'es']]);
176     $assert_session->pageTextContains('English');
177     $assert_session->pageTextNotContains('Español');
178     $assert_session->pageTextContains('Funciona con');
179     $assert_session->pageTextNotContains('Powered by');
180
181     // Set the session language to English but load the node page in Spanish.
182     $this->drupalGet('/es/node/1', ['query' => ['language' => 'en']]);
183     $assert_session->pageTextNotContains('English');
184     $assert_session->pageTextContains('Español');
185     $assert_session->pageTextNotContains('Funciona con');
186     $assert_session->pageTextContains('Powered by');
187   }
188
189 }