Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\comment\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\Entity\Comment;
|
Chris@0
|
6 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
Chris@0
|
7 use Drupal\comment\Tests\CommentTestTrait;
|
Chris@0
|
8 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
9 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Tests for comment language.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group comment
|
Chris@0
|
15 */
|
Chris@0
|
16 class CommentLanguageTest extends BrowserTestBase {
|
Chris@0
|
17
|
Chris@0
|
18 use CommentTestTrait;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * Modules to install.
|
Chris@0
|
22 *
|
Chris@0
|
23 * We also use the language_test module here to be able to turn on content
|
Chris@0
|
24 * language negotiation. Drupal core does not provide a way in itself to do
|
Chris@0
|
25 * that.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var array
|
Chris@0
|
28 */
|
Chris@0
|
29 public static $modules = ['node', 'language', 'language_test', 'comment_test'];
|
Chris@0
|
30
|
Chris@0
|
31 protected function setUp() {
|
Chris@0
|
32 parent::setUp();
|
Chris@0
|
33
|
Chris@0
|
34 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
|
Chris@0
|
35
|
Chris@0
|
36 // Create and log in user.
|
Chris@0
|
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']);
|
Chris@0
|
38 $this->drupalLogin($admin_user);
|
Chris@0
|
39
|
Chris@0
|
40 // Add language.
|
Chris@0
|
41 $edit = ['predefined_langcode' => 'fr'];
|
Chris@0
|
42 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
|
Chris@0
|
43
|
Chris@0
|
44 // Set "Article" content type to use multilingual support.
|
Chris@0
|
45 $edit = ['language_configuration[language_alterable]' => TRUE];
|
Chris@0
|
46 $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
|
Chris@0
|
47
|
Chris@0
|
48 // Enable content language negotiation UI.
|
Chris@0
|
49 \Drupal::state()->set('language_test.content_language_type', TRUE);
|
Chris@0
|
50
|
Chris@0
|
51 // Set interface language detection to user and content language detection
|
Chris@0
|
52 // to URL. Disable inheritance from interface language to ensure content
|
Chris@0
|
53 // language will fall back to the default language if no URL language can be
|
Chris@0
|
54 // detected.
|
Chris@0
|
55 $edit = [
|
Chris@0
|
56 'language_interface[enabled][language-user]' => TRUE,
|
Chris@0
|
57 'language_content[enabled][language-url]' => TRUE,
|
Chris@0
|
58 'language_content[enabled][language-interface]' => FALSE,
|
Chris@0
|
59 ];
|
Chris@0
|
60 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
|
Chris@0
|
61
|
Chris@0
|
62 // Change user language preference, this way interface language is always
|
Chris@0
|
63 // French no matter what path prefix the URLs have.
|
Chris@0
|
64 $edit = ['preferred_langcode' => 'fr'];
|
Chris@0
|
65 $this->drupalPostForm("user/" . $admin_user->id() . "/edit", $edit, t('Save'));
|
Chris@0
|
66
|
Chris@0
|
67 // Create comment field on article.
|
Chris@0
|
68 $this->addDefaultCommentField('node', 'article');
|
Chris@0
|
69
|
Chris@0
|
70 // Make comment body translatable.
|
Chris@0
|
71 $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
Chris@0
|
72 $field_storage->setTranslatable(TRUE);
|
Chris@0
|
73 $field_storage->save();
|
Chris@0
|
74 $this->assertTrue($field_storage->isTranslatable(), 'Comment body is translatable.');
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Test that comment language is properly set.
|
Chris@0
|
79 */
|
Chris@0
|
80 public function testCommentLanguage() {
|
Chris@0
|
81
|
Chris@0
|
82 // Create two nodes, one for english and one for french, and comment each
|
Chris@0
|
83 // node using both english and french as content language by changing URL
|
Chris@0
|
84 // language prefixes. Meanwhile interface language is always French, which
|
Chris@0
|
85 // is the user language preference. This way we can ensure that node
|
Chris@0
|
86 // language and interface language do not influence comment language, as
|
Chris@0
|
87 // only content language has to.
|
Chris@0
|
88 foreach ($this->container->get('language_manager')->getLanguages() as $node_langcode => $node_language) {
|
Chris@0
|
89 // Create "Article" content.
|
Chris@0
|
90 $title = $this->randomMachineName();
|
Chris@0
|
91 $edit = [
|
Chris@0
|
92 'title[0][value]' => $title,
|
Chris@0
|
93 'body[0][value]' => $this->randomMachineName(),
|
Chris@0
|
94 'langcode[0][value]' => $node_langcode,
|
Chris@0
|
95 'comment[0][status]' => CommentItemInterface::OPEN,
|
Chris@0
|
96 ];
|
Chris@0
|
97 $this->drupalPostForm("node/add/article", $edit, t('Save'));
|
Chris@0
|
98 $node = $this->drupalGetNodeByTitle($title);
|
Chris@0
|
99
|
Chris@0
|
100 $prefixes = language_negotiation_url_prefixes();
|
Chris@0
|
101 foreach ($this->container->get('language_manager')->getLanguages() as $langcode => $language) {
|
Chris@0
|
102 // Post a comment with content language $langcode.
|
Chris@0
|
103 $prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/';
|
Chris@0
|
104 $comment_values[$node_langcode][$langcode] = $this->randomMachineName();
|
Chris@0
|
105 $edit = [
|
Chris@0
|
106 'subject[0][value]' => $this->randomMachineName(),
|
Chris@0
|
107 'comment_body[0][value]' => $comment_values[$node_langcode][$langcode],
|
Chris@0
|
108 ];
|
Chris@0
|
109 $this->drupalPostForm($prefix . 'node/' . $node->id(), $edit, t('Preview'));
|
Chris@0
|
110 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
111
|
Chris@0
|
112 // Check that comment language matches the current content language.
|
Chris@0
|
113 $cids = \Drupal::entityQuery('comment')
|
Chris@0
|
114 ->condition('entity_id', $node->id())
|
Chris@0
|
115 ->condition('entity_type', 'node')
|
Chris@0
|
116 ->condition('field_name', 'comment')
|
Chris@0
|
117 ->sort('cid', 'DESC')
|
Chris@0
|
118 ->range(0, 1)
|
Chris@0
|
119 ->execute();
|
Chris@0
|
120 $comment = Comment::load(reset($cids));
|
Chris@0
|
121 $args = ['%node_language' => $node_langcode, '%comment_language' => $comment->langcode->value, '%langcode' => $langcode];
|
Chris@0
|
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));
|
Chris@0
|
123 $this->assertEqual($comment->comment_body->value, $comment_values[$node_langcode][$langcode], 'Comment body correctly stored.');
|
Chris@0
|
124 }
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 // Check that comment bodies appear in the administration UI.
|
Chris@0
|
128 $this->drupalGet('admin/content/comment');
|
Chris@0
|
129 foreach ($comment_values as $node_values) {
|
Chris@0
|
130 foreach ($node_values as $value) {
|
Chris@0
|
131 $this->assertRaw($value);
|
Chris@0
|
132 }
|
Chris@0
|
133 }
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 }
|