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