3 namespace Drupal\Tests\locale\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\node\NodeInterface;
10 * Tests you can enable multilingual support on content types and configure a
11 * language for a node.
15 class LocaleContentTest extends BrowserTestBase {
22 public static $modules = ['node', 'locale'];
25 * Verifies that machine name fields are always LTR.
27 public function testMachineNameLTR() {
28 // User to add and remove language.
29 $admin_user = $this->drupalCreateUser(['administer languages', 'administer content types', 'access administration pages', 'administer site configuration']);
32 $this->drupalLogin($admin_user);
34 // Verify that the machine name field is LTR for a new content type.
35 $this->drupalGet('admin/structure/types/add');
36 $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when no additional language is configured.');
38 // Install the Arabic language (which is RTL) and configure as the default.
40 $edit['predefined_langcode'] = 'ar';
41 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
44 'site_default_language' => 'ar',
46 $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
48 // Verify that the machine name field is still LTR for a new content type.
49 $this->drupalGet('admin/structure/types/add');
50 $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when the default language is RTL.');
54 * Test if a content type can be set to multilingual and language is present.
56 public function testContentTypeLanguageConfiguration() {
57 $type1 = $this->drupalCreateContentType();
58 $type2 = $this->drupalCreateContentType();
60 // User to add and remove language.
61 $admin_user = $this->drupalCreateUser(['administer languages', 'administer content types', 'access administration pages']);
62 // User to create a node.
63 $web_user = $this->drupalCreateUser(["create {$type1->id()} content", "create {$type2->id()} content", "edit any {$type2->id()} content"]);
65 // Add custom language.
66 $this->drupalLogin($admin_user);
67 // Code for the language.
69 // The English name for the language.
70 $name = $this->randomMachineName(16);
72 'predefined_langcode' => 'custom',
73 'langcode' => $langcode,
75 'direction' => LanguageInterface::DIRECTION_LTR,
77 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
79 // Set the content type to use multilingual support.
80 $this->drupalGet("admin/structure/types/manage/{$type2->id()}");
81 $this->assertText(t('Language settings'), 'Multilingual support widget present on content type configuration form.');
83 'language_configuration[language_alterable]' => TRUE,
85 $this->drupalPostForm("admin/structure/types/manage/{$type2->id()}", $edit, t('Save content type'));
86 $this->assertRaw(t('The content type %type has been updated.', ['%type' => $type2->label()]));
87 $this->drupalLogout();
88 \Drupal::languageManager()->reset();
90 // Verify language selection is not present on the node add form.
91 $this->drupalLogin($web_user);
92 $this->drupalGet("node/add/{$type1->id()}");
93 // Verify language select list is not present.
94 $this->assertNoFieldByName('langcode[0][value]', NULL, 'Language select not present on the node add form.');
96 // Verify language selection appears on the node add form.
97 $this->drupalGet("node/add/{$type2->id()}");
98 // Verify language select list is present.
99 $this->assertFieldByName('langcode[0][value]', NULL, 'Language select present on the node add form.');
100 // Ensure language appears.
101 $this->assertText($name, 'Language present.');
104 $node_title = $this->randomMachineName();
105 $node_body = $this->randomMachineName();
107 'type' => $type2->id(),
108 'title' => $node_title,
109 'body' => [['value' => $node_body]],
110 'langcode' => $langcode,
112 $node = $this->drupalCreateNode($edit);
113 // Edit the content and ensure correct language is selected.
114 $path = 'node/' . $node->id() . '/edit';
115 $this->drupalGet($path);
116 $this->assertRaw('<option value="' . $langcode . '" selected="selected">' . $name . '</option>', 'Correct language selected.');
117 // Ensure we can change the node language.
119 'langcode[0][value]' => 'en',
121 $this->drupalPostForm($path, $edit, t('Save'));
122 $this->assertText(t('@title has been updated.', ['@title' => $node_title]));
124 // Verify that the creation message contains a link to a node.
125 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/' . $node->id()]);
126 $this->assert(isset($view_link), 'The message area contains the link to the edited node');
128 $this->drupalLogout();
132 * Test if a dir and lang tags exist in node's attributes.
134 public function testContentTypeDirLang() {
135 $type = $this->drupalCreateContentType();
137 // User to add and remove language.
138 $admin_user = $this->drupalCreateUser(['administer languages', 'administer content types', 'access administration pages']);
139 // User to create a node.
140 $web_user = $this->drupalCreateUser(["create {$type->id()} content", "edit own {$type->id()} content"]);
143 $this->drupalLogin($admin_user);
145 // Install Arabic language.
147 $edit['predefined_langcode'] = 'ar';
148 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
150 // Install Spanish language.
152 $edit['predefined_langcode'] = 'es';
153 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
154 \Drupal::languageManager()->reset();
156 // Set the content type to use multilingual support.
157 $this->drupalGet("admin/structure/types/manage/{$type->id()}");
159 'language_configuration[language_alterable]' => TRUE,
161 $this->drupalPostForm("admin/structure/types/manage/{$type->id()}", $edit, t('Save content type'));
162 $this->assertRaw(t('The content type %type has been updated.', ['%type' => $type->label()]));
163 $this->drupalLogout();
165 // Log in as web user to add new node.
166 $this->drupalLogin($web_user);
168 // Create three nodes: English, Arabic and Spanish.
170 foreach (['en', 'es', 'ar'] as $langcode) {
171 $nodes[$langcode] = $this->drupalCreateNode([
172 'langcode' => $langcode,
173 'type' => $type->id(),
174 'promote' => NodeInterface::PROMOTED,
178 // Check if English node does not have lang tag.
179 $this->drupalGet('node/' . $nodes['en']->id());
180 $element = $this->cssSelect('article.node[lang="en"]');
181 $this->assertTrue(empty($element), 'The lang tag has not been assigned to the English node.');
183 // Check if English node does not have dir tag.
184 $element = $this->cssSelect('article.node[dir="ltr"]');
185 $this->assertTrue(empty($element), 'The dir tag has not been assigned to the English node.');
187 // Check if Arabic node has lang="ar" & dir="rtl" tags.
188 $this->drupalGet('node/' . $nodes['ar']->id());
189 $element = $this->cssSelect('article.node[lang="ar"][dir="rtl"]');
190 $this->assertTrue(!empty($element), 'The lang and dir tags have been assigned correctly to the Arabic node.');
192 // Check if Spanish node has lang="es" tag.
193 $this->drupalGet('node/' . $nodes['es']->id());
194 $element = $this->cssSelect('article.node[lang="es"]');
195 $this->assertTrue(!empty($element), 'The lang tag has been assigned correctly to the Spanish node.');
197 // Check if Spanish node does not have dir="ltr" tag.
198 $element = $this->cssSelect('article.node[lang="es"][dir="ltr"]');
199 $this->assertTrue(empty($element), 'The dir tag has not been assigned to the Spanish node.');