3 namespace Drupal\Tests\language\Functional;
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;
13 * Tests Language Negotiation.
15 * Uses different negotiators for content and interface.
19 class ConfigurableLanguageManagerTest extends BrowserTestBase {
24 public static $modules = [
26 'content_translation',
37 protected function setUp() {
40 /** @var \Drupal\user\UserInterface $user */
41 $user = $this->createUser([], '', TRUE);
42 $this->drupalLogin($user);
43 ConfigurableLanguage::createFromLangcode('es')->save();
45 // Create a page node type and make it translatable.
51 $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page');
52 $config->setDefaultLangcode('en')
53 ->setLanguageAlterable(TRUE)
56 // Create a Node with title 'English' and translate it to Spanish.
57 $node = Node::create([
62 $node->addTranslation('es', ['title' => 'Español']);
65 // Enable both language_interface and language_content language negotiation.
66 \Drupal::getContainer()->get('language_negotiator')->updateConfiguration([
71 // Set the preferred language of the user for admin pages to English.
72 $user->set('preferred_admin_langcode', 'en')->save();
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();
78 // Place a Block with a translatable string on the page.
79 $this->placeBlock('system_powered_by_block', ['region' => 'content']);
81 // Load the Spanish Node page once, to register the translatable string.
82 $this->drupalGet('/es/node/1');
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,
91 'translation' => 'Funciona con ...',
93 // Invalidate caches so that the new translation will be used.
94 Cache::invalidateTags(['rendered', 'locale']);
98 * Test translation with URL and Preferred Admin Language negotiators.
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.
103 public function testUrlContentTranslationWithPreferredAdminLanguage() {
104 $assert_session = $this->assertSession();
105 // Set the interface language to use the preferred administration language
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,
112 'language-selected' => 3,
114 // Set Content Language Negotiator to use just the URL.
115 $language_negotiator->saveConfiguration('language_content', [
117 'language-selected' => 5,
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');
126 // Load the spanish node page again and see if both the node and the string
128 $this->drupalGet('/es/node/1');
129 $assert_session->pageTextContains('Español');
130 $assert_session->pageTextContains('Funciona con');
131 $assert_session->pageTextNotContains('Powered by');
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');
142 * Test translation with URL and Session Language Negotiators.
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,
152 'language-selected' => 3,
155 // Set Content Language Negotiator to URL.
156 $language_negotiator->saveConfiguration('language_content', [
158 'language-selected' => 5,
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');
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');
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');
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');