From 9a126f56f275afdef59380336c44bee44e6488bb Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 21 Aug 2014 16:48:55 +0200 Subject: [PATCH] Bug 12802: Sent notices using several email addresses Currently it is not possible to select several email addresses to notify a patron. The only place where the mechanism exists is in the overdue_notices.pl script. This patch reuses the AutoEmailPrimaryAddress syspref and changes its type to "multiple". Like that it is now possible to select several email addresses. Note that there is no sense to select OFF and another value for this pref. So if the 'OFF' (first valid) value exist, it takes the priority. It will add the ability to choose the email addresses to use to notify patrons. The option is "email", "emailpro" and "B_email". If "OFF" is selected, the first valid address will be returned (same behavior as before this patch). Note for the QA step: I found a possible regression, but IMO it's not a big deal: Before this patch if a letter did not contain a "to_address", the C4::Letters::_send_message_by_email subroutine retrieve the email from the given borrowernumber. This is now done in the EnqueueMessage subroutine. What it means: If a borrower didn't have an email address when the notice was sent to the queue, the email address won't be check again when the notice will really sent to the patron. This change permits to sent a letter to the queue (EnqueueLetter) without any information (from_address, to_address), only the borrowernumber is mandatory to retrieve them. The _send_message_by_email subroutine should not contain any useful code, only sent the letter we ask it to sent. What this patch does: The GetNoticeEmailAddress subroutine has been renamed to GetNoticeEmailAddresses (reflect the plural). It only gets the patron' emails defined in the AutoEmailPrimaryAddress pref. If no 'to_address' parameter is given to EnqueueMessage, the emails will be retrieved at this moment. In C4::Message: An old form was found. The AutoEmailPrimaryAddress was not check. The smsalertnumber was sent for the to_address param, which is wrong. C4::Reserves: AddReserve and _koha_notify_reserve: The from address is built in the QueueLetter. It is useless to do it here. overdue_notices.pl: The script could be cleaned a little bit if we remove the --email parameter. Indeed it is redundant with this new enhancement. I can propose another patch with a die statement and a message: "you should use the pref AutoEmailPrimaryAddress" if the --email is provided. opac/opac-shareshelf.pl and opac/opac-memberentry.pl: just remove the from and to address. They will be filled on sending to the queue (because of the borrowernumber). Test plan: 1/ Apply this patch without updating the pref AutoEmailPrimaryAddress (or fill it with a single value if it is not done yet). 2/ Try the different way to sent notices to a patron (check the following letter code: HOLD, CHECKIN, CHECKOUT, PREDUE, RENEW, DUE). 3/ Verify the email is correctly sent to the message_queue. 4/ Fill the pref with email and emailpro (for instance) 5/ Verify that 2 notices are sent to the message_queue: 1 with the email and 1 with the emailpro. 6/ You can also verify that only 1 notice is generated if the emailpro is empty. Important note for testers: Pending messages must be removed each time you change the value of AutoEmailPrimaryAddress. When there are already existing pending messages for a borrower, Koha tries to update them rather than enqueuing new ones, but it will not update the recipient's email address, nor add/remove messages depending on the new value of AutoEmailPrimaryAddress; not sure if this is a bug or not. Signed-off-by: Brendan Gallagher --- C4/Letters.pm | 126 +++++++++++++++++++-------------------- C4/Members.pm | 45 +++++++------- C4/Message.pm | 26 -------- C4/Reserves.pm | 17 +++--- members/memberentry.pl | 20 +++---- misc/cronjobs/overdue_notices.pl | 10 ++-- opac/opac-memberentry.pl | 4 +- opac/opac-shareshelf.pl | 10 ++-- t/db_dependent/Letters.t | 77 ++++++++++++++++++++---- t/db_dependent/Members.t | 16 +++-- 10 files changed, 191 insertions(+), 160 deletions(-) diff --git a/C4/Letters.pm b/C4/Letters.pm index 3f23090c6b..6aac8ad3b3 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -34,7 +34,7 @@ use C4::SMS; use C4::Debug; use Koha::DateUtils; use Koha::SMS::Providers; - +use Koha::Database; use Koha::Email; use Koha::DateUtils qw( format_sqldatetime dt_from_string ); use Koha::Patrons; @@ -977,7 +977,6 @@ sub EnqueueLetter { my $params = shift or return; return unless exists $params->{'letter'}; -# return unless exists $params->{'borrowernumber'}; return unless exists $params->{'message_transport_type'}; my $content = $params->{letter}->{content}; @@ -997,28 +996,41 @@ sub EnqueueLetter { ); } - my $dbh = C4::Context->dbh(); - my $statement = << 'ENDSQL'; -INSERT INTO message_queue -( borrowernumber, subject, content, metadata, letter_code, message_transport_type, status, time_queued, to_address, from_address, content_type ) -VALUES -( ?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?, ? ) -ENDSQL + my @to_addresses; + if ( $params->{to_address} ) { + @to_addresses = ( $params->{to_address} ); + } elsif ( $params->{borrowernumber} and $params->{message_transport_type} eq 'email' ) { + @to_addresses = @{ C4::Members::GetNoticeEmailAddresses( $params->{borrowernumber} ) }; + } - my $sth = $dbh->prepare($statement); - my $result = $sth->execute( - $params->{'borrowernumber'}, # borrowernumber - $params->{'letter'}->{'title'}, # subject - $params->{'letter'}->{'content'}, # content - $params->{'letter'}->{'metadata'} || '', # metadata - $params->{'letter'}->{'code'} || '', # letter_code - $params->{'message_transport_type'}, # message_transport_type - 'pending', # status - $params->{'to_address'}, # to_address - $params->{'from_address'}, # from_address - $params->{'letter'}->{'content-type'}, # content_type - ); - return $dbh->last_insert_id(undef,undef,'message_queue', undef); + my $from_address; + if ( $params->{from_address} ) { + $from_address = $params->{from_address}; + } elsif ( $params->{message_transport_type} eq 'email' ) { + if ( $params->{borrowernumber} ) { + my $rs = Koha::Database->new->schema->resultset('Borrower')->search({ borrowernumber => $params->{borrowernumber} }, {join => 'branchcode'}); + $from_address = $rs->next->branchcode->branchemail; + } + + $from_address ||= C4::Context->preference('KohaAdminEmailAddress'); + } + + my $rs = Koha::Database->new->schema->resultset('MessageQueue'); + for my $to_address ( @to_addresses ) { + $rs->create({ + borrowernumber => $params->{borrowernumber}, + subject => $params->{letter}{title}, + content => $params->{letter}{content}, + metadata => ( $params->{letter}{metadata} || '' ), + letter_code => ( $params->{letter}{code} || ''), + message_transport_type => $params->{message_transport_type}, + status => 'pending', + to_address => $to_address, + from_address => $from_address, + content_type => $params->{letter}{'content-type'}, + }); + } + return scalar( @to_addresses ); } =head2 SendQueuedMessages ([$hashref]) @@ -1043,6 +1055,7 @@ sub SendQueuedMessages { if $params->{'verbose'} or $debug; # This is just begging for subclassing next MESSAGE if ( lc($message->{'message_transport_type'}) eq 'rss' ); + if ( lc( $message->{'message_transport_type'} ) eq 'email' ) { _send_message_by_email( $message, $params->{'username'}, $params->{'password'}, $params->{'method'} ); } @@ -1123,31 +1136,24 @@ list of hashes, each has represents a message in the message queue. sub GetQueuedMessages { my $params = shift; - my $dbh = C4::Context->dbh(); - my $statement = << 'ENDSQL'; -SELECT message_id, borrowernumber, subject, content, message_transport_type, status, time_queued -FROM message_queue -ENDSQL - - my @query_params; - my @whereclauses; - if ( exists $params->{'borrowernumber'} ) { - push @whereclauses, ' borrowernumber = ? '; - push @query_params, $params->{'borrowernumber'}; - } - - if ( @whereclauses ) { - $statement .= ' WHERE ' . join( 'AND', @whereclauses ); - } - - if ( defined $params->{'limit'} ) { - $statement .= ' LIMIT ? '; - push @query_params, $params->{'limit'}; - } - - my $sth = $dbh->prepare( $statement ); - my $result = $sth->execute( @query_params ); - return $sth->fetchall_arrayref({}); + my $rs = Koha::Database->new->schema->resultset('MessageQueue')->search( + { + ( + $params->{borrowernumber} + ? ( borrowernumber => $params->{borrowernumber} ) + : () + ), + }, + { + ( + $params->{limit} + ? ( rows => $params->{limit} ) + : () + ), + }, + ); + $rs->result_class('DBIx::Class::ResultClass::HashRefInflator'); + return [ $rs->all ]; } =head2 GetMessageTransportTypes @@ -1304,21 +1310,13 @@ sub _send_message_by_email { my $patron = Koha::Patrons->find( $message->{borrowernumber} ); my $to_address = $message->{'to_address'}; - unless ($to_address) { - unless ($patron) { - warn "FAIL: No 'to_address' and 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? - _set_message_status( { message_id => $message->{'message_id'}, - status => 'failed' } ); - return; - } + unless ($patron and $to_address) { + warn "FAIL: No 'to_address' and INVALID borrowernumber ($message->{borrowernumber})" unless $patron; + _set_message_status({ + message_id => $message->{message_id}, + status => 'failed', + }); + return; } my $utf8 = decode('MIME-Header', $message->{'subject'} ); @@ -1354,8 +1352,6 @@ sub _send_message_by_email { $sendmail_params{ Bcc } = $bcc; } - _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' } ); diff --git a/C4/Members.pm b/C4/Members.pm index f31ef8d40c..e80d9ff191 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -65,7 +65,7 @@ BEGIN { &GetAllIssues &GetFirstValidEmailAddress - &GetNoticeEmailAddress + &GetNoticeEmailAddresses &GetMemberAccountRecords &GetBorNotifyAcctRecord @@ -936,33 +936,38 @@ sub GetFirstValidEmailAddress { return $borrower->first_valid_email_address(); } -=head2 GetNoticeEmailAddress +=head2 GetNoticeEmailAddresses - $email = GetNoticeEmailAddress($borrowernumber); + $emails = GetNoticeEmailAddresses( borrowernumber ); -Return the email address of borrower used for notices, given the borrowernumber. -Returns the empty string if no email address. +Return the email addresses of borrower used for notices, given the borrowernumber. +Returns an empty arrayref if no email address. =cut -sub GetNoticeEmailAddress { +sub GetNoticeEmailAddresses { my $borrowernumber = shift; - 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); + my $which_addresses = C4::Context->preference("AutoEmailPrimaryAddress"); + my @email_addresses; + for my $email_address_field ( split /,/, $which_addresses ) { + + # if syspref is set to 'first valid' (value == OFF), look up email address + if ( $email_address_field eq 'OFF' ) { + return [ GetFirstValidEmailAddress($borrowernumber) ]; + } + + # specified email address field + my $email_address = C4::Context->dbh->selectcol_arrayref( + qq| + SELECT $email_address_field AS email + FROM borrowers + WHERE borrowernumber = ? + |, {}, $borrowernumber + ); + push @email_addresses, $email_address->[0] if $email_address->[0]; } - # 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'} || ''; + return \@email_addresses; } =head2 GetBorrowersToExpunge diff --git a/C4/Message.pm b/C4/Message.pm index 30b71c827c..800b82f30d 100644 --- a/C4/Message.pm +++ b/C4/Message.pm @@ -158,7 +158,6 @@ the message. sub enqueue { my ($class, $letter, $borrower, $transport) = @_; my $metadata = _metadata($letter); - my $to_address = _to_address($borrower, $transport); # Same as render_metadata my $format ||= sub { $_[0] || "" }; @@ -170,34 +169,9 @@ sub enqueue { letter => $letter, borrowernumber => $borrower->{borrowernumber}, message_transport_type => $transport, - to_address => $to_address, }); } -# based on message $transport, pick an appropriate address to send to -sub _to_address { - my ($borrower, $transport) = @_; - my $address; - if ($transport eq 'email') { - $address = $borrower->{email} - || $borrower->{emailpro} - || $borrower->{B_email}; - } elsif ($transport eq 'sms') { - $address = $borrower->{smsalertnumber} - || $borrower->{phone} - || $borrower->{phonepro} - || $borrower->{B_phone}; - } else { - warn "'$transport' is an unknown message transport."; - } - if (not defined $address) { - warn "An appropriate $transport address " - . "for borrower $borrower->{userid} " - . "could not be found."; - } - return $address; -} - # _metadata($letter) -- return the letter split into head/body/footer sub _metadata { my ($letter) = @_; diff --git a/C4/Reserves.pm b/C4/Reserves.pm index 78f5ca39f2..fdd3216f1f 100644 --- a/C4/Reserves.pm +++ b/C4/Reserves.pm @@ -245,8 +245,7 @@ sub AddReserve { { letter => $letter, borrowernumber => $borrowernumber, message_transport_type => 'email', - from_address => $admin_email_address, - to_address => $admin_email_address, + to_address => $admin_email_address, } ); } @@ -604,12 +603,13 @@ sub GetReservesForBranch { Takes an itemnumber and returns the status of the reserve placed on it. If several reserves exist, the reserve with the lower priority is given. -=cut - ## FIXME: I don't think this does what it thinks it does. ## It only ever checks the first reserve result, even though ## multiple reserves for that bib can have the itemnumber set ## the sub is only used once in the codebase. + +=cut + sub GetReserveStatus { my ($itemnumber) = @_; @@ -1640,9 +1640,6 @@ sub _koha_notify_reserve { my $patron = Koha::Patrons->find( $borrowernumber ); - # Try to get the borrower's email address - my $to_address = C4::Members::GetNoticeEmailAddress($borrowernumber); - my $messagingprefs = C4::Members::Messaging::GetMessagingPreferences( { borrowernumber => $borrowernumber, message_name => 'Hold_Filled' @@ -1681,14 +1678,16 @@ sub _koha_notify_reserve { C4::Letters::EnqueueLetter( { letter => $letter, borrowernumber => $borrowernumber, - from_address => $admin_email_address, message_transport_type => $mtt, } ); }; + # Try to get the borrower's email addresses + my $to_addresses = C4::Members::GetNoticeEmailAddresses($borrowernumber); + while ( my ( $mtt, $letter_code ) = each %{ $messagingprefs->{transports} } ) { next if ( - ( $mtt eq 'email' and not $to_address ) # No email address + ( $mtt eq 'email' and not @$to_addresses ) # No email address or ( $mtt eq 'sms' and not $patron->smsalertnumber ) # No SMS number or ( $mtt eq 'phone' and C4::Context->preference('TalkingTechItivaPhoneNotification') ) # Notice is handled by TalkingTech_itiva_outbound.pl ); diff --git a/members/memberentry.pl b/members/memberentry.pl index c58b2a9bff..1b34c91209 100755 --- a/members/memberentry.pl +++ b/members/memberentry.pl @@ -418,19 +418,15 @@ if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){ # If 'AutoEmailOpacUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode. if ( C4::Context->preference("AutoEmailOpacUser") == 1 && $newdata{'userid'} && $newdata{'password'}) { #look for defined primary email address, if blank - attempt to use borr.email and borr.emailpro instead - my $emailaddr; - if (C4::Context->preference("AutoEmailPrimaryAddress") ne 'OFF' && - $newdata{C4::Context->preference("AutoEmailPrimaryAddress")} =~ /\w\@\w/ ) { - $emailaddr = $newdata{C4::Context->preference("AutoEmailPrimaryAddress")} - } - elsif ($newdata{email} =~ /\w\@\w/) { - $emailaddr = $newdata{email} + my @email_addresses; + for my $field ( split /,/, C4::Context->preference("AutoEmailPrimaryAddress") ) { + push @email_addresses, $_ if $newdata{$_} =~ /\w@\w/ } - elsif ($newdata{emailpro} =~ /\w\@\w/) { - $emailaddr = $newdata{emailpro} - } - elsif ($newdata{B_email} =~ /\w\@\w/) { - $emailaddr = $newdata{B_email} + my $emailaddr = join ", ", @email_addresses; + unless ( $emailaddr ) { + for my $field ( qw( email emailpro B_email ) ) { + $emailaddr = $newdata{$field} if $newdata{$field} =~ m|\w\@\w|; + } } # if we manage to find a valid email address, send notice if ($emailaddr) { diff --git a/misc/cronjobs/overdue_notices.pl b/misc/cronjobs/overdue_notices.pl index a1b477c731..638463dcbb 100755 --- a/misc/cronjobs/overdue_notices.pl +++ b/misc/cronjobs/overdue_notices.pl @@ -581,8 +581,6 @@ END_SQL and warn "borrower $borr has items triggering level $i."; @emails_to_use = (); - my $notice_email = - C4::Members::GetNoticeEmailAddress($borrowernumber); unless ($nomail) { if (@emails) { foreach (@emails) { @@ -590,7 +588,9 @@ END_SQL } } else { - push @emails_to_use, $notice_email if ($notice_email); + my $notice_emails = + C4::Members::GetNoticeEmailAddresses($borrowernumber); + push @emails_to_use, @$notice_emails; } } @@ -730,7 +730,7 @@ END_SQL letternumber => $i, postcode => $data->{'zipcode'}, country => $data->{'country'}, - email => $notice_email, + email => ( scalar( @emails_to_use ) ? $emails_to_use[0] : undef ), itemcount => $itemcount, titles => $titles, outputformat => defined $csvfilename ? 'csv' : defined $htmlfilename ? 'html' : defined $text_filename ? 'text' : '', @@ -749,7 +749,7 @@ END_SQL city => $data->{'city'}, postcode => $data->{'zipcode'}, country => $data->{'country'}, - email => $notice_email, + email => ( scalar( @emails_to_use ) ? $emails_to_use[0] : undef ), itemcount => $itemcount, titles => $titles, outputformat => defined $csvfilename ? 'csv' : defined $htmlfilename ? 'html' : defined $text_filename ? 'text' : '', diff --git a/opac/opac-memberentry.pl b/opac/opac-memberentry.pl index 9784a3deef..b8560cd762 100755 --- a/opac/opac-memberentry.pl +++ b/opac/opac-memberentry.pl @@ -182,11 +182,9 @@ if ( $action eq 'create' ) { C4::Letters::EnqueueLetter( { + borrowernumber => $borrower{borrowernumber}, letter => $letter, message_transport_type => 'email', - to_address => $borrower{'email'}, - from_address => - C4::Context->preference('KohaAdminEmailAddress'), } ); } diff --git a/opac/opac-shareshelf.pl b/opac/opac-shareshelf.pl index 89d7dc9d5d..70bdccabaa 100755 --- a/opac/opac-shareshelf.pl +++ b/opac/opac-shareshelf.pl @@ -166,8 +166,9 @@ sub show_accept { sub notify_owner { my ($param) = @_; - my $toaddr = C4::Members::GetNoticeEmailAddress( $param->{owner} ); - return if !$toaddr; + my $toaddr = C4::Members::GetNoticeEmailAddresses( $param->{owner} ); + return unless scalar( @$toaddr ); + my $patron = Koha::Patrons->find( $param->{owner} ); @@ -184,10 +185,9 @@ sub notify_owner { #send letter to queue C4::Letters::EnqueueLetter( { + borrowernumber => $param->{owner}, letter => $letter, message_transport_type => 'email', - from_address => C4::Context->preference('KohaAdminEmailAddress'), - to_address => $toaddr, } ); } @@ -213,7 +213,6 @@ sub process_addrlist { sub send_invitekey { my ($param) = @_; - my $fromaddr = C4::Context->preference('KohaAdminEmailAddress'); my $url = C4::Context->preference('OPACBaseURL') . "/cgi-bin/koha/opac-shareshelf.pl?shelfnumber=" @@ -255,7 +254,6 @@ sub send_invitekey { { letter => $letter, message_transport_type => 'email', - from_address => $fromaddr, to_address => $a, } ); diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index 21206f128b..4e84e6e171 100644 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -18,7 +18,7 @@ # along with Koha; if not, see . use Modern::Perl; -use Test::More tests => 76; +use Test::More tests => 82; use Test::MockModule; use Test::Warn; @@ -64,6 +64,7 @@ my $library = $builder->build({ }); my $patron_category = $builder->build({ source => 'Category' })->{categorycode}; my $date = dt_from_string; +my ( $email, $emailpro ) = ( 'email@example.org', 'emailpro@example.org' ); my $borrowernumber = AddMember( firstname => 'Jane', surname => 'Smith', @@ -71,6 +72,8 @@ my $borrowernumber = AddMember( branchcode => $library->{branchcode}, dateofbirth => $date, smsalertnumber => undef, + email => $email, + emailpro => $emailpro, ); my $marc_record = MARC::Record->new; @@ -98,8 +101,7 @@ my $my_message = { to_address => undef, from_address => 'from@example.com', }; -my $message_id = C4::Letters::EnqueueLetter($my_message); -is( $message_id, undef, 'EnqueueLetter without the letter argument returns undef' ); +is( C4::Letters::EnqueueLetter($my_message), undef, 'EnqueueLetter without the letter argument returns undef' ); delete $my_message->{message_transport_type}; $my_message->{letter} = { @@ -109,13 +111,11 @@ $my_message->{letter} = { code => 'TEST_MESSAGE', content_type => 'text/plain', }; -$message_id = C4::Letters::EnqueueLetter($my_message); -is( $message_id, undef, 'EnqueueLetter without the message type argument argument returns undef' ); +is( C4::Letters::EnqueueLetter($my_message), undef, 'EnqueueLetter without the message_transport_type argument returns undef' ); $my_message->{message_transport_type} = 'sms'; -$message_id = C4::Letters::EnqueueLetter($my_message); -ok(defined $message_id && $message_id > 0, 'new message successfully queued'); - +my $messages_queued = C4::Letters::EnqueueLetter($my_message); +is($messages_queued, 1, 'new message successfully queued'); # GetQueuedMessages my $messages = C4::Letters::GetQueuedMessages(); @@ -123,7 +123,6 @@ is( @$messages, 1, 'GetQueuedMessages without argument returns all the entries' $messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); is( @$messages, 1, 'one message stored for the borrower' ); -is( $messages->[0]->{message_id}, $message_id, 'EnqueueLetter returns the message id correctly' ); is( $messages->[0]->{borrowernumber}, $borrowernumber, 'EnqueueLetter stores the borrower number correctly' ); is( $messages->[0]->{subject}, $my_message->{letter}->{title}, 'EnqueueLetter stores the subject correctly' ); is( $messages->[0]->{content}, $my_message->{letter}->{content}, 'EnqueueLetter stores the content correctly' ); @@ -485,6 +484,64 @@ is($mail{'To'}, 'testemail@mydomain.com', "mailto correct in sent claim"); is($mail{'Message'}, 'my vendor|John Smith|Ordernumber ' . $ordernumber . ' (Silence in the library) (1 ordered)', 'Claim notice text constructed successfully'); } +# EnqueueLetter and AutoEmailPrimaryAddress +$my_message = { + borrowernumber => $borrowernumber, + message_transport_type => 'email', + to_address => 'to@example.com', + from_address => 'from@example.com', + letter => { + content => 'a message', + title => 'message title', + metadata => 'metadata', + code => 'TEST_MESSAGE', + content_type => 'text/plain', + }, +}; + +$dbh->do(q|DELETE FROM message_queue|); +t::lib::Mocks::mock_preference('AutoEmailPrimaryAddress', 'email'); +is( C4::Letters::EnqueueLetter($my_message), 1, 'message successfully queued' ); +$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); +is( scalar( @$messages ), 1, 'The message has been queued for the expected patron' ); +is( $messages->[0]{to_address}, 'to@example.com', 'AutoEmailPrimaryAddress=email, EnqueueLetter used the to_address given in parameter' ); + +$dbh->do(q|DELETE FROM message_queue|); +delete $my_message->{to_address}; +is( C4::Letters::EnqueueLetter($my_message), 1, 'message successfully queued' ); +$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); +is( scalar( @$messages ), 1, 'The message has been queued for the expected patron' ); +is( $messages->[0]{to_address}, $email, 'AutoEmailPrimaryAddress=email, EnqueueLetter used the patron email address if no to_address is given in parameter' ); + +$dbh->do(q|DELETE FROM message_queue|); +t::lib::Mocks::mock_preference('AutoEmailPrimaryAddress', 'emailpro'); +is( C4::Letters::EnqueueLetter($my_message), 1, 'message successfully queued' ); +$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); +is( scalar( @$messages ), 1, 'The message has been queued for the expected patron' ); +is( $messages->[0]{to_address}, $emailpro, 'AutoEmailPrimaryAddress=emailpro, EnqueueLetter used the patron emailpro address if no to_address is given in parameter' ); + +$dbh->do(q|DELETE FROM message_queue|); +t::lib::Mocks::mock_preference('AutoEmailPrimaryAddress', 'OFF'); +is( C4::Letters::EnqueueLetter($my_message), 1, 'message successfully queued' ); +$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); +is( scalar( @$messages ), 1, 'The message has been queued for the expected patron' ); +is( $messages->[0]{to_address}, $email, 'AutoEmailPrimaryAddress=OFF, EnqueueLetter used the patron email address if no to_address is given in parameter' ); + +$dbh->do(q|DELETE FROM message_queue|); +t::lib::Mocks::mock_preference('AutoEmailPrimaryAddress', 'email|OFF|emailpro'); # This is no consistent. OFF should be alone. +is( C4::Letters::EnqueueLetter($my_message), 1, 'message successfully queued' ); +$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); +is( scalar( @$messages ), 1, 'The message has been queued for the expected patron' ); +is( $messages->[0]{to_address}, $email, 'AutoEmailPrimaryAddress=email|OFF|emailpro, EnqueueLetter used the patron email address if no to_address is given in parameter' ); + +$dbh->do(q|DELETE FROM message_queue|); +t::lib::Mocks::mock_preference('AutoEmailPrimaryAddress', 'email|emailpro'); +is( C4::Letters::EnqueueLetter($my_message), 2, 'messages successfully queued' ); +$messages = C4::Letters::GetQueuedMessages({ borrowernumber => $borrowernumber }); +is( scalar( @$messages ), 2, 'The messages have been queued for the expected patron' ); +is( $messages->[0]{to_address}, $email, 'AutoEmailPrimaryAddress=email|emailpro, EnqueueLetter used the patron email address for the first letter' ); +is( $messages->[1]{to_address}, $emailpro, 'AutoEmailPrimaryAddress=email|emailpro, EnqueueLetter used the patron emailpro address for the second letter' ); + { use C4::Serials; @@ -645,7 +702,7 @@ subtest 'SendQueuedMessages' => sub { my $sms_pro = $builder->build_object({ class => 'Koha::SMS::Providers', value => { domain => 'kidclamp.rocks' } }); ModMember( borrowernumber => $borrowernumber, smsalertnumber => '5555555555', sms_provider_id => $sms_pro->id() ); - $message_id = C4::Letters::EnqueueLetter($my_message); #using datas set around line 95 and forward + C4::Letters::EnqueueLetter($my_message); #using datas set around line 95 and forward C4::Letters::SendQueuedMessages(); my $sms_message_address = $schema->resultset('MessageQueue')->search({ borrowernumber => $borrowernumber, diff --git a/t/db_dependent/Members.t b/t/db_dependent/Members.t index a933827a30..57d27f864d 100755 --- a/t/db_dependent/Members.t +++ b/t/db_dependent/Members.t @@ -140,15 +140,23 @@ is ($checkcardnum, "2", "Card number is too long"); t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'OFF' ); C4::Context->clear_syspref_cache(); +my $notice_emails = GetNoticeEmailAddresses($member->{'borrowernumber'}); +is_deeply( $notice_emails, [ $EMAIL ], "GetNoticeEmailAddresses returns correct value when AutoEmailPrimaryAddress is off" ); -my $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'}); -is ($notice_email, $EMAIL, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is off"); +C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'emailpro,OFF' ); +C4::Context->clear_syspref_cache(); +$notice_emails = GetNoticeEmailAddresses($member->{'borrowernumber'}); +is_deeply( $notice_emails, [ $EMAIL ], "GetNoticeEmailAddresses returns correct value when AutoEmailPrimaryAddress contains off" ); t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'emailpro' ); C4::Context->clear_syspref_cache(); +$notice_emails = GetNoticeEmailAddresses($member->{'borrowernumber'}); +is_deeply ($notice_emails, [ $EMAILPRO ], "GetNoticeEmailAddresses returns correct value when AutoEmailPrimaryAddress is emailpro"); -$notice_email = GetNoticeEmailAddress($member->{'borrowernumber'}); -is ($notice_email, $EMAILPRO, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is emailpro"); +C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'emailpro,email' ); +C4::Context->clear_syspref_cache(); +$notice_emails = GetNoticeEmailAddresses($member->{'borrowernumber'}); +is_deeply ($notice_emails, [ $EMAILPRO, $EMAIL ], "GetNoticeEmailAddresses returns correct value when AutoEmailPrimaryAddress is emailpro|email"); # Check_Userid tests %data = ( -- 2.11.0