Commit a505ec39 authored by greggles's avatar greggles

bug #617070 by greggles: Doesn't record subscription when only one...

bug #617070 by greggles: Doesn't record subscription when only one subscription type is enabled (and lots more tests)
parent 35cde666
......@@ -62,7 +62,7 @@ function _comment_notify_options() {
);
$available_options = array();
$options = variable_get('comment_notify_available_alerts', array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT));
$options = variable_get('comment_notify_available_alerts', drupal_map_assoc(array(COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT)));
foreach ($options as $key => $available) {
if ($key == $available) {
$available_options[$available] = $total_options[$available];
......@@ -78,72 +78,70 @@ function _comment_notify_options() {
function comment_notify_form_alter(&$form, &$form_state, $form_id) {
global $user;
// Only do alter the form if it's a comment form and the user has the permission to subscribe
if ($form_id != 'comment_form' || !user_access('subscribe to comments')) {
return;
}
// Only add the checkbox if this is an enabled content type
$node = node_load($form['nid']['#value'] ? $form['nid']['#value'] : $form['nid']['#default_value']);
$enabled_types = variable_get('comment_notify_node_types', array($node->type => TRUE));
if (empty($enabled_types[$node->type])) {
return;
}
// Only alter the form if it's a comment form and the user has the permission to subscribe.
if ($form_id == 'comment_form' && (user_access('subscribe to comments') || user_access('administer comments'))) {
// Only add the checkbox if this is an enabled content type
$node = node_load($form['nid']['#value'] ? $form['nid']['#value'] : $form['nid']['#default_value']);
$enabled_types = variable_get('comment_notify_node_types', drupal_map_assoc(array($node->type)));
if (empty($enabled_types[$node->type])) {
return;
}
$available_options = _comment_notify_options();
$available_options = _comment_notify_options();
drupal_add_css(drupal_get_path('module', 'comment_notify') .'/comment_notify.css');
drupal_add_js(drupal_get_path('module', 'comment_notify') .'/comment_notify.js');
drupal_add_css(drupal_get_path('module', 'comment_notify') .'/comment_notify.css');
drupal_add_js(drupal_get_path('module', 'comment_notify') .'/comment_notify.js');
// Add the checkbox for anonymous users and set the default based on admin settings.
if ($user->uid == 0) {
// If anonymous user's can't enter their e-mail don't tempt them with the checkbox.
if (empty($form['mail'])) {
return;
// Add the checkbox for anonymous users and set the default based on admin settings.
if ($user->uid == 0) {
// If anonymous user's can't enter their e-mail don't tempt them with the checkbox.
if (empty($form['mail'])) {
return;
}
$preference = variable_get('comment_notify_default_anon_mailalert', COMMENT_NOTIFY_DISABLED);
}
else {
$user_preference = db_result(db_query("SELECT comment_notify FROM {comment_notify_user_settings} WHERE uid = %d", $user->uid));
$preference = strlen($user_preference) ? $user_preference : variable_get('comment_notify_default_registered_mailalert', COMMENT_NOTIFY_DISABLED);
}
$preference = variable_get('comment_notify_default_anon_mailalert', COMMENT_NOTIFY_DISABLED);
}
else {
$user_preference = db_result(db_query("SELECT comment_notify FROM {comment_notify_user_settings} WHERE uid = %d", $user->uid));
$preference = strlen($user_preference) ? $user_preference : variable_get('comment_notify_default_registered_mailalert', COMMENT_NOTIFY_DISABLED);
}
// If you want to hide this on your site see http://drupal.org/node/322482
$form['notify_settings'] = array(
'#prefix' => '<div class="clear-block">',
'#suffix' => '</div>',
);
$form['notify_settings']['notify'] = array(
'#type' => 'checkbox',
'#title' => t('Notify me when new comments are posted'),
'#default_value' => $preference,
);
// If you want to hide this on your site see http://drupal.org/node/322482
$form['notify_settings'] = array(
'#prefix' => '<div class="clear-block">',
'#suffix' => '</div>',
);
if (count($available_options) > 1) {
$form['notify_settings']['notify_type'] = array(
'#type' => 'radios',
'#options' => $available_options,
$form['notify_settings']['notify'] = array(
'#type' => 'checkbox',
'#title' => t('Notify me when new comments are posted'),
'#default_value' => $preference,
);
}
else {
$form['notify_settings']['notify_type'] = array(
'#type' => 'hidden',
'#default_value' => key($available_options),
);
}
$form['notify_settings']['notify_type']['#default_value'] = $preference;
// If this is an existing comment we set the default value based on their selection last time.
if ($form['cid']['#value'] != '') {
$notify = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $form['cid']['#value']));
$form['notify_settings']['notify']['#default_value'] = $notify;
if (count($available_options) > 1) {
$form['notify_settings']['notify_type']['#default_value'] = $notify;
$form['notify_settings']['notify_type'] = array(
'#type' => 'radios',
'#options' => $available_options,
'#default_value' => $preference,
);
}
else {
$form['notify_settings']['notify_type']['#default_value'] = key($available_options);
$form['notify_settings']['notify_type'] = array(
'#type' => 'hidden',
'#value' => key($available_options),
);
}
$form['notify_settings']['notify_type']['#default_value'] = $preference;
// If this is an existing comment we set the default value based on their selection last time.
if ($form['cid']['#value'] != '') {
$notify = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $form['cid']['#value']));
$form['notify_settings']['notify']['#default_value'] = $notify;
if (count($available_options) > 1) {
$form['notify_settings']['notify_type']['#default_value'] = $notify;
}
else {
$form['notify_settings']['notify_type']['#default_value'] = key($available_options);
}
}
}
}
......@@ -248,6 +246,7 @@ function comment_notify_comment($comment, $op) {
if ($comment['notify']) {
$notify = $comment['notify_type'];
// If they don't have a preference, save one.
$current = db_result(db_query("SELECT count(1) from {comment_notify_user_settings} WHERE uid = %d", $user->uid));
if ($current == 0 && $user->uid) {
db_query("INSERT INTO {comment_notify_user_settings} (uid, comment_notify) VALUES (%d, %d)", $user->uid, $comment['notify_type']);
......@@ -591,7 +590,7 @@ function comment_notify_settings() {
}
if (!empty($anonymous_problems)) {
drupal_set_message(t('Anonymous commenters have the permission to subscribe to comments but cannot leave their contact infromation on the following content types: !types. You should either disable subscriptions on those types here, revoke the permission for anonymous users, or enable anonymous users to leave their contact information in the comment settings.', array('!types' => implode(', ', $anonymous_problems))));
drupal_set_message(t('Anonymous commenters have the permission to subscribe to comments but cannot leave their contact infromation on the following content types: !types. You should either disable subscriptions on those types here, revoke the permission for anonymous users, or enable anonymous users to leave their contact information in the comment settings.', array('!types' => implode(', ', $anonymous_problems))), 'status', FALSE);
}
$form['comment_notify_settings']['comment_notify_node_types'] = array(
......
......@@ -31,7 +31,7 @@ class CommentNotifyTestCase extends DrupalWebTestCase {
function testCommentNotifyAnonymousUserFunctionalTest() {
// Code that does something to be tested.
// Create and login administrative user.
$admin_user = $this->drupalCreateUser(array('administer comment notify', 'administer permissions'));
$admin_user = $this->drupalCreateUser(array('administer comment notify', 'administer permissions', 'administer comments'));
$this->drupalLogin($admin_user);
// Enable comment notify on this node and allow anonymous information in comments.
......@@ -48,9 +48,35 @@ class CommentNotifyTestCase extends DrupalWebTestCase {
$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();
$anonymous_comment = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, TRUE, TRUE);
$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.'));
$anonymous_comment_1 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, $subscribe_1, array('mail' => $this->randomName() .'@'. $this->randomName()));
// Confirm that the notification is saved.
$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.');
// Notify type 2.
$subscribe_2 = array('notify' => TRUE, 'notify_type' => 2);
$anonymous_comment_2 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, $subscribe_2, array('mail' => $this->randomName() .'@'. $this->randomName()));
// Confirm that the notification is saved.
$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.');
// Notify type 0 (i.e. only one mode is enabled).
variable_set('comment_notify_available_alerts', array(1 => 0, 2 => 2));
$subscribe_0 = array('notify' => TRUE);
$anonymous_comment_0 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, $subscribe_0, array('mail' => $this->randomName() .'@'. $this->randomName()));
// Confirm that the notification is saved.
$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.');
// TODO a whole bunch more tests that check if mail was "sent".
}
......@@ -96,7 +122,10 @@ class CommentNotifyTestCase extends DrupalWebTestCase {
$edit = array();
$edit['subject'] = $subject;
$edit['comment'] = $comment;
$edit['notify'] = $notify;
if ($notify !== NULL && is_array($notify)) {
$edit += $notify;
}
if ($contact !== NULL && is_array($contact)) {
$edit += $contact;
......@@ -116,7 +145,7 @@ class CommentNotifyTestCase extends DrupalWebTestCase {
preg_match('/#comment-([^"]+)/', $this->getURL(), $match);
// Get comment.
if ($contact !== TRUE) { // If true then attempting to find error message.
if (!empty($match[1])) { // If true then attempting to find error message.
if ($subject) {
$this->assertText($subject, 'Comment subject posted.');
}
......@@ -125,7 +154,7 @@ class CommentNotifyTestCase extends DrupalWebTestCase {
}
if (isset($match[1])) {
return (object) array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
return array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment