Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/comment/tests/src/Functional/CommentLanguageTest.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\comment\Functional; | |
4 | |
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; | |
10 | |
11 /** | |
12 * Tests for comment language. | |
13 * | |
14 * @group comment | |
15 */ | |
16 class CommentLanguageTest extends BrowserTestBase { | |
17 | |
18 use CommentTestTrait; | |
19 | |
20 /** | |
21 * Modules to install. | |
22 * | |
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 | |
25 * that. | |
26 * | |
27 * @var array | |
28 */ | |
29 public static $modules = ['node', 'language', 'language_test', 'comment_test']; | |
30 | |
31 protected function setUp() { | |
32 parent::setUp(); | |
33 | |
34 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']); | |
35 | |
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); | |
39 | |
40 // Add language. | |
41 $edit = ['predefined_langcode' => 'fr']; | |
42 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); | |
43 | |
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')); | |
47 | |
48 // Enable content language negotiation UI. | |
49 \Drupal::state()->set('language_test.content_language_type', TRUE); | |
50 | |
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 | |
54 // detected. | |
55 $edit = [ | |
56 'language_interface[enabled][language-user]' => TRUE, | |
57 'language_content[enabled][language-url]' => TRUE, | |
58 'language_content[enabled][language-interface]' => FALSE, | |
59 ]; | |
60 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); | |
61 | |
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')); | |
66 | |
67 // Create comment field on article. | |
68 $this->addDefaultCommentField('node', 'article'); | |
69 | |
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.'); | |
75 } | |
76 | |
77 /** | |
78 * Test that comment language is properly set. | |
79 */ | |
80 public function testCommentLanguage() { | |
81 | |
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(); | |
91 $edit = [ | |
92 'title[0][value]' => $title, | |
93 'body[0][value]' => $this->randomMachineName(), | |
94 'langcode[0][value]' => $node_langcode, | |
95 'comment[0][status]' => CommentItemInterface::OPEN, | |
96 ]; | |
97 $this->drupalPostForm("node/add/article", $edit, t('Save')); | |
98 $node = $this->drupalGetNodeByTitle($title); | |
99 | |
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(); | |
105 $edit = [ | |
106 'subject[0][value]' => $this->randomMachineName(), | |
107 'comment_body[0][value]' => $comment_values[$node_langcode][$langcode], | |
108 ]; | |
109 $this->drupalPostForm($prefix . 'node/' . $node->id(), $edit, t('Preview')); | |
110 $this->drupalPostForm(NULL, $edit, t('Save')); | |
111 | |
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') | |
118 ->range(0, 1) | |
119 ->execute(); | |
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.'); | |
124 } | |
125 } | |
126 | |
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); | |
132 } | |
133 } | |
134 } | |
135 | |
136 } |