From 3dc1b1149ff580fbb165ef6c00d3b9e754d0d7bb Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 16 Feb 2016 15:55:13 +0000 Subject: [PATCH] Bug 15857 - Add test mode to process_message_queue.pl It would be nice for testing and migration purposes if we could run process_message_queue.pl but have the emails go to a testing address instead of the patron's email address, and have SMS messages go to a testing phone number instead of the patron's sms number. Test Plan: 1) Apply this patch 2) Add some stuff to the message queue however you wish 3) Run process_message_queue.pl with the addition parameters --test --test-email your@email.com 4) Note that you recieve those emails, and not the address listed in the message_queue table! 5) Bonus points: If you have an SMS service, repeat the test with SMS notices and --test-sms --- C4/Letters.pm | 72 ++++++++++++++++++++++++++-------- misc/cronjobs/process_message_queue.pl | 23 ++++++++--- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 0452df2..5e6c0a5 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -989,9 +989,9 @@ sub SendQueuedMessages { my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} ); my $sms_provider = Koha::SMS::Providers->find( $member->{'sms_provider_id'} ); $message->{to_address} .= '@' . $sms_provider->domain(); - _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} ); + _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'}, $params->('test_mode'), $params->{'test_email'} ); } else { - _send_message_by_sms( $message ); + _send_message_by_sms( $message, $params->{'test_mode'}, $params->{test_sms} ); } } } @@ -1226,7 +1226,7 @@ ENDSQL sub _send_message_by_email { my $message = shift or return; - my ($username, $password, $method) = @_; + my ( $username, $password, $method, $test_mode, $test_email ) = @_; my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} ); my $to_address = $message->{'to_address'}; @@ -1282,15 +1282,28 @@ sub _send_message_by_email { _update_message_to_address($message->{'message_id'},$to_address) unless $message->{to_address}; #if initial message address was empty, coming here means that a to address was found and queue should be updated - if ( sendmail( %sendmail_params ) ) { - _set_message_status( { message_id => $message->{'message_id'}, - status => 'sent' } ); - return 1; - } else { - _set_message_status( { message_id => $message->{'message_id'}, - status => 'failed' } ); - carp $Mail::Sendmail::error; - return; + $sendmail_params{to} = $test_email if ( $test_mode && $test_email ); + + if ( !$test_mode || ( $test_mode && $test_email ) ) { + if ( sendmail(%sendmail_params) ) { + _set_message_status( + { + message_id => $message->{'message_id'}, + status => 'sent' + } + ); + return 1; + } + else { + _set_message_status( + { + message_id => $message->{'message_id'}, + status => 'failed' + } + ); + carp $Mail::Sendmail::error; + return; + } } } @@ -1333,6 +1346,9 @@ sub _is_duplicate { sub _send_message_by_sms { my $message = shift or return; + my $test_mode = shift; + my $test_sms = shift; + my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} ); unless ( $member->{smsalertnumber} ) { @@ -1347,11 +1363,33 @@ sub _send_message_by_sms { return; } - my $success = C4::SMS->send_sms( { destination => $member->{'smsalertnumber'}, - message => $message->{'content'}, - } ); - _set_message_status( { message_id => $message->{'message_id'}, - status => ($success ? 'sent' : 'failed') } ); + my $success; + if ($test_mode) { + if ( $test_sms ) { + $success = C4::SMS->send_sms( + { + destination => $test_sms, + message => $message->{'content'}, + } + ); + } + } + else { + $success = C4::SMS->send_sms( + { + destination => $member->{'smsalertnumber'}, + message => $message->{'content'}, + } + ); + } + + _set_message_status( + { + message_id => $message->{'message_id'}, + status => ( $success ? 'sent' : 'failed' ) + } + ); + return $success; } diff --git a/misc/cronjobs/process_message_queue.pl b/misc/cronjobs/process_message_queue.pl index 3325c8a..b9ee68e 100755 --- a/misc/cronjobs/process_message_queue.pl +++ b/misc/cronjobs/process_message_queue.pl @@ -34,6 +34,9 @@ my $password = undef; my $method = 'LOGIN'; my $help = 0; my $verbose = 0; +my $test; +my $test_email; +my $test_sms; GetOptions( 'u|username:s' => \$username, @@ -41,6 +44,9 @@ GetOptions( 'm|method:s' => \$method, 'h|help|?' => \$help, 'v|verbose' => \$verbose, + 't|test' => \$test, + 'e|test-email' => \$test_email, + 's|test-sms' => \$test_sms, ); my $usage = << 'ENDUSAGE'; @@ -51,11 +57,16 @@ you run this regularly from cron, especially if you are using the advance_notices.pl script. This script has the following parameters : - -u --username: username of mail account - -p --password: password of mail account - -m --method: authentication method required by SMTP server (See perldoc Sendmail.pm for supported authentication types.) - -h --help: this message - -v --verbose: provides verbose output to STDOUT + -u --username : username of mail account + -p --password : password of mail account + -m --method : authentication method required by SMTP server (See perldoc Sendmail.pm for supported authentication types.) + -h --help : this message + -v --verbose : provides verbose output to STDOUT + -t --test-mode : Enables test mode where all messages are redirected or supressed + -e --test-email : Email address for test mode. All emails will be sent to this address instead of the patron's email address. + If test mode is set but test email is not, emails will not actually be sent + -s --test-sms : SMS number for test mode. All SMS messages will be sent to this number instead of the patron's sms number. + If test mode is set but test SMS is not, SMS messages will not actually be sent ENDUSAGE @@ -63,5 +74,5 @@ die $usage if $help; cronlogaction(); -C4::Letters::SendQueuedMessages( { verbose => $verbose, username => $username, password => $password, method => $method } ); +C4::Letters::SendQueuedMessages( { verbose => $verbose, username => $username, password => $password, method => $method, test_mode => $test, test_email => $test_email, test_sms => $test_sms } ); -- 2.1.4