From 02be97983cb4dcd252773d7cf96b87e0d1406325 Mon Sep 17 00:00:00 2001 From: mxbeaulieu Date: Tue, 2 Jun 2015 14:22:31 -0400 Subject: [PATCH] Bug 12532 - Redirect guarantee email to guarantor Added unit tests. Added an atomicupdate file. Reordered the sysprefs in alphabetical order. Refactored some code. TEST PLAN: I Apply the patch II Run updatedatabase.pl III Run db_dependent/Letters.t and db_dependant/Members.t 0) Enable EnhancedMessagingPreferences preference; 0) Enable EnableRedirectGuaranteeEmail preference; 1) Select a patron with child; 2) Enable Item checkout -> email in patron messaging preferences; 3) Select the patron's child; 4) Checkout an item; 5) Valide child checkout message in patron's inbox; sponsored by the ville de victoriaville Signed-off-by: Nick Clemens --- C4/Letters.pm | 8 ++-- C4/Members.pm | 27 +++++------ ..._12532-EnableRedirectGuaranteeEmail_syspref.sql | 2 + installer/data/mysql/sysprefs.sql | 1 + .../prog/en/modules/admin/preferences/patrons.pref | 6 +++ t/db_dependent/Letters.t | 55 ++++++++++++++++++++++ t/db_dependent/Members.t | 22 +++++++++ 7 files changed, 103 insertions(+), 18 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_12532-EnableRedirectGuaranteeEmail_syspref.sql mode change 100644 => 100755 t/db_dependent/Letters.t diff --git a/C4/Letters.pm b/C4/Letters.pm index 0d092ac..42d1fec 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -1179,18 +1179,18 @@ sub _send_message_by_email { my ($username, $password, $method) = @_; my $member = C4::Members::GetMember( 'borrowernumber' => $message->{'borrowernumber'} ); + my $useGurantor = $member->{'guarantorid'} && C4::Context->preference("EnableRedirectGuaranteeEmail"); my $to_address = $message->{'to_address'}; - unless ($to_address) { + if ($useGurantor || !$to_address) { unless ($member) { - warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})"; + warn "FAIL: INVALID borrowernumber ($message->{borrowernumber})"; _set_message_status( { message_id => $message->{'message_id'}, status => 'failed' } ); return; } $to_address = C4::Members::GetNoticeEmailAddress( $message->{'borrowernumber'} ); unless ($to_address) { - # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})"; - # warning too verbose for this more common case? + warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})"; _set_message_status( { message_id => $message->{'message_id'}, status => 'failed' } ); return; diff --git a/C4/Members.pm b/C4/Members.pm index 98e9fc5..24b3340 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -1402,22 +1402,21 @@ Returns the empty string if no email address. sub GetNoticeEmailAddress { my $borrowernumber = shift; + my $dbh = C4::Context->dbh(); + + my $member = C4::Members::GetMember( 'borrowernumber' => $borrowernumber ); + + # Determine who we send the email to. + my $guarantorid = $member->{'guarantorid'}; + my $useGuarantor = $guarantorid && C4::Context->preference("EnableRedirectGuaranteeEmail"); + + my $recipient = $useGuarantor ? C4::Members::GetMember( 'borrowernumber' => $guarantorid ) : $member; - my $which_address = C4::Context->preference("AutoEmailPrimaryAddress"); # if syspref is set to 'first valid' (value == OFF), look up email address - if ( $which_address eq 'OFF' ) { - return GetFirstValidEmailAddress($borrowernumber); - } - # specified email address field - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare( qq{ - SELECT $which_address AS primaryemail - FROM borrowers - WHERE borrowernumber=? - } ); - $sth->execute($borrowernumber); - my $data = $sth->fetchrow_hashref; - return $data->{'primaryemail'} || ''; + my $which_address = C4::Context->preference("AutoEmailPrimaryAddress"); + my $to_address = $which_address eq 'OFF' ? GetFirstValidEmailAddress($recipient->{'borrowernumber'}) : $recipient->{ $which_address }; + + return $to_address || ''; } =head2 GetExpiryDate diff --git a/installer/data/mysql/atomicupdate/bug_12532-EnableRedirectGuaranteeEmail_syspref.sql b/installer/data/mysql/atomicupdate/bug_12532-EnableRedirectGuaranteeEmail_syspref.sql new file mode 100644 index 0000000..2e08159 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_12532-EnableRedirectGuaranteeEmail_syspref.sql @@ -0,0 +1,2 @@ +INSERT IGNORE INTO systempreferences (variable,value,explanation,options,type) +VALUES ('EnableRedirectGuaranteeEmail','0',NULL,'Enable the ability to redirect guarantee email messages to guarantor.','YesNo'); diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql index 72138f7..a106a50 100644 --- a/installer/data/mysql/sysprefs.sql +++ b/installer/data/mysql/sysprefs.sql @@ -133,6 +133,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('EnableAdvancedCatalogingEditor','0','','Enable the Rancor advanced cataloging editor','YesNo'), ('EnableBorrowerFiles','0',NULL,'If enabled, allows librarians to upload and attach arbitrary files to a borrower record.','YesNo'), ('EnableOpacSearchHistory','1','YesNo','Enable or disable opac search history',''), +('EnableRedirectGuaranteeEmail', '0', NULL, 'Enable the ability to redirect guarantee email messages to guarantor.', 'YesNo'), ('EnableSearchHistory','0','','Enable or disable search history','YesNo'), ('EnhancedMessagingPreferences','0','','If ON, allows patrons to select to receive additional messages about items due or nearly due.','YesNo'), ('expandedSearchOption','0',NULL,'If ON, set advanced search to be expanded by default','YesNo'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref index 22c2b27..95aedf4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -159,6 +159,12 @@ Patrons: yes: Do no: "Don't" - charge a fee when a patron changes to a category with an enrollment fee. + - + - pref: EnableRedirectGuaranteeEmail + choices: + yes: Do + no: "Don't" + - enable the ability to redirect guarantee email messages to guarantor. All email message placed in message_queue table will be affected. "Norwegian patron database": - - pref: NorwegianPatronDBEnable diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t old mode 100644 new mode 100755 index dd3e07a..9110206 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -137,6 +137,61 @@ is( 'message marked failed if tried to send SMS message for borrower with no smsalertnumber set (bug 11208)' ); +#Test guarantee-guarantor email redirection +my ($GUARANTEE_EMAIL, $GUARANTOR_EMAIL) = qw| guarantee@example.com guarantor@example.com |; +my $guarantor = { + email => $GUARANTOR_EMAIL, + categorycode => 'PT', + branchcode => 'CPL', + surname => "Guarantor" +}; +$guarantor->{'borrowernumber'} = AddMember(%$guarantor); +my $guarantee = { + guarantorid => $guarantor->{'borrowernumber'}, + email => $GUARANTEE_EMAIL, + categorycode => 'PT', + branchcode => 'CPL', + surname => "Guarantee" +}; +$guarantee->{'borrowernumber'} = AddMember(%$guarantee); + +my $GUARANTEE_TO_ADDRESS = 'toto@exemple.com'; +my $guarantee_message = { + borrowernumber => $guarantee->{'borrowernumber'}, + message_transport_type => 'email', + to_address => $GUARANTEE_TO_ADDRESS, + from_address => 'from@example.com', + letter => $my_message->{'letter'} +}; + +sub SendToGuarantor { + my ($useGuarantor, $message) = @_; + C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'OFF' ); + C4::Context->set_preference( 'EnableRedirectGuaranteeEmail', $useGuarantor ); + C4::Context->clear_syspref_cache(); + my $id = C4::Letters::EnqueueLetter($message); + C4::Letters::SendQueuedMessages(); + my $result = $dbh->selectrow_hashref( 'SELECT to_address, status FROM message_queue WHERE message_id = ?', undef, ($id) ); + return $result; +} +my $result = SendToGuarantor(0,$guarantee_message); +is($result->{'status'}, "sent", "With EnableRedirectGuaranteeEmail off, message is sent."); +is($result->{'to_address'}, $GUARANTEE_TO_ADDRESS, "With EnableRedirectGuaranteeEmail off, Message is sent to the specified to_address."); + +$result = SendToGuarantor(1,$guarantee_message); +is($result->{'status'}, "sent", "With EnableRedirectGuaranteeEmail on, message is sent."); +is($result->{'to_address'}, $GUARANTOR_EMAIL, "With EnableRedirectGuaranteeEmail on, Message is sent to the guarantor's email address."); + +$guarantor->{'email'} = undef; +ModMember(%$guarantor); +$result = SendToGuarantor(1,$guarantee_message); +is($result->{'status'}, "failed", "With EnableRedirectGuaranteeEmail on, message fails if guarantor has no address."); + +$guarantee->{'guarantorid'} = undef; +ModMember(%$guarantee); +$result = SendToGuarantor(1,$guarantee_message); +is($result->{'to_address'}, $GUARANTEE_TO_ADDRESS, "With EnableRedirectGuaranteeEmail on, Message is sent to specicied to_address if he has no guarantor."); + # GetLetters my $letters = C4::Letters::GetLetters(); is( @$letters, 0, 'GetLetters returns the correct number of letters' ); diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index 55bdf82..c3fb817 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -147,6 +147,7 @@ is ($checkcardnum, "2", "Card number is too long"); +C4::Context->set_preference( 'EnableRedirectGuaranteeEmail', 0 ); C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'OFF' ); C4::Context->clear_syspref_cache(); @@ -159,6 +160,27 @@ C4::Context->clear_syspref_cache(); $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'}); is ($notice_email, $EMAILPRO, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is emailpro"); +#Test guarantee-guarantor email redirection +my $GUARANTEE_EMAIL = 'guanrantee@email.test'; +my $guarantee = { + guarantorid => $member->{'borrowernumber'}, + emailpro => $GUARANTEE_EMAIL, + categorycode => $CATEGORYCODE, + branchcode => $BRANCHCODE, + surname => "surname" +}; +$guarantee->{'borrowernumber'} = AddMember(%$guarantee); + +$notice_email = GetNoticeEmailAddress($guarantee->{'borrowernumber'}); +is ($notice_email, $GUARANTEE_EMAIL, "GetNoticeEmailAddress returns correct value when EnableRedirectGuaranteeEmail is disabled"); + +C4::Context->set_preference( 'EnableRedirectGuaranteeEmail', 1 ); +C4::Context->clear_syspref_cache(); + +$notice_email = GetNoticeEmailAddress($guarantee->{'borrowernumber'}); +is ($notice_email, $EMAILPRO, "GetNoticeEmailAddress returns correct value when EnableRedirectGuaranteeEmail is activated"); + + ok(!$member->{is_expired}, "GetMemberDetails() indicates that patron is not expired"); ModMember(borrowernumber => $member->{'borrowernumber'}, dateexpiry => '2001-01-1'); $member = GetMemberDetails($member->{'borrowernumber'}); -- 1.9.1