3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\comment\Entity\Comment;
6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
7 use Drupal\comment\Tests\CommentTestTrait;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\Tests\BrowserTestBase;
12 * Tests for comment language.
16 class CommentLanguageTest extends BrowserTestBase {
23 * We also use the language_test module here to be able to turn on content
24 * language negotiation. Drupal core does not provide a way in itself to do
29 public static $modules = ['node', 'language', 'language_test', 'comment_test'];
31 protected function setUp() {
34 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
36 // Create and log in user.
37 $admin_user = $this->drupalCreateUser(['administer site configuration', 'administer languages', 'access administration pages', 'administer content types', 'administer comments', 'create article content', 'access comments', 'post comments', 'skip comment approval']);
38 $this->drupalLogin($admin_user);
41 $edit = ['predefined_langcode' => 'fr'];
42 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
44 // Set "Article" content type to use multilingual support.
45 $edit = ['language_configuration[language_alterable]' => TRUE];
46 $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
48 // Enable content language negotiation UI.
49 \Drupal::state()->set('language_test.content_language_type', TRUE);
51 // Set interface language detection to user and content language detection
52 // to URL. Disable inheritance from interface language to ensure content
53 // language will fall back to the default language if no URL language can be
56 'language_interface[enabled][language-user]' => TRUE,
57 'language_content[enabled][language-url]' => TRUE,
58 'language_content[enabled][language-interface]' => FALSE,
60 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
62 // Change user language preference, this way interface language is always
63 // French no matter what path prefix the URLs have.
64 $edit = ['preferred_langcode' => 'fr'];
65 $this->drupalPostForm("user/" . $admin_user->id() . "/edit", $edit, t('Save'));
67 // Create comment field on article.
68 $this->addDefaultCommentField('node', 'article');
70 // Make comment body translatable.
71 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
72 $field_storage->setTranslatable(TRUE);
73 $field_storage->save();
74 $this->assertTrue($field_storage->isTranslatable(), 'Comment body is translatable.');
78 * Test that comment language is properly set.
80 public function testCommentLanguage() {
82 // Create two nodes, one for english and one for french, and comment each
83 // node using both english and french as content language by changing URL
84 // language prefixes. Meanwhile interface language is always French, which
85 // is the user language preference. This way we can ensure that node
86 // language and interface language do not influence comment language, as
87 // only content language has to.
88 foreach ($this->container->get('language_manager')->getLanguages() as $node_langcode => $node_language) {
89 // Create "Article" content.
90 $title = $this->randomMachineName();
92 'title[0][value]' => $title,
93 'body[0][value]' => $this->randomMachineName(),
94 'langcode[0][value]' => $node_langcode,
95 'comment[0][status]' => CommentItemInterface::OPEN,
97 $this->drupalPostForm("node/add/article", $edit, t('Save'));
98 $node = $this->drupalGetNodeByTitle($title);
100 $prefixes = language_negotiation_url_prefixes();
101 foreach ($this->container->get('language_manager')->getLanguages() as $langcode => $language) {
102 // Post a comment with content language $langcode.
103 $prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/';
104 $comment_values[$node_langcode][$langcode] = $this->randomMachineName();
106 'subject[0][value]' => $this->randomMachineName(),
107 'comment_body[0][value]' => $comment_values[$node_langcode][$langcode],
109 $this->drupalPostForm($prefix . 'node/' . $node->id(), $edit, t('Preview'));
110 $this->drupalPostForm(NULL, $edit, t('Save'));
112 // Check that comment language matches the current content language.
113 $cids = \Drupal::entityQuery('comment')
114 ->condition('entity_id', $node->id())
115 ->condition('entity_type', 'node')
116 ->condition('field_name', 'comment')
117 ->sort('cid', 'DESC')
120 $comment = Comment::load(reset($cids));
121 $args = ['%node_language' => $node_langcode, '%comment_language' => $comment->langcode->value, '%langcode' => $langcode];
122 $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));
123 $this->assertEqual($comment->comment_body->value, $comment_values[$node_langcode][$langcode], 'Comment body correctly stored.');
127 // Check that comment bodies appear in the administration UI.
128 $this->drupalGet('admin/content/comment');
129 foreach ($comment_values as $node_values) {
130 foreach ($node_values as $value) {
131 $this->assertRaw($value);