From 9dc1852d8fbb65294c5d725bfdb8df89485e99f1 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 20 Sep 2023 15:09:26 +0100 Subject: [PATCH] Bug 12532: (follow-up) Catch more cases in tests This patch added lots more cases to the unit tests to try and catch all the new functionality. Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Letters.t | 78 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 7 deletions(-) diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index e6eab1a167..91f4dee2d3 100755 --- a/t/db_dependent/Letters.t +++ b/t/db_dependent/Letters.t @@ -982,7 +982,7 @@ subtest 'Test SMS handling in SendQueuedMessages' => sub { subtest 'Test guarantor handling in SendQueuedMessages' => sub { - plan tests => 7; + plan tests => 19; t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test' ); @@ -1007,9 +1007,20 @@ subtest 'Test guarantor handling in SendQueuedMessages' => sub { }; $message_id = C4::Letters::EnqueueLetter($my_message); + # feature disabled + t::lib::Mocks::mock_preference( 'RedirectGuaranteeEmail', '0' ); + + warning_like { C4::Letters::SendQueuedMessages(); } + qr|No 'to_address', email address or guarantors email address for borrowernumber|, + "SendQueuedMessages fails when no to_address, patron notice email and RedirectGuaranteeEmail is not set"; + # feature enabled t::lib::Mocks::mock_preference( 'RedirectGuaranteeEmail', '1' ); + # reset message - testing without to or borrower valid email + Koha::Notice::Messages->find($message_id)->delete; + $message_id = C4::Letters::EnqueueLetter($my_message); + warning_like { C4::Letters::SendQueuedMessages(); } qr|Fake send_or_die|, "SendQueuedMessages is using the mocked send_or_die routine"; @@ -1036,16 +1047,69 @@ subtest 'Test guarantor handling in SendQueuedMessages' => sub { is( $email_object->email->header('To'), $guarantor1->email, "mailto correctly uses first guarantor" ); is( $email_object->email->header('Cc'), $guarantor2->email, "cc correctly uses second guarantor" ); - # feature disabled - t::lib::Mocks::mock_preference( 'RedirectGuaranteeEmail', '0' ); - - # reset message + # reset message - testing borrower with valid email Koha::Notice::Messages->find($message_id)->delete; + $message_id = C4::Letters::EnqueueLetter($my_message); + + $patron->email('patron@example.com')->store(); + + warning_like { C4::Letters::SendQueuedMessages(); } + qr|Fake send_or_die|, + "SendQueuedMessages is using the mocked send_or_die routine"; + + $message = $schema->resultset('MessageQueue')->search( + { + borrowernumber => $borrowernumber, + status => 'sent' + } + )->next(); + + is( + $message->to_address(), + $patron->email, + 'SendQueuedMessages uses patron email when defined' + ); + + is( + $message->cc_address(), + $guarantor1->email.",".$guarantor2->email, + 'SendQueuedMessages sets cc address to both guarantor emails when patron has email defined' + ); + + is( $email_object->email->header('To'), $patron->email, "mailto correctly uses patrons email address" ); + is( $email_object->email->header('Cc'), $guarantor1->email.", ".$guarantor2->email, "cc correctly uses both guarantors" ); + + # reset message - testing explicit to passed to enqueue + Koha::Notice::Messages->find($message_id)->delete; + $my_message->{'to_address'} = 'to@example.com'; $message_id = C4::Letters::EnqueueLetter($my_message); + warning_like { C4::Letters::SendQueuedMessages(); } - qr|No 'to_address', email address or guarantors email address for borrowernumber|, - "SendQueuedMessages fails when no to_address, patron notice email and RedirectGuaranteeEmail is not set"; + qr|Fake send_or_die|, + "SendQueuedMessages is using the mocked send_or_die routine"; + + $message = $schema->resultset('MessageQueue')->search( + { + borrowernumber => $borrowernumber, + status => 'sent' + } + )->next(); + + is( + $message->to_address(), + 'to@example.com', + 'SendQueuedMessages uses to_address if it was specified at enqueue time' + ); + + is( + $message->cc_address(), + $guarantor1->email.",".$guarantor2->email, + 'SendQueuedMessages sets cc address to both guarantor emails when "to" is already specified' + ); + + is( $email_object->email->header('To'), 'to@example.com', "mailto correctly uses passed email" ); + is( $email_object->email->header('Cc'), $guarantor1->email.", ".$guarantor2->email, "cc correctly uses both guarantors" ); # clear borrower queue Koha::Notice::Messages->find($message_id)->delete; -- 2.39.5