function CommentAdminTest::testApprovalAdminInterface

Same name in this branch
  1. 8.9.x core/modules/comment/tests/src/Functional/CommentAdminTest.php \Drupal\Tests\comment\Functional\CommentAdminTest::testApprovalAdminInterface()
Same name in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentAdminTest.php \Drupal\Tests\comment\Functional\CommentAdminTest::testApprovalAdminInterface()
  2. 9 core/modules/comment/tests/src/Functional/Views/CommentAdminTest.php \Drupal\Tests\comment\Functional\Views\CommentAdminTest::testApprovalAdminInterface()
  3. 10 core/modules/comment/tests/src/Functional/CommentAdminTest.php \Drupal\Tests\comment\Functional\CommentAdminTest::testApprovalAdminInterface()
  4. 10 core/modules/comment/tests/src/Functional/Views/CommentAdminTest.php \Drupal\Tests\comment\Functional\Views\CommentAdminTest::testApprovalAdminInterface()
  5. 11.x core/modules/comment/tests/src/Functional/CommentAdminTest.php \Drupal\Tests\comment\Functional\CommentAdminTest::testApprovalAdminInterface()
  6. 11.x core/modules/comment/tests/src/Functional/Views/CommentAdminTest.php \Drupal\Tests\comment\Functional\Views\CommentAdminTest::testApprovalAdminInterface()

Test comment approval functionality through admin/content/comment.

File

core/modules/comment/tests/src/Functional/Views/CommentAdminTest.php, line 42

Class

CommentAdminTest
Tests comment approval functionality.

Namespace

Drupal\Tests\comment\Functional\Views

Code

public function testApprovalAdminInterface() {
    // Set anonymous comments to require approval.
    user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
        'access comments' => TRUE,
        'post comments' => TRUE,
        'skip comment approval' => FALSE,
    ]);
    $this->drupalPlaceBlock('page_title_block');
    $this->drupalLogin($this->adminUser);
    // Ensure that doesn't require contact info.
    $this->setCommentAnonymous('0');
    // Test that the comments page loads correctly when there are no comments.
    $this->drupalGet('admin/content/comment');
    $this->assertText(t('No comments available.'));
    // Assert the expose filters on the admin page.
    $this->assertField('subject');
    $this->assertField('author_name');
    $this->assertField('langcode');
    $this->drupalLogout();
    // Post anonymous comment without contact info.
    $body = $this->getRandomGenerator()
        ->sentences(4);
    $subject = Unicode::truncate(trim(Html::decodeEntities(strip_tags($body))), 29, TRUE, TRUE);
    $author_name = $this->randomMachineName();
    $this->drupalPostForm('comment/reply/node/' . $this->node
        ->id() . '/comment', [
        'name' => $author_name,
        'comment_body[0][value]' => $body,
    ], t('Save'));
    $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
    // Get unapproved comment id.
    $this->drupalLogin($this->adminUser);
    $anonymous_comment4 = $this->getUnapprovedComment($subject);
    $anonymous_comment4 = Comment::create([
        'cid' => $anonymous_comment4,
        'subject' => $subject,
        'comment_body' => $body,
        'entity_id' => $this->node
            ->id(),
        'entity_type' => 'node',
        'field_name' => 'comment',
    ]);
    $this->drupalLogout();
    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
    // Approve comment.
    $this->drupalLogin($this->adminUser);
    $edit = [];
    $edit['action'] = 'comment_publish_action';
    $edit['comment_bulk_form[0]'] = $anonymous_comment4->id();
    $this->drupalPostForm('admin/content/comment/approval', $edit, t('Apply to selected items'));
    $this->assertText('Publish comment was applied to 1 item.', new FormattableMarkup('Operation "@operation" was performed on comment.', [
        '@operation' => 'publish',
    ]));
    $this->drupalLogout();
    $this->drupalGet('node/' . $this->node
        ->id());
    $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
    // Post 2 anonymous comments without contact info.
    $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
    $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
    // Publish multiple comments in one operation.
    $this->drupalLogin($this->adminUser);
    $this->drupalGet('admin/content/comment/approval');
    $this->assertText(t('Unapproved comments (@count)', [
        '@count' => 2,
    ]), 'Two unapproved comments waiting for approval.');
    // Assert the expose filters on the admin page.
    $this->assertField('subject');
    $this->assertField('author_name');
    $this->assertField('langcode');
    $edit = [
        "action" => 'comment_publish_action',
        "comment_bulk_form[1]" => $comments[0]->id(),
        "comment_bulk_form[0]" => $comments[1]->id(),
    ];
    $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
    $this->assertText(t('Unapproved comments (@count)', [
        '@count' => 0,
    ]), 'All comments were approved.');
    // Test message when no comments selected.
    $this->drupalPostForm('admin/content/comment', [], t('Apply to selected items'));
    $this->assertText(t('Select one or more comments to perform the update on.'));
    $subject_link = $this->xpath('//table/tbody/tr/td/a[contains(@href, :href) and contains(@title, :title) and text()=:text]', [
        ':href' => $comments[0]->permalink()
            ->toString(),
        ':title' => Unicode::truncate($comments[0]->get('comment_body')->value, 128),
        ':text' => $comments[0]->getSubject(),
    ]);
    $this->assertTrue(!empty($subject_link), 'Comment listing shows the correct subject link.');
    $this->assertText($author_name . ' (not verified)', 'Anonymous author name is displayed correctly.');
    $subject_link = $this->xpath('//table/tbody/tr/td/a[contains(@href, :href) and contains(@title, :title) and text()=:text]', [
        ':href' => $anonymous_comment4->permalink()
            ->toString(),
        ':title' => Unicode::truncate($body, 128),
        ':text' => $subject,
    ]);
    $this->assertTrue(!empty($subject_link), 'Comment listing shows the correct subject link.');
    $this->assertText($author_name . ' (not verified)', 'Anonymous author name is displayed correctly.');
    // Delete multiple comments in one operation.
    $edit = [
        'action' => 'comment_delete_action',
        "comment_bulk_form[1]" => $comments[0]->id(),
        "comment_bulk_form[0]" => $comments[1]->id(),
        "comment_bulk_form[2]" => $anonymous_comment4->id(),
    ];
    $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
    $this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
    $this->drupalPostForm(NULL, [], t('Delete'));
    $this->assertText(t('No comments available.'), 'All comments were deleted.');
    // Make sure the label of unpublished node is not visible on listing page.
    $this->drupalGet('admin/content/comment');
    $this->postComment($this->node, $this->randomMachineName());
    $this->drupalLogout();
    $this->drupalLogin($this->adminUser);
    $this->drupalGet('admin/content/comment');
    $this->assertText(Html::escape($this->node
        ->label()), 'Comment admin can see the title of a published node');
    $this->node
        ->setUnpublished()
        ->save();
    $this->assertFalse($this->node
        ->isPublished(), 'Node is unpublished now.');
    $this->drupalGet('admin/content/comment');
    $this->assertNoText(Html::escape($this->node
        ->label()), 'Comment admin cannot see the title of an unpublished node');
    $this->drupalLogout();
    $node_access_user = $this->drupalCreateUser([
        'administer comments',
        'bypass node access',
    ]);
    $this->drupalLogin($node_access_user);
    $this->drupalGet('admin/content/comment');
    $this->assertText(Html::escape($this->node
        ->label()), 'Comment admin with bypass node access permissions can still see the title of a published node');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.