function LocaleContentTest::testContentTypeDirLang

Test if a dir and lang tags exist in node's attributes.

File

drupal/core/modules/locale/lib/Drupal/locale/Tests/LocaleContentTest.php, line 141
Definition of Drupal\locale\Tests\LocaleContentTest.

Class

LocaleContentTest
Functional tests for multilingual support on nodes.

Namespace

Drupal\locale\Tests

Code

function testContentTypeDirLang() {
  $type = $this
    ->drupalCreateContentType();

  // User to add and remove language.
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer languages',
    'administer content types',
    'access administration pages',
  ));

  // User to create a node.
  $web_user = $this
    ->drupalCreateUser(array(
    "create {$type->type} content",
    "edit own {$type->type} content",
  ));

  // 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'));
  drupal_static_reset('language_list');

  // Set the content type to use multilingual support.
  $this
    ->drupalGet("admin/structure/types/manage/{$type->type}");
  $edit = array(
    'language_configuration[language_show]' => TRUE,
  );
  $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,
  )));
  $this
    ->drupalLogout();

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

  // Create three nodes: English, Arabic and Spanish.
  $nodes = array();
  foreach (array(
    'en',
    'es',
    'ar',
  ) as $langcode) {
    $nodes[$langcode] = $this
      ->drupalCreateNode(array(
      'langcode' => $langcode,
      'type' => $type->type,
      'promote' => NODE_PROMOTED,
    ));
  }

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

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

  // Check if Arabic node has lang="ar" & dir="rtl" tags.
  $this
    ->drupalGet('node/' . $nodes['ar']->nid);
  $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.');

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

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