Chris@0: drupalLogin($this->adminUser);
Chris@0: $this->setCommentPreview(DRUPAL_OPTIONAL);
Chris@0: $this->setCommentForm(TRUE);
Chris@0: $this->setCommentSubject(TRUE);
Chris@0: $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: // Log in as web user.
Chris@0: $this->drupalLogin($this->webUser);
Chris@0:
Chris@0: // Test escaping of the username on the preview form.
Chris@0: \Drupal::service('module_installer')->install(['user_hooks_test']);
Chris@0: \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
Chris@0: $edit = [];
Chris@0: $edit['subject[0][value]'] = $this->randomMachineName(8);
Chris@0: $edit['comment_body[0][value]'] = $this->randomMachineName(16);
Chris@0: $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
Chris@0: $this->assertEscaped('' . $this->webUser->id() . '');
Chris@0:
Chris@0: \Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE);
Chris@0: $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
Chris@0: $this->assertTrue($this->webUser->getDisplayName() instanceof MarkupInterface, 'Username is marked safe');
Chris@0: $this->assertNoEscaped('' . $this->webUser->id() . '');
Chris@0: $this->assertRaw('' . $this->webUser->id() . '');
Chris@0:
Chris@0: // Add a user picture.
Chris@0: $image = current($this->drupalGetTestFiles('image'));
Chris@14: $user_edit['files[user_picture_0]'] = \Drupal::service('file_system')->realpath($image->uri);
Chris@0: $this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $user_edit, t('Save'));
Chris@0:
Chris@0: // As the web user, fill in the comment form and preview the comment.
Chris@0: $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
Chris@0:
Chris@0: // Check that the preview is displaying the title and body.
Chris@0: $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
Chris@0: $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
Chris@0: $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
Chris@0:
Chris@0: // Check that the title and body fields are displayed with the correct values.
Chris@0: $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
Chris@0: $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
Chris@0:
Chris@0: // Check that the user picture is displayed.
Chris@0: $this->assertFieldByXPath("//article[contains(@class, 'preview')]//div[contains(@class, 'user-picture')]//img", NULL, 'User picture displayed.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests comment preview.
Chris@0: */
Chris@0: public function testCommentPreviewDuplicateSubmission() {
Chris@0: // As admin user, configure comment settings.
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: $this->setCommentPreview(DRUPAL_OPTIONAL);
Chris@0: $this->setCommentForm(TRUE);
Chris@0: $this->setCommentSubject(TRUE);
Chris@0: $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: // Log in as web user.
Chris@0: $this->drupalLogin($this->webUser);
Chris@0:
Chris@0: // As the web user, fill in the comment form and preview the comment.
Chris@0: $edit = [];
Chris@0: $edit['subject[0][value]'] = $this->randomMachineName(8);
Chris@0: $edit['comment_body[0][value]'] = $this->randomMachineName(16);
Chris@0: $this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
Chris@0:
Chris@0: // Check that the preview is displaying the title and body.
Chris@0: $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
Chris@0: $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
Chris@0: $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
Chris@0:
Chris@0: // Check that the title and body fields are displayed with the correct values.
Chris@0: $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
Chris@0: $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
Chris@0:
Chris@0: // Store the content of this page.
Chris@0: $this->drupalPostForm(NULL, [], 'Save');
Chris@0: $this->assertText('Your comment has been posted.');
Chris@0: $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
Chris@0: $this->assertEqual(1, count($elements));
Chris@0:
Chris@0: // Go back and re-submit the form.
Chris@0: $this->getSession()->getDriver()->back();
Chris@14: $submit_button = $this->assertSession()->buttonExists('Save');
Chris@14: $submit_button->click();
Chris@0: $this->assertText('Your comment has been posted.');
Chris@0: $elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
Chris@0: $this->assertEqual(2, count($elements));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests comment edit, preview, and save.
Chris@0: */
Chris@0: public function testCommentEditPreviewSave() {
Chris@0: $web_user = $this->drupalCreateUser(['access comments', 'post comments', 'skip comment approval', 'edit own comments']);
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: $this->setCommentPreview(DRUPAL_OPTIONAL);
Chris@0: $this->setCommentForm(TRUE);
Chris@0: $this->setCommentSubject(TRUE);
Chris@0: $this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
Chris@0:
Chris@0: $edit = [];
Chris@0: $date = new DrupalDateTime('2008-03-02 17:23');
Chris@0: $edit['subject[0][value]'] = $this->randomMachineName(8);
Chris@0: $edit['comment_body[0][value]'] = $this->randomMachineName(16);
Chris@18: $edit['uid'] = $web_user->getAccountName() . ' (' . $web_user->id() . ')';
Chris@0: $edit['date[date]'] = $date->format('Y-m-d');
Chris@0: $edit['date[time]'] = $date->format('H:i:s');
Chris@0: $raw_date = $date->getTimestamp();
Chris@18: $expected_text_date = $this->container->get('date.formatter')->format($raw_date);
Chris@0: $expected_form_date = $date->format('Y-m-d');
Chris@0: $expected_form_time = $date->format('H:i:s');
Chris@0: $comment = $this->postComment($this->node, $edit['subject[0][value]'], $edit['comment_body[0][value]'], TRUE);
Chris@0: $this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Preview'));
Chris@0:
Chris@0: // Check that the preview is displaying the subject, comment, author and date correctly.
Chris@0: $this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
Chris@0: $this->assertText($edit['subject[0][value]'], 'Subject displayed.');
Chris@0: $this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
Chris@18: $this->assertText($web_user->getAccountName(), 'Author displayed.');
Chris@0: $this->assertText($expected_text_date, 'Date displayed.');
Chris@0:
Chris@0: // Check that the subject, comment, author and date fields are displayed with the correct values.
Chris@0: $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
Chris@0: $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
Chris@0: $this->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
Chris@0: $this->assertFieldByName('date[date]', $edit['date[date]'], 'Date field displayed.');
Chris@0: $this->assertFieldByName('date[time]', $edit['date[time]'], 'Time field displayed.');
Chris@0:
Chris@0: // Check that saving a comment produces a success message.
Chris@0: $this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Save'));
Chris@0: $this->assertText(t('Your comment has been posted.'), 'Comment posted.');
Chris@0:
Chris@0: // Check that the comment fields are correct after loading the saved comment.
Chris@0: $this->drupalGet('comment/' . $comment->id() . '/edit');
Chris@0: $this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
Chris@0: $this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
Chris@0: $this->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
Chris@0: $this->assertFieldByName('date[date]', $expected_form_date, 'Date field displayed.');
Chris@0: $this->assertFieldByName('date[time]', $expected_form_time, 'Time field displayed.');
Chris@0:
Chris@0: // Submit the form using the displayed values.
Chris@0: $displayed = [];
Chris@0: $displayed['subject[0][value]'] = current($this->xpath("//input[@id='edit-subject-0-value']"))->getValue();
Chris@0: $displayed['comment_body[0][value]'] = current($this->xpath("//textarea[@id='edit-comment-body-0-value']"))->getValue();
Chris@0: $displayed['uid'] = current($this->xpath("//input[@id='edit-uid']"))->getValue();
Chris@0: $displayed['date[date]'] = current($this->xpath("//input[@id='edit-date-date']"))->getValue();
Chris@0: $displayed['date[time]'] = current($this->xpath("//input[@id='edit-date-time']"))->getValue();
Chris@0: $this->drupalPostForm('comment/' . $comment->id() . '/edit', $displayed, t('Save'));
Chris@0:
Chris@0: // Check that the saved comment is still correct.
Chris@0: $comment_storage = \Drupal::entityManager()->getStorage('comment');
Chris@0: $comment_storage->resetCache([$comment->id()]);
Chris@0: /** @var \Drupal\comment\CommentInterface $comment_loaded */
Chris@0: $comment_loaded = Comment::load($comment->id());
Chris@0: $this->assertEqual($comment_loaded->getSubject(), $edit['subject[0][value]'], 'Subject loaded.');
Chris@0: $this->assertEqual($comment_loaded->comment_body->value, $edit['comment_body[0][value]'], 'Comment body loaded.');
Chris@0: $this->assertEqual($comment_loaded->getOwner()->id(), $web_user->id(), 'Name loaded.');
Chris@0: $this->assertEqual($comment_loaded->getCreatedTime(), $raw_date, 'Date loaded.');
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: // Check that the date and time of the comment are correct when edited by
Chris@0: // non-admin users.
Chris@0: $user_edit = [];
Chris@0: $expected_created_time = $comment_loaded->getCreatedTime();
Chris@0: $this->drupalLogin($web_user);
Chris@0: // Web user cannot change the comment author.
Chris@0: unset($edit['uid']);
Chris@0: $this->drupalPostForm('comment/' . $comment->id() . '/edit', $user_edit, t('Save'));
Chris@0: $comment_storage->resetCache([$comment->id()]);
Chris@0: $comment_loaded = Comment::load($comment->id());
Chris@0: $this->assertEqual($comment_loaded->getCreatedTime(), $expected_created_time, 'Expected date and time for comment edited.');
Chris@0: $this->drupalLogout();
Chris@0: }
Chris@0:
Chris@0: }