4613d66ecc707e07a18f1363ea88016d6a1d0db2
[yaffs-website] / web / core / modules / comment / src / Tests / CommentTranslationUITest.php
1 <?php
2
3 namespace Drupal\comment\Tests;
4
5 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
6 use Drupal\content_translation\Tests\ContentTranslationUITestBase;
7 use Drupal\language\Entity\ConfigurableLanguage;
8
9 /**
10  * Tests the Comment Translation UI.
11  *
12  * @group comment
13  */
14 class CommentTranslationUITest extends ContentTranslationUITestBase {
15
16   use CommentTestTrait;
17
18   /**
19    * The subject of the test comment.
20    */
21   protected $subject;
22
23   /**
24    * An administrative user with permission to administer comments.
25    *
26    * @var \Drupal\user\UserInterface
27    */
28   protected $adminUser;
29
30   /**
31    * {inheritdoc}
32    */
33   protected $defaultCacheContexts = [
34     'languages:language_interface',
35     'session',
36     'theme',
37     'timezone',
38     'url.query_args:_wrapper_format',
39     'url.query_args.pagers:0',
40     'user.permissions',
41     'user.roles',
42   ];
43
44   /**
45    * Modules to install.
46    *
47    * @var array
48    */
49   public static $modules = ['language', 'content_translation', 'node', 'comment'];
50
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();
57     parent::setUp();
58   }
59
60   /**
61    * {@inheritdoc}
62    */
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
71     // translatable.
72     $this->addDefaultCommentField('node', 'page', 'comment');
73     // Mark this bundle as translatable.
74     $this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE);
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   protected function getTranslatorPermissions() {
81     return array_merge(parent::getTranslatorPermissions(), ['post comments', 'administer comments', 'access comments']);
82   }
83
84   /**
85    * {@inheritdoc}
86    */
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';
92     }
93     else {
94       // This is the page node type with the non-translatable 'comment' field.
95       $node_type = 'page';
96       $field_name = 'comment';
97     }
98     $node = $this->drupalCreateNode([
99       'type' => $node_type,
100       $field_name => [
101         ['status' => CommentItemInterface::OPEN]
102       ],
103     ]);
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);
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   protected function getNewEntityValues($langcode) {
115     // Comment subject is not translatable hence we use a fixed value.
116     return [
117       'subject' => [['value' => $this->subject]],
118       'comment_body' => [['value' => $this->randomMachineName(16)]],
119     ] + parent::getNewEntityValues($langcode);
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   protected function doTestPublishedStatus() {
126     $entity_manager = \Drupal::entityManager();
127     $storage = $entity_manager->getStorage($this->entityTypeId);
128
129     $storage->resetCache();
130     $entity = $storage->load($this->entityId);
131
132     // Unpublish translations.
133     foreach ($this->langcodes as $index => $langcode) {
134       if ($index > 0) {
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.');
141       }
142     }
143   }
144
145   /**
146    * {@inheritdoc}
147    */
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();
154     $values = [];
155
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),
163       ];
164       $edit = [
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'),
168       ];
169       $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
170     }
171
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.');
178     }
179   }
180
181   /**
182    * Tests translate link on comment content admin page.
183    */
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);
187
188     $cid_translatable = $this->createEntity([], $this->langcodes[0]);
189     $cid_untranslatable = $this->createEntity([], $this->langcodes[0], 'comment');
190
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');
196   }
197
198   /**
199    * {@inheritdoc}
200    */
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();
207
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);
214
215         $title = t('Edit @type @title [%language translation]', [
216           '@type' => $this->entityTypeId,
217           '@title' => $entity->getTranslation($langcode)->label(),
218           '%language' => $languages[$langcode]->getName(),
219         ]);
220         $this->assertRaw($title);
221       }
222     }
223   }
224
225 }