From 88fd58dd6c3f12fc3402fa5364bff89cf623d9a0 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 19 May 2026 16:49:35 +0100 Subject: [PATCH] Bug 42645: Add tests for FallbackToSMSIfNoEmail with alternate email fields Verify that the SMS fallback in ModSuggestion() respects EmailFieldPrimary and EmailFieldPrecedence: a patron with smsalertnumber and an emailpro address (but no borrowers.email) must receive the notice by email, not SMS. Sponsored-by: OpenFifth Signed-off-by: David Nind Signed-off-by: Lisette Scheer Signed-off-by: Pedro Amorim --- t/db_dependent/Suggestions.t | 44 +++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Suggestions.t b/t/db_dependent/Suggestions.t index f071ef2b9f..50e0496c94 100755 --- a/t/db_dependent/Suggestions.t +++ b/t/db_dependent/Suggestions.t @@ -19,7 +19,7 @@ use Modern::Perl; use DateTime::Duration; use Test::NoWarnings; -use Test::More tests => 49; +use Test::More tests => 51; use Test::Warn; use t::lib::Mocks; @@ -285,6 +285,48 @@ is( 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is email if the borrower has an email' ); +# Check that FallbackToSMSIfNoEmail respects notice_email_address, i.e. consults +# EmailFieldPrimary / EmailFieldPrecedence rather than only borrowers.email. +# A patron with no raw email but a populated alternate field must not fall back to SMS. +my $emailpro_patron = Koha::Patron->new( + { + firstname => 'Test', + surname => 'Emailpro', + categorycode => $patron_category->{categorycode}, + branchcode => 'CPL', + smsalertnumber => 99999, + emailpro => 'emailpro@example.com', + } +)->store; +my $suggestion_for_emailpro_patron = Koha::Suggestion->new( + { + title => 'emailpro suggestion', + author => 'test author', + suggestedby => $emailpro_patron->borrowernumber, + biblionumber => $biblio_1->biblionumber, + branchcode => 'CPL', + note => 'test', + } +)->store; + +t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 1 ); +t::lib::Mocks::mock_preference( 'EmailFieldPrimary', 'emailpro' ); +ModSuggestion( { STATUS => 'CHECKED', suggestionid => $suggestion_for_emailpro_patron->id } ); +my $emailpro_messages = C4::Letters::GetQueuedMessages( { borrowernumber => $emailpro_patron->borrowernumber } ); +is( + $emailpro_messages->[0]->{message_transport_type}, 'email', + 'FallbackToSMSIfNoEmail does not fall back to SMS when patron has emailpro set and EmailFieldPrimary=emailpro' +); + +t::lib::Mocks::mock_preference( 'EmailFieldPrimary', '' ); +t::lib::Mocks::mock_preference( 'EmailFieldPrecedence', 'emailpro|email|B_email' ); +ModSuggestion( { STATUS => 'ORDERED', suggestionid => $suggestion_for_emailpro_patron->id } ); +$emailpro_messages = C4::Letters::GetQueuedMessages( { borrowernumber => $emailpro_patron->borrowernumber } ); +is( + $emailpro_messages->[1]->{message_transport_type}, 'email', + 'FallbackToSMSIfNoEmail does not fall back to SMS when patron has emailpro set and EmailFieldPrecedence includes emailpro' +); + # changing STATUS from ORDERED to CHECKED should generate a message my $mod_suggestion5 = { STATUS => 'CHECKED',