Mercurial > hg > isophonics-drupal-site
comparison core/modules/comment/tests/src/Functional/CommentTestBase.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\comment\Functional; | |
4 | |
5 use Drupal\comment\Entity\CommentType; | |
6 use Drupal\comment\Entity\Comment; | |
7 use Drupal\comment\CommentInterface; | |
8 use Drupal\comment\Tests\CommentTestTrait; | |
9 use Drupal\field\Entity\FieldConfig; | |
10 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; | |
11 use Drupal\node\Entity\NodeType; | |
12 use Drupal\Tests\BrowserTestBase; | |
13 | |
14 /** | |
15 * Provides setup and helper methods for comment tests. | |
16 */ | |
17 abstract class CommentTestBase extends BrowserTestBase { | |
18 | |
19 use CommentTestTrait; | |
20 | |
21 /** | |
22 * Modules to install. | |
23 * | |
24 * @var array | |
25 */ | |
26 public static $modules = ['block', 'comment', 'node', 'history', 'field_ui', 'datetime']; | |
27 | |
28 /** | |
29 * An administrative user with permission to configure comment settings. | |
30 * | |
31 * @var \Drupal\user\UserInterface | |
32 */ | |
33 protected $adminUser; | |
34 | |
35 /** | |
36 * A normal user with permission to post comments. | |
37 * | |
38 * @var \Drupal\user\UserInterface | |
39 */ | |
40 protected $webUser; | |
41 | |
42 /** | |
43 * A test node to which comments will be posted. | |
44 * | |
45 * @var \Drupal\node\NodeInterface | |
46 */ | |
47 protected $node; | |
48 | |
49 protected function setUp() { | |
50 parent::setUp(); | |
51 | |
52 // Create an article content type only if it does not yet exist, so that | |
53 // child classes may specify the standard profile. | |
54 $types = NodeType::loadMultiple(); | |
55 if (empty($types['article'])) { | |
56 $this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]); | |
57 } | |
58 | |
59 // Create two test users. | |
60 $this->adminUser = $this->drupalCreateUser([ | |
61 'administer content types', | |
62 'administer comments', | |
63 'administer comment types', | |
64 'administer comment fields', | |
65 'administer comment display', | |
66 'skip comment approval', | |
67 'post comments', | |
68 'access comments', | |
69 // Usernames aren't shown in comment edit form autocomplete unless this | |
70 // permission is granted. | |
71 'access user profiles', | |
72 'access content', | |
73 ]); | |
74 $this->webUser = $this->drupalCreateUser([ | |
75 'access comments', | |
76 'post comments', | |
77 'create article content', | |
78 'edit own comments', | |
79 'skip comment approval', | |
80 'access content', | |
81 ]); | |
82 | |
83 // Create comment field on article. | |
84 $this->addDefaultCommentField('node', 'article'); | |
85 | |
86 // Create a test node authored by the web user. | |
87 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]); | |
88 $this->drupalPlaceBlock('local_tasks_block'); | |
89 } | |
90 | |
91 /** | |
92 * Posts a comment. | |
93 * | |
94 * @param \Drupal\Core\Entity\EntityInterface|null $entity | |
95 * Node to post comment on or NULL to post to the previously loaded page. | |
96 * @param string $comment | |
97 * Comment body. | |
98 * @param string $subject | |
99 * Comment subject. | |
100 * @param string $contact | |
101 * Set to NULL for no contact info, TRUE to ignore success checking, and | |
102 * array of values to set contact info. | |
103 * @param string $field_name | |
104 * (optional) Field name through which the comment should be posted. | |
105 * Defaults to 'comment'. | |
106 * | |
107 * @return \Drupal\comment\CommentInterface|null | |
108 * The posted comment or NULL when posted comment was not found. | |
109 */ | |
110 public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') { | |
111 $edit = []; | |
112 $edit['comment_body[0][value]'] = $comment; | |
113 | |
114 if ($entity !== NULL) { | |
115 $field = FieldConfig::loadByName($entity->getEntityTypeId(), $entity->bundle(), $field_name); | |
116 } | |
117 else { | |
118 $field = FieldConfig::loadByName('node', 'article', $field_name); | |
119 } | |
120 $preview_mode = $field->getSetting('preview'); | |
121 | |
122 // Must get the page before we test for fields. | |
123 if ($entity !== NULL) { | |
124 $this->drupalGet('comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name); | |
125 } | |
126 | |
127 // Determine the visibility of subject form field. | |
128 if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) { | |
129 // Subject input allowed. | |
130 $edit['subject[0][value]'] = $subject; | |
131 } | |
132 else { | |
133 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); | |
134 } | |
135 | |
136 if ($contact !== NULL && is_array($contact)) { | |
137 $edit += $contact; | |
138 } | |
139 switch ($preview_mode) { | |
140 case DRUPAL_REQUIRED: | |
141 // Preview required so no save button should be found. | |
142 $this->assertNoFieldByName('op', t('Save'), 'Save button not found.'); | |
143 $this->drupalPostForm(NULL, $edit, t('Preview')); | |
144 // Don't break here so that we can test post-preview field presence and | |
145 // function below. | |
146 case DRUPAL_OPTIONAL: | |
147 $this->assertFieldByName('op', t('Preview'), 'Preview button found.'); | |
148 $this->assertFieldByName('op', t('Save'), 'Save button found.'); | |
149 $this->drupalPostForm(NULL, $edit, t('Save')); | |
150 break; | |
151 | |
152 case DRUPAL_DISABLED: | |
153 $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.'); | |
154 $this->assertFieldByName('op', t('Save'), 'Save button found.'); | |
155 $this->drupalPostForm(NULL, $edit, t('Save')); | |
156 break; | |
157 } | |
158 $match = []; | |
159 // Get comment ID | |
160 preg_match('/#comment-([0-9]+)/', $this->getURL(), $match); | |
161 | |
162 // Get comment. | |
163 if ($contact !== TRUE) { | |
164 // If true then attempting to find error message. | |
165 if ($subject) { | |
166 $this->assertText($subject, 'Comment subject posted.'); | |
167 } | |
168 $this->assertText($comment, 'Comment body posted.'); | |
169 $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment id found.'); | |
170 } | |
171 | |
172 if (isset($match[1])) { | |
173 \Drupal::entityManager()->getStorage('comment')->resetCache([$match[1]]); | |
174 return Comment::load($match[1]); | |
175 } | |
176 } | |
177 | |
178 /** | |
179 * Checks current page for specified comment. | |
180 * | |
181 * @param \Drupal\comment\CommentInterface $comment | |
182 * The comment object. | |
183 * @param bool $reply | |
184 * Boolean indicating whether the comment is a reply to another comment. | |
185 * | |
186 * @return bool | |
187 * Boolean indicating whether the comment was found. | |
188 */ | |
189 public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) { | |
190 if ($comment) { | |
191 $comment_element = $this->cssSelect('.comment-wrapper ' . ($reply ? '.indented ' : '') . '#comment-' . $comment->id() . ' ~ article'); | |
192 if (empty($comment_element)) { | |
193 return FALSE; | |
194 } | |
195 | |
196 $comment_title = $comment_element[0]->find('xpath', 'div/h3/a'); | |
197 if (empty($comment_title) || $comment_title->getText() !== $comment->getSubject()) { | |
198 return FALSE; | |
199 } | |
200 | |
201 $comment_body = $comment_element[0]->find('xpath', 'div/div/p'); | |
202 if (empty($comment_body) || $comment_body->getText() !== $comment->comment_body->value) { | |
203 return FALSE; | |
204 } | |
205 | |
206 return TRUE; | |
207 } | |
208 else { | |
209 return FALSE; | |
210 } | |
211 } | |
212 | |
213 /** | |
214 * Deletes a comment. | |
215 * | |
216 * @param \Drupal\comment\CommentInterface $comment | |
217 * Comment to delete. | |
218 */ | |
219 public function deleteComment(CommentInterface $comment) { | |
220 $this->drupalPostForm('comment/' . $comment->id() . '/delete', [], t('Delete')); | |
221 $this->assertText(t('The comment and all its replies have been deleted.'), 'Comment deleted.'); | |
222 } | |
223 | |
224 /** | |
225 * Sets the value governing whether the subject field should be enabled. | |
226 * | |
227 * @param bool $enabled | |
228 * Boolean specifying whether the subject field should be enabled. | |
229 */ | |
230 public function setCommentSubject($enabled) { | |
231 $form_display = entity_get_form_display('comment', 'comment', 'default'); | |
232 if ($enabled) { | |
233 $form_display->setComponent('subject', [ | |
234 'type' => 'string_textfield', | |
235 ]); | |
236 } | |
237 else { | |
238 $form_display->removeComponent('subject'); | |
239 } | |
240 $form_display->save(); | |
241 // Display status message. | |
242 $this->pass('Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.'); | |
243 } | |
244 | |
245 /** | |
246 * Sets the value governing the previewing mode for the comment form. | |
247 * | |
248 * @param int $mode | |
249 * The preview mode: DRUPAL_DISABLED, DRUPAL_OPTIONAL or DRUPAL_REQUIRED. | |
250 * @param string $field_name | |
251 * (optional) Field name through which the comment should be posted. | |
252 * Defaults to 'comment'. | |
253 */ | |
254 public function setCommentPreview($mode, $field_name = 'comment') { | |
255 switch ($mode) { | |
256 case DRUPAL_DISABLED: | |
257 $mode_text = 'disabled'; | |
258 break; | |
259 | |
260 case DRUPAL_OPTIONAL: | |
261 $mode_text = 'optional'; | |
262 break; | |
263 | |
264 case DRUPAL_REQUIRED: | |
265 $mode_text = 'required'; | |
266 break; | |
267 } | |
268 $this->setCommentSettings('preview', $mode, format_string('Comment preview @mode_text.', ['@mode_text' => $mode_text]), $field_name); | |
269 } | |
270 | |
271 /** | |
272 * Sets the value governing whether the comment form is on its own page. | |
273 * | |
274 * @param bool $enabled | |
275 * TRUE if the comment form should be displayed on the same page as the | |
276 * comments; FALSE if it should be displayed on its own page. | |
277 * @param string $field_name | |
278 * (optional) Field name through which the comment should be posted. | |
279 * Defaults to 'comment'. | |
280 */ | |
281 public function setCommentForm($enabled, $field_name = 'comment') { | |
282 $this->setCommentSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name); | |
283 } | |
284 | |
285 /** | |
286 * Sets the value governing restrictions on anonymous comments. | |
287 * | |
288 * @param int $level | |
289 * The level of the contact information allowed for anonymous comments: | |
290 * - 0: No contact information allowed. | |
291 * - 1: Contact information allowed but not required. | |
292 * - 2: Contact information required. | |
293 */ | |
294 public function setCommentAnonymous($level) { | |
295 $this->setCommentSettings('anonymous', $level, format_string('Anonymous commenting set to level @level.', ['@level' => $level])); | |
296 } | |
297 | |
298 /** | |
299 * Sets the value specifying the default number of comments per page. | |
300 * | |
301 * @param int $number | |
302 * Comments per page value. | |
303 * @param string $field_name | |
304 * (optional) Field name through which the comment should be posted. | |
305 * Defaults to 'comment'. | |
306 */ | |
307 public function setCommentsPerPage($number, $field_name = 'comment') { | |
308 $this->setCommentSettings('per_page', $number, format_string('Number of comments per page set to @number.', ['@number' => $number]), $field_name); | |
309 } | |
310 | |
311 /** | |
312 * Sets a comment settings variable for the article content type. | |
313 * | |
314 * @param string $name | |
315 * Name of variable. | |
316 * @param string $value | |
317 * Value of variable. | |
318 * @param string $message | |
319 * Status message to display. | |
320 * @param string $field_name | |
321 * (optional) Field name through which the comment should be posted. | |
322 * Defaults to 'comment'. | |
323 */ | |
324 public function setCommentSettings($name, $value, $message, $field_name = 'comment') { | |
325 $field = FieldConfig::loadByName('node', 'article', $field_name); | |
326 $field->setSetting($name, $value); | |
327 $field->save(); | |
328 // Display status message. | |
329 $this->pass($message); | |
330 } | |
331 | |
332 /** | |
333 * Checks whether the commenter's contact information is displayed. | |
334 * | |
335 * @return bool | |
336 * Contact info is available. | |
337 */ | |
338 public function commentContactInfoAvailable() { | |
339 return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getRawContent()); | |
340 } | |
341 | |
342 /** | |
343 * Performs the specified operation on the specified comment. | |
344 * | |
345 * @param \Drupal\comment\CommentInterface $comment | |
346 * Comment to perform operation on. | |
347 * @param string $operation | |
348 * Operation to perform. | |
349 * @param bool $approval | |
350 * Operation is found on approval page. | |
351 */ | |
352 public function performCommentOperation(CommentInterface $comment, $operation, $approval = FALSE) { | |
353 $edit = []; | |
354 $edit['operation'] = $operation; | |
355 $edit['comments[' . $comment->id() . ']'] = TRUE; | |
356 $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update')); | |
357 | |
358 if ($operation == 'delete') { | |
359 $this->drupalPostForm(NULL, [], t('Delete')); | |
360 $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation])); | |
361 } | |
362 else { | |
363 $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation])); | |
364 } | |
365 } | |
366 | |
367 /** | |
368 * Gets the comment ID for an unapproved comment. | |
369 * | |
370 * @param string $subject | |
371 * Comment subject to find. | |
372 * | |
373 * @return int | |
374 * Comment id. | |
375 */ | |
376 public function getUnapprovedComment($subject) { | |
377 $this->drupalGet('admin/content/comment/approval'); | |
378 preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getRawContent(), $match); | |
379 | |
380 return $match[2]; | |
381 } | |
382 | |
383 /** | |
384 * Creates a comment comment type (bundle). | |
385 * | |
386 * @param string $label | |
387 * The comment type label. | |
388 * | |
389 * @return \Drupal\comment\Entity\CommentType | |
390 * Created comment type. | |
391 */ | |
392 protected function createCommentType($label) { | |
393 $bundle = CommentType::create([ | |
394 'id' => $label, | |
395 'label' => $label, | |
396 'description' => '', | |
397 'target_entity_type_id' => 'node', | |
398 ]); | |
399 $bundle->save(); | |
400 return $bundle; | |
401 } | |
402 | |
403 } |