| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
class CommentNotifyTestCase extends DrupalWebTestCase {
|
| 5 |
/**
|
| 6 |
* Implementation of getInfo().
|
| 7 |
*/
|
| 8 |
public static function getInfo() {
|
| 9 |
return array(
|
| 10 |
'name' => t('Comment notify general tests'),
|
| 11 |
'description' => t('Test the various comment notification settings.'),
|
| 12 |
'group' => t('Comment notify'),
|
| 13 |
);
|
| 14 |
}
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of setUp().
|
| 18 |
*/
|
| 19 |
function setUp() {
|
| 20 |
parent::setUp('comment_notify');
|
| 21 |
// Create a content type where commenting is enabled.
|
| 22 |
// Allow contact info for anons on that content type, and make preview optional.
|
| 23 |
|
| 24 |
// Do some default comment notify settings?.
|
| 25 |
// variable_set('some_variable', 'some value');
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Test various behaviors for anonymous users.
|
| 30 |
*/
|
| 31 |
function testCommentNotifyAnonymousUserFunctionalTest() {
|
| 32 |
// Code that does something to be tested.
|
| 33 |
// Create and login administrative user.
|
| 34 |
$admin_user = $this->drupalCreateUser(array('administer comment notify', 'administer permissions'));
|
| 35 |
$this->drupalLogin($admin_user);
|
| 36 |
|
| 37 |
// Enable comment notify on this node and allow anonymous information in comments.
|
| 38 |
variable_set('comment_notify_node_types', array('story' => 'story'));
|
| 39 |
variable_set('comment_anonymous_story', 1);
|
| 40 |
|
| 41 |
// Create a node with comments allowed.
|
| 42 |
$this->node = $this->drupalCreateNode(array('type' => 'story', 'promote' => 1, 'comment' => 2));
|
| 43 |
|
| 44 |
// Get the page and post the comment.
|
| 45 |
$this->drupalGet('comment/reply/' . $this->node->nid);
|
| 46 |
|
| 47 |
// Ensure that the contact form won't be shown without categories.
|
| 48 |
$this->setPermission('anonymous user', array('access comments' => TRUE, 'access content' => TRUE, 'post comments' => TRUE, 'post comments without approval' => TRUE, 'subscribe to comments' => TRUE));
|
| 49 |
$this->drupalLogout();
|
| 50 |
|
| 51 |
$anonymous_comment = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, TRUE, TRUE);
|
| 52 |
$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.'));
|
| 53 |
|
| 54 |
// TODO a whole bunch more tests that check if mail was "sent".
|
| 55 |
}
|
| 56 |
|
| 57 |
|
| 58 |
/**
|
| 59 |
* Set permission.
|
| 60 |
*
|
| 61 |
* @param string $role User role to set permissions for.
|
| 62 |
* @param array $permissions Key-value array of permissions to set.
|
| 63 |
*/
|
| 64 |
function setPermission($role, $permissions) {
|
| 65 |
// Get role id (rid) for specified role.
|
| 66 |
$rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", array('%s' => $role)));
|
| 67 |
if ($rid === FALSE) {
|
| 68 |
$this->fail(t(' [permission] Role "' . $role . '" not found.'));
|
| 69 |
}
|
| 70 |
|
| 71 |
// Create edit array from permission.
|
| 72 |
$edit = array();
|
| 73 |
foreach ($permissions as $name => $value) {
|
| 74 |
$edit[$rid . '[' . $name . ']'] = $value;
|
| 75 |
}
|
| 76 |
|
| 77 |
$this->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
|
| 78 |
$this->assertText(t('The changes have been saved.'), t(' [permission] Saved changes.'));
|
| 79 |
}
|
| 80 |
|
| 81 |
/////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
|
| 82 |
////////////////////////// COPIED FROM 7.X COMMENT.TEST \\\\\\\\\\\\\\\\\\\\\
|
| 83 |
//////////////////////////////and tweaked a little\\\\\\\\\\\\\\\\\\\\\\\\\\\
|
| 84 |
/////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Post comment.
|
| 88 |
*
|
| 89 |
* @param object $node Node to post comment on.
|
| 90 |
* @param string $subject Comment subject.
|
| 91 |
* @param string $comment Comment body.
|
| 92 |
* @param boolean $preview Should preview be required.
|
| 93 |
* @param mixed $contact Set to NULL for no contact info, TRUE to ignore success checking, and array of values to set contact info.
|
| 94 |
*/
|
| 95 |
function postCommentNotifyComment($node, $subject, $comment, $preview = TRUE, $notify, $contact = NULL) {
|
| 96 |
$edit = array();
|
| 97 |
$edit['subject'] = $subject;
|
| 98 |
$edit['comment'] = $comment;
|
| 99 |
$edit['notify'] = $notify;
|
| 100 |
|
| 101 |
if ($contact !== NULL && is_array($contact)) {
|
| 102 |
$edit += $contact;
|
| 103 |
}
|
| 104 |
|
| 105 |
if ($node !== NULL) {
|
| 106 |
$this->drupalGet('comment/reply/' . $node->nid);
|
| 107 |
}
|
| 108 |
|
| 109 |
if ($preview) {
|
| 110 |
$this->assertNoFieldByName('op', t('Save'), t('Save button not found.')); // Preview required so no save button should be found.
|
| 111 |
$this->drupalPost(NULL, $edit, t('Preview'));
|
| 112 |
}
|
| 113 |
$this->drupalPost(NULL, $edit, t('Save'));
|
| 114 |
$match = array();
|
| 115 |
// Get comment ID
|
| 116 |
preg_match('/#comment-([^"]+)/', $this->getURL(), $match);
|
| 117 |
|
| 118 |
// Get comment.
|
| 119 |
if ($contact !== TRUE) { // If true then attempting to find error message.
|
| 120 |
if ($subject) {
|
| 121 |
$this->assertText($subject, 'Comment subject posted.');
|
| 122 |
}
|
| 123 |
$this->assertText($comment, 'Comment body posted.');
|
| 124 |
$this->assertTrue((!empty($match) && !empty($match[1])), t('Comment id found.'));
|
| 125 |
}
|
| 126 |
|
| 127 |
if (isset($match[1])) {
|
| 128 |
return (object) array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
|
| 129 |
}
|
| 130 |
}
|
| 131 |
|
| 132 |
/**
|
| 133 |
* Checks current page for specified comment.
|
| 134 |
*
|
| 135 |
* @param object $comment Comment object.
|
| 136 |
* @param boolean $reply The comment is a reply to another comment.
|
| 137 |
* @return boolean Comment found.
|
| 138 |
*/
|
| 139 |
function commentExists($comment, $reply = FALSE) {
|
| 140 |
if ($comment && is_object($comment)) {
|
| 141 |
$regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
|
| 142 |
$regex .= '<a id="comment-' . $comment->id . '"(.*?)'; // Comment anchor.
|
| 143 |
$regex .= '<div(.*?)'; // Begin in comment div.
|
| 144 |
$regex .= $comment->subject . '(.*?)'; // Match subject.
|
| 145 |
$regex .= $comment->comment . '(.*?)'; // Match comment.
|
| 146 |
$regex .= '<\/div>/s'; // Dot matches newlines and ensure that match doesn't bleed outside comment div.
|
| 147 |
|
| 148 |
return (boolean)preg_match($regex, $this->drupalGetContent());
|
| 149 |
}
|
| 150 |
else {
|
| 151 |
return FALSE;
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
}
|