CommentInterfaceTest.php 8.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Definition of Drupal\comment\Tests\CommentInterfaceTest.
 */

namespace Drupal\comment\Tests;

10
11
use Drupal\Core\Language\Language;

12
13
14
/**
 * Tests the comment module administrative and end-user-facing interfaces.
 */
15
class CommentInterfaceTest extends CommentTestBase {
16

17
18
19
20
21
22
23
24
25
26
27
28
  public static function getInfo() {
    return array(
      'name' => 'Comment interface',
      'description' => 'Test comment user interfaces.',
      'group' => 'Comment',
    );
  }

  /**
   * Tests the comment interface.
   */
  function testCommentInterface() {
29
    $langcode = Language::LANGCODE_NOT_SPECIFIED;
30
31
32
33
34
    // Set comments to have subject and preview disabled.
    $this->drupalLogin($this->admin_user);
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(FALSE);
35
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
36
37
38
39
40
41
    $this->drupalLogout();

    // Post comment #1 without subject or preview.
    $this->drupalLogin($this->web_user);
    $comment_text = $this->randomName();
    $comment = $this->postComment($this->node, $comment_text);
42
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
43
44
45
46
47
48
49
50
51
52
53
54
55

    // Set comments to have subject and preview to required.
    $this->drupalLogout();
    $this->drupalLogin($this->admin_user);
    $this->setCommentSubject(TRUE);
    $this->setCommentPreview(DRUPAL_REQUIRED);
    $this->drupalLogout();

    // Create comment #2 that allows subject and requires preview.
    $this->drupalLogin($this->web_user);
    $subject_text = $this->randomName();
    $comment_text = $this->randomName();
    $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
56
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
57
58

    // Check comment display.
59
    $this->drupalGet('node/' . $this->node->id() . '/' . $comment->id());
60
61
    $this->assertText($subject_text, 'Individual comment subject found.');
    $this->assertText($comment_text, 'Individual comment body found.');
62
63
64
65
66
67
68
69

    // Set comments to have subject and preview to optional.
    $this->drupalLogout();
    $this->drupalLogin($this->admin_user);
    $this->setCommentSubject(TRUE);
    $this->setCommentPreview(DRUPAL_OPTIONAL);

    // Test changing the comment author to "Anonymous".
70
71
    $this->drupalGet('comment/' . $comment->id() . '/edit');
    $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->subject->value, array('name' => ''));
72
    $this->assertTrue(empty($comment->name->value) && $comment->uid->target_id == 0, 'Comment author successfully changed to anonymous.');
73
74
75

    // Test changing the comment author to an unverified user.
    $random_name = $this->randomName();
76
77
    $this->drupalGet('comment/' . $comment->id() . '/edit');
    $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->subject->value, array('name' => $random_name));
78
    $this->drupalGet('node/' . $this->node->id());
79
    $this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.');
80
81

    // Test changing the comment author to a verified user.
82
    $this->drupalGet('comment/' . $comment->id() . '/edit');
83
84
    $comment = $this->postComment(NULL, $comment->comment_body->value, $comment->subject->value, array('name' => $this->web_user->getUsername()));
    $this->assertTrue($comment->name->value == $this->web_user->getUsername() && $comment->uid->target_id == $this->web_user->id(), 'Comment author successfully changed to a registered user.');
85
86
87
88
89
90

    $this->drupalLogout();

    // Reply to comment #2 creating comment #3 with optional preview and no
    // subject though field enabled.
    $this->drupalLogin($this->web_user);
91
    $this->drupalGet('comment/reply/' . $this->node->id() . '/' . $comment->id());
92
93
    $this->assertText($subject_text, 'Individual comment-reply subject found.');
    $this->assertText($comment_text, 'Individual comment-reply body found.');
94
    $reply = $this->postComment(NULL, $this->randomName(), '', TRUE);
95
    $reply_loaded = comment_load($reply->id());
96
    $this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
