CommentPreviewTest.php 10.2 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\comment\Functional;
4

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;
10

11
/**
12 13 14
 * Tests comment preview.
 *
 * @group comment
15 16
 */
class CommentPreviewTest extends CommentTestBase {
17

18 19 20 21
  use TestFileCreationTrait {
    getTestFiles as drupalGetTestFiles;
  }

22
  /**
23
   * The profile to install as a basis for testing.
24
   *
25 26 27
   * Using the standard profile to test user picture display in comments.
   *
   * @var string
28
   */
29
  protected $profile = 'standard';
30

31 32 33
  /**
   * Tests comment preview.
   */
34
  public function testCommentPreview() {
35
    // As admin user, configure comment settings.
36
    $this->drupalLogin($this->adminUser);
37 38 39
    $this->setCommentPreview(DRUPAL_OPTIONAL);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);
40
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
41 42
    $this->drupalLogout();

43
    // Log in as web user.
44
    $this->drupalLogin($this->webUser);
45

46 47 48
    // Test escaping of the username on the preview form.
    \Drupal::service('module_installer')->install(['user_hooks_test']);
    \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
49
    $edit = [];
50 51
    $edit['subject[0][value]'] = $this->randomMachineName(8);
    $edit['comment_body[0][value]'] = $this->randomMachineName(16);
52
    $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
53
    $this->assertSession()->assertEscaped('<em>' . $this->webUser->id() . '</em>');
54 55 56

    \Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE);
    $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
57
    $this->assertInstanceOf(MarkupInterface::class, $this->webUser->getDisplayName());
58
    $this->assertSession()->assertNoEscaped('<em>' . $this->webUser->id() . '</em>');
59 60 61 62
    $this->assertRaw('<em>' . $this->webUser->id() . '</em>');

    // Add a user picture.
    $image = current($this->drupalGetTestFiles('image'));
63
    $user_edit['files[user_picture_0]'] = \Drupal::service('file_system')->realpath($image->uri);
64 65 66 67
    $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $user_edit, t('Save'));

    // As the web user, fill in the comment form and preview the comment.
    $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
68 69

    // Check that the preview is displaying the title and body.
70
    $this->assertSession()->titleEquals('Preview comment | Drupal');
71
    $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
72
    $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
73 74

    // Check that the title and body fields are displayed with the correct values.
75 76
    $this->assertSession()->fieldValueEquals('subject[0][value]', $edit['subject[0][value]']);
    $this->assertSession()->fieldValueEquals('comment_body[0][value]', $edit['comment_body[0][value]']);
77 78

    // Check that the user picture is displayed.
79
    $this->assertSession()->elementExists('xpath', "//article[contains(@class, 'preview')]//div[contains(@class, 'user-picture')]//img");
80 81
  }

82 83 84 85 86 87 88 89 90 91 92 93
  /**
   * Tests comment preview.
   */
  public function testCommentPreviewDuplicateSubmission() {
    // As admin user, configure comment settings.
    $this->drupalLogin($this->adminUser);
    $this->setCommentPreview(DRUPAL_OPTIONAL);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
    $this->drupalLogout();

94
    // Log in as web user.
95 96 97
    $this->drupalLogin($this->webUser);

    // As the web user, fill in the comment form and preview the comment.
98
    $edit = [];
99 100 101 102 103
    $edit['subject[0][value]'] = $this->randomMachineName(8);
    $edit['comment_body[0][value]'] = $this->randomMachineName(16);
    $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));

    // Check that the preview is displaying the title and body.
104
    $this->assertSession()->titleEquals('Preview comment | Drupal');
105 106 107 108
    $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
    $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');

    // Check that the title and body fields are displayed with the correct values.
109 110
    $this->assertSession()->fieldValueEquals('subject[0][value]', $edit['subject[0][value]']);
    $this->assertSession()->fieldValueEquals('comment_body[0][value]', $edit['comment_body[0][value]']);
111 112 113 114 115

    // Store the content of this page.
    $this->drupalPostForm(NULL, [], 'Save');
    $this->assertText('Your comment has been posted.');
    $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
116
    $this->assertCount(1, $elements);
117

118 119
    // Go back and re-submit the form.
    $this->getSession()->getDriver()->back();
120 121
    $submit_button = $this->assertSession()->buttonExists('Save');
    $submit_button->click();
122 123
    $this->assertText('Your comment has been posted.');
    $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
124
    $this->assertCount(2, $elements);
125 126
  }

127 128 129
  /**
   * Tests comment edit, preview, and save.
   */
