View source
<?php
class Notifications_Content_Tests extends DrupalTestCase {
function get_info() {
return array(
'name' => 'Notifications Content',
'group' => 'Notifications',
'desc' => 'Content Notifications API functions',
);
}
function testNotificationsContent() {
$this
->drupalModuleEnable('messaging_debug');
$this
->drupalVariableSet('messaging_debug', 0);
$this
->drupalModuleEnable('notifications_content');
$this
->drupalVariableSet('notifications_default_send_interval', 0);
$info = new Stdclass();
$info->type = $this
->randomName();
$info->name = 'Notifications ' . str_replace('_', ' ', $this
->randomName());
$info->description = 'Test content type for notifications';
$info->module = 'node';
$info->has_title = TRUE;
$info->title_label = t('Title');
$info->has_body = TRUE;
$info->body_label = t('Body');
$info->min_word_count = 0;
$info->custom = TRUE;
node_type_save($info);
$author = user_load(array(
'uid' => 1,
));
$node = new Stdclass();
$node->title = 'Notifications ' . $this
->randomName();
$node->body = 'This is a test node for content subscriptions.';
$node->type = $info->type;
$node->uid = $author->uid;
$node->status = 1;
node_save($node);
$subs_thread = new Stdclass();
$subs_thread->type = 'thread';
$subs_thread->event_type = 'node';
$subs_thread->fields['nid'] = $node->nid;
$this
->assertEqual(notifications_subscription_types('thread', 'event_type'), 'node', 'Types hook retrieves right value.');
$event_type = notifications_event_types('node', 'update');
$this
->assertEqual($event_type['digest'], array(
'node',
'nid',
), 'Event types hook retrieves right value.');
$user = drupal_anonymous_user();
$this
->assertEqual(notifications_user_allowed('subscription', $user, $subs_thread), FALSE, 'Subscription not allowed for anonymous user');
$user = $this
->drupalCreateUserRolePerm(array(
'access content',
'subscribe to content',
'subscribe to content type',
'subscribe to author',
));
$this
->assertEqual(notifications_user_allowed('subscription', $user, $subs_thread), TRUE, 'Subscription is allowed for user with the right permissions');
$this
->drupalLoginUser($user);
$link = notifications_get_link('subscribe', array(
'uid' => $user->uid,
'type' => 'thread',
'fields' => array(
'nid' => $node->nid,
),
'confirm' => TRUE,
));
$this
->drupalGet(url($link['href'], $link['query'], NULL, TRUE));
$this
->assertText(t('Confirm your subscription'), 'Thread subscription: Subscriptions confirmation page is shown');
$this
->assertWantedRaw(t('Thread: %title', array(
'%title' => check_plain($node->title),
)), 'Confirmation page parameters are ok');
$this
->clickSubmit(t('Subscribe'));
$this
->assertText(t('Your subscription was activated'), 'Confirmation message is displayed');
$allowed = variable_get('notifications_content_types', array());
$allowed[$info->type] = 0;
$this
->drupalVariableSet('notifications_content_types', $allowed);
$link = notifications_get_link('subscribe', array(
'uid' => $user->uid,
'type' => 'nodetype',
'fields' => array(
'type' => $node->type,
),
'confirm' => TRUE,
));
$this
->drupalGet(url($link['href'], $link['query'], NULL, TRUE));
$this
->assertText(t('Subscription type or parameters not allowed'), 'Error message for not allowed content type');
$this
->drupalGet(url('user/' . $user->uid . '/notifications/nodetype'));
$this
->assertNoText($info->name, 'User account subscriptions doesn\'t show content type.');
$allowed[$info->type] = 1;
$this
->drupalVariableSet('notifications_content_types', $allowed);
$this
->drupalGet(url($link['href'], $link['query'], NULL, TRUE));
$this
->assertText(t('Confirm your subscription'), 'Content type: Subscriptions confirmation page is shown');
$this
->assertWantedRaw(t('Content type: %type', array(
'%type' => $info->name,
)), 'Confirmation page parameters are ok');
$this
->clickSubmit(t('Subscribe'));
$this
->assertText(t('Your subscription was activated'), 'Confirmation message is displayed');
$this
->drupalGet(url('user/' . $user->uid . '/notifications/nodetype'));
$this
->assertText($info->name, 'User account subscriptions shows content type.');
$link = notifications_get_link('subscribe', array(
'uid' => $user->uid,
'type' => 'author',
'fields' => array(
'author' => $author->uid,
),
'confirm' => TRUE,
));
$this
->drupalGet(url($link['href'], $link['query'], NULL, TRUE));
$this
->assertText(t('Confirm your subscription'), 'Author: Subscriptions confirmation page is shown');
$this
->assertWantedRaw(t('Author: %name', array(
'%name' => $author->name,
)), 'Confirmation page parameters are ok');
$this
->clickSubmit(t('Subscribe'));
$this
->assertText(t('Your subscription was activated'), 'Confirmation message is displayed');
$subs = notifications_user_get_subscriptions($user->uid, 'node', $node->nid, $node, TRUE);
$this
->assertEqual(count($subs), 3, 'The 3 subscriptions have actually been created');
$this
->drupalGet(url('user/' . $user->uid . '/notifications'));
$this
->assertText(t('Thread'), 'User account overview shows threads.');
$this
->assertText(t('Content type'), 'User account overview shows content type.');
$this
->assertText(t('Author'), 'User account overview shows author.');
$this
->drupalGet(url('user/' . $user->uid . '/notifications/thread'));
$this
->assertText($node->title, 'User account subscriptions shows threads.');
$this
->drupalGet(url('user/' . $user->uid . '/notifications/author'));
$this
->assertText($author->name, 'User account subscriptions shows author.');
$this
->drupalVariableSet('notifications_send_immediate', 0);
$this
->drupalVariableSet('notifications_sendself', 1);
$node = node_load($node->nid, NULL, TRUE);
$node->body .= 'Updated.';
node_save($node);
$send_method = notifications_user_setting('send_method', $user);
$send_interval = notifications_user_setting('send_interval', $user);
$count = db_result(db_query("SELECT count(*) FROM {notifications_queue} WHERE uid = %d AND send_method = '%s' AND send_interval = %d", $user->uid, $send_method, $send_interval));
$this
->assertEqual($count, 3, 'We have the right number of rows in queue:' . $count);
include_once drupal_get_path('module', 'notifications') . '/notifications.cron.inc';
$queued = notifications_process_pull($send_method, array(
$user->uid,
));
$this
->assertEqual(count($queued), 1, 'Messages for this event have been queued.');
$max_sqid = notifications_process_prepare();
$this
->assertEqual($max_sqid > 0, TRUE, 'Cleanup and queue prepare.');
db_query("UPDATE {notifications_queue} SET module = 'notificationstesting' WHERE uid = %d", $user->uid);
notifications_process_queue($send_interval, $max_sqid, 'notificationstesting');
$count = db_result(db_query("SELECT count(*) FROM {notifications_queue} WHERE uid = %d", $user->uid));
$this
->assertEqual($count, 0, 'All rows in queue have been processed.');
notifications_delete_subscriptions(array(
'uid' => $user->uid,
));
node_delete($node->nid);
node_type_delete($info->type);
}
}