You are here

function CommentLanguageTest::testCommentLanguage in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/comment/src/Tests/CommentLanguageTest.php \Drupal\comment\Tests\CommentLanguageTest::testCommentLanguage()

Test that comment language is properly set.

File

core/modules/comment/src/Tests/CommentLanguageTest.php, line 84
Contains \Drupal\comment\Tests\CommentLanguageTest.

Class

CommentLanguageTest
Tests for comment language.

Namespace

Drupal\comment\Tests

Code

function testCommentLanguage() {

  // Create two nodes, one for english and one for french, and comment each
  // node using both english and french as content language by changing URL
  // language prefixes. Meanwhile interface language is always French, which
  // is the user language preference. This way we can ensure that node
  // language and interface language do not influence comment language, as
  // only content language has to.
  foreach ($this->container
    ->get('language_manager')
    ->getLanguages() as $node_langcode => $node_language) {

    // Create "Article" content.
    $title = $this
      ->randomMachineName();
    $edit = array(
      'title[0][value]' => $title,
      'body[0][value]' => $this
        ->randomMachineName(),
      'langcode[0][value]' => $node_langcode,
      'comment[0][status]' => CommentItemInterface::OPEN,
    );
    $this
      ->drupalPostForm("node/add/article", $edit, t('Save'));
    $node = $this
      ->drupalGetNodeByTitle($title);
    $prefixes = language_negotiation_url_prefixes();
    foreach ($this->container
      ->get('language_manager')
      ->getLanguages() as $langcode => $language) {

      // Post a comment with content language $langcode.
      $prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/';
      $comment_values[$node_langcode][$langcode] = $this
        ->randomMachineName();
      $edit = array(
        'subject[0][value]' => $this
          ->randomMachineName(),
        'comment_body[0][value]' => $comment_values[$node_langcode][$langcode],
      );
      $this
        ->drupalPostForm($prefix . 'node/' . $node
        ->id(), $edit, t('Preview'));
      $this
        ->drupalPostForm(NULL, $edit, t('Save'));

      // Check that comment language matches the current content language.
      $cids = \Drupal::entityQuery('comment')
        ->condition('entity_id', $node
        ->id())
        ->condition('entity_type', 'node')
        ->condition('field_name', 'comment')
        ->sort('cid', 'DESC')
        ->range(0, 1)
        ->execute();
      $comment = Comment::load(reset($cids));
      $args = array(
        '%node_language' => $node_langcode,
        '%comment_language' => $comment->langcode->value,
        '%langcode' => $langcode,
      );
      $this
        ->assertEqual($comment->langcode->value, $langcode, format_string('The comment posted with content language %langcode and belonging to the node with language %node_language has language %comment_language', $args));
      $this
        ->assertEqual($comment->comment_body->value, $comment_values[$node_langcode][$langcode], 'Comment body correctly stored.');
    }
  }

  // Check that comment bodies appear in the administration UI.
  $this
    ->drupalGet('admin/content/comment');
  foreach ($comment_values as $node_values) {
    foreach ($node_values as $value) {
      $this
        ->assertRaw($value);
    }
  }
}