View source
<?php
require_once drupal_get_path('module', 'notifications') . '/tests/notifications_test_case.inc';
class NotificationsLiteTests extends NotificationsTestCase {
function getInfo() {
return array(
'name' => 'Notifications Lite',
'group' => 'Notifications',
'description' => 'Notifications Lite message sending and composition',
);
}
function setUp() {
parent::setUp('messaging_simple', 'notifications_digest', 'notifications_lite');
variable_set('notifications_default_send_interval', 0);
variable_set('notifications_default_send_method', 'simple');
variable_set('site_name', 'Test Site');
}
function testNotificationsLite() {
$user = $this
->drupalCreateUser();
$send_interval = 0;
$send_method = 'simple';
foreach (array(
1,
2,
) as $index) {
notifications_lite_send($user->uid, "Subject {$index}", "Body {$index}");
}
$this
->assertUserRows('notifications_queue', 2, $user->uid, 'Two queued notifications');
$this
->assertTableRows('notifications_event', 2, NULL, 'Two queued events');
$sqid = notifications_queue()
->process_prepare();
$count = notifications_queue()
->process_queue($send_interval, $sqid);
$this
->assertEqual($count, 2, "Processed two rows in queue ({$count})");
$this
->assertTableRows('notifications_event', 0);
$this
->assertUserRows('notifications_queue', 0, $user->uid, 'No queued notifications');
$messages = messaging_simple_get_messages(array(
'uid' => $user->uid,
));
$this
->assertEqual(count($messages), 2, 'Retrieved two messages from store');
foreach (array(
1,
2,
) as $index) {
$message = array_shift($messages);
$this
->assertEqual("Subject {$index}", $message->subject, "Right subject for message {$index}: {$message->subject}");
}
db_query("DELETE FROM {messaging_simple}");
variable_set('notifications_digest_methods', array(
$send_interval => 'short',
));
notifications_build_method(0, TRUE);
$sent = $this
->sendLiteMessages($user->uid, 4);
$messages = messaging_simple_get_messages(array(
'uid' => $user->uid,
));
$this
->assertEqual($count = count($messages), 1, "Retrieved just one message from store ({$count})");
$message = array_shift($messages);
foreach ($sent as $index => $msg) {
$this
->assertTrue(strpos($message->body, $msg->subject), "The message {$index} is contained in the digest");
}
db_query("DELETE FROM {messaging_simple}");
variable_set('notifications_digest_methods', array(
$send_interval => 'long',
));
notifications_build_method($send_interval, TRUE);
$sent = $this
->sendLiteMessages($user->uid, 2);
$messages = messaging_simple_get_messages(array(
'uid' => $user->uid,
));
$this
->assertEqual($count = count($messages), 1, "Retrieved one sent message from store ({$count})");
$message = array_shift($messages);
foreach ($sent as $index => $msg) {
$this
->assertTrue(strpos($message->body, $msg->subject), "The message {$index} subject is contained in the digest");
$this
->assertTrue(strpos($message->body, $msg->body), "The message {$index} body is contained in the digest");
}
}
function sendLiteMessages($uid, $count) {
$messages = array();
for ($i = 1; $i <= $count; $i++) {
$msg = new Stdclass();
$msg->subject = "Lite Subject {$i}";
$msg->body = "Body for message {$i}";
notifications_lite_send($uid, $msg->subject, $msg->body);
$messages[$i] = $msg;
}
$this
->assertUserRows('notifications_queue', $count, $uid, "We have {$count} notifications in queue");
$rows = notifications_queue()
->process_rows(array(
'uid' => $uid,
));
$this
->assertEqual($count, $rows, "Processed all rows in queue ({$rows})");
return $messages;
}
}