3 namespace Drupal\comment\Tests;
5 use Drupal\comment\CommentManagerInterface;
6 use Drupal\Component\Render\MarkupInterface;
7 use Drupal\Core\Datetime\DrupalDateTime;
8 use Drupal\comment\Entity\Comment;
11 * Tests comment preview.
15 class CommentPreviewTest extends CommentTestBase {
18 * The profile to install as a basis for testing.
20 * Using the standard profile to test user picture display in comments.
24 protected $profile = 'standard';
27 * Tests comment preview.
29 public function testCommentPreview() {
30 // As admin user, configure comment settings.
31 $this->drupalLogin($this->adminUser);
32 $this->setCommentPreview(DRUPAL_OPTIONAL);
33 $this->setCommentForm(TRUE);
34 $this->setCommentSubject(TRUE);
35 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
36 $this->drupalLogout();
38 // Log in as web user.
39 $this->drupalLogin($this->webUser);
41 // Test escaping of the username on the preview form.
42 \Drupal::service('module_installer')->install(['user_hooks_test']);
43 \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
45 $edit['subject[0][value]'] = $this->randomMachineName(8);
46 $edit['comment_body[0][value]'] = $this->randomMachineName(16);
47 $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
48 $this->assertEscaped('<em>' . $this->webUser->id() . '</em>');
50 \Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE);
51 $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
52 $this->assertTrue($this->webUser->getDisplayName() instanceof MarkupInterface, 'Username is marked safe');
53 $this->assertNoEscaped('<em>' . $this->webUser->id() . '</em>');
54 $this->assertRaw('<em>' . $this->webUser->id() . '</em>');
56 // Add a user picture.
57 $image = current($this->drupalGetTestFiles('image'));
58 $user_edit['files[user_picture_0]'] = drupal_realpath($image->uri);
59 $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $user_edit, t('Save'));
61 // As the web user, fill in the comment form and preview the comment.
62 $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
64 // Check that the preview is displaying the title and body.
65 $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
66 $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
67 $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
69 // Check that the title and body fields are displayed with the correct values.
70 $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
71 $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
73 // Check that the user picture is displayed.
74 $this->assertFieldByXPath("//article[contains(@class, 'preview')]//div[contains(@class, 'user-picture')]//img", NULL, 'User picture displayed.');
78 * Tests comment preview.
80 public function testCommentPreviewDuplicateSubmission() {
81 // As admin user, configure comment settings.
82 $this->drupalLogin($this->adminUser);
83 $this->setCommentPreview(DRUPAL_OPTIONAL);
84 $this->setCommentForm(TRUE);
85 $this->setCommentSubject(TRUE);
86 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
87 $this->drupalLogout();
89 // Log in as web user.
90 $this->drupalLogin($this->webUser);
92 // As the web user, fill in the comment form and preview the comment.
94 $edit['subject[0][value]'] = $this->randomMachineName(8);
95 $edit['comment_body[0][value]'] = $this->randomMachineName(16);
96 $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
98 // Check that the preview is displaying the title and body.
99 $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
100 $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
101 $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
103 // Check that the title and body fields are displayed with the correct values.
104 $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
105 $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
107 // Store the content of this page.
108 $content = $this->getRawContent();
109 $this->drupalPostForm(NULL, [], 'Save');
110 $this->assertText('Your comment has been posted.');
111 $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
112 $this->assertEqual(1, count($elements));
114 // Reset the content of the page to simulate the browser's back button, and
115 // re-submit the form.
116 $this->setRawContent($content);
117 $this->drupalPostForm(NULL, [], 'Save');
118 $this->assertText('Your comment has been posted.');
119 $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
120 $this->assertEqual(2, count($elements));
124 * Tests comment edit, preview, and save.
126 public function testCommentEditPreviewSave() {
127 $web_user = $this->drupalCreateUser(['access comments', 'post comments', 'skip comment approval', 'edit own comments']);
128 $this->drupalLogin($this->adminUser);
129 $this->setCommentPreview(DRUPAL_OPTIONAL);
130 $this->setCommentForm(TRUE);
131 $this->setCommentSubject(TRUE);
132 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
135 $date = new DrupalDateTime('2008-03-02 17:23');
136 $edit['subject[0][value]'] = $this->randomMachineName(8);
137 $edit['comment_body[0][value]'] = $this->randomMachineName(16);
138 $edit['uid'] = $web_user->getUsername() . ' (' . $web_user->id() . ')';
139 $edit['date[date]'] = $date->format('Y-m-d');
140 $edit['date[time]'] = $date->format('H:i:s');
141 $raw_date = $date->getTimestamp();
142 $expected_text_date = format_date($raw_date);
143 $expected_form_date = $date->format('Y-m-d');
144 $expected_form_time = $date->format('H:i:s');
145 $comment = $this->postComment($this->node, $edit['subject[0][value]'], $edit['comment_body[0][value]'], TRUE);
146 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Preview'));
148 // Check that the preview is displaying the subject, comment, author and date correctly.
149 $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
150 $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
151 $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
152 $this->assertText($web_user->getUsername(), 'Author displayed.');
153 $this->assertText($expected_text_date, 'Date displayed.');
155 // Check that the subject, comment, author and date fields are displayed with the correct values.
156 $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
157 $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
158 $this->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
159 $this->assertFieldByName('date[date]', $edit['date[date]'], 'Date field displayed.');
160 $this->assertFieldByName('date[time]', $edit['date[time]'], 'Time field displayed.');
162 // Check that saving a comment produces a success message.
163 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Save'));
164 $this->assertText(t('Your comment has been posted.'), 'Comment posted.');
166 // Check that the comment fields are correct after loading the saved comment.
167 $this->drupalGet('comment/' . $comment->id() . '/edit');
168 $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
169 $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
170 $this->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
171 $this->assertFieldByName('date[date]', $expected_form_date, 'Date field displayed.');
172 $this->assertFieldByName('date[time]', $expected_form_time, 'Time field displayed.');
174 // Submit the form using the displayed values.
176 $displayed['subject[0][value]'] = (string) current($this->xpath("//input[@id='edit-subject-0-value']/@value"));
177 $displayed['comment_body[0][value]'] = (string) current($this->xpath("//textarea[@id='edit-comment-body-0-value']"));
178 $displayed['uid'] = (string) current($this->xpath("//input[@id='edit-uid']/@value"));
179 $displayed['date[date]'] = (string) current($this->xpath("//input[@id='edit-date-date']/@value"));
180 $displayed['date[time]'] = (string) current($this->xpath("//input[@id='edit-date-time']/@value"));
181 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $displayed, t('Save'));
183 // Check that the saved comment is still correct.
184 $comment_storage = \Drupal::entityManager()->getStorage('comment');
185 $comment_storage->resetCache([$comment->id()]);
186 /** @var \Drupal\comment\CommentInterface $comment_loaded */
187 $comment_loaded = Comment::load($comment->id());
188 $this->assertEqual($comment_loaded->getSubject(), $edit['subject[0][value]'], 'Subject loaded.');
189 $this->assertEqual($comment_loaded->comment_body->value, $edit['comment_body[0][value]'], 'Comment body loaded.');
190 $this->assertEqual($comment_loaded->getOwner()->id(), $web_user->id(), 'Name loaded.');
191 $this->assertEqual($comment_loaded->getCreatedTime(), $raw_date, 'Date loaded.');
192 $this->drupalLogout();
194 // Check that the date and time of the comment are correct when edited by
197 $expected_created_time = $comment_loaded->getCreatedTime();
198 $this->drupalLogin($web_user);
199 // Web user cannot change the comment author.
201 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $user_edit, t('Save'));
202 $comment_storage->resetCache([$comment->id()]);
203 $comment_loaded = Comment::load($comment->id());
204 $this->assertEqual($comment_loaded->getCreatedTime(), $expected_created_time, 'Expected date and time for comment edited.');
205 $this->drupalLogout();