Commit 6c6b495b authored by greggles's avatar greggles

task #628878 by JacobSingh: update Comment Notify for 7.x - yay #D7CX

parent 11e532e2
......@@ -2,4 +2,7 @@
name = Comment Notify
description = "Comment follow-up e-mail notification for anonymous as well as registered users."
dependencies[] = comment
core = 6.x
core = 7.x
files[] = comment_notify.install
files[] = comment_notify.module
files[] = comment_notify.test
......@@ -11,17 +11,26 @@
function comment_notify_install() {
// Create the table.
drupal_install_schema('comment_notify');
// Insert a record for each existing comment.
if ( $GLOBALS['db_type']=='pgsql') {
db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) SELECT c.cid, 0, md5(c.mail || coalesce(u.mail, u.init) || c.uid || c.name || c.nid) FROM {comments} c LEFT OUTER JOIN {users} u on c.uid = u.uid");
$comments_select = db_select('comment', 'c');
$comments_select->join('users', 'u', 'c.uid = u.uid');
$comments_select->addField('c', 'cid');
$comments_select->addExpression('0', 'notify');
if (db_driver() == 'pgsql') {
$comments_select->addExpression('md5(c.mail || coalesce(u.mail, u.init) || c.uid || c.name || c.nid)', 'notify_hash');
}
else {
db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) SELECT c.cid, 0, md5(concat(c.mail, ifnull(u.mail, u.init), c.uid, c.name, c.nid)) FROM {comments} c LEFT OUTER JOIN {users} u on c.uid = u.uid");
$comments_select->addExpression('md5(concat(c.mail, ifnull(u.mail, u.init), c.uid, c.name, c.nid))', 'notify_hash');
}
$comments_select->addField('c', 'cid');
// Set module weight low so that other modules act on the comment first.
db_query("UPDATE {system} SET weight = 10 WHERE name = 'comment_notify'");
db_insert('comment_notify')->from($comments_select)->execute();
db_update('system')->
fields(array(
'weight' => 10
))
->condition('name', 'comment_notify');
}
/**
......@@ -30,7 +39,8 @@ function comment_notify_install() {
function comment_notify_uninstall() {
drupal_uninstall_schema('comment_notify');
variable_del('node_notify_default_mailtext');
db_query("DELETE FROM {variable} WHERE name LIKE 'comment_notify_%'");
db_delete('variable')
->where('name', "comment_notify_%", 'LIKE');
}
/**
......@@ -95,208 +105,4 @@ function comment_notify_schema() {
);
return $schema;
}
/**
* Sync up the two tables after as part of http://drupal.org/node/297791
*/
function comment_notify_update_6000() {
$return[] = update_sql("DELETE FROM {comment_notify} WHERE cid NOT IN (SELECT cid FROM {comments})");
return $return;
}
/**
* Permissions got renamed, update them.
*/
function comment_notify_update_6001() {
$ret = array();
$result = db_query("SELECT pid, perm FROM {permission}");
while ($row = db_fetch_object($result)) {
$permissions = explode(', ', $row->perm);
if (in_array('Subscribe to comments', $permissions) && !in_array('subscribe to comments', $permissions)) {
$permissions[] = 'subscribe to comments';
}
if (in_array('Administer comment notify', $permissions) && !in_array('administer comment notify', $permissions)) {
$permissions[] = 'administer comment notify';
}
$permissions = implode(', ', $permissions);
$ret[] = update_sql("UPDATE {permission} SET perm = '%s' WHERE pid = %d", $permissions, $row->pid);
}
return $ret;
}
/**
* Drop the notify column from the {comments} table. This column will only exist
* if the 5.x-1.x version of comment_notify was installed at some point. Since
* the 5.x-2.x version of the module {comment_notify}.notify has been used instead.
*/
function comment_notify_update_6002() {
$ret = array();
if (db_column_exists('comments', 'notify')) {
db_drop_field($ret, 'comments', 'notify');
}
return $ret;
}
/**
* Create a new table to store user preferences and move the $user->data there.
*/
function comment_notify_update_6003() {
$ret = array();
// This determines how many users will be processed in each batch run.
$num_users_per_batch = 100;
// Multi-part update.
if (!isset($_SESSION['comment_notify_update_6003'])) {
// We need to start at uid 1, so initialize our variable
// to the value below that.
$_SESSION['comment_notify_update_6003'] = 1;
$_SESSION['comment_notify_update_6003_max'] = db_result(db_query("SELECT MAX(uid) FROM {users}"));
// Create the table.
$schema['comment_notify_user_settings'] = array(
'fields' => array(
'uid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'description' => 'The user id from {users}.cid',
'not null' => TRUE,
'disp-width' => '11'),
'node_notify' => array(
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11'),
'comment_notify' => array(
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11'),
),
'primary key' => array('uid'),
);
db_create_table($ret, 'comment_notify_user_settings', $schema['comment_notify_user_settings']);
}
// Do the next batch of the deed.
// Find the next N records to update, or do the final batch.
$next = min($_SESSION['comment_notify_update_6003'] + $num_users_per_batch, $_SESSION['comment_notify_update_6003_max']);
// Check to make sure that the {comment_notify_user_settings} table exists.
// If for some reason it was not created above, we might lose data when
// we delete the comment_notify data that is currently in {users}.data.
// If the table doesn't exist, then alert the user and don't allow any
// more batches to be processed.
if (!db_table_exists('comment_notify_user_settings')) {
unset($_SESSION['comment_notify_update_6003']);
unset($_SESSION['comment_notify_update_6003_max']);
// Alert the user that there was an error.
$ret[] = array('success' => FALSE, 'query' => t('For some reason the {comment_notify_user_settings} table was not properly created, and so per-user comment_notify settings could not be copied from {users}.data. You will need to run this update again.'));
return $ret;
}
// Transfer the data in our specified range of uid values.
$uid = $_SESSION['comment_notify_update_6003'];
while ($uid <= $next) {
// Get the value of {users}.data.
$data = db_result(db_query('SELECT data FROM {users} WHERE uid = %d', $uid));
$settings = array('uid' => $uid);
if (!empty($data)) {
$data = unserialize($data);
if (isset($data['node_notify_mailalert'])) {
$settings['node_notify'] = $data['node_notify_mailalert'];
unset($data['node_notify_mailalert']);
}
if (isset($data['comment_notify_mailalert'])) {
$settings['comment_notify'] = $data['comment_notify_mailalert'];
unset($data['comment_notify_mailalert']);
}
$fields_sql = '';
$values_sql = '';
foreach ($settings as $field => $value) {
$fields_sql .= "$field, ";
$values_sql .= '%d, ';
}
// Trim off any trailing commas and spaces.
$fields_sql = rtrim($fields_sql, ', ');
$values_sql = rtrim($values_sql, ', ');
// Add this user and settings to {comment_notify_user_settings} only if
// at least one setting was found in {users}.data for this user.
if (count($settings) > 1) {
db_query("INSERT INTO {comment_notify_user_settings} ($fields_sql) VALUES ($values_sql)", $settings);
// Remove this comment_notify data from {users}.data.
db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $uid);
}
}
$uid++;
}
// Remember where we left off.
$_SESSION['comment_notify_update_6003'] = $next;
if ($_SESSION['comment_notify_update_6003'] == $_SESSION['comment_notify_update_6003_max']) {
// We're done, clear these out.
unset($_SESSION['comment_notify_update_6003']);
unset($_SESSION['comment_notify_update_6003_max']);
// Provide an explaination of what we did.
$ret[] = array('success' => TRUE, 'query' => t('Moved comment_notify user settings data from the {users} table into the {comment_notify_user_settings} table.'));
}
elseif ($uid == $next) {
unset($_SESSION['comment_notify_update_6003']);
unset($_SESSION['comment_notify_update_6003_max']);
$ret[] = array('success' => FALSE, 'query' => t('Something is maybe not right.'));
}
else {
// Report how much is left to complete.
$ret['#finished'] = $_SESSION['comment_notify_update_6003'] / $_SESSION['comment_notify_update_6003_max'];
}
return $ret;
}
/**
* Add a "notified" column to {comment_notify} to keep track of whether
* notifications have already been sent out for a given comment. This
* prevents new notifications from being sent when a comment is edited.
*/
function comment_notify_update_6004() {
$ret = array();
db_add_field($ret, 'comment_notify', 'notified', array('type' => 'int', 'size' => 'small', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0));
// Set the value in the notified column to 1 for all existing records.
$ret[] = update_sql('UPDATE {comment_notify} SET notified = 1');
return $ret;
}
/**
* Having our foreign keys auto_increment could lead to unexpected results.
*/
function comment_notify_update_6005() {
$ret = array();
db_change_field($ret, 'comment_notify', 'cid', 'cid',
array('type' => 'int',
'unsigned' => TRUE,
'description' => 'The comment id from {comments}.cid',
'not null' => TRUE,
'disp-width' => '11'));
db_change_field($ret, 'comment_notify_user_settings', 'uid', 'uid',
array('type' => 'int',
'unsigned' => TRUE,
'description' => 'The user id from {users}.cid',
'not null' => TRUE,
'disp-width' => '11'));
return $ret;
}
\ No newline at end of file
This diff is collapsed.
......@@ -20,9 +20,6 @@ class CommentNotifyTestCase extends DrupalWebTestCase {
parent::setUp('comment_notify');
// Create a content type where commenting is enabled.
// Allow contact info for anons on that content type, and make preview optional.
// Do some default comment notify settings?.
// variable_set('some_variable', 'some value');
}
/**
......@@ -42,21 +39,24 @@ class CommentNotifyTestCase extends DrupalWebTestCase {
$this->node = $this->drupalCreateNode(array('type' => 'story', 'promote' => 1, 'comment' => 2));
// Allow anonymous users to post comments and get notifications.
$this->setPermission('anonymous user', array('access comments' => TRUE, 'access content' => TRUE, 'post comments' => TRUE, 'post comments without approval' => TRUE, 'subscribe to comments' => TRUE));
$this->setPermission('anonymous', array('access comments' => TRUE, 'access content' => TRUE, 'post comments' => TRUE, 'post comments without approval' => TRUE, 'subscribe to comments' => TRUE));
$this->drupalLogout();
// Notify type 1 - All comments on the node.
// First confirm that we properly require an e-mail address.
$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.'));
// This is still a bad test to test for a static string showing on the page, but at least the definition of the string is centralized.
$expected_error = comment_notify_variable_registry_get('error_anonymous_email_missing');
$this->assertText(t($expected_error));
// Try again with an e-mail address.
$contact_1 = array('mail' => $this->randomName() .'@'. $this->randomName());
$anonymous_comment_1 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, $subscribe_1, $contact_1);
// Confirm that the notification is saved.
$result = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $anonymous_comment_1['id']));
$result = comment_notify_get_notification_type($anonymous_comment_1['id']);
$this->assertEqual($result, $subscribe_1['notify_type'], 'Notify selection option 1 is saved properly.');
// Notify type 2 - replies to a comment.
......@@ -65,7 +65,7 @@ class CommentNotifyTestCase extends DrupalWebTestCase {
$anonymous_comment_2 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, $subscribe_2, $contact_2);
// Confirm that the notification is saved.
$result = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $anonymous_comment_2['id']));
$result = comment_notify_get_notification_type($anonymous_comment_2['id']);
$this->assertEqual($result, $subscribe_2['notify_type'], 'Notify selection option 2 is saved properly.');
// Confirm that the original subscriber with all comments on this node got their mail.
......@@ -78,7 +78,7 @@ class CommentNotifyTestCase extends DrupalWebTestCase {
$anonymous_comment_0 = $this->postCommentNotifyComment($this->node, $this->randomName(), $this->randomName(), TRUE, $subscribe_0, $contact_0);
// Confirm that the notification is saved.
$result = db_result(db_query("SELECT notify FROM {comment_notify} WHERE cid = %d", $anonymous_comment_0['id']));
$result = comment_notify_get_notification_type($anonymous_comment_0['id']);
$this->assertEqual($result, 2, 'Notify selection option 0 is saved properly.');
// TODO yet more tests.
......@@ -91,21 +91,13 @@ class CommentNotifyTestCase extends DrupalWebTestCase {
* @param string $role User role to set permissions for.
* @param array $permissions Key-value array of permissions to set.
*/
function setPermission($role, $permissions) {
function setPermission($role_name, $permissions) {
// Get role id (rid) for specified role.
$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.'));
}
// Create edit array from permission.
$edit = array();
foreach ($permissions as $name => $value) {
$edit[$rid . '[' . $name . ']'] = $value;
$role = user_role_load($role_name);
if ($role) {
$this->fail('Unable to load role: ' . $role_name);
}
$this->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
$this->assertText(t('The changes have been saved.'), t(' [permission] Saved changes.'));
user_role_change_permissions($role->rid, $permissions);
}
/////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
......
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