View source
<?php
class CommentModuleTestCase extends DrupalTestCase {
var $admin_user;
var $web_user;
var $node;
function get_info() {
return array(
'name' => t('Comment functionality'),
'desc' => t('Thoroughly test comment administration and user interfaces.'),
'group' => t('Comment Tests'),
);
}
function setUp() {
parent::setUp();
$this
->drupalModuleEnable('comment');
$this->admin_user = $this
->drupalCreateUserRolePerm(array(
'administer content types',
'administer comments',
'administer permissions',
));
$this->web_user = $this
->drupalCreateUserRolePerm(array(
'access comments',
'post comments',
'create story content',
));
$this
->drupalLoginUser($this->web_user);
$this->node = $this
->drupalCreateNode(array(
'type' => 'story',
));
$this
->assertTrue($this->node, 'Story node created.');
$this
->drupalGet('logout');
}
function testFormOnPage() {
$this
->drupalLoginUser($this->admin_user);
$this
->set_comment_form(TRUE);
$this
->drupalGet('logout');
$this
->drupalLoginUser($this->web_user);
$this
->drupalGet('node/' . $this->node->nid);
$form_comment = $this
->post_comment(NULL, $this
->randomName(), $this
->randomName());
$this
->assertTrue($this
->comment_exists($form_comment), 'Form comment found.');
$this
->drupalGet('logout');
$this
->drupalLoginUser($this->admin_user);
$this
->set_comment_form(FALSE);
}
function testAnonymous() {
$this
->drupalLoginUser($this->admin_user);
$this
->set_anonymous_user_comment(TRUE, TRUE);
$this
->set_comment_anonymous('0');
$this
->drupalGet('logout');
$anonymous_comment1 = $this
->post_comment($this->node, $this
->randomName(), $this
->randomName());
$this
->assertTrue($this
->comment_exists($anonymous_comment1), 'Anonymous comment without contact info found.');
$this
->drupalLoginUser($this->admin_user);
$this
->set_comment_anonymous('1');
$this
->drupalGet('logout');
$this
->drupalGet('comment/reply/' . $this->node->nid);
$this
->assertTrue($this
->comment_contact_info_available(), 'Contact information available.');
$anonymous_comment2 = $this
->post_comment($this->node, $this
->randomName(), $this
->randomName());
$this
->assertTrue($this
->comment_exists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
$this
->drupalLoginUser($this->admin_user);
$this
->set_comment_anonymous('2');
$this
->drupalGet('logout');
$this
->drupalGet('comment/reply/' . $this->node->nid);
$this
->assertTrue($this
->comment_contact_info_available(), 'Contact information available.');
$anonymous_comment3 = $this
->post_comment($this->node, $this
->randomName(), $this
->randomName(), TRUE, TRUE);
$this
->assertText(t('E-mail field is required.'), 'E-mail required.');
$this
->assertFalse($this
->comment_exists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');
$anonymous_comment3 = $this
->post_comment($this->node, $this
->randomName(), $this
->randomName(), TRUE, array(
'mail' => 'tester@simpletest.org',
));
$this
->assertTrue($this
->comment_exists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');
$this
->drupalLoginUser($this->admin_user);
$this
->perform_comment_operation($anonymous_comment3, 'unpublish');
$this
->drupalGet('admin/content/comment/approval');
$this
->assertWantedRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was unpublished.');
$this
->perform_comment_operation($anonymous_comment3, 'publish', TRUE);
$this
->drupalGet('admin/content/comment');
$this
->assertWantedRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was published.');
$this
->perform_comment_operation($anonymous_comment3, 'delete');
$this
->drupalGet('admin/content/comment');
$this
->assertNoUnwantedRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was deleted.');
$this
->set_anonymous_user_comment(TRUE, FALSE);
$this
->set_comment_anonymous('0');
$this
->drupalGet('logout');
$subject = $this
->randomName();
$body = $this
->randomName();
$this
->post_comment($this->node, $subject, $body, TRUE, TRUE);
$this
->assertText(t('Your comment has been queued for moderation by site administrators and will be published after approval.'), 'Comment requires approval.');
$this
->drupalLoginUser($this->admin_user);
$anonymous_comment4 = $this
->get_unaproved_comment($subject);
$anonymous_comment4 = (object) array(
'id' => $anonymous_comment4,
'subject' => $subject,
'comment' => $body,
);
$this
->drupalGet('logout');
$this
->assertFalse($this
->comment_exists($anonymous_comment4), 'Anonymous comment was not published.');
$this
->drupalLoginUser($this->admin_user);
$this
->perform_comment_operation($anonymous_comment4, 'publish', TRUE);
$this
->drupalGet('logout');
$this
->drupalGet('node/' . $this->node->nid);
$this
->assertTrue($this
->comment_exists($anonymous_comment4), 'Anonymous comment visible.');
$this
->drupalLoginUser($this->admin_user);
$this
->set_anonymous_user_comment(FALSE, FALSE);
}
function post_comment($node, $subject, $comment, $preview = TRUE, $contact = NULL) {
$edit = array();
$edit['subject'] = $subject;
$edit['comment'] = $comment;
if ($contact !== NULL && is_array($contact)) {
$edit += $contact;
}
if ($node !== NULL) {
$this
->drupalGet('comment/reply/' . $node->nid);
}
if ($preview) {
$this
->assertFieldById('edit-comment');
$this
->assertNoPattern('/(input)(.*?)(value="Save")/', 'Save button not found.');
$this
->drupalPost(NULL, $edit, 'Preview');
}
$this
->drupalPost(NULL, array(), 'Save');
$match = array();
preg_match('/#comment-([^"]+)/', $this->_browser
->getURL(), $match);
if ($contact !== TRUE) {
$this
->assertText($subject, 'Comment posted.');
$this
->assertTrue(!empty($match) && !empty($match[1]), 'Comment id found.');
}
if (isset($match[1])) {
return (object) array(
'id' => $match[1],
'subject' => $subject,
'comment' => $comment,
);
}
else {
return NULL;
}
}
function comment_exists($comment, $reply = FALSE) {
if ($comment && is_object($comment)) {
$regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
$regex .= '<a id="comment-' . $comment->id . '"(.*?)';
$regex .= '<div(.*?)';
$regex .= $comment->subject . '(.*?)';
$regex .= $comment->comment . '(.*?)';
$regex .= '<\\/div>/s';
return preg_match($regex, $this
->drupalGetContent());
}
else {
return FALSE;
}
}
function delete_comment($comment) {
$this
->drupalPost('comment/delete/' . $comment->id, array(), 'Delete');
$this
->assertWantedText(t('The comment and all its replies have been deleted.'), 'Comment deleted.');
}
function set_comment_subject($enabled) {
$this
->set_comment_settings('comment_subject_field', $enabled ? '1' : '0', 'Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.');
}
function set_comment_preview($required) {
$this
->set_comment_settings('comment_preview', $required ? '1' : '0', 'Comment preview ' . ($required ? 'required' : 'optional') . '.');
}
function set_comment_form($enabled) {
$this
->set_comment_settings('comment_form_location', $enabled ? '1' : '3', 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.');
}
function set_comment_anonymous($level) {
$this
->set_comment_settings('comment_anonymous', $level, 'Anonymous commenting set to level ' . $level . '.');
}
function set_comment_settings($name, $value, $message) {
$this
->drupalVariableSet($name . '_story', $value);
$this
->assertTrue(TRUE, $message);
}
function set_anonymous_user_comment($enabled, $without_approval) {
$edit = array();
$edit['1-access-comments'] = $enabled;
$edit['1-post-comments'] = $enabled;
$edit['1-post-comments-without-approval'] = $without_approval;
$this
->drupalPost('admin/user/permissions', $edit, 'Save permissions');
$this
->assertText(t('The changes have been saved.'), 'Anonymous user comments ' . ($enabled ? 'enabled' : 'disabled') . '.');
}
function comment_contact_info_available() {
return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this
->drupalGetContent());
}
function perform_comment_operation($comment, $operation, $approval = FALSE) {
$edit = array();
$edit['operation'] = $operation;
$edit['comments[' . $comment->id . ']'] = TRUE;
$this
->drupalPost('admin/content/comment' . ($approval ? '/approval' : ''), $edit, 'Update');
if ($operation == 'delete') {
$this
->drupalPost(NULL, array(), 'Delete comments');
$this
->assertText(t('The comments have been deleted.'), 'Operation "' . $operation . '" was performed on comment.');
}
else {
$this
->assertText(t('The update has been performed.'), 'Operation "' . $operation . '" was performed on comment.');
}
}
function get_unaproved_comment($subject) {
$this
->drupalGet('admin/content/comment/approval');
preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this
->drupalGetContent(), $match);
return $match[2];
}
}