annotate core/modules/locale/tests/src/Functional/LocaleContentTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\locale\Functional;
Chris@0 4
Chris@0 5 use Drupal\Tests\BrowserTestBase;
Chris@0 6 use Drupal\Core\Language\LanguageInterface;
Chris@0 7 use Drupal\node\NodeInterface;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests you can enable multilingual support on content types and configure a
Chris@0 11 * language for a node.
Chris@0 12 *
Chris@0 13 * @group locale
Chris@0 14 */
Chris@0 15 class LocaleContentTest extends BrowserTestBase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Modules to enable.
Chris@0 19 *
Chris@0 20 * @var array
Chris@0 21 */
Chris@0 22 public static $modules = ['node', 'locale'];
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Verifies that machine name fields are always LTR.
Chris@0 26 */
Chris@0 27 public function testMachineNameLTR() {
Chris@0 28 // User to add and remove language.
Chris@0 29 $admin_user = $this->drupalCreateUser(['administer languages', 'administer content types', 'access administration pages', 'administer site configuration']);
Chris@0 30
Chris@0 31 // Log in as admin.
Chris@0 32 $this->drupalLogin($admin_user);
Chris@0 33
Chris@0 34 // Verify that the machine name field is LTR for a new content type.
Chris@0 35 $this->drupalGet('admin/structure/types/add');
Chris@0 36 $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when no additional language is configured.');
Chris@0 37
Chris@0 38 // Install the Arabic language (which is RTL) and configure as the default.
Chris@0 39 $edit = [];
Chris@0 40 $edit['predefined_langcode'] = 'ar';
Chris@0 41 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
Chris@0 42
Chris@0 43 $edit = [
Chris@0 44 'site_default_language' => 'ar',
Chris@0 45 ];
Chris@0 46 $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
Chris@0 47
Chris@0 48 // Verify that the machine name field is still LTR for a new content type.
Chris@0 49 $this->drupalGet('admin/structure/types/add');
Chris@0 50 $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when the default language is RTL.');
Chris@0 51 }
Chris@0 52
Chris@0 53 /**
Chris@0 54 * Test if a content type can be set to multilingual and language is present.
Chris@0 55 */
Chris@0 56 public function testContentTypeLanguageConfiguration() {
Chris@0 57 $type1 = $this->drupalCreateContentType();
Chris@0 58 $type2 = $this->drupalCreateContentType();
Chris@0 59
Chris@0 60 // User to add and remove language.
Chris@0 61 $admin_user = $this->drupalCreateUser(['administer languages', 'administer content types', 'access administration pages']);
Chris@0 62 // User to create a node.
Chris@0 63 $web_user = $this->drupalCreateUser(["create {$type1->id()} content", "create {$type2->id()} content", "edit any {$type2->id()} content"]);
Chris@0 64
Chris@0 65 // Add custom language.
Chris@0 66 $this->drupalLogin($admin_user);
Chris@0 67 // Code for the language.
Chris@0 68 $langcode = 'xx';
Chris@0 69 // The English name for the language.
Chris@0 70 $name = $this->randomMachineName(16);
Chris@0 71 $edit = [
Chris@0 72 'predefined_langcode' => 'custom',
Chris@0 73 'langcode' => $langcode,
Chris@0 74 'label' => $name,
Chris@0 75 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@0 76 ];
Chris@0 77 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@0 78
Chris@0 79 // Set the content type to use multilingual support.
Chris@0 80 $this->drupalGet("admin/structure/types/manage/{$type2->id()}");
Chris@0 81 $this->assertText(t('Language settings'), 'Multilingual support widget present on content type configuration form.');
Chris@0 82 $edit = [
Chris@0 83 'language_configuration[language_alterable]' => TRUE,
Chris@0 84 ];
Chris@0 85 $this->drupalPostForm("admin/structure/types/manage/{$type2->id()}", $edit, t('Save content type'));
Chris@0 86 $this->assertRaw(t('The content type %type has been updated.', ['%type' => $type2->label()]));
Chris@0 87 $this->drupalLogout();
Chris@0 88 \Drupal::languageManager()->reset();
Chris@0 89
Chris@0 90 // Verify language selection is not present on the node add form.
Chris@0 91 $this->drupalLogin($web_user);
Chris@0 92 $this->drupalGet("node/add/{$type1->id()}");
Chris@0 93 // Verify language select list is not present.
Chris@0 94 $this->assertNoFieldByName('langcode[0][value]', NULL, 'Language select not present on the node add form.');
Chris@0 95
Chris@0 96 // Verify language selection appears on the node add form.
Chris@0 97 $this->drupalGet("node/add/{$type2->id()}");
Chris@0 98 // Verify language select list is present.
Chris@0 99 $this->assertFieldByName('langcode[0][value]', NULL, 'Language select present on the node add form.');
Chris@0 100 // Ensure language appears.
Chris@0 101 $this->assertText($name, 'Language present.');
Chris@0 102
Chris@0 103 // Create a node.
Chris@0 104 $node_title = $this->randomMachineName();
Chris@0 105 $node_body = $this->randomMachineName();
Chris@0 106 $edit = [
Chris@0 107 'type' => $type2->id(),
Chris@0 108 'title' => $node_title,
Chris@0 109 'body' => [['value' => $node_body]],
Chris@0 110 'langcode' => $langcode,
Chris@0 111 ];
Chris@0 112 $node = $this->drupalCreateNode($edit);
Chris@0 113 // Edit the content and ensure correct language is selected.
Chris@0 114 $path = 'node/' . $node->id() . '/edit';
Chris@0 115 $this->drupalGet($path);
Chris@0 116 $this->assertRaw('<option value="' . $langcode . '" selected="selected">' . $name . '</option>', 'Correct language selected.');
Chris@0 117 // Ensure we can change the node language.
Chris@0 118 $edit = [
Chris@0 119 'langcode[0][value]' => 'en',
Chris@0 120 ];
Chris@0 121 $this->drupalPostForm($path, $edit, t('Save'));
Chris@0 122 $this->assertText(t('@title has been updated.', ['@title' => $node_title]));
Chris@0 123
Chris@0 124 // Verify that the creation message contains a link to a node.
Chris@0 125 $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'node/' . $node->id()]);
Chris@0 126 $this->assert(isset($view_link), 'The message area contains the link to the edited node');
Chris@0 127
Chris@0 128 $this->drupalLogout();
Chris@0 129 }
Chris@0 130
Chris@0 131 /**
Chris@0 132 * Test if a dir and lang tags exist in node's attributes.
Chris@0 133 */
Chris@0 134 public function testContentTypeDirLang() {
Chris@0 135 $type = $this->drupalCreateContentType();
Chris@0 136
Chris@0 137 // User to add and remove language.
Chris@0 138 $admin_user = $this->drupalCreateUser(['administer languages', 'administer content types', 'access administration pages']);
Chris@0 139 // User to create a node.
Chris@0 140 $web_user = $this->drupalCreateUser(["create {$type->id()} content", "edit own {$type->id()} content"]);
Chris@0 141
Chris@0 142 // Log in as admin.
Chris@0 143 $this->drupalLogin($admin_user);
Chris@0 144
Chris@0 145 // Install Arabic language.
Chris@0 146 $edit = [];
Chris@0 147 $edit['predefined_langcode'] = 'ar';
Chris@0 148 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
Chris@0 149
Chris@0 150 // Install Spanish language.
Chris@0 151 $edit = [];
Chris@0 152 $edit['predefined_langcode'] = 'es';
Chris@0 153 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
Chris@0 154 \Drupal::languageManager()->reset();
Chris@0 155
Chris@0 156 // Set the content type to use multilingual support.
Chris@0 157 $this->drupalGet("admin/structure/types/manage/{$type->id()}");
Chris@0 158 $edit = [
Chris@0 159 'language_configuration[language_alterable]' => TRUE,
Chris@0 160 ];
Chris@0 161 $this->drupalPostForm("admin/structure/types/manage/{$type->id()}", $edit, t('Save content type'));
Chris@0 162 $this->assertRaw(t('The content type %type has been updated.', ['%type' => $type->label()]));
Chris@0 163 $this->drupalLogout();
Chris@0 164
Chris@0 165 // Log in as web user to add new node.
Chris@0 166 $this->drupalLogin($web_user);
Chris@0 167
Chris@0 168 // Create three nodes: English, Arabic and Spanish.
Chris@0 169 $nodes = [];
Chris@0 170 foreach (['en', 'es', 'ar'] as $langcode) {
Chris@0 171 $nodes[$langcode] = $this->drupalCreateNode([
Chris@0 172 'langcode' => $langcode,
Chris@0 173 'type' => $type->id(),
Chris@0 174 'promote' => NodeInterface::PROMOTED,
Chris@0 175 ]);
Chris@0 176 }
Chris@0 177
Chris@0 178 // Check if English node does not have lang tag.
Chris@0 179 $this->drupalGet('node/' . $nodes['en']->id());
Chris@0 180 $element = $this->cssSelect('article.node[lang="en"]');
Chris@0 181 $this->assertTrue(empty($element), 'The lang tag has not been assigned to the English node.');
Chris@0 182
Chris@0 183 // Check if English node does not have dir tag.
Chris@0 184 $element = $this->cssSelect('article.node[dir="ltr"]');
Chris@0 185 $this->assertTrue(empty($element), 'The dir tag has not been assigned to the English node.');
Chris@0 186
Chris@0 187 // Check if Arabic node has lang="ar" & dir="rtl" tags.
Chris@0 188 $this->drupalGet('node/' . $nodes['ar']->id());
Chris@0 189 $element = $this->cssSelect('article.node[lang="ar"][dir="rtl"]');
Chris@0 190 $this->assertTrue(!empty($element), 'The lang and dir tags have been assigned correctly to the Arabic node.');
Chris@0 191
Chris@0 192 // Check if Spanish node has lang="es" tag.
Chris@0 193 $this->drupalGet('node/' . $nodes['es']->id());
Chris@0 194 $element = $this->cssSelect('article.node[lang="es"]');
Chris@0 195 $this->assertTrue(!empty($element), 'The lang tag has been assigned correctly to the Spanish node.');
Chris@0 196
Chris@0 197 // Check if Spanish node does not have dir="ltr" tag.
Chris@0 198 $element = $this->cssSelect('article.node[lang="es"][dir="ltr"]');
Chris@0 199 $this->assertTrue(empty($element), 'The dir tag has not been assigned to the Spanish node.');
Chris@0 200 }
Chris@0 201
Chris@0 202 }