LocaleContentTest.php 9.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Definition of Drupal\locale\Tests\LocaleContentTest.
 */

namespace Drupal\locale\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Functional tests for multilingual support on nodes.
 */
class LocaleContentTest extends WebTestBase {
16 17 18 19 20 21

  /**
   * Modules to enable.
   *
   * @var array
   */
22
  public static $modules = array('node', 'locale');
23

24 25 26 27 28 29 30 31 32 33 34 35 36
  public static function getInfo() {
    return array(
      'name' => 'Content language settings',
      'description' => 'Checks you can enable multilingual support on content types and configure a language for a node.',
      'group' => 'Locale',
    );
  }

  /**
   * Verifies that machine name fields are always LTR.
   */
  function testMachineNameLTR() {
    // User to add and remove language.
37
    $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages', 'administer site configuration'));
38 39 40 41 42 43 44 45 46 47 48

    // Log in as admin.
    $this->drupalLogin($admin_user);

    // Verify that the machine name field is LTR for a new content type.
    $this->drupalGet('admin/structure/types/add');
    $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when no additional language is configured.');

    // Install the Arabic language (which is RTL) and configure as the default.
    $edit = array();
    $edit['predefined_langcode'] = 'ar';
49
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
50

51 52 53
    $edit = array(
      'site_default_language' => 'ar',
    );
54
    $this->drupalPostForm('admin/config/regional/settings', $edit, t('Save configuration'));
55 56 57 58 59 60 61 62 63 64

    // Verify that the machine name field is still LTR for a new content type.
    $this->drupalGet('admin/structure/types/add');
    $this->assertFieldByXpath('//input[@name="type" and @dir="ltr"]', NULL, 'The machine name field is LTR when the default language is RTL.');
  }

  /**
   * Test if a content type can be set to multilingual and language is present.
   */
  function testContentTypeLanguageConfiguration() {
65 66 67
    $type1 = $this->drupalCreateContentType();
    $type2 = $this->drupalCreateContentType();

68 69 70
    // User to add and remove language.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
    // User to create a node.
71
    $web_user = $this->drupalCreateUser(array("create {$type1->type} content", "create {$type2->type} content", "edit any {$type2->type} content"));
72 73 74 75 76 77 78 79 80 81 82 83 84

    // Add custom language.
    $this->drupalLogin($admin_user);
    // Code for the language.
    $langcode = 'xx';
    // The English name for the language.
    $name = $this->randomName(16);
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
      'name' => $name,
      'direction' => '0',
    );
85
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
86

87 88
    // Set the content type to use multilingual support.
    $this->drupalGet("admin/structure/types/manage/{$type2->type}");
89
    $this->assertText(t('Language settings'), 'Multilingual support widget present on content type configuration form.');
90
    $edit = array(
91
      'language_configuration[language_show]' => TRUE,
92
    );
93
    $this->drupalPostForm("admin/structure/types/manage/{$type2->type}", $edit, t('Save content type'));
94
    $this->assertRaw(t('The content type %type has been updated.', array('%type' => $type2->name)));
95
    $this->drupalLogout();
96
    \Drupal::languageManager()->reset();
97

98
    // Verify language selection is not present on the node add form.
99
    $this->drupalLogin($web_user);
100
    $this->drupalGet("node/add/{$type1->type}");
101
    // Verify language select list is not present.
102
    $this->assertNoFieldByName('language', NULL, 'Language select not present on the node add form.');
103

104 105
    // Verify language selection appears on the node add form.
    $this->drupalGet("node/add/{$type2->type}");
106
    // Verify language select list is present.
107
    $this->assertFieldByName('langcode', NULL, 'Language select present on the node add form.');
108
    // Ensure language appears.
109
    $this->assertText($name, 'Language present.');
110

111
    // Create a node.
112 113 114
    $node_title = $this->randomName();
    $node_body =  $this->randomName();
    $edit = array(
115
      'type' => $type2->type,
116
      'title' => $node_title,
117
      'body' => array(array('value' => $node_body)),
118 119 120 121
      'langcode' => $langcode,
    );
    $node = $this->drupalCreateNode($edit);
    // Edit the content and ensure correct language is selected.
122
    $path = 'node/' . $node->id() . '/edit';
123
    $this->drupalGet($path);
124
    $this->assertRaw('<option value="' . $langcode . '" selected="selected">' .  $name . '</option>', 'Correct language selected.');
125 126 127 128
    // Ensure we can change the node language.
    $edit = array(
      'langcode' => 'en',
    );
129
    $this->drupalPostForm($path, $edit, t('Save'));
130
    $this->assertRaw(t('%title has been updated.', array('%title' => $node_title)));
131 132 133 134 135 136 137 138