97
    $this->assertEqual($comment->id(), $reply_loaded->pid->target_id, 'Pid of a reply to a comment is set correctly.');
98
    $this->assertEqual(rtrim($comment->thread->value, '/') . '.00/', $reply_loaded->thread->value, 'Thread of reply grows correctly.');
99
100

    // Second reply to comment #3 creating comment #4.
101
    $this->drupalGet('comment/reply/' . $this->node->id() . '/' . $comment->id());
102
    $this->assertText($subject_text, 'Individual comment-reply subject found.');
103
    $this->assertText($comment_text, 'Individual comment-reply body found.');
104
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
105
    $reply_loaded = comment_load($reply->id());
106
    $this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
107
    $this->assertEqual(rtrim($comment->thread->value, '/') . '.01/', $reply_loaded->thread->value, 'Thread of second reply grows correctly.');
108
109

    // Edit reply.
110
    $this->drupalGet('comment/' . $reply->id() . '/edit');
111
    $reply = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
112
    $this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.');
113
114
115
116

    // Confirm a new comment is posted to the correct page.
    $this->setCommentsPerPage(2);
    $comment_new_page = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE);
117
    $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
118
    $this->drupalGet('node/' . $this->node->id(), array('query' => array('page' => 1)));
119
    $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
120
121
122
    $this->setCommentsPerPage(50);

    // Attempt to reply to an unpublished comment.
123
    $reply_loaded->status->value = COMMENT_NOT_PUBLISHED;
124
    $reply_loaded->save();
125
    $this->drupalGet('comment/reply/' . $this->node->id() . '/' . $reply_loaded->id());
126
127
128
129
    $this->assertText(t('The comment you are replying to does not exist.'), 'Replying to an unpublished comment');

    // Attempt to post to node with comments disabled.
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_HIDDEN));
130
    $this->assertTrue($this->node, 'Article node created.');
131
    $this->drupalGet('comment/reply/' . $this->node->id());
132
133
    $this->assertText('This discussion is closed', 'Posting to node with comments disabled');
    $this->assertNoField('edit-comment', 'Comment body field found.');
134
135
136

    // Attempt to post to node with read-only comments.
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_CLOSED));
137
    $this->assertTrue($this->node, 'Article node created.');
138
    $this->drupalGet('comment/reply/' . $this->node->id());
139
140
    $this->assertText('This discussion is closed', 'Posting to node with comments read-only');
    $this->assertNoField('edit-comment', 'Comment body field found.');
141
142
143

    // Attempt to post to node with comments enabled (check field names etc).
    $this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => COMMENT_NODE_OPEN));
144
    $this->assertTrue($this->node, 'Article node created.');
145
    $this->drupalGet('comment/reply/' . $this->node->id());
146
147
    $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled');
    $this->assertField('edit-comment-body-' . $langcode . '-0-value', 'Comment body field found.');
148
149
150
151
152
153
154

    // Delete comment and make sure that reply is also removed.
    $this->drupalLogout();
    $this->drupalLogin($this->admin_user);
    $this->deleteComment($comment);
    $this->deleteComment($comment_new_page);

155
    $this->drupalGet('node/' . $this->node->id());
156
157
    $this->assertFalse($this->commentExists($comment), 'Comment not found.');
    $this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.');
158
159
160
161
162
163
164
165

    // Enabled comment form on node page.
    $this->drupalLogin($this->admin_user);
    $this->setCommentForm(TRUE);
    $this->drupalLogout();

    // Submit comment through node form.
    $this->drupalLogin($this->web_user);
166
    $this->drupalGet('node/' . $this->node->id());
167
    $form_comment = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
168
    $this->assertTrue($this->commentExists($form_comment), 'Form comment found.');
169
170
171
172
173
174
175

    // Disable comment form on node page.
    $this->drupalLogout();
    $this->drupalLogin($this->admin_user);
    $this->setCommentForm(FALSE);
  }
}