Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / locale / tests / src / Functional / LocaleContentTest.php
1 <?php
2
3 namespace Drupal\Tests\locale\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\node\NodeInterface;
8
9 /**
10  * Tests you can enable multilingual support on content types and configure a
11  * language for a node.
12  *
13  * @group locale
14  */
15 class LocaleContentTest extends BrowserTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['node', 'locale'];
23
24   /**
25    * Verifies that machine name fields are always LTR.
26    */
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']);
30
31     // Log in as admin.
32     $this->drupalLogin($admin_user);
33
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.');
37
38     // Install the Arabic language (which is RTL) and configure as the default.
39     $edit = [];
40     $edit['predefined_langcode'] = 'ar';
41     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
42
43     $edit = [
44       'site_default_language' => 'ar',
45     ];
46     $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
47
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.');
51   }
52
53   /**
54    * Test if a content type can be set to multilingual and language is present.
55    */
56   public function testContentTypeLanguageConfiguration() {
57     $type1 = $this->drupalCreateContentType();
58     $type2 = $this->drupalCreateContentType();
59
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"]);
64
65     // Add custom language.
66     $this->drupalLogin($admin_user);
67     // Code for the language.
68     $langcode = 'xx';
69     // The English name for the language.
70     $name = $this->randomMachineName(16);
71     $edit = [
72       'predefined_langcode' => 'custom',
73       'langcode' => $langcode,
74       'label' => $name,
75       'direction' => LanguageInterface::DIRECTION_LTR,
76     ];
77     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
78
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.');
82     $edit = [
83       'language_configuration[language_alterable]' => TRUE,
84     ];
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();
89
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.');
95
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.');
102
103     // Create a node.
104     $node_title = $this->randomMachineName();
105     $node_body = $this->randomMachineName();
106     $edit = [
107       'type' => $type2->id(),
108       'title' => $node_title,
109       'body' => [['value' => $node_body]],
110       'langcode' => $langcode,
111     ];
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.
118     $edit = [
119       'langcode[0][value]' => 'en',
120     ];
121     $this->drupalPostForm($path, $edit, t('Save'));
122     $this->assertText(t('@title has been updated.', ['@title' => $node_title]));
123
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');
127
128     $this->drupalLogout();
129   }
130
131   /**
132    * Test if a dir and lang tags exist in node's attributes.
133    */
134   public function testContentTypeDirLang() {
135     $type = $this->drupalCreateContentType();
136
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"]);
141
142     // Log in as admin.
143     $this->drupalLogin($admin_user);
144
145     // Install Arabic language.
146     $edit = [];
147     $edit['predefined_langcode'] = 'ar';
148     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
149
150     // Install Spanish language.
151     $edit = [];
152     $edit['predefined_langcode'] = 'es';
153     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
154     \Drupal::languageManager()->reset();
155
156     // Set the content type to use multilingual support.
157     $this->drupalGet("admin/structure/types/manage/{$type->id()}");
158     $edit = [
159       'language_configuration[language_alterable]' => TRUE,
160     ];
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();
164
165     // Log in as web user to add new node.
166     $this->drupalLogin($web_user);
167
168     // Create three nodes: English, Arabic and Spanish.
169     $nodes = [];
170     foreach (['en', 'es', 'ar'] as $langcode) {
171       $nodes[$langcode] = $this->drupalCreateNode([
172         'langcode' => $langcode,
173         'type' => $type->id(),
174         'promote' => NodeInterface::PROMOTED,
175       ]);
176     }
177
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.');
182
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.');
186
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.');
191
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.');
196
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.');
200   }
201
202 }