#!/usr/bin/perl use strict; use Test::More 'no_plan'; use lib "$ENV{LJHOME}/cgi-bin"; require 'ljlib.pl'; no warnings 'redefine'; use LJ::Test qw (temp_user memcache_stress alloc_sms_num); use Class::Autouse qw ( LJ::SMS::Message LJ::Event ); # make time() be overridable in the future at runtime, rather than be an opcode: BEGIN { *CORE::GLOBAL::time = sub { time() }; } # mark time as being overridden for notification limit test local $LJ::_T_SMS_NOTIF_LIMIT_TIME_OVERRIDE = 1; my $lastmsg = ''; # don't actually send messages local $LJ::_T_SMS_SEND = sub { my $msg = shift; $lastmsg = $msg->body_text; }; sub run_tests { my $u = temp_user(); my $u2 = temp_user(); my $sms_num = alloc_sms_num(); $u->set_sms_number($sms_num, verified => 'Y'); # give user some message quota $u->add_sms_quota(20, 'free'); # set notification limit to 5 notifications per day $u->set_prop('sms_perday_notif_limit', 5); # make a subscription my $subsc = $u->subscribe( event => "JournalNewEntry", method => "SMS", journal => $u2, ); my $send_notifs = sub { # get some notifications for (1..5) { $lastmsg = ''; $u2->t_post_fake_entry; LJ::Event->process_fired_events; ok($lastmsg, 'Got notification'); } # should not receive any more notifications today $lastmsg = ''; $u2->t_post_fake_entry; LJ::Event->process_fired_events; ok(! $lastmsg, 'Did not receive any notifications after limit reached'); }; $send_notifs->(); # zoom into the future, see if it works correctly tomorrow too my $future = 86400; # 1 day local *CORE::GLOBAL::time = sub { CORE::time() + $future }; $send_notifs->(); } run_tests();