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

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

namespace Drupal\comment\Tests;

/**
 * Tests comment approval functionality.
12
13
 *
 * @group comment
14
 */
15
class CommentAdminTest extends CommentTestBase {
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  /**
   * Test comment approval functionality through admin/content/comment.
   */
  function testApprovalAdminInterface() {
    // Set anonymous comments to require approval.
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
      'access comments' => TRUE,
      'post comments' => TRUE,
      'skip comment approval' => FALSE,
    ));
    $this->drupalLogin($this->admin_user);
    $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.
36
37
    $subject = $this->randomMachineName();
    $body = $this->randomMachineName();
38
    $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
39
    $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
40
41
42
43

    // Get unapproved comment id.
    $this->drupalLogin($this->admin_user);
    $anonymous_comment4 = $this->getUnapprovedComment($subject);
44
45
46
47
48
49
50
51
    $anonymous_comment4 = entity_create('comment', array(
      'cid' => $anonymous_comment4,
      'subject' => $subject,
      'comment_body' => $body,
      'entity_id' => $this->node->id(),
      'entity_type' => 'node',
      'field_name' => 'comment'
    ));
52
53
    $this->drupalLogout();

54
    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
55
56
57
58
59
60

    // Approve comment.
    $this->drupalLogin($this->admin_user);
    $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
    $this->drupalLogout();

61
    $this->drupalGet('node/' . $this->node->id());
62
    $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
63
64

    // Post 2 anonymous comments without contact info.
65
66
    $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
    $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
67
68
69
70

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

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

  /**
   * Tests comment approval functionality through the node interface.
   */
  function testApprovalNodeInterface() {
    // Set anonymous comments to require approval.
    user_role_change_permissions(DRUPAL_ANONYMOUS_RID, array(
      'access comments' => TRUE,
      'post comments' => TRUE,
      'skip comment approval' => FALSE,
    ));
    $this->drupalLogin($this->admin_user);
    $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
    $this->drupalLogout();

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

    // Get unapproved comment id.
    $this->drupalLogin($this->admin_user);
    $anonymous_comment4 = $this->getUnapprovedComment($subject);
121
122
123
124
125
126
127
128
    $anonymous_comment4 = entity_create('comment', array(
      'cid' => $anonymous_comment4,
      'subject' => $subject,
      'comment_body' => $body,
      'entity_id' => $this->node->id(),
      'entity_type' => 'node',
      'field_name' => 'comment'
    ));
129
130
    $this->drupalLogout();

131
    $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
132
133
134
135

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

145
    $this->drupalGet('node/' . $this->node->id());
146
    $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
147
  }
148
149
150
151
152
153
154
155

  /**
   * Tests comment bundle admin.
   */
  public function testCommentAdmin() {
    // Login.
    $this->drupalLogin($this->admin_user);
    // Browse to comment bundle overview.
156
    $this->drupalGet('admin/structure/comment');
157
158
    $this->assertResponse(200);
    // Make sure titles visible.
159
    $this->assertText('Comment type');
160
161
162
    $this->assertText('Description');
    // Make sure the description is present.
    $this->assertText('Default comment field');
163
164
165
166
167
168
169
170
    // 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.
  }

171
}