View source
<?php
namespace Drupal\comment\Tests;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\WebTestBase;
class CommentLanguageTest extends WebTestBase {
use CommentTestTrait;
public static $modules = array(
'node',
'language',
'language_test',
'comment_test',
);
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
$admin_user = $this
->drupalCreateUser(array(
'administer site configuration',
'administer languages',
'access administration pages',
'administer content types',
'administer comments',
'create article content',
'access comments',
'post comments',
'skip comment approval',
));
$this
->drupalLogin($admin_user);
$edit = array(
'predefined_langcode' => 'fr',
);
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
$edit = array(
'language_configuration[language_alterable]' => TRUE,
);
$this
->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
\Drupal::state()
->set('language_test.content_language_type', TRUE);
$edit = array(
'language_interface[enabled][language-user]' => TRUE,
'language_content[enabled][language-url]' => TRUE,
'language_content[enabled][language-interface]' => FALSE,
);
$this
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$edit = array(
'preferred_langcode' => 'fr',
);
$this
->drupalPostForm("user/" . $admin_user
->id() . "/edit", $edit, t('Save'));
$this
->addDefaultCommentField('node', 'article');
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
$field_storage
->setTranslatable(TRUE);
$field_storage
->save();
$this
->assertTrue($field_storage
->isTranslatable(), 'Comment body is translatable.');
}
function testCommentLanguage() {
foreach ($this->container
->get('language_manager')
->getLanguages() as $node_langcode => $node_language) {
$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) {
$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'));
$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.');
}
}
$this
->drupalGet('admin/content/comment');
foreach ($comment_values as $node_values) {
foreach ($node_values as $value) {
$this
->assertRaw($value);
}
}
}
}