Chris@0: drupalPlaceBlock('page_title_block');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test comment approval functionality through admin/content/comment.
Chris@0: */
Chris@0: public function testApprovalAdminInterface() {
Chris@0: // Set anonymous comments to require approval.
Chris@0: user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
Chris@0: 'access comments' => TRUE,
Chris@0: 'post comments' => TRUE,
Chris@0: 'skip comment approval' => FALSE,
Chris@0: ]);
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: // Ensure that doesn't require contact info.
Chris@0: $this->setCommentAnonymous('0');
Chris@0:
Chris@0: // Test that the comments page loads correctly when there are no comments
Chris@0: $this->drupalGet('admin/content/comment');
Chris@0: $this->assertText(t('No comments available.'));
Chris@0:
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: // Post anonymous comment without contact info.
Chris@0: $subject = $this->randomMachineName();
Chris@0: $body = $this->randomMachineName();
Chris@0: // Set $contact to true so that it won't check for id and message.
Chris@0: $this->postComment($this->node, $body, $subject, TRUE);
Chris@0: $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
Chris@0:
Chris@0: // Get unapproved comment id.
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: $anonymous_comment4 = $this->getUnapprovedComment($subject);
Chris@0: $anonymous_comment4 = Comment::create([
Chris@0: 'cid' => $anonymous_comment4,
Chris@0: 'subject' => $subject,
Chris@0: 'comment_body' => $body,
Chris@0: 'entity_id' => $this->node->id(),
Chris@0: 'entity_type' => 'node',
Chris@17: 'field_name' => 'comment',
Chris@0: ]);
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
Chris@0:
Chris@0: // Approve comment.
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: $this->drupalGet('node/' . $this->node->id());
Chris@0: $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
Chris@0:
Chris@0: // Post 2 anonymous comments without contact info.
Chris@0: $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
Chris@0: $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
Chris@0:
Chris@0: // Publish multiple comments in one operation.
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: $this->drupalGet('admin/content/comment/approval');
Chris@0: $this->assertText(t('Unapproved comments (@count)', ['@count' => 2]), 'Two unapproved comments waiting for approval.');
Chris@0: $edit = [
Chris@0: "comments[{$comments[0]->id()}]" => 1,
Chris@0: "comments[{$comments[1]->id()}]" => 1,
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Update'));
Chris@0: $this->assertText(t('Unapproved comments (@count)', ['@count' => 0]), 'All comments were approved.');
Chris@0:
Chris@0: // Delete multiple comments in one operation.
Chris@0: $edit = [
Chris@0: 'operation' => 'delete',
Chris@0: "comments[{$comments[0]->id()}]" => 1,
Chris@0: "comments[{$comments[1]->id()}]" => 1,
Chris@0: "comments[{$anonymous_comment4->id()}]" => 1,
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Update'));
Chris@0: $this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
Chris@0: $this->drupalPostForm(NULL, [], t('Delete'));
Chris@0: $this->assertText(t('No comments available.'), 'All comments were deleted.');
Chris@0: // Test message when no comments selected.
Chris@0: $edit = [
Chris@0: 'operation' => 'delete',
Chris@0: ];
Chris@0: $this->drupalPostForm(NULL, $edit, t('Update'));
Chris@0: $this->assertText(t('Select one or more comments to perform the update on.'));
Chris@0:
Chris@0: // Make sure the label of unpublished node is not visible on listing page.
Chris@0: $this->drupalGet('admin/content/comment');
Chris@0: $this->postComment($this->node, $this->randomMachineName());
Chris@0: $this->drupalGet('admin/content/comment');
Chris@0: $this->assertText(Html::escape($this->node->label()));
Chris@0: $this->node->setUnpublished()->save();
Chris@0: $this->drupalGet('admin/content/comment');
Chris@0: $this->assertNoText(Html::escape($this->node->label()));
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests comment approval functionality through the node interface.
Chris@0: */
Chris@0: public function testApprovalNodeInterface() {
Chris@0: // Set anonymous comments to require approval.
Chris@0: user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
Chris@0: 'access comments' => TRUE,
Chris@0: 'post comments' => TRUE,
Chris@0: 'skip comment approval' => FALSE,
Chris@0: ]);
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: // Ensure that doesn't require contact info.
Chris@0: $this->setCommentAnonymous('0');
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: // Post anonymous comment without contact info.
Chris@0: $subject = $this->randomMachineName();
Chris@0: $body = $this->randomMachineName();
Chris@0: // Set $contact to true so that it won't check for id and message.
Chris@0: $this->postComment($this->node, $body, $subject, TRUE);
Chris@0: $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
Chris@0:
Chris@0: // Get unapproved comment id.
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: $anonymous_comment4 = $this->getUnapprovedComment($subject);
Chris@0: $anonymous_comment4 = Comment::create([
Chris@0: 'cid' => $anonymous_comment4,
Chris@0: 'subject' => $subject,
Chris@0: 'comment_body' => $body,
Chris@0: 'entity_id' => $this->node->id(),
Chris@0: 'entity_type' => 'node',
Chris@17: 'field_name' => 'comment',
Chris@0: ]);
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
Chris@0:
Chris@0: // Approve comment.
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: $this->drupalGet('comment/1/approve');
Chris@0: $this->assertResponse(403, 'Forged comment approval was denied.');
Chris@0: $this->drupalGet('comment/1/approve', ['query' => ['token' => 'forged']]);
Chris@0: $this->assertResponse(403, 'Forged comment approval was denied.');
Chris@0: $this->drupalGet('comment/1/edit');
Chris@0: $this->assertFieldChecked('edit-status-0');
Chris@0: $this->drupalGet('node/' . $this->node->id());
Chris@0: $this->clickLink(t('Approve'));
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: $this->drupalGet('node/' . $this->node->id());
Chris@0: $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests comment bundle admin.
Chris@0: */
Chris@0: public function testCommentAdmin() {
Chris@0: // Login.
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: // Browse to comment bundle overview.
Chris@0: $this->drupalGet('admin/structure/comment');
Chris@0: $this->assertResponse(200);
Chris@0: // Make sure titles visible.
Chris@0: $this->assertText('Comment type');
Chris@0: $this->assertText('Description');
Chris@0: // Make sure the description is present.
Chris@0: $this->assertText('Default comment field');
Chris@0: // Manage fields.
Chris@0: $this->clickLink('Manage fields');
Chris@0: $this->assertResponse(200);
Chris@0: // Make sure comment_body field is shown.
Chris@0: $this->assertText('comment_body');
Chris@0: // Rest from here on in is field_ui.
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests editing a comment as an admin.
Chris@0: */
Chris@0: public function testEditComment() {
Chris@0: // Enable anonymous user comments.
Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
Chris@0: 'access comments',
Chris@0: 'post comments',
Chris@0: 'skip comment approval',
Chris@0: ]);
Chris@0:
Chris@0: // Log in as a web user.
Chris@0: $this->drupalLogin($this->webUser);
Chris@0: // Post a comment.
Chris@0: $comment = $this->postComment($this->node, $this->randomMachineName());
Chris@0:
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: // Post anonymous comment.
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: // Ensure that we need email id before posting comment.
Chris@0: $this->setCommentAnonymous('2');
Chris@0: $this->drupalLogout();
Chris@0:
Chris@0: // Post comment with contact info (required).
Chris@0: $author_name = $this->randomMachineName();
Chris@0: $author_mail = $this->randomMachineName() . '@example.com';
Chris@0: $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]);
Chris@0:
Chris@0: // Log in as an admin user.
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0:
Chris@0: // Make sure the comment field is not visible when
Chris@0: // the comment was posted by an authenticated user.
Chris@0: $this->drupalGet('comment/' . $comment->id() . '/edit');
Chris@0: $this->assertNoFieldById('edit-mail', $comment->getAuthorEmail());
Chris@0:
Chris@0: // Make sure the comment field is visible when
Chris@0: // the comment was posted by an anonymous user.
Chris@0: $this->drupalGet('comment/' . $anonymous_comment->id() . '/edit');
Chris@0: $this->assertFieldById('edit-mail', $anonymous_comment->getAuthorEmail());
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests commented translation deletion admin view.
Chris@0: */
Chris@0: public function testCommentedTranslationDeletion() {
Chris@0: \Drupal::service('module_installer')->install([
Chris@0: 'language',
Chris@0: 'locale',
Chris@0: ]);
Chris@0: \Drupal::service('router.builder')->rebuildIfNeeded();
Chris@0:
Chris@0: ConfigurableLanguage::createFromLangcode('ur')->save();
Chris@0: // Rebuild the container to update the default language container variable.
Chris@0: $this->rebuildContainer();
Chris@0: // Ensure that doesn't require contact info.
Chris@0: $this->setCommentAnonymous('0');
Chris@0: $this->drupalLogin($this->webUser);
Chris@0: $count_query = \Drupal::entityTypeManager()
Chris@0: ->getStorage('comment')
Chris@0: ->getQuery()
Chris@0: ->count();
Chris@0: $before_count = $count_query->execute();
Chris@0: // Post 2 anonymous comments without contact info.
Chris@0: $comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
Chris@0: $comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
Chris@0:
Chris@0: $comment1->addTranslation('ur', ['subject' => 'ur ' . $comment1->label()])
Chris@0: ->save();
Chris@0: $comment2->addTranslation('ur', ['subject' => 'ur ' . $comment1->label()])
Chris@0: ->save();
Chris@0: $this->drupalLogout();
Chris@0: $this->drupalLogin($this->adminUser);
Chris@0: // Delete multiple comments in one operation.
Chris@0: $edit = [
Chris@0: 'operation' => 'delete',
Chris@0: "comments[{$comment1->id()}]" => 1,
Chris@0: "comments[{$comment2->id()}]" => 1,
Chris@0: ];
Chris@0: $this->drupalPostForm('admin/content/comment', $edit, t('Update'));
Chris@0: $this->assertRaw(new FormattableMarkup('@label (Original translation) - The following comment translations will be deleted:', ['@label' => $comment1->label()]));
Chris@0: $this->assertRaw(new FormattableMarkup('@label (Original translation) - The following comment translations will be deleted:', ['@label' => $comment2->label()]));
Chris@0: $this->assertText('English');
Chris@0: $this->assertText('Urdu');
Chris@0: $this->drupalPostForm(NULL, [], t('Delete'));
Chris@0: $after_count = $count_query->execute();
Chris@0: $this->assertEqual($after_count, $before_count, 'No comment or translation found.');
Chris@0: }
Chris@0:
Chris@0: }