CommentAdminTest.php 8.19 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\comment\Tests;

5
use Drupal\user\RoleInterface;
6
use Drupal\comment\Entity\Comment;
7

8 9
/**
 * Tests comment approval functionality.
10 11
 *
 * @group comment
12
 */
13
class CommentAdminTest extends CommentTestBase {
14 15 16 17 18 19 20

  protected function setUp() {
    parent::setUp();

    $this->drupalPlaceBlock('page_title_block');
  }

21 22 23 24 25
  /**
   * Test comment approval functionality through admin/content/comment.
   */
  function testApprovalAdminInterface() {
    // Set anonymous comments to require approval.
26
    user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
27 28 29 30
      'access comments' => TRUE,
      'post comments' => TRUE,
      'skip comment approval' => FALSE,
    ));
31
    $this->drupalLogin($this->adminUser);
32 33 34 35 36 37 38 39 40
    $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.

    // Test that the comments page loads correctly when there are no comments
    $this->drupalGet('admin/content/comment');
    $this->assertText(t('No comments available.'));

    $this->drupalLogout();

    // Post anonymous comment without contact info.
41 42
    $subject = $this->randomMachineName();
    $body = $this->randomMachineName();
43
    $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
44
    $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
45 46

    // Get unapproved comment id.
47
    $this->drupalLogin($this->adminUser);
48
    $anonymous_comment4 = $this->getUnapprovedComment($subject);
49
    $anonymous_comment4 = Comment::create(array(
50 51 52 53 54 55 56
      'cid' => $anonymous_comment4,
      'subject' => $subject,
      'comment_body' => $body,
      'entity_id' => $this->node->id(),
      'entity_type' => 'node',
      'field_name' => 'comment'
    ));
57 58
    $this->drupalLogout();

59
    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
60 61

    // Approve comment.
62
    $this->drupalLogin($this->adminUser);
63 64 65
    $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
    $this->drupalLogout();

66
    $this->drupalGet('node/' . $this->node->id());
67
    $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
68 69

    // Post 2 anonymous comments without contact info.
70 71
    $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
    $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
72 73

    // Publish multiple comments in one operation.
74
    $this->drupalLogin($this->adminUser);
75
    $this->drupalGet('admin/content/comment/approval');
76
    $this->assertText(t('Unapproved comments (@count)', array('@count' => 2)), 'Two unapproved comments waiting for approval.');
77
    $edit = array(
78 79
      "comments[{$comments[0]->id()}]" => 1,
      "comments[{$comments[1]->id()}]" => 1,
80
    );
81
    $this->drupalPostForm(NULL, $edit, t('Update'));
82
    $this->assertText(t('Unapproved comments (@count)', array('@count' => 0)), 'All comments were approved.');
83 84 85 86

    // Delete multiple comments in one operation.
    $edit = array(
      'operation' => 'delete',
87 88 89
      "comments[{$comments[0]->id()}]" => 1,
      "comments[{$comments[1]->id()}]" => 1,
      "comments[{$anonymous_comment4->id()}]" => 1,
90
    );
91
    $this->drupalPostForm(NULL, $edit, t('Update'));
92
    $this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
93
    $this->drupalPostForm(NULL, $edit, t('Delete comments'));
94
    $this->assertText(t('No comments available.'), 'All comments were deleted.');
95 96 97 98 99 100
    // Test message when no comments selected.
    $edit = array(
      'operation' => 'delete',
    );
    $this->drupalPostForm(NULL, $edit, t('Update'));
    $this->assertText(t('Select one or more comments to perform the update on.'));
101 102 103 104 105 106 107
  }

  /**
   * Tests comment approval functionality through the node interface.
   */
  function testApprovalNodeInterface() {
    // Set anonymous comments to require approval.
108
    user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
109 110 111 112
      'access comments' => TRUE,
      'post comments' => TRUE,
      'skip comment approval' => FALSE,
    ));
113
    $this->drupalLogin($this->adminUser);
114 115 116 117
    $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
    $this->drupalLogout();

    // Post anonymous comment without contact info.
118 119
    $subject = $this->randomMachineName();
    $body = $this->randomMachineName();
120
    $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
121
    $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
122 123

    // Get unapproved comment id.
124
    $this->drupalLogin($this->adminUser);
125
    $anonymous_comment4 = $this->getUnapprovedComment($subject);
126
    $anonymous_comment4 = Comment::create(array(
127 128 129 130 131 132 133
      'cid' => $anonymous_comment4,
      'subject' => $subject,
      'comment_body' => $body,
      'entity_id' => $this->node->id(),
      'entity_type' => 'node',
      'field_name' => 'comment'
    ));
134 135
    $this->drupalLogout();

136
    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
137 138

    // Approve comment.
139
    $this->drupalLogin($this->adminUser);
140
    $this->drupalGet('comment/1/approve');
141
    $this->assertResponse(403, 'Forged comment approval was denied.');
142
    $this->drupalGet('comment/1/approve', array('query' => array('token' => 'forged')));
143
    $this->assertResponse(403, 'Forged comment approval was denied.');
144 145
    $this->drupalGet('comment/1/edit');
    $this->assertFieldChecked('edit-status-0');
146
    $this->drupalGet('node/' . $this->node->id());
147
    $this->clickLink(t('Approve'));
148 149
    $this->drupalLogout();

150
    $this->drupalGet('node/' . $this->node->id());
151
    $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
152
  }
153 154 155 156 157 158

  /**
   * Tests comment bundle admin.
   */
  public function testCommentAdmin() {
    // Login.
159
    $this->drupalLogin($this->adminUser);
160
    // Browse to comment bundle overview.
161
    $this->drupalGet('admin/structure/comment');
162 163
    $this->assertResponse(200);
    // Make sure titles visible.
164
    $this->assertText('Comment type');
165 166 167
    $this->assertText('Description');
    // Make sure the description is present.
    $this->assertText('Default comment field');
168 169 170 171 172 173 174 175
    // Manage fields.
    $this->clickLink('Manage fields');
    $this->assertResponse(200);
    // Make sure comment_body field is shown.
    $this->assertText('comment_body');
    // Rest from here on in is field_ui.
  }

176 177 178 179 180
  /**
   * Tests editing a comment as an admin.
   */
  public function testEditComment() {
    // Enable anonymous user comments.
181
    user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
182 183 184 185 186
      'access comments',
      'post comments',
      'skip comment approval',
    ));

187
    // Log in as a web user.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    $this->drupalLogin($this->webUser);
    // Post a comment.
    $comment = $this->postComment($this->node, $this->randomMachineName());

    $this->drupalLogout();

    // Post anonymous comment.
    $this->drupalLogin($this->adminUser);
    $this->setCommentAnonymous('2'); // Ensure that we need email id before posting comment.
    $this->drupalLogout();

    // Post comment with contact info (required).
    $author_name = $this->randomMachineName();
    $author_mail = $this->randomMachineName() . '@example.com';
    $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), array('name' => $author_name, 'mail' => $author_mail));

204
    // Log in as an admin user.
205 206 207 208 209 210 211 212 213 214 215 216
    $this->drupalLogin($this->adminUser);

    // Make sure the comment field is not visible when
    // the comment was posted by an authenticated user.
    $this->drupalGet('comment/' . $comment->id() . '/edit');
    $this->assertNoFieldById('edit-mail', $comment->getAuthorEmail());

    // Make sure the comment field is visible when
    // the comment was posted by an anonymous user.
    $this->drupalGet('comment/' . $anonymous_comment->id() . '/edit');
    $this->assertFieldById('edit-mail', $anonymous_comment->getAuthorEmail());
  }
217
}