3 namespace Drupal\comment\Tests;
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\content_translation\Tests\ContentTranslationUITestBase;
7 use Drupal\language\Entity\ConfigurableLanguage;
10 * Tests the Comment Translation UI.
14 class CommentTranslationUITest extends ContentTranslationUITestBase {
19 * The subject of the test comment.
24 * An administrative user with permission to administer comments.
26 * @var \Drupal\user\UserInterface
33 protected $defaultCacheContexts = [
34 'languages:language_interface',
38 'url.query_args:_wrapper_format',
39 'url.query_args.pagers:0',
49 public static $modules = ['language', 'content_translation', 'node', 'comment'];
51 protected function setUp() {
52 $this->entityTypeId = 'comment';
53 $this->nodeBundle = 'article';
54 $this->bundle = 'comment_article';
55 $this->testLanguageSelector = FALSE;
56 $this->subject = $this->randomMachineName();
63 public function setupBundle() {
64 parent::setupBundle();
65 $this->drupalCreateContentType(['type' => $this->nodeBundle, 'name' => $this->nodeBundle]);
66 // Add a comment field to the article content type.
67 $this->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
68 // Create a page content type.
69 $this->drupalCreateContentType(['type' => 'page', 'name' => 'page']);
70 // Add a comment field to the page content type - this one won't be
72 $this->addDefaultCommentField('node', 'page', 'comment');
73 // Mark this bundle as translatable.
74 $this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE);
80 protected function getTranslatorPermissions() {
81 return array_merge(parent::getTranslatorPermissions(), ['post comments', 'administer comments', 'access comments']);
87 protected function createEntity($values, $langcode, $comment_type = 'comment_article') {
88 if ($comment_type == 'comment_article') {
89 // This is the article node type, with the 'comment_article' field.
90 $node_type = 'article';
91 $field_name = 'comment_article';
94 // This is the page node type with the non-translatable 'comment' field.
96 $field_name = 'comment';
98 $node = $this->drupalCreateNode([
101 ['status' => CommentItemInterface::OPEN]
104 $values['entity_id'] = $node->id();
105 $values['entity_type'] = 'node';
106 $values['field_name'] = $field_name;
107 $values['uid'] = $node->getOwnerId();
108 return parent::createEntity($values, $langcode, $comment_type);
114 protected function getNewEntityValues($langcode) {
115 // Comment subject is not translatable hence we use a fixed value.
117 'subject' => [['value' => $this->subject]],
118 'comment_body' => [['value' => $this->randomMachineName(16)]],
119 ] + parent::getNewEntityValues($langcode);
125 protected function doTestPublishedStatus() {
126 $entity_manager = \Drupal::entityManager();
127 $storage = $entity_manager->getStorage($this->entityTypeId);
129 $storage->resetCache();
130 $entity = $storage->load($this->entityId);
132 // Unpublish translations.
133 foreach ($this->langcodes as $index => $langcode) {
135 $edit = ['status' => 0];
136 $url = $entity->urlInfo('edit-form', ['language' => ConfigurableLanguage::load($langcode)]);
137 $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
138 $storage->resetCache();
139 $entity = $storage->load($this->entityId);
140 $this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished(), 'The translation has been correctly unpublished.');
148 protected function doTestAuthoringInfo() {
149 $storage = $this->container->get('entity_type.manager')
150 ->getStorage($this->entityTypeId);
151 $storage->resetCache([$this->entityId]);
152 $entity = $storage->load($this->entityId);
153 $languages = $this->container->get('language_manager')->getLanguages();
156 // Post different authoring information for each translation.
157 foreach ($this->langcodes as $langcode) {
158 $url = $entity->urlInfo('edit-form', ['language' => $languages[$langcode]]);
159 $user = $this->drupalCreateUser();
160 $values[$langcode] = [
161 'uid' => $user->id(),
162 'created' => REQUEST_TIME - mt_rand(0, 1000),
165 'uid' => $user->getUsername() . ' (' . $user->id() . ')',
166 'date[date]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d'),
167 'date[time]' => format_date($values[$langcode]['created'], 'custom', 'H:i:s'),
169 $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
172 $storage->resetCache([$this->entityId]);
173 $entity = $storage->load($this->entityId);
174 foreach ($this->langcodes as $langcode) {
175 $metadata = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
176 $this->assertEqual($metadata->getAuthor()->id(), $values[$langcode]['uid'], 'Translation author correctly stored.');
177 $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.');
182 * Tests translate link on comment content admin page.
184 public function testTranslateLinkCommentAdminPage() {
185 $this->adminUser = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), ['access administration pages', 'administer comments', 'skip comment approval']));
186 $this->drupalLogin($this->adminUser);
188 $cid_translatable = $this->createEntity([], $this->langcodes[0]);
189 $cid_untranslatable = $this->createEntity([], $this->langcodes[0], 'comment');
191 // Verify translation links.
192 $this->drupalGet('admin/content/comment');
193 $this->assertResponse(200);
194 $this->assertLinkByHref('comment/' . $cid_translatable . '/translations');
195 $this->assertNoLinkByHref('comment/' . $cid_untranslatable . '/translations');
201 protected function doTestTranslationEdit() {
202 $storage = $this->container->get('entity_type.manager')
203 ->getStorage($this->entityTypeId);
204 $storage->resetCache([$this->entityId]);
205 $entity = $storage->load($this->entityId);
206 $languages = $this->container->get('language_manager')->getLanguages();
208 foreach ($this->langcodes as $langcode) {
209 // We only want to test the title for non-english translations.
210 if ($langcode != 'en') {
211 $options = ['language' => $languages[$langcode]];
212 $url = $entity->urlInfo('edit-form', $options);
213 $this->drupalGet($url);
215 $title = t('Edit @type @title [%language translation]', [
216 '@type' => $this->entityTypeId,
217 '@title' => $entity->getTranslation($langcode)->label(),
218 '%language' => $languages[$langcode]->getName(),
220 $this->assertRaw($title);