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

/**
 * @file
5
 * Contains \Drupal\comment\Tests\CommentAdminTest.
6 7 8 9
 */

namespace Drupal\comment\Tests;

10
use Drupal\user\RoleInterface;
11
use Drupal\comment\Entity\Comment;
12

13 14
/**
 * Tests comment approval functionality.
15 16
 *
 * @group comment
17
 */
18
class CommentAdminTest extends CommentTestBase {
19 20 21 22 23 24 25

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

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

26 27 28 29 30
  /**
   * Test comment approval functionality through admin/content/comment.
   */
  function testApprovalAdminInterface() {
    // Set anonymous comments to require approval.
31
    user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
32 33 34 35
      'access comments' => TRUE,
      'post comments' => TRUE,
      'skip comment approval' => FALSE,
    ));
36
    $this->drupalLogin($this->adminUser);
37 38 39 40 41 42 43 44 45
    $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.
46 47
    $subject = $this->randomMachineName();
    $body = $this->randomMachineName();
48
    $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
49
    $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
50 51

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

64
    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
65 66

    // Approve comment.
67
    $this->drupalLogin($this->adminUser);
68 69 70
    $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
    $this->drupalLogout();

71
    $this->drupalGet('node/' . $this->node->id());
72
    $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
73 74

    // Post 2 anonymous comments without contact info.
75 76
    $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
    $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
77 78

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

    // Delete multiple comments in one operation.
    $edit = array(
      'operation' => 'delete',
92 93 94
      "comments[{$comments[0]->id()}]" => 1,
      "comments[{$comments[1]->id()}]" => 1,
      "comments[{$anonymous_comment4->id()}]" => 1,
95
    );
96
    $this->drupalPostForm(NULL, $edit, t('Update'));
97
    $this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
98
    $this->drupalPostForm(NULL, $edit, t('Delete comments'));
99
    $this->assertText(t('No comments available.'), 'All comments were deleted.');
100 101 102 103 104 105
    // 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.'));
106 107 108 109 110 111 112
  }

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

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

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

141
    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
142 143

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

155
    $this->drupalGet('node/' . $this->node->id());
156
    $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
157
  }
158 159 160 161 162 163

  /**
   * Tests comment bundle admin.
   */
  public function testCommentAdmin() {
    // Login.
164
    $this->drupalLogin($this->adminUser);
165
    // Browse to comment bundle overview.
166
    $this->drupalGet('admin/structure/comment');
167 168
    $this->assertResponse(200);
    // Make sure titles visible.
169
    $this->assertText('Comment type');
170 171 172
    $this->assertText('Description');
    // Make sure the description is present.
    $this->assertText('Default comment field');
173 174 175 176 177 178 179 180
    // 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.
  }

181 182 183 184 185
  /**
   * Tests editing a comment as an admin.
   */
  public function testEditComment() {
    // Enable anonymous user comments.
186
    user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      'access comments',
      'post comments',
      'skip comment approval',
    ));

    // Login as a web user.
    $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));

    // Login as an admin user.
    $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());
  }
222
}