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_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 $this->assertText('Your comment has been posted.');
121 $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
122 $this->assertEqual(2, count($elements));
126 * Tests comment edit, preview, and save.
128 public function testCommentEditPreviewSave() {
129 $web_user = $this->drupalCreateUser(['access comments', 'post comments', 'skip comment approval', 'edit own comments']);
130 $this->drupalLogin($this->adminUser);
131 $this->setCommentPreview(DRUPAL_OPTIONAL);
132 $this->setCommentForm(TRUE);
133 $this->setCommentSubject(TRUE);
134 $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
137 $date = new DrupalDateTime('2008-03-02 17:23');
138 $edit['subject[0][value]'] = $this->randomMachineName(8);
139 $edit['comment_body[0][value]'] = $this->randomMachineName(16);
140 $edit['uid'] = $web_user->getUsername() . ' (' . $web_user->id() . ')';
141 $edit['date[date]'] = $date->format('Y-m-d');
142 $edit['date[time]'] = $date->format('H:i:s');
143 $raw_date = $date->getTimestamp();
144 $expected_text_date = format_date($raw_date);
145 $expected_form_date = $date->format('Y-m-d');
146 $expected_form_time = $date->format('H:i:s');
147 $comment = $this->postComment($this->node, $edit['subject[0][value]'], $edit['comment_body[0][value]'], TRUE);
148 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Preview'));
150 // Check that the preview is displaying the subject, comment, author and date correctly.
151 $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
152 $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
153 $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
154 $this->assertText($web_user->getUsername(), 'Author displayed.');
155 $this->assertText($expected_text_date, 'Date displayed.');
157 // Check that the subject, comment, author and date fields are displayed with the correct values.
158 $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
159 $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
160 $this->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
161 $this->assertFieldByName('date[date]', $edit['date[date]'], 'Date field displayed.');
162 $this->assertFieldByName('date[time]', $edit['date[time]'], 'Time field displayed.');
164 // Check that saving a comment produces a success message.
165 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Save'));
166 $this->assertText(t('Your comment has been posted.'), 'Comment posted.');
168 // Check that the comment fields are correct after loading the saved comment.
169 $this->drupalGet('comment/' . $comment->id() . '/edit');
170 $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
171 $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
172 $this->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
173 $this->assertFieldByName('date[date]', $expected_form_date, 'Date field displayed.');
174 $this->assertFieldByName('date[time]', $expected_form_time, 'Time field displayed.');
176 // Submit the form using the displayed values.
178 $displayed['subject[0][value]'] = current($this->xpath("//input[@id='edit-subject-0-value']"))->getValue();
179 $displayed['comment_body[0][value]'] = current($this->xpath("//textarea[@id='edit-comment-body-0-value']"))->getValue();
180 $displayed['uid'] = current($this->xpath("//input[@id='edit-uid']"))->getValue();
181 $displayed['date[date]'] = current($this->xpath("//input[@id='edit-date-date']"))->getValue();
182 $displayed['date[time]'] = current($this->xpath("//input[@id='edit-date-time']"))->getValue();
183 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $displayed, t('Save'));
185 // Check that the saved comment is still correct.
186 $comment_storage = \Drupal::entityManager()->getStorage('comment');
187 $comment_storage->resetCache([$comment->id()]);
188 /** @var \Drupal\comment\CommentInterface $comment_loaded */
189 $comment_loaded = Comment::load($comment->id());
190 $this->assertEqual($comment_loaded->getSubject(), $edit['subject[0][value]'], 'Subject loaded.');
191 $this->assertEqual($comment_loaded->comment_body->value, $edit['comment_body[0][value]'], 'Comment body loaded.');
192 $this->assertEqual($comment_loaded->getOwner()->id(), $web_user->id(), 'Name loaded.');
193 $this->assertEqual($comment_loaded->getCreatedTime(), $raw_date, 'Date loaded.');
194 $this->drupalLogout();
196 // Check that the date and time of the comment are correct when edited by
199 $expected_created_time = $comment_loaded->getCreatedTime();
200 $this->drupalLogin($web_user);
201 // Web user cannot change the comment author.
203 $this->drupalPostForm('comment/' . $comment->id() . '/edit', $user_edit, t('Save'));
204 $comment_storage->resetCache([$comment->id()]);
205 $comment_loaded = Comment::load($comment->id());
206 $this->assertEqual($comment_loaded->getCreatedTime(), $expected_created_time, 'Expected date and time for comment edited.');
207 $this->drupalLogout();