View source
<?php
class CommentNotifyTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => t('Comment notify general tests'),
'description' => t('Test the various comment notification settings.'),
'group' => t('Comment notify'),
);
}
function setUp() {
parent::setUp('comment_notify');
}
function testCommentNotifyAnonymousUserFunctionalTest() {
$admin_user = $this
->drupalCreateUser(array(
'administer comment notify',
'administer permissions',
'administer comments',
));
$this
->drupalLogin($admin_user);
variable_set('comment_notify_node_types', array(
'story' => 'story',
));
variable_set('comment_anonymous_story', 1);
$this->node = $this
->drupalCreateNode(array(
'type' => 'story',
'promote' => 1,
'comment' => 2,
));
$this
->setPermission('anonymous user', array(
'access comments' => TRUE,
'access content' => TRUE,
'post comments' => TRUE,
'post comments without approval' => TRUE,
'subscribe to comments' => TRUE,
));
$this
->drupalLogout();
$subscribe_1 = array(
'notify' => TRUE,
'notify_type' => 1,
);
$this
->postCommentNotifyComment($this->node, $this
->randomName(), $this
->randomName(), TRUE, TRUE, $subscribe_1);
$this
->assertText(t('If you want to subscribe to comments you must supply a valid e-mail address.'), t('Anonymous user must leave E-mail if they want to get notifications.'));
$contact_1 = array(
'mail' => $this
->randomName() . '@' . $this
->randomName(),
);
$anonymous_comment_1 = $this
->postCommentNotifyComment($this->node, $this
->randomName(), $this
->randomName(), TRUE, $subscribe_1, $contact_1);
$result = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $anonymous_comment_1['id']));
$this
->assertEqual($result, $subscribe_1['notify_type'], 'Notify selection option 1 is saved properly.');
$subscribe_2 = array(
'notify' => TRUE,
'notify_type' => 2,
);
$contact_2 = array(
'mail' => $this
->randomName() . '@' . $this
->randomName(),
);
$anonymous_comment_2 = $this
->postCommentNotifyComment($this->node, $this
->randomName(), $this
->randomName(), TRUE, $subscribe_2, $contact_2);
$result = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $anonymous_comment_2['id']));
$this
->assertEqual($result, $subscribe_2['notify_type'], 'Notify selection option 2 is saved properly.');
$this
->assertMail('to', $contact_1['mail'], t('Message was sent to the proper anonymous user.'));
variable_set('comment_notify_available_alerts', array(
1 => 0,
2 => 2,
));
$subscribe_0 = array(
'notify' => TRUE,
);
$contact_0 = array(
'mail' => $this
->randomName() . '@' . $this
->randomName(),
);
$anonymous_comment_0 = $this
->postCommentNotifyComment($this->node, $this
->randomName(), $this
->randomName(), TRUE, $subscribe_0, $contact_0);
$result = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $anonymous_comment_0['id']));
$this
->assertEqual($result, 2, 'Notify selection option 0 is saved properly.');
}
function setPermission($role, $permissions) {
$rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", array(
'%s' => $role,
)));
if ($rid === FALSE) {
$this
->fail(t(' [permission] Role "' . $role . '" not found.'));
}
$edit = array();
foreach ($permissions as $name => $value) {
$edit[$rid . '[' . $name . ']'] = $value;
}
$this
->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
$this
->assertText(t('The changes have been saved.'), t(' [permission] Saved changes.'));
}
function postCommentNotifyComment($node, $subject, $comment, $preview = TRUE, $notify, $contact = NULL) {
$edit = array();
$edit['subject'] = $subject;
$edit['comment'] = $comment;
if ($notify !== NULL && is_array($notify)) {
$edit += $notify;
}
if ($contact !== NULL && is_array($contact)) {
$edit += $contact;
}
if ($node !== NULL) {
$this
->drupalGet('comment/reply/' . $node->nid);
}
if ($preview) {
$this
->assertNoFieldByName('op', t('Save'), t('Save button not found.'));
$this
->drupalPost(NULL, $edit, t('Preview'));
}
$this
->drupalPost(NULL, $edit, t('Save'));
$match = array();
preg_match('/#comment-([^"]+)/', $this
->getURL(), $match);
if (!empty($match[1])) {
if ($subject) {
$this
->assertText($subject, 'Comment subject posted.');
}
$this
->assertText($comment, 'Comment body posted.');
$this
->assertTrue(!empty($match) && !empty($match[1]), t('Comment id found.'));
}
if (isset($match[1])) {
return array(
'id' => $match[1],
'subject' => $subject,
'comment' => $comment,
);
}
}
function commentExists($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 (bool) preg_match($regex, $this
->drupalGetContent());
}
else {
return FALSE;
}
}
}