3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\comment\CommentManagerInterface;
6 use Drupal\Component\Render\MarkupInterface;
7 use Drupal\Core\Datetime\DrupalDateTime;
8 use Drupal\comment\Entity\Comment;
9 use Drupal\Tests\TestFileCreationTrait;
12 * Tests comment preview.
16 class CommentPreviewTest extends CommentTestBase {
18 use TestFileCreationTrait {
19 getTestFiles as drupalGetTestFiles;
23 * The profile to install as a basis for testing.
25 * Using the standard profile to test user picture display in comments.
29 protected $profile = 'standard';
32 * Tests comment preview.
34 public function testCommentPreview() {
35 // As admin user, configure comment settings.
36 $this->drupalLogin($this->adminUser);
37 $this->setCommentPreview(DRUPAL_OPTIONAL);
38 $this->setCommentForm(TRUE);
39 $this->setCommentSubject(TRUE);
40 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
41 $this->drupalLogout();
43 // Log in as web user.
44 $this->drupalLogin($this->webUser);
46 // Test escaping of the username on the preview form.
47 \Drupal::service('module_installer')->install(['user_hooks_test']);
48 \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
50 $edit['subject[0][value]'] = $this->randomMachineName(8);
51 $edit['comment_body[0][value]'] = $this->randomMachineName(16);
52 $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
53 $this->assertEscaped('<em>' . $this->webUser->id() . '</em>');
55 \Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE);
56 $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
57 $this->assertTrue($this->webUser->getDisplayName() instanceof MarkupInterface, 'Username is marked safe');
58 $this->assertNoEscaped('<em>' . $this->webUser->id() . '</em>');
59 $this->assertRaw('<em>' . $this->webUser->id() . '</em>');
61 // Add a user picture.
62 $image = current($this->drupalGetTestFiles('image'));
63 $user_edit['files[user_picture_0]'] = \Drupal::service('file_system')->realpath($image->uri);
64 $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $user_edit, t('Save'));
66 // As the web user, fill in the comment form and preview the comment.
67 $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
69 // Check that the preview is displaying the title and body.
70 $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
71 $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
72 $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
74 // Check that the title and body fields are displayed with the correct values.
75 $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
76 $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
78 // Check that the user picture is displayed.
79 $this->assertFieldByXPath("//article[contains(@class, 'preview')]//div[contains(@class, 'user-picture')]//img", NULL, 'User picture displayed.');
83 * Tests comment preview.
85 public function testCommentPreviewDuplicateSubmission() {
86 // As admin user, configure comment settings.
87 $this->drupalLogin($this->adminUser);
88 $this->setCommentPreview(DRUPAL_OPTIONAL);
89 $this->setCommentForm(TRUE);
90 $this->setCommentSubject(TRUE);
91 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
92 $this->drupalLogout();
94 // Log in as web user.
95 $this->drupalLogin($this->webUser);
97 // As the web user, fill in the comment form and preview the comment.
99 $edit['subject[0][value]'] = $this->randomMachineName(8);
100 $edit['comment_body[0][value]'] = $this->randomMachineName(16);
101 $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
103 // Check that the preview is displaying the title and body.
104 $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
105 $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
106 $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
108 // Check that the title and body fields are displayed with the correct values.
109 $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
110 $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
112 // Store the content of this page.
113 $this->drupalPostForm(NULL, [], 'Save');
114 $this->assertText('Your comment has been posted.');
115 $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
116 $this->assertEqual(1, count($elements));
118 // Go back and re-submit the form.
119 $this->getSession()->getDriver()->back();
120 $submit_button = $this->assertSession()->buttonExists('Save');
121 $submit_button->click();
122 $this->assertText('Your comment has been posted.');
123 $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
124 $this->assertEqual(2, count($elements));
128 * Tests comment edit, preview, and save.
130 public function testCommentEditPreviewSave() {
131 $web_user = $this->drupalCreateUser(['access comments', 'post comments', 'skip comment approval', 'edit own comments']);
132 $this->drupalLogin($this->adminUser);
133 $this->setCommentPreview(DRUPAL_OPTIONAL);
134 $this->setCommentForm(TRUE);
135 $this->setCommentSubject(TRUE);
136 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
139 $date = new DrupalDateTime('2008-03-02 17:23');
140 $edit['subject[0][value]'] = $this->randomMachineName(8);
141 $edit['comment_body[0][value]'] = $this->randomMachineName(16);
142 $edit['uid'] = $web_user->getUsername() . ' (' . $web_user->id() . ')';
143 $edit['date[date]'] = $date->format('Y-m-d');
144 $edit['date[time]'] = $date->format('H:i:s');
145 $raw_date = $date->getTimestamp();
146 $expected_text_date = format_date($raw_date);
147 $expected_form_date = $date->format('Y-m-d');
148 $expected_form_time = $date->format('H:i:s');
149 $comment = $this->postComment($this->node, $edit['subject[0][value]'], $edit['comment_body[0][value]'], TRUE);
150 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Preview'));
152 // Check that the preview is displaying the subject, comment, author and date correctly.
153 $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
154 $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
155 $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
156 $this->assertText($web_user->getUsername(), 'Author displayed.');
157 $this->assertText($expected_text_date, 'Date displayed.');
159 // Check that the subject, comment, author and date fields are displayed with the correct values.
160 $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
161 $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
162 $this->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
163 $this->assertFieldByName('date[date]', $edit['date[date]'], 'Date field displayed.');
164 $this->assertFieldByName('date[time]', $edit['date[time]'], 'Time field displayed.');
166 // Check that saving a comment produces a success message.
167 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Save'));
168 $this->assertText(t('Your comment has been posted.'), 'Comment posted.');
170 // Check that the comment fields are correct after loading the saved comment.
171 $this->drupalGet('comment/' . $comment->id() . '/edit');
172 $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
173 $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
174 $this->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
175 $this->assertFieldByName('date[date]', $expected_form_date, 'Date field displayed.');
176 $this->assertFieldByName('date[time]', $expected_form_time, 'Time field displayed.');
178 // Submit the form using the displayed values.
180 $displayed['subject[0][value]'] = current($this->xpath("//input[@id='edit-subject-0-value']"))->getValue();
181 $displayed['comment_body[0][value]'] = current($this->xpath("//textarea[@id='edit-comment-body-0-value']"))->getValue();
182 $displayed['uid'] = current($this->xpath("//input[@id='edit-uid']"))->getValue();
183 $displayed['date[date]'] = current($this->xpath("//input[@id='edit-date-date']"))->getValue();
184 $displayed['date[time]'] = current($this->xpath("//input[@id='edit-date-time']"))->getValue();
185 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $displayed, t('Save'));
187 // Check that the saved comment is still correct.
188 $comment_storage = \Drupal::entityManager()->getStorage('comment');
189 $comment_storage->resetCache([$comment->id()]);
190 /** @var \Drupal\comment\CommentInterface $comment_loaded */
191 $comment_loaded = Comment::load($comment->id());
192 $this->assertEqual($comment_loaded->getSubject(), $edit['subject[0][value]'], 'Subject loaded.');
193 $this->assertEqual($comment_loaded->comment_body->value, $edit['comment_body[0][value]'], 'Comment body loaded.');
194 $this->assertEqual($comment_loaded->getOwner()->id(), $web_user->id(), 'Name loaded.');
195 $this->assertEqual($comment_loaded->getCreatedTime(), $raw_date, 'Date loaded.');
196 $this->drupalLogout();
198 // Check that the date and time of the comment are correct when edited by
201 $expected_created_time = $comment_loaded->getCreatedTime();
202 $this->drupalLogin($web_user);
203 // Web user cannot change the comment author.
205 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $user_edit, t('Save'));
206 $comment_storage->resetCache([$comment->id()]);
207 $comment_loaded = Comment::load($comment->id());
208 $this->assertEqual($comment_loaded->getCreatedTime(), $expected_created_time, 'Expected date and time for comment edited.');
209 $this->drupalLogout();