CommentInterfaceTest.php 8.48 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 12
/**
 * Tests the comment module administrative and end-user-facing interfaces.
 */
13
class CommentInterfaceTest extends CommentTestBase {
14

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

  /**
   * Tests the comment interface.
   */
  function testCommentInterface() {
    // Set comments to have subject and preview disabled.
    $this->drupalLogin($this->admin_user);
    $this->setCommentPreview(DRUPAL_DISABLED);
    $this->setCommentForm(TRUE);
    $this->setCommentSubject(FALSE);
32
    $this->setCommentSettings('comment_default_mode', COMMENT_MODE_THREADED, 'Comment paging changed.');
33 34 35 36 37 38
    $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);
39
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
40 41 42 43 44 45 46 47 48 49 50 51 52

    // 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);
53
    $this->assertTrue($this->commentExists($comment), 'Comment found.');
54 55

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

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

66
    $this->drupalGet('comment/' . $comment->id() . '/edit');
67 68 69 70 71
    $this->assertTitle(t('Edit comment @title | Drupal', array(
      '@title' => $comment->subject->value,
    )));

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

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

    // Test changing the comment author to a verified user.
83
    $this->drupalGet('comment/' . $comment->id() . '/edit');
84 85
    $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.');
86 87 88 89 90 91

    $this->drupalLogout();

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

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

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

    // 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);
118
    $this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
119
    $this->drupalGet('node/' . $this->node->id(), array('query' => array('page' => 1)));
120
    $this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
121 122 123
    $this->setCommentsPerPage(50);

    // Attempt to reply to an unpublished comment.
124
    $reply_loaded->status->value = COMMENT_NOT_PUBLISHED;
125
    $reply_loaded->save();
126
    $this->drupalGet('comment/reply/' . $this->node->id() . '/' . $reply_loaded->id());
127 128 129 130
    $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));
131
    $this->assertTrue($this->node, 'Article node created.');
132
    $this->drupalGet('comment/reply/' . $this->node->id());
133 134
    $this->assertText('This discussion is closed', 'Posting to node with comments disabled');
    $this->assertNoField('edit-comment', 'Comment body field found.');
135 136 137

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

    // 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));
145
    $this->assertTrue($this->node, 'Article node created.');
146
    $this->drupalGet('comment/reply/' . $this->node->id());
147
    $this->assertNoText('This discussion is closed', 'Posting to node with comments enabled');
148
    $this->assertField('edit-comment-body-0-value', 'Comment body field found.');
149 150 151 152 153 154 155

    // 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);

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

    // 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);
167
    $this->drupalGet('node/' . $this->node->id());
168
    $form_comment = $this->postComment(NULL, $this->randomName(), $this->randomName(), TRUE);
169
    $this->assertTrue($this->commentExists($form_comment), 'Form comment found.');
170 171 172 173 174 175 176

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