From 86e3ad4c4c3b2b38244dd73aa4eca9976a98ba3c Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Thu, 30 Oct 2014 13:02:09 +0100 Subject: [PATCH] Bug 13096: Adding code from memberentry to Members.pm module Content-Type: text/plain; charset=utf-8 This patch adds routine SendAutoEmail to Members.pm (and adds some tests). The code is taken from memberentry.pl. Follow-up patches modify the code in memberentry and add new calls to this routine too. Test plan: Run the revised unit test Members.t. --- C4/Members.pm | 58 ++++++++++++++++++++++++++++++++++++++++++++++ t/db_dependent/Members.t | 37 ++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 1 deletions(-) diff --git a/C4/Members.pm b/C4/Members.pm index 0978078..d72afb2 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -2478,6 +2478,12 @@ sub AddEnrolmentFeeIfNeeded { } } +=head2 HasOverdues + + HasOverdues( $borrowernumber ); + +=cut + sub HasOverdues { my ( $borrowernumber ) = @_; @@ -2489,6 +2495,58 @@ sub HasOverdues { return $count; } +=head2 SendAutoEmail + + SendAutoEmail( $borrowerdata, $template ); + + Send an alert with login detail (account notice) when userid or password + has been changed, if pref AutoEmailOpacUser has been enabled. + The hashref $borrowerdata should contain the keys: + borrowernumber, branchcode, userid, password, email, emailpro, B_email + The template parameter refers to a template object. Errors are passed + via the template. + +=cut + +sub SendAutoEmail { #optionally send account detail notice + my ( $dataref, $template ) = @_; + + # check AutoEmailOpacUser syspref first + return if !C4::Context->preference("AutoEmailOpacUser"); + # no mail too if no userid or password + return if ref($dataref) ne 'HASH' || !$dataref->{userid} || + !$dataref->{password}; + + # first look for defined primary email addres, otherwise try others + my $emailaddr; + my $primary = C4::Context->preference("AutoEmailPrimaryAddress"); + return if !$primary; + if( $primary ne 'OFF' && $dataref->{primary} && + $dataref->{$primary} =~ /\w\@\w/ ) { + $emailaddr = $dataref->{$primary}; + } elsif( $dataref->{email} && $dataref->{email} =~ /\w\@\w/ ) { + $emailaddr = $dataref->{email}; + } elsif( $dataref->{emailpro} && $dataref->{emailpro} =~ /\w\@\w/ ) { + $emailaddr = $dataref->{emailpro}; + } elsif( $dataref->{B_email} && $dataref->{B_email} =~ /\w\@\w/ ) { + $emailaddr = $dataref->{B_email}; + } + return if !$emailaddr; + + # if we manage to find a valid email address, send notice + $dataref->{emailaddr} = $emailaddr; + my $err; + eval { $err = SendAlerts ( 'members', $dataref, "ACCTDETAILS" ); }; + if ( $@ ) { + $template->param(error_alert => $@); + } elsif ( ref($err) eq "HASH" && defined $err->{error} && + $err->{error} eq "no_email" ) { + $template->{VARS}->{'error_alert'} = "no_email"; + } else { + $template->{VARS}->{'info_alert'} = 1; + } +} + END { } # module clean-up code here (global destructor) 1; diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index 998320b..ad4172e 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 58; +use Test::More tests => 59; use Test::MockModule; use Data::Dumper; use C4::Context; @@ -337,6 +337,41 @@ subtest 'GetMemberAccountBalance' => sub { $dbh->rollback(); }; +# tests for routine SendAutoEmail +subtest 'SendAutoEmail' => sub { + plan tests => 4; + my $mock1 = new Test::MockModule('C4::Members'); #mocks imported routine + my $mock2 = new Test::MockModule('C4::Templates'); + my $alerts = 0; + $mock1->mock( 'SendAlerts', sub { $alerts++; } ); + $mock2->mock( 'param', sub { 1; } ); + my $data = { userid => 'marcel', password => 'N0t_MyPw' }; + my $template = { VARS => {} }; + bless $template, 'C4::Templates'; + + C4::Context->set_preference( 'AutoEmailOpacUser' , '0' ); + C4::Members::SendAutoEmail( $data, $template ); + is( $alerts, 0, 'No mails when pref is disabled' ); + + $data->{email} = 'something@domain.com'; + C4::Context->set_preference( 'AutoEmailOpacUser' , '1' ); + C4::Context->set_preference( 'AutoEmailPrimaryAddress' , 'OFF' ); + C4::Members::SendAutoEmail( $data, $template ); + is( $alerts, 1, 'Alert to first valid email address' ); + + C4::Context->set_preference( 'AutoEmailPrimaryAddress' , 'emailpro' ); + C4::Members::SendAutoEmail( $data, $template ); + is( $alerts, 2, 'Fallback to email when emailpro missing' ); + + delete $data->{email}; + C4::Members::SendAutoEmail( $data, $template ); + is( $alerts, 2, 'No fallback when emailpro missing' ); + + $dbh->rollback(); +}; + +# SUBROUTINES + sub _find_member { my ($resultset) = @_; my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset ); -- 1.7.7.6