    $this->drupalLogout();
  }

  /**
   * Test if a dir and lang tags exist in node's attributes.
   */
  function testContentTypeDirLang() {
139 140
    $type = $this->drupalCreateContentType();

141 142 143
    // User to add and remove language.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));
    // User to create a node.
144
    $web_user = $this->drupalCreateUser(array("create {$type->type} content", "edit own {$type->type} content"));
145 146 147 148 149 150 151

    // Login as admin.
    $this->drupalLogin($admin_user);

    // Install Arabic language.
    $edit = array();
    $edit['predefined_langcode'] = 'ar';
152
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
153 154 155 156

    // Install Spanish language.
    $edit = array();
    $edit['predefined_langcode'] = 'es';
157
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
158
    \Drupal::languageManager()->reset();
159

160 161
    // Set the content type to use multilingual support.
    $this->drupalGet("admin/structure/types/manage/{$type->type}");
162
    $edit = array(
163
      'language_configuration[language_show]' => TRUE,
164
    );
165
    $this->drupalPostForm("admin/structure/types/manage/{$type->type}", $edit, t('Save content type'));
166
    $this->assertRaw(t('The content type %type has been updated.', array('%type' => $type->name)));
167 168
    $this->drupalLogout();

169
    // Login as web user to add new node.
170 171 172
    $this->drupalLogin($web_user);

    // Create three nodes: English, Arabic and Spanish.
173 174 175 176 177 178 179 180
    $nodes = array();
    foreach (array('en', 'es', 'ar') as $langcode) {
      $nodes[$langcode] = $this->drupalCreateNode(array(
        'langcode' => $langcode,
        'type' => $type->type,
        'promote' => NODE_PROMOTED,
      ));
    }
181 182 183


    // Check if English node does not have lang tag.
184
    $this->drupalGet('node/' . $nodes['en']->id());
185
    $pattern = '|class="[^"]*node[^"]*"[^<>]*lang="en"|';
186
    $this->assertNoPattern($pattern, 'The lang tag has not been assigned to the English node.');
187 188

    // Check if English node does not have dir tag.
189
    $pattern = '|class="[^"]*node[^"]*"[^<>]*dir="ltr"|';
190
    $this->assertNoPattern($pattern, 'The dir tag has not been assigned to the English node.');
191 192

    // Check if Arabic node has lang="ar" & dir="rtl" tags.
193
    $this->drupalGet('node/' . $nodes['ar']->id());
194
    $pattern = '|class="[^"]*node[^"]*"[^<>]*lang="ar" dir="rtl"|';
195
    $this->assertPattern($pattern, 'The lang and dir tags have been assigned correctly to the Arabic node.');
196 197

    // Check if Spanish node has lang="es" tag.
198
    $this->drupalGet('node/' . $nodes['es']->id());
199
    $pattern = '|class="[^"]*node[^"]*"[^<>]*lang="es"|';
200
    $this->assertPattern($pattern, 'The lang tag has been assigned correctly to the Spanish node.');
201 202

    // Check if Spanish node does not have dir="ltr" tag.
203
    $pattern = '|class="[^"]*node[^"]*"[^<>]*lang="es" dir="ltr"|';
204
    $this->assertNoPattern($pattern, 'The dir tag has not been assigned to the Spanish node.');
205 206
  }

207 208 209 210 211

  /**
   *  Test filtering Node content by language.
   */
  function testNodeAdminLanguageFilter() {
212
    \Drupal::moduleHandler()->install(array('views'));
213 214 215 216 217 218 219
    // User to add and remove language.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'access content overview', 'administer nodes', 'bypass node access'));

    // Log in as admin.
    $this->drupalLogin($admin_user);

    // Enable multiple languages.
220 221
    $this->drupalPostForm('admin/config/regional/language/edit/en', array('locale_translate_english' => TRUE), t('Save language'));
    $this->drupalPostForm('admin/config/regional/language/add', array('predefined_langcode' => 'zh-hant'), t('Add language'));
222 223 224 225 226 227

    // Create two nodes: English and Chinese.
    $node_en = $this->drupalCreateNode(array('langcode' => 'en'));
    $node_zh_hant = $this->drupalCreateNode(array('langcode' => 'zh-hant'));

    // Verify filtering by language.
228
    $this->drupalGet('admin/content', array('query' => array('langcode' => 'zh-hant')));
229 230
    $this->assertLinkByHref('node/' . $node_zh_hant->id() . '/edit');
    $this->assertNoLinkByHref('node/' . $node_en->id() . '/edit');
231 232
  }

233
}