130
  public function testCommentEditPreviewSave() {
131 132 133 134 135 136
    $web_user = $this->drupalCreateUser([
      'access comments',
      'post comments',
      'skip comment approval',
      'edit own comments',
    ]);
137
    $this->drupalLogin($this->adminUser);
138 139 140
    $this->setCommentPreview(DRUPAL_OPTIONAL);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(TRUE);
141
    $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
142

143
    $edit = [];
144
    $date = new DrupalDateTime('2008-03-02 17:23');
145 146
    $edit['subject[0][value]'] = $this->randomMachineName(8);
    $edit['comment_body[0][value]'] = $this->randomMachineName(16);
147
    $edit['uid'] = $web_user->getAccountName() . ' (' . $web_user->id() . ')';
148 149 150
    $edit['date[date]'] = $date->format('Y-m-d');
    $edit['date[time]'] = $date->format('H:i:s');
    $raw_date = $date->getTimestamp();
151
    $expected_text_date = $this->container->get('date.formatter')->format($raw_date);
152 153
    $expected_form_date = $date->format('Y-m-d');
    $expected_form_time = $date->format('H:i:s');
154
    $comment = $this->postComment($this->node, $edit['subject[0][value]'], $edit['comment_body[0][value]'], TRUE);
155
    $this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Preview'));
156 157

    // Check that the preview is displaying the subject, comment, author and date correctly.
158
    $this->assertSession()->titleEquals('Preview comment | Drupal');
159
    $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
160
    $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
161
    $this->assertText($web_user->getAccountName(), 'Author displayed.');
162
    $this->assertText($expected_text_date, 'Date displayed.');
163 164

    // Check that the subject, comment, author and date fields are displayed with the correct values.
165 166 167 168 169
    $this->assertSession()->fieldValueEquals('subject[0][value]', $edit['subject[0][value]']);
    $this->assertSession()->fieldValueEquals('comment_body[0][value]', $edit['comment_body[0][value]']);
    $this->assertSession()->fieldValueEquals('uid', $edit['uid']);
    $this->assertSession()->fieldValueEquals('date[date]', $edit['date[date]']);
    $this->assertSession()->fieldValueEquals('date[time]', $edit['date[time]']);
170 171

    // Check that saving a comment produces a success message.
172
    $this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Save'));
173
    $this->assertText(t('Your comment has been posted.'), 'Comment posted.');
174 175

    // Check that the comment fields are correct after loading the saved comment.
176
    $this->drupalGet('comment/' . $comment->id() . '/edit');
177 178 179 180 181
    $this->assertSession()->fieldValueEquals('subject[0][value]', $edit['subject[0][value]']);
    $this->assertSession()->fieldValueEquals('comment_body[0][value]', $edit['comment_body[0][value]']);
    $this->assertSession()->fieldValueEquals('uid', $edit['uid']);
    $this->assertSession()->fieldValueEquals('date[date]', $expected_form_date);
    $this->assertSession()->fieldValueEquals('date[time]', $expected_form_time);
182 183

    // Submit the form using the displayed values.
184
    $displayed = [];
185 186 187 188 189
    $displayed['subject[0][value]'] = current($this->xpath("//input[@id='edit-subject-0-value']"))->getValue();
    $displayed['comment_body[0][value]'] = current($this->xpath("//textarea[@id='edit-comment-body-0-value']"))->getValue();
    $displayed['uid'] = current($this->xpath("//input[@id='edit-uid']"))->getValue();
    $displayed['date[date]'] = current($this->xpath("//input[@id='edit-date-date']"))->getValue();
    $displayed['date[time]'] = current($this->xpath("//input[@id='edit-date-time']"))->getValue();
190
    $this->drupalPostForm('comment/' . $comment->id() . '/edit', $displayed, t('Save'));
191 192

    // Check that the saved comment is still correct.
193
    $comment_storage = \Drupal::entityTypeManager()->getStorage('comment');
194
    $comment_storage->resetCache([$comment->id()]);
195
    /** @var \Drupal\comment\CommentInterface $comment_loaded */
196
    $comment_loaded = Comment::load($comment->id());
197
    $this->assertEqual($comment_loaded->getSubject(), $edit['subject[0][value]'], 'Subject loaded.');
198
    $this->assertEqual($comment_loaded->comment_body->value, $edit['comment_body[0][value]'], 'Comment body loaded.');
199
    $this->assertEqual($comment_loaded->getOwner()->id(), $web_user->id(), 'Name loaded.');
200
    $this->assertEqual($comment_loaded->getCreatedTime(), $raw_date, 'Date loaded.');
201 202 203 204
    $this->drupalLogout();

    // Check that the date and time of the comment are correct when edited by
    // non-admin users.
205
    $user_edit = [];
206
    $expected_created_time = $comment_loaded->getCreatedTime();
207
    $this->drupalLogin($web_user);
208 209
    // Web user cannot change the comment author.
    unset($edit['uid']);
210
    $this->drupalPostForm('comment/' . $comment->id() . '/edit', $user_edit, t('Save'));
211
    $comment_storage->resetCache([$comment->id()]);
212
    $comment_loaded = Comment::load($comment->id());
213
    $this->assertEqual($comment_loaded->getCreatedTime(), $expected_created_time, 'Expected date and time for comment edited.');
214
    $this->drupalLogout();
215 216 217
  }

}