From 21948f87f45c6914c45ccc26b20af1cb66778e56 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 7 Jan 2026 15:48:56 +0000 Subject: [PATCH] Bug 40934: Add tests for exclude_letter_code in process_message_queue.pl This patch adds unit tests for the new exclude_letter_code functionality in SendQueuedMessages, which will allow excluding specific letter codes when processing the message queue. Test coverage includes: - Excluding a single letter code (array format) - Excluding multiple letter codes - Excluding with scalar parameter format Sponsored-by: OpenFifth --- t/db_dependent/Letters.t | 128 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index f248a07f7f5..67a2c69b38a 100755 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -19,7 +19,7 @@ use Modern::Perl; use File::Basename qw(dirname); -use Test::More tests => 105; +use Test::More tests => 106; use Test::NoWarnings; use Test::MockModule; @@ -1825,4 +1825,130 @@ subtest 'Virtual method ->strftime in notices' => sub { is( $get_letter->()->{content}, $expected_output, 'Check generated content for us dateformat' ); }; +subtest 'Test exclude_letter_code parameter for SendQueuedMessages' => sub { + plan tests => 10; + + my $dbh = C4::Context->dbh; + + my $borrowernumber = Koha::Patron->new( + { + firstname => 'Jane', + surname => 'Smith', + categorycode => $patron_category, + branchcode => $library->{branchcode}, + dateofbirth => $date, + smsalertnumber => '5555555555', + } + )->store->borrowernumber; + + $dbh->do(q|DELETE FROM message_queue|); + + # Create messages with different letter codes + my $message_digest1 = { + 'letter' => { + 'content' => 'digest message 1', + 'metadata' => 'metadata', + 'code' => 'DUEDGST', + 'content_type' => 'text/plain', + 'title' => 'digest title' + }, + 'borrowernumber' => $borrowernumber, + 'to_address' => undef, + 'message_transport_type' => 'sms', + 'from_address' => 'from@example.com' + }; + my $message_digest2 = { + 'letter' => { + 'content' => 'digest message 2', + 'metadata' => 'metadata', + 'code' => 'PREDUEDGST', + 'content_type' => 'text/plain', + 'title' => 'predigest title' + }, + 'borrowernumber' => $borrowernumber, + 'to_address' => undef, + 'message_transport_type' => 'sms', + 'from_address' => 'from@example.com' + }; + my $message_regular = { + 'letter' => { + 'content' => 'regular message', + 'metadata' => 'metadata', + 'code' => 'ACQ_NOTIF', + 'content_type' => 'text/plain', + 'title' => 'regular title' + }, + 'borrowernumber' => $borrowernumber, + 'to_address' => undef, + 'message_transport_type' => 'sms', + 'from_address' => 'from@example.com' + }; + my $message_regular2 = { + 'letter' => { + 'content' => 'another regular message', + 'metadata' => 'metadata', + 'code' => 'TEST_MESSAGE', + 'content_type' => 'text/plain', + 'title' => 'regular title 2' + }, + 'borrowernumber' => $borrowernumber, + 'to_address' => undef, + 'message_transport_type' => 'sms', + 'from_address' => 'from@example.com' + }; + + my @id = ( + C4::Letters::EnqueueLetter($message_digest1), + C4::Letters::EnqueueLetter($message_digest2), + C4::Letters::EnqueueLetter($message_regular), + C4::Letters::EnqueueLetter($message_regular2), + ); + + # Test excluding single letter code (array) + C4::Letters::SendQueuedMessages( + { + exclude_letter_code => ['DUEDGST'], + type => 'sms', + } + ); + + is( Koha::Notice::Messages->find( $id[0] )->status, 'pending', 'DUEDGST message excluded, still pending' ); + is( Koha::Notice::Messages->find( $id[1] )->status, 'sent', 'PREDUEDGST message processed' ); + is( Koha::Notice::Messages->find( $id[2] )->status, 'sent', 'ACQ_NOTIF message processed' ); + is( Koha::Notice::Messages->find( $id[3] )->status, 'sent', 'TEST_MESSAGE message processed' ); + + # Reset messages to pending + Koha::Notice::Messages->find( $id[1] )->update( { status => 'pending' } ); + Koha::Notice::Messages->find( $id[2] )->update( { status => 'pending' } ); + Koha::Notice::Messages->find( $id[3] )->update( { status => 'pending' } ); + + # Test excluding multiple letter codes + C4::Letters::SendQueuedMessages( + { + exclude_letter_code => [ 'DUEDGST', 'PREDUEDGST' ], + type => 'sms', + } + ); + + is( Koha::Notice::Messages->find( $id[0] )->status, 'pending', 'DUEDGST message excluded, still pending' ); + is( Koha::Notice::Messages->find( $id[1] )->status, 'pending', 'PREDUEDGST message excluded, still pending' ); + is( Koha::Notice::Messages->find( $id[2] )->status, 'sent', 'ACQ_NOTIF message processed' ); + is( Koha::Notice::Messages->find( $id[3] )->status, 'sent', 'TEST_MESSAGE message processed' ); + + # Reset messages to pending + Koha::Notice::Messages->find( $id[2] )->update( { status => 'pending' } ); + Koha::Notice::Messages->find( $id[3] )->update( { status => 'pending' } ); + + # Test with scalar exclude_letter_code + C4::Letters::SendQueuedMessages( + { + exclude_letter_code => 'TEST_MESSAGE', + type => 'sms', + } + ); + + is( Koha::Notice::Messages->find( $id[2] )->status, 'sent', 'ACQ_NOTIF message processed' ); + is( Koha::Notice::Messages->find( $id[3] )->status, 'pending', 'TEST_MESSAGE message excluded, still pending' ); +}; + $schema->storage->txn_rollback; -- 2.52.0