LocaleContentTest.php 8.9 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 22 23

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

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  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.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'administer content types', 'access administration pages'));

    // 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';
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));

    $edit = array();
    $edit['site_default'] = 'ar';
    $this->drupalPost(NULL, $edit, t('Save configuration'));

    // 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() {
    global $base_url;

66 67 68
    $type1 = $this->drupalCreateContentType();
    $type2 = $this->drupalCreateContentType();

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

    // 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',
    );
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add custom language'));

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

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 117 118 119 120 121 122 123 124 125 126 127 128 129
      'title' => $node_title,
      'body' => array($langcode => array(array('value' => $node_body))),
      'langcode' => $langcode,
    );
    $node = $this->drupalCreateNode($edit);
    // Edit the content and ensure correct language is selected.
    $path = 'node/' . $node->nid . '/edit';
    $this->drupalGet($path);
    $this->assertRaw('<option value="' . $langcode . '" selected="selected">' .  $name . '</option>', t('Correct language selected.'));
    // Ensure we can change the node language.
    $edit = array(
      'langcode' => 'en',
    );
    $this->drupalPost($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 152 153 154 155 156 157 158

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

    // Install Arabic language.
    $edit = array();
    $edit['predefined_langcode'] = 'ar';
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));

    // Install Spanish language.
    $edit = array();
    $edit['predefined_langcode'] = 'es';
    $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));

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

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

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

    $this->drupalGet('node');

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

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

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

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

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

    $this->drupalLogout();
  }

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

  /**
   *  Test filtering Node content by language.
   */
  function testNodeAdminLanguageFilter() {
    // 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.
    $this->drupalPost('admin/config/regional/language/edit/en', array('locale_translate_english' => TRUE), t('Save language'));
    $this->drupalPost('admin/config/regional/language/add', array('predefined_langcode' => 'zh-hant'), t('Add language'));

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

    $this->drupalGet('admin/content');

    // Verify filtering by language.
    $edit = array(
      'langcode' => 'zh-hant',
    );
    $this->drupalPost(NULL, $edit, t('Filter'));

    $this->assertLinkByHref('node/' . $node_zh_hant->nid . '/edit');
    $this->assertNoLinkByHref('node/' . $node_en->nid . '/edit');
  }

237
}