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