Chris@0: drupalCreateContentType(['type' => 'article', 'name' => 'Article']); Chris@0: Chris@0: // Create and log in user. Chris@0: $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: $this->drupalLogin($admin_user); Chris@0: Chris@0: // Add language. Chris@0: $edit = ['predefined_langcode' => 'fr']; Chris@0: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); Chris@0: Chris@0: // Set "Article" content type to use multilingual support. Chris@0: $edit = ['language_configuration[language_alterable]' => TRUE]; Chris@0: $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type')); Chris@0: Chris@0: // Enable content language negotiation UI. Chris@0: \Drupal::state()->set('language_test.content_language_type', TRUE); Chris@0: Chris@0: // Set interface language detection to user and content language detection Chris@0: // to URL. Disable inheritance from interface language to ensure content Chris@0: // language will fall back to the default language if no URL language can be Chris@0: // detected. Chris@0: $edit = [ Chris@0: 'language_interface[enabled][language-user]' => TRUE, Chris@0: 'language_content[enabled][language-url]' => TRUE, Chris@0: 'language_content[enabled][language-interface]' => FALSE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: Chris@0: // Change user language preference, this way interface language is always Chris@0: // French no matter what path prefix the URLs have. Chris@0: $edit = ['preferred_langcode' => 'fr']; Chris@0: $this->drupalPostForm("user/" . $admin_user->id() . "/edit", $edit, t('Save')); Chris@0: Chris@0: // Create comment field on article. Chris@0: $this->addDefaultCommentField('node', 'article'); Chris@0: Chris@0: // Make comment body translatable. Chris@0: $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body'); Chris@0: $field_storage->setTranslatable(TRUE); Chris@0: $field_storage->save(); Chris@0: $this->assertTrue($field_storage->isTranslatable(), 'Comment body is translatable.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test that comment language is properly set. Chris@0: */ Chris@0: public function testCommentLanguage() { Chris@0: Chris@0: // Create two nodes, one for english and one for french, and comment each Chris@0: // node using both english and french as content language by changing URL Chris@0: // language prefixes. Meanwhile interface language is always French, which Chris@0: // is the user language preference. This way we can ensure that node Chris@0: // language and interface language do not influence comment language, as Chris@0: // only content language has to. Chris@0: foreach ($this->container->get('language_manager')->getLanguages() as $node_langcode => $node_language) { Chris@0: // Create "Article" content. Chris@0: $title = $this->randomMachineName(); Chris@0: $edit = [ Chris@0: 'title[0][value]' => $title, Chris@0: 'body[0][value]' => $this->randomMachineName(), Chris@0: 'langcode[0][value]' => $node_langcode, Chris@0: 'comment[0][status]' => CommentItemInterface::OPEN, Chris@0: ]; Chris@0: $this->drupalPostForm("node/add/article", $edit, t('Save')); Chris@0: $node = $this->drupalGetNodeByTitle($title); Chris@0: Chris@0: $prefixes = language_negotiation_url_prefixes(); Chris@0: foreach ($this->container->get('language_manager')->getLanguages() as $langcode => $language) { Chris@0: // Post a comment with content language $langcode. Chris@0: $prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/'; Chris@0: $comment_values[$node_langcode][$langcode] = $this->randomMachineName(); Chris@0: $edit = [ Chris@0: 'subject[0][value]' => $this->randomMachineName(), Chris@0: 'comment_body[0][value]' => $comment_values[$node_langcode][$langcode], Chris@0: ]; Chris@0: $this->drupalPostForm($prefix . 'node/' . $node->id(), $edit, t('Preview')); Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: Chris@0: // Check that comment language matches the current content language. Chris@0: $cids = \Drupal::entityQuery('comment') Chris@0: ->condition('entity_id', $node->id()) Chris@0: ->condition('entity_type', 'node') Chris@0: ->condition('field_name', 'comment') Chris@0: ->sort('cid', 'DESC') Chris@0: ->range(0, 1) Chris@0: ->execute(); Chris@0: $comment = Comment::load(reset($cids)); Chris@0: $args = ['%node_language' => $node_langcode, '%comment_language' => $comment->langcode->value, '%langcode' => $langcode]; Chris@0: $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: $this->assertEqual($comment->comment_body->value, $comment_values[$node_langcode][$langcode], 'Comment body correctly stored.'); Chris@0: } Chris@0: } Chris@0: Chris@0: // Check that comment bodies appear in the administration UI. Chris@0: $this->drupalGet('admin/content/comment'); Chris@0: foreach ($comment_values as $node_values) { Chris@0: foreach ($node_values as $value) { Chris@0: $this->assertRaw($value); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: }