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