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