comparison core/modules/comment/tests/src/Functional/CommentAnonymousTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\comment\Functional; 3 namespace Drupal\Tests\comment\Functional;
4 4
5 use Drupal\comment\CommentInterface;
5 use Drupal\user\RoleInterface; 6 use Drupal\user\RoleInterface;
6 7
7 /** 8 /**
8 * Tests anonymous commenting. 9 * Tests anonymous commenting.
9 * 10 *
30 /** 31 /**
31 * Tests anonymous comment functionality. 32 * Tests anonymous comment functionality.
32 */ 33 */
33 public function testAnonymous() { 34 public function testAnonymous() {
34 $this->drupalLogin($this->adminUser); 35 $this->drupalLogin($this->adminUser);
35 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAYNOT_CONTACT); 36 $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
36 $this->drupalLogout(); 37 $this->drupalLogout();
37 38
38 // Preview comments (with `skip comment approval` permission). 39 // Preview comments (with `skip comment approval` permission).
39 $edit = []; 40 $edit = [];
40 $title = 'comment title with skip comment approval'; 41 $title = 'comment title with skip comment approval';
41 $body = 'comment body with skip comment approval'; 42 $body = 'comment body with skip comment approval';
42 $edit['subject[0][value]'] = $title; 43 $edit['subject[0][value]'] = $title;
43 $edit['comment_body[0][value]'] = $body; 44 $edit['comment_body[0][value]'] = $body;
44 $this->drupalPostForm($this->node->urlInfo(), $edit, t('Preview')); 45 $this->drupalPostForm($this->node->toUrl(), $edit, t('Preview'));
45 // Cannot use assertRaw here since both title and body are in the form. 46 // Cannot use assertRaw here since both title and body are in the form.
46 $preview = (string) $this->cssSelect('.preview')[0]->getHtml(); 47 $preview = (string) $this->cssSelect('.preview')[0]->getHtml();
47 $this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.'); 48 $this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.');
48 $this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.'); 49 $this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.');
49 50
52 $edit = []; 53 $edit = [];
53 $title = 'comment title without skip comment approval'; 54 $title = 'comment title without skip comment approval';
54 $body = 'comment body without skip comment approval'; 55 $body = 'comment body without skip comment approval';
55 $edit['subject[0][value]'] = $title; 56 $edit['subject[0][value]'] = $title;
56 $edit['comment_body[0][value]'] = $body; 57 $edit['comment_body[0][value]'] = $body;
57 $this->drupalPostForm($this->node->urlInfo(), $edit, t('Preview')); 58 $this->drupalPostForm($this->node->toUrl(), $edit, t('Preview'));
58 // Cannot use assertRaw here since both title and body are in the form. 59 // Cannot use assertRaw here since both title and body are in the form.
59 $preview = (string) $this->cssSelect('.preview')[0]->getHtml(); 60 $preview = (string) $this->cssSelect('.preview')[0]->getHtml();
60 $this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.'); 61 $this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.');
61 $this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.'); 62 $this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.');
62 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']); 63 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
65 $anonymous_comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName()); 66 $anonymous_comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
66 $this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.'); 67 $this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');
67 68
68 // Ensure anonymous users cannot post in the name of registered users. 69 // Ensure anonymous users cannot post in the name of registered users.
69 $edit = [ 70 $edit = [
70 'name' => $this->adminUser->getUsername(), 71 'name' => $this->adminUser->getAccountName(),
71 'comment_body[0][value]' => $this->randomMachineName(), 72 'comment_body[0][value]' => $this->randomMachineName(),
72 ]; 73 ];
73 $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save')); 74 $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save'));
74 $this->assertRaw(t('The name you used (%name) belongs to a registered user.', [ 75 $this->assertRaw(t('The name you used (%name) belongs to a registered user.', [
75 '%name' => $this->adminUser->getUsername(), 76 '%name' => $this->adminUser->getAccountName(),
76 ])); 77 ]));
77 78
78 // Allow contact info. 79 // Allow contact info.
79 $this->drupalLogin($this->adminUser); 80 $this->drupalLogin($this->adminUser);
80 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT); 81 $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAY_CONTACT);
81 82
82 // Attempt to edit anonymous comment. 83 // Attempt to edit anonymous comment.
83 $this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit'); 84 $this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit');
84 $edited_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName()); 85 $edited_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
85 $this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.'); 86 $this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.');
96 $anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName()); 97 $anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
97 $this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.'); 98 $this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
98 99
99 // Ensure anonymous users cannot post in the name of registered users. 100 // Ensure anonymous users cannot post in the name of registered users.
100 $edit = [ 101 $edit = [
101 'name' => $this->adminUser->getUsername(), 102 'name' => $this->adminUser->getAccountName(),
102 'mail' => $this->randomMachineName() . '@example.com', 103 'mail' => $this->randomMachineName() . '@example.com',
103 'subject[0][value]' => $this->randomMachineName(), 104 'subject[0][value]' => $this->randomMachineName(),
104 'comment_body[0][value]' => $this->randomMachineName(), 105 'comment_body[0][value]' => $this->randomMachineName(),
105 ]; 106 ];
106 $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save')); 107 $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save'));
107 $this->assertRaw(t('The name you used (%name) belongs to a registered user.', [ 108 $this->assertRaw(t('The name you used (%name) belongs to a registered user.', [
108 '%name' => $this->adminUser->getUsername(), 109 '%name' => $this->adminUser->getAccountName(),
109 ])); 110 ]));
110 111
111 // Require contact info. 112 // Require contact info.
112 $this->drupalLogin($this->adminUser); 113 $this->drupalLogin($this->adminUser);
113 $this->setCommentAnonymous(COMMENT_ANONYMOUS_MUST_CONTACT); 114 $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MUST_CONTACT);
114 $this->drupalLogout(); 115 $this->drupalLogout();
115 116
116 // Try to post comment with contact info (required). 117 // Try to post comment with contact info (required).
117 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); 118 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
118 $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.'); 119 $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
167 168
168 // Attempt to view comments while disallowed. 169 // Attempt to view comments while disallowed.
169 // NOTE: if authenticated user has permission to post comments, then a 170 // NOTE: if authenticated user has permission to post comments, then a
170 // "Login or register to post comments" type link may be shown. 171 // "Login or register to post comments" type link may be shown.
171 $this->drupalGet('node/' . $this->node->id()); 172 $this->drupalGet('node/' . $this->node->id());
172 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.'); 173 $this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
173 $this->assertNoLink('Add new comment', 'Link to add comment was found.'); 174 $this->assertNoLink('Add new comment', 'Link to add comment was found.');
174 175
175 // Attempt to view node-comment form while disallowed. 176 // Attempt to view node-comment form while disallowed.
176 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment'); 177 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
177 $this->assertResponse(403); 178 $this->assertResponse(403);
190 'access comments' => FALSE, 191 'access comments' => FALSE,
191 'post comments' => TRUE, 192 'post comments' => TRUE,
192 'skip comment approval' => TRUE, 193 'skip comment approval' => TRUE,
193 ]); 194 ]);
194 $this->drupalGet('node/' . $this->node->id()); 195 $this->drupalGet('node/' . $this->node->id());
195 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.'); 196 $this->assertSession()->responseNotMatches('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
196 $this->assertFieldByName('subject[0][value]', '', 'Subject field found.'); 197 $this->assertFieldByName('subject[0][value]', '', 'Subject field found.');
197 $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.'); 198 $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.');
198 199
199 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment2->id()); 200 $this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment2->id());
200 $this->assertResponse(403); 201 $this->assertResponse(403);