3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\comment\Tests\CommentTestTrait;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase;
11 * Tests the Comment Translation UI.
15 class CommentTranslationUITest extends ContentTranslationUITestBase {
20 * The subject of the test comment.
25 * An administrative user with permission to administer comments.
27 * @var \Drupal\user\UserInterface
34 protected $defaultCacheContexts = [
35 'languages:language_interface',
39 'url.query_args:_wrapper_format',
40 'url.query_args.pagers:0',
50 public static $modules = ['language', 'content_translation', 'node', 'comment'];
52 protected function setUp() {
53 $this->entityTypeId = 'comment';
54 $this->nodeBundle = 'article';
55 $this->bundle = 'comment_article';
56 $this->testLanguageSelector = FALSE;
57 $this->subject = $this->randomMachineName();
64 public function setupBundle() {
65 parent::setupBundle();
66 $this->drupalCreateContentType(['type' => $this->nodeBundle, 'name' => $this->nodeBundle]);
67 // Add a comment field to the article content type.
68 $this->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
69 // Create a page content type.
70 $this->drupalCreateContentType(['type' => 'page', 'name' => 'page']);
71 // Add a comment field to the page content type - this one won't be
73 $this->addDefaultCommentField('node', 'page', 'comment');
74 // Mark this bundle as translatable.
75 $this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE);
81 protected function getTranslatorPermissions() {
82 return array_merge(parent::getTranslatorPermissions(), ['post comments', 'administer comments', 'access comments']);
88 protected function createEntity($values, $langcode, $comment_type = 'comment_article') {
89 if ($comment_type == 'comment_article') {
90 // This is the article node type, with the 'comment_article' field.
91 $node_type = 'article';
92 $field_name = 'comment_article';
95 // This is the page node type with the non-translatable 'comment' field.
97 $field_name = 'comment';
99 $node = $this->drupalCreateNode([
100 'type' => $node_type,
102 ['status' => CommentItemInterface::OPEN]
105 $values['entity_id'] = $node->id();
106 $values['entity_type'] = 'node';
107 $values['field_name'] = $field_name;
108 $values['uid'] = $node->getOwnerId();
109 return parent::createEntity($values, $langcode, $comment_type);
115 protected function getNewEntityValues($langcode) {
116 // Comment subject is not translatable hence we use a fixed value.
118 'subject' => [['value' => $this->subject]],
119 'comment_body' => [['value' => $this->randomMachineName(16)]],
120 ] + parent::getNewEntityValues($langcode);
126 protected function doTestPublishedStatus() {
127 $entity_manager = \Drupal::entityManager();
128 $storage = $entity_manager->getStorage($this->entityTypeId);
130 $storage->resetCache();
131 $entity = $storage->load($this->entityId);
133 // Unpublish translations.
134 foreach ($this->langcodes as $index => $langcode) {
136 $edit = ['status' => 0];
137 $url = $entity->urlInfo('edit-form', ['language' => ConfigurableLanguage::load($langcode)]);
138 $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
139 $storage->resetCache();
140 $entity = $storage->load($this->entityId);
141 $this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished(), 'The translation has been correctly unpublished.');
149 protected function doTestAuthoringInfo() {
150 $storage = $this->container->get('entity_type.manager')
151 ->getStorage($this->entityTypeId);
152 $storage->resetCache([$this->entityId]);
153 $entity = $storage->load($this->entityId);
154 $languages = $this->container->get('language_manager')->getLanguages();
157 // Post different authoring information for each translation.
158 foreach ($this->langcodes as $langcode) {
159 $url = $entity->urlInfo('edit-form', ['language' => $languages[$langcode]]);
160 $user = $this->drupalCreateUser();
161 $values[$langcode] = [
162 'uid' => $user->id(),
163 'created' => REQUEST_TIME - mt_rand(0, 1000),
166 'uid' => $user->getUsername() . ' (' . $user->id() . ')',
167 'date[date]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d'),
168 'date[time]' => format_date($values[$langcode]['created'], 'custom', 'H:i:s'),
170 $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
173 $storage->resetCache([$this->entityId]);
174 $entity = $storage->load($this->entityId);
175 foreach ($this->langcodes as $langcode) {
176 $metadata = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
177 $this->assertEqual($metadata->getAuthor()->id(), $values[$langcode]['uid'], 'Translation author correctly stored.');
178 $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.');
183 * Tests translate link on comment content admin page.
185 public function testTranslateLinkCommentAdminPage() {
186 $this->adminUser = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), ['access administration pages', 'administer comments', 'skip comment approval']));
187 $this->drupalLogin($this->adminUser);
189 $cid_translatable = $this->createEntity([], $this->langcodes[0]);
190 $cid_untranslatable = $this->createEntity([], $this->langcodes[0], 'comment');
192 // Verify translation links.
193 $this->drupalGet('admin/content/comment');
194 $this->assertResponse(200);
195 $this->assertLinkByHref('comment/' . $cid_translatable . '/translations');
196 $this->assertNoLinkByHref('comment/' . $cid_untranslatable . '/translations');
202 protected function doTestTranslationEdit() {
203 $storage = $this->container->get('entity_type.manager')
204 ->getStorage($this->entityTypeId);
205 $storage->resetCache([$this->entityId]);
206 $entity = $storage->load($this->entityId);
207 $languages = $this->container->get('language_manager')->getLanguages();
209 foreach ($this->langcodes as $langcode) {
210 // We only want to test the title for non-english translations.
211 if ($langcode != 'en') {
212 $options = ['language' => $languages[$langcode]];
213 $url = $entity->urlInfo('edit-form', $options);
214 $this->drupalGet($url);
216 $title = t('Edit @type @title [%language translation]', [
217 '@type' => $this->entityTypeId,
218 '@title' => $entity->getTranslation($langcode)->label(),
219 '%language' => $languages[$langcode]->getName(),
221 $this->assertRaw($title);