Mercurial > hg > isophonics-drupal-site
comparison core/modules/comment/tests/src/Functional/CommentNonNodeTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\comment\Functional; | |
4 | |
5 use Drupal\comment\CommentInterface; | |
6 use Drupal\comment\Entity\Comment; | |
7 use Drupal\comment\Entity\CommentType; | |
8 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; | |
9 use Drupal\comment\Tests\CommentTestTrait; | |
10 use Drupal\entity_test\Entity\EntityTest; | |
11 use Drupal\field\Entity\FieldConfig; | |
12 use Drupal\field\Entity\FieldStorageConfig; | |
13 use Drupal\field_ui\Tests\FieldUiTestTrait; | |
14 use Drupal\Tests\BrowserTestBase; | |
15 use Drupal\Core\Entity\EntityInterface; | |
16 use Drupal\user\RoleInterface; | |
17 | |
18 /** | |
19 * Tests commenting on a test entity. | |
20 * | |
21 * @group comment | |
22 */ | |
23 class CommentNonNodeTest extends BrowserTestBase { | |
24 | |
25 use FieldUiTestTrait; | |
26 use CommentTestTrait; | |
27 | |
28 public static $modules = ['comment', 'user', 'field_ui', 'entity_test', 'block']; | |
29 | |
30 /** | |
31 * An administrative user with permission to configure comment settings. | |
32 * | |
33 * @var \Drupal\user\UserInterface | |
34 */ | |
35 protected $adminUser; | |
36 | |
37 /** | |
38 * The entity to use within tests. | |
39 * | |
40 * @var \Drupal\entity_test\Entity\EntityTest | |
41 */ | |
42 protected $entity; | |
43 | |
44 /** | |
45 * {@inheritdoc} | |
46 */ | |
47 protected function setUp() { | |
48 parent::setUp(); | |
49 $this->drupalPlaceBlock('system_breadcrumb_block'); | |
50 $this->drupalPlaceBlock('page_title_block'); | |
51 | |
52 // Create a bundle for entity_test. | |
53 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test'); | |
54 CommentType::create([ | |
55 'id' => 'comment', | |
56 'label' => 'Comment settings', | |
57 'description' => 'Comment settings', | |
58 'target_entity_type_id' => 'entity_test', | |
59 ])->save(); | |
60 // Create comment field on entity_test bundle. | |
61 $this->addDefaultCommentField('entity_test', 'entity_test'); | |
62 | |
63 // Verify that bundles are defined correctly. | |
64 $bundles = \Drupal::entityManager()->getBundleInfo('comment'); | |
65 $this->assertEqual($bundles['comment']['label'], 'Comment settings'); | |
66 | |
67 // Create test user. | |
68 $this->adminUser = $this->drupalCreateUser([ | |
69 'administer comments', | |
70 'skip comment approval', | |
71 'post comments', | |
72 'access comments', | |
73 'view test entity', | |
74 'administer entity_test content', | |
75 ]); | |
76 | |
77 // Enable anonymous and authenticated user comments. | |
78 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [ | |
79 'access comments', | |
80 'post comments', | |
81 'skip comment approval', | |
82 ]); | |
83 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [ | |
84 'access comments', | |
85 'post comments', | |
86 'skip comment approval', | |
87 ]); | |
88 | |
89 // Create a test entity. | |
90 $random_label = $this->randomMachineName(); | |
91 $data = ['type' => 'entity_test', 'name' => $random_label]; | |
92 $this->entity = EntityTest::create($data); | |
93 $this->entity->save(); | |
94 } | |
95 | |
96 /** | |
97 * Posts a comment. | |
98 * | |
99 * @param \Drupal\Core\Entity\EntityInterface|null $entity | |
100 * Entity to post comment on or NULL to post to the previously loaded page. | |
101 * @param string $comment | |
102 * Comment body. | |
103 * @param string $subject | |
104 * Comment subject. | |
105 * @param mixed $contact | |
106 * Set to NULL for no contact info, TRUE to ignore success checking, and | |
107 * array of values to set contact info. | |
108 * | |
109 * @return \Drupal\comment\CommentInterface | |
110 * The new comment entity. | |
111 */ | |
112 public function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) { | |
113 $edit = []; | |
114 $edit['comment_body[0][value]'] = $comment; | |
115 | |
116 $field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment'); | |
117 $preview_mode = $field->getSetting('preview'); | |
118 | |
119 // Must get the page before we test for fields. | |
120 if ($entity !== NULL) { | |
121 $this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment'); | |
122 } | |
123 | |
124 // Determine the visibility of subject form field. | |
125 if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) { | |
126 // Subject input allowed. | |
127 $edit['subject[0][value]'] = $subject; | |
128 } | |
129 else { | |
130 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); | |
131 } | |
132 | |
133 if ($contact !== NULL && is_array($contact)) { | |
134 $edit += $contact; | |
135 } | |
136 switch ($preview_mode) { | |
137 case DRUPAL_REQUIRED: | |
138 // Preview required so no save button should be found. | |
139 $this->assertNoFieldByName('op', t('Save'), 'Save button not found.'); | |
140 $this->drupalPostForm(NULL, $edit, t('Preview')); | |
141 // Don't break here so that we can test post-preview field presence and | |
142 // function below. | |
143 case DRUPAL_OPTIONAL: | |
144 $this->assertFieldByName('op', t('Preview'), 'Preview button found.'); | |
145 $this->assertFieldByName('op', t('Save'), 'Save button found.'); | |
146 $this->drupalPostForm(NULL, $edit, t('Save')); | |
147 break; | |
148 | |
149 case DRUPAL_DISABLED: | |
150 $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.'); | |
151 $this->assertFieldByName('op', t('Save'), 'Save button found.'); | |
152 $this->drupalPostForm(NULL, $edit, t('Save')); | |
153 break; | |
154 } | |
155 $match = []; | |
156 // Get comment ID | |
157 preg_match('/#comment-([0-9]+)/', $this->getURL(), $match); | |
158 | |
159 // Get comment. | |
160 if ($contact !== TRUE) { | |
161 // If true then attempting to find error message. | |
162 if ($subject) { | |
163 $this->assertText($subject, 'Comment subject posted.'); | |
164 } | |
165 $this->assertText($comment, 'Comment body posted.'); | |
166 $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment ID found.'); | |
167 } | |
168 | |
169 if (isset($match[1])) { | |
170 return Comment::load($match[1]); | |
171 } | |
172 } | |
173 | |
174 /** | |
175 * Checks current page for specified comment. | |
176 * | |
177 * @param \Drupal\comment\CommentInterface $comment | |
178 * The comment object. | |
179 * @param bool $reply | |
180 * Boolean indicating whether the comment is a reply to another comment. | |
181 * | |
182 * @return bool | |
183 * Boolean indicating whether the comment was found. | |
184 */ | |
185 public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) { | |
186 if ($comment) { | |
187 $regex = '/' . ($reply ? '<div class="indented">(.*?)' : ''); | |
188 $regex .= '<a id="comment-' . $comment->id() . '"(.*?)'; | |
189 $regex .= $comment->getSubject() . '(.*?)'; | |
190 $regex .= $comment->comment_body->value . '(.*?)'; | |
191 $regex .= '/s'; | |
192 | |
193 return (boolean) preg_match($regex, $this->getRawContent()); | |
194 } | |
195 else { | |
196 return FALSE; | |
197 } | |
198 } | |
199 | |
200 /** | |
201 * Checks whether the commenter's contact information is displayed. | |
202 * | |
203 * @return bool | |
204 * Contact info is available. | |
205 */ | |
206 public function commentContactInfoAvailable() { | |
207 return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getRawContent()); | |
208 } | |
209 | |
210 /** | |
211 * Performs the specified operation on the specified comment. | |
212 * | |
213 * @param object $comment | |
214 * Comment to perform operation on. | |
215 * @param string $operation | |
216 * Operation to perform. | |
217 * @param bool $approval | |
218 * Operation is found on approval page. | |
219 */ | |
220 public function performCommentOperation($comment, $operation, $approval = FALSE) { | |
221 $edit = []; | |
222 $edit['operation'] = $operation; | |
223 $edit['comments[' . $comment->id() . ']'] = TRUE; | |
224 $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update')); | |
225 | |
226 if ($operation == 'delete') { | |
227 $this->drupalPostForm(NULL, [], t('Delete')); | |
228 $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation])); | |
229 } | |
230 else { | |
231 $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation])); | |
232 } | |
233 } | |
234 | |
235 /** | |
236 * Gets the comment ID for an unapproved comment. | |
237 * | |
238 * @param string $subject | |
239 * Comment subject to find. | |
240 * | |
241 * @return int | |
242 * Comment ID. | |
243 */ | |
244 public function getUnapprovedComment($subject) { | |
245 $this->drupalGet('admin/content/comment/approval'); | |
246 preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getRawContent(), $match); | |
247 | |
248 return $match[2]; | |
249 } | |
250 | |
251 /** | |
252 * Tests anonymous comment functionality. | |
253 */ | |
254 public function testCommentFunctionality() { | |
255 $limited_user = $this->drupalCreateUser([ | |
256 'administer entity_test fields' | |
257 ]); | |
258 $this->drupalLogin($limited_user); | |
259 // Test that default field exists. | |
260 $this->drupalGet('entity_test/structure/entity_test/fields'); | |
261 $this->assertText(t('Comments')); | |
262 $this->assertLinkByHref('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); | |
263 // Test widget hidden option is not visible when there's no comments. | |
264 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); | |
265 $this->assertResponse(200); | |
266 $this->assertNoField('edit-default-value-input-comment-und-0-status-0'); | |
267 // Test that field to change cardinality is not available. | |
268 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment/storage'); | |
269 $this->assertResponse(200); | |
270 $this->assertNoField('cardinality_number'); | |
271 $this->assertNoField('cardinality'); | |
272 | |
273 $this->drupalLogin($this->adminUser); | |
274 | |
275 // Test breadcrumb on comment add page. | |
276 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); | |
277 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; | |
278 $this->assertEqual(current($this->xpath($xpath))->getText(), $this->entity->label(), 'Last breadcrumb item is equal to node title on comment reply page.'); | |
279 | |
280 // Post a comment. | |
281 /** @var \Drupal\comment\CommentInterface $comment1 */ | |
282 $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName()); | |
283 $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.'); | |
284 | |
285 // Test breadcrumb on comment reply page. | |
286 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id()); | |
287 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; | |
288 $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment title on comment reply page.'); | |
289 | |
290 // Test breadcrumb on comment edit page. | |
291 $this->drupalGet('comment/' . $comment1->id() . '/edit'); | |
292 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; | |
293 $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on edit page.'); | |
294 | |
295 // Test breadcrumb on comment delete page. | |
296 $this->drupalGet('comment/' . $comment1->id() . '/delete'); | |
297 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; | |
298 $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on delete confirm page.'); | |
299 | |
300 // Unpublish the comment. | |
301 $this->performCommentOperation($comment1, 'unpublish'); | |
302 $this->drupalGet('admin/content/comment/approval'); | |
303 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was unpublished.'); | |
304 | |
305 // Publish the comment. | |
306 $this->performCommentOperation($comment1, 'publish', TRUE); | |
307 $this->drupalGet('admin/content/comment'); | |
308 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.'); | |
309 | |
310 // Delete the comment. | |
311 $this->performCommentOperation($comment1, 'delete'); | |
312 $this->drupalGet('admin/content/comment'); | |
313 $this->assertNoRaw('comments[' . $comment1->id() . ']', 'Comment was deleted.'); | |
314 | |
315 // Post another comment. | |
316 $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName()); | |
317 $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.'); | |
318 | |
319 // Check that the comment was found. | |
320 $this->drupalGet('admin/content/comment'); | |
321 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.'); | |
322 | |
323 // Check that entity access applies to administrative page. | |
324 $this->assertText($this->entity->label(), 'Name of commented account found.'); | |
325 $limited_user = $this->drupalCreateUser([ | |
326 'administer comments', | |
327 ]); | |
328 $this->drupalLogin($limited_user); | |
329 $this->drupalGet('admin/content/comment'); | |
330 $this->assertNoText($this->entity->label(), 'No commented account name found.'); | |
331 | |
332 $this->drupalLogout(); | |
333 | |
334 // Deny anonymous users access to comments. | |
335 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ | |
336 'access comments' => FALSE, | |
337 'post comments' => FALSE, | |
338 'skip comment approval' => FALSE, | |
339 'view test entity' => TRUE, | |
340 ]); | |
341 | |
342 // Attempt to view comments while disallowed. | |
343 $this->drupalGet('entity-test/' . $this->entity->id()); | |
344 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.'); | |
345 $this->assertNoLink('Add new comment', 'Link to add comment was found.'); | |
346 | |
347 // Attempt to view test entity comment form while disallowed. | |
348 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); | |
349 $this->assertResponse(403); | |
350 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); | |
351 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.'); | |
352 | |
353 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ | |
354 'access comments' => TRUE, | |
355 'post comments' => FALSE, | |
356 'view test entity' => TRUE, | |
357 'skip comment approval' => FALSE, | |
358 ]); | |
359 $this->drupalGet('entity_test/' . $this->entity->id()); | |
360 $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.'); | |
361 $this->assertLink('Log in', 0, 'Link to login was found.'); | |
362 $this->assertLink('register', 0, 'Link to register was found.'); | |
363 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); | |
364 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.'); | |
365 | |
366 // Test the combination of anonymous users being able to post, but not view | |
367 // comments, to ensure that access to post comments doesn't grant access to | |
368 // view them. | |
369 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ | |
370 'access comments' => FALSE, | |
371 'post comments' => TRUE, | |
372 'skip comment approval' => TRUE, | |
373 'view test entity' => TRUE, | |
374 ]); | |
375 $this->drupalGet('entity_test/' . $this->entity->id()); | |
376 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.'); | |
377 $this->assertFieldByName('subject[0][value]', '', 'Subject field found.'); | |
378 $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.'); | |
379 | |
380 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id()); | |
381 $this->assertResponse(403); | |
382 $this->assertNoText($comment1->getSubject(), 'Comment not displayed.'); | |
383 | |
384 // Test comment field widget changes. | |
385 $limited_user = $this->drupalCreateUser([ | |
386 'administer entity_test fields', | |
387 'view test entity', | |
388 'administer entity_test content', | |
389 'administer comments', | |
390 ]); | |
391 $this->drupalLogin($limited_user); | |
392 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); | |
393 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0'); | |
394 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-1'); | |
395 $this->assertFieldChecked('edit-default-value-input-comment-0-status-2'); | |
396 // Test comment option change in field settings. | |
397 $edit = [ | |
398 'default_value_input[comment][0][status]' => CommentItemInterface::CLOSED, | |
399 'settings[anonymous]' => COMMENT_ANONYMOUS_MAY_CONTACT, | |
400 ]; | |
401 $this->drupalPostForm(NULL, $edit, t('Save settings')); | |
402 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); | |
403 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0'); | |
404 $this->assertFieldChecked('edit-default-value-input-comment-0-status-1'); | |
405 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-2'); | |
406 $this->assertFieldByName('settings[anonymous]', COMMENT_ANONYMOUS_MAY_CONTACT); | |
407 | |
408 // Add a new comment-type. | |
409 $bundle = CommentType::create([ | |
410 'id' => 'foobar', | |
411 'label' => 'Foobar', | |
412 'description' => '', | |
413 'target_entity_type_id' => 'entity_test', | |
414 ]); | |
415 $bundle->save(); | |
416 | |
417 // Add a new comment field. | |
418 $storage_edit = [ | |
419 'settings[comment_type]' => 'foobar', | |
420 ]; | |
421 $this->fieldUIAddNewField('entity_test/structure/entity_test', 'foobar', 'Foobar', 'comment', $storage_edit); | |
422 | |
423 // Add a third comment field. | |
424 $this->fieldUIAddNewField('entity_test/structure/entity_test', 'barfoo', 'BarFoo', 'comment', $storage_edit); | |
425 | |
426 // Check the field contains the correct comment type. | |
427 $field_storage = FieldStorageConfig::load('entity_test.field_barfoo'); | |
428 $this->assertTrue($field_storage); | |
429 $this->assertEqual($field_storage->getSetting('comment_type'), 'foobar'); | |
430 $this->assertEqual($field_storage->getCardinality(), 1); | |
431 | |
432 // Test the new entity commenting inherits default. | |
433 $random_label = $this->randomMachineName(); | |
434 $data = ['bundle' => 'entity_test', 'name' => $random_label]; | |
435 $new_entity = EntityTest::create($data); | |
436 $new_entity->save(); | |
437 $this->drupalGet('entity_test/manage/' . $new_entity->id() . '/edit'); | |
438 $this->assertNoFieldChecked('edit-field-foobar-0-status-1'); | |
439 $this->assertFieldChecked('edit-field-foobar-0-status-2'); | |
440 $this->assertNoField('edit-field-foobar-0-status-0'); | |
441 | |
442 // @todo Check proper url and form https://www.drupal.org/node/2458323 | |
443 $this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id()); | |
444 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field found.'); | |
445 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field found.'); | |
446 | |
447 // Test removal of comment_body field. | |
448 $limited_user = $this->drupalCreateUser([ | |
449 'administer entity_test fields', | |
450 'post comments', | |
451 'administer comment fields', | |
452 'administer comment types', | |
453 ]); | |
454 $this->drupalLogin($limited_user); | |
455 | |
456 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); | |
457 $this->assertFieldByName('comment_body[0][value]', '', 'Comment body field found.'); | |
458 $this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings'); | |
459 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); | |
460 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment body field not found.'); | |
461 // Set subject field to autogenerate it. | |
462 $edit = ['subject[0][value]' => '']; | |
463 $this->drupalPostForm(NULL, $edit, t('Save')); | |
464 } | |
465 | |
466 /** | |
467 * Tests comment fields cannot be added to entity types without integer IDs. | |
468 */ | |
469 public function testsNonIntegerIdEntities() { | |
470 // Create a bundle for entity_test_string_id. | |
471 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_string_id'); | |
472 $limited_user = $this->drupalCreateUser([ | |
473 'administer entity_test_string_id fields', | |
474 ]); | |
475 $this->drupalLogin($limited_user); | |
476 // Visit the Field UI field add page. | |
477 $this->drupalGet('entity_test_string_id/structure/entity_test/fields/add-field'); | |
478 // Ensure field isn't shown for string IDs. | |
479 $this->assertNoOption('edit-new-storage-type', 'comment'); | |
480 // Ensure a core field type shown. | |
481 $this->assertOption('edit-new-storage-type', 'boolean'); | |
482 | |
483 // Create a bundle for entity_test_no_id. | |
484 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_no_id'); | |
485 $this->drupalLogin($this->drupalCreateUser([ | |
486 'administer entity_test_no_id fields', | |
487 ])); | |
488 // Visit the Field UI field add page. | |
489 $this->drupalGet('entity_test_no_id/structure/entity_test/fields/add-field'); | |
490 // Ensure field isn't shown for empty IDs. | |
491 $this->assertNoOption('edit-new-storage-type', 'comment'); | |
492 // Ensure a core field type shown. | |
493 $this->assertOption('edit-new-storage-type', 'boolean'); | |
494 } | |
495 | |
496 } |