From 5bf2e358d1a1e95e82a19ce7c208a72952a6cece Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Tue, 13 Aug 2024 05:13:23 +0000 Subject: [PATCH] Bug 37598: Tests for t/db_dependent/Letters.t To test: 1. Apply patches and update database, restart services 2. Go to Koha administration -> system preferences, search for EmailFieldPrimary 3. Change EmailFieldPrimary to 'selected addresses' 4. Make a selection in the EmailFieldSelection system preference 5. Notice the new EmailRecipientField system preference, it should be set to "To" by default. 6. Have a patron with different addresses in the email fields, depending on the selection you made in EmailFieldSelection: - primary email - alternate email - secondary email 7. Test the new EmailRecipientField system preference using the Welcome email, found by going to the Patron, clicking More, then choosing "Send Welcome notice" 8. In the command line, go to the database `sudo koha-mysql ` and look at the message_queue to see who the emails are sent to `select * from message_queue\G`. The to_address will contain all the selected email addresses. 9. In the command line, run `misc/cronjobs/process_message_queue.pl` 10. Confirm that the to_address and cc_address are set appropriately based on EmailRecipientField 12. Repeat steps 7-9, testing with different values of EmailRecipientField. When testing EmailRecipientField with bcc, confirm that the to_address is just the first valid email address. Unfortunately we can't test bcc without actually sending the email. 13. Confirm tests pass t/db_dependent/Letters.t Sponsored-by: Pymble Ladies' College --- t/db_dependent/Letters.t | 96 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Letters.t b/t/db_dependent/Letters.t index d607fa7d04a..11c052e5c80 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 => 103; +use Test::More tests => 104; use Test::MockModule; use Test::Warn; @@ -75,6 +75,7 @@ $dbh->do(q|DELETE FROM letter|); $dbh->do(q|DELETE FROM message_queue|); $dbh->do(q|DELETE FROM message_transport_types|); t::lib::Mocks::mock_preference( 'EmailFieldPrimary', 'OFF' ); +t::lib::Mocks::mock_preference( 'EmailRecipientField', 'to' ); my $library = $builder->build({ source => 'Branch', @@ -1582,3 +1583,96 @@ subtest 'Quote user params in GetPreparedLetter' => sub { $exec_time ); }; + +subtest 'Tests for EmailRecipientField' => sub { + plan tests => 11; + + Koha::Notice::Messages->delete; + + t::lib::Mocks::mock_preference( 'EmailRecipientField', 'to' ); + t::lib::Mocks::mock_preference( 'EmailFieldPrimary', 'OFF' ); + + my $library = $builder->build_object( + { + class => 'Koha::Libraries', + value => { + branchemail => 'email@library.com', branchreplyto => 'reply@library.com', + branchreturnpath => 'return@library.com' + } + } + ); + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { + email => 'primary@test.com', emailpro => 'secondary@test.com', B_email => 'alternate@test.com', + branchcode => $library->id + } + } + ); + + my $my_message = { + 'letter' => { + 'content' => '

a message

', + 'metadata' => 'metadata', + 'code' => 'TEST_MESSAGE', + 'content_type' => 'text/html; charset="UTF-8"', + 'title' => 'message title' + }, + 'borrowernumber' => $patron->id, + 'to_address' => undef, + 'message_transport_type' => 'email', + 'from_address' => 'from@example.com' + }; + + my $message_1_id = C4::Letters::EnqueueLetter($my_message); + + C4::Letters::SendQueuedMessages({ message_id => $message_1_id }); + + my $message_1 = Koha::Notice::Messages->find($message_1_id)->unblessed; + is( $message_1->{status}, 'sent', 'Message sent' ); + is( + $message_1->{to_address}, $patron->notice_email_address, + 'A to_address was generated in the sending of the message' + ); + + t::lib::Mocks::mock_preference( 'EmailFieldPrimary', 'MULTI' ); + t::lib::Mocks::mock_preference( 'EmailFieldSelection', 'B_email,email,emailpro' ); + + my $message_2_id = C4::Letters::EnqueueLetter($my_message); + + C4::Letters::SendQueuedMessages({ message_id => $message_2_id }); + + my $message_2 = Koha::Notice::Messages->find($message_2_id)->unblessed; + is( $message_2->{status}, 'sent', 'Message sent' ); + my $message_2_to_address = $message_2->{to_address}; + $message_2_to_address =~ s/\s+//g; + is( + $message_2_to_address, $patron->notice_email_address, + 'A to_address was generated in the sending of the message and uses the multiple selected addresses' + ); + + t::lib::Mocks::mock_preference( 'EmailRecipientField', 'cc' ); + + my $message_3_id = C4::Letters::EnqueueLetter($my_message); + + C4::Letters::SendQueuedMessages({ message_id => $message_3_id }); + + my $message_3 = Koha::Notice::Messages->find($message_3_id)->unblessed; + is( $message_3->{status}, 'sent', 'Message sent' ); + my $message_3_cc_address = $message_3->{cc_address}; + $message_3_cc_address =~ s/\s+//g; + is( + $message_3_cc_address, $patron->notice_email_address, + 'A cc_address was generated in the sending of the message and uses the multiple selected addresses' + ); + + t::lib::Mocks::mock_preference( 'EmailRecipientField', 'bcc' ); + + my $message_4_id = C4::Letters::EnqueueLetter($my_message); + + C4::Letters::SendQueuedMessages({ message_id => $message_4_id }); + + my $message_4 = Koha::Notice::Messages->find($message_4_id)->unblessed; + is( $message_4->{status}, 'sent', 'Unable to specifically test BCC because this is only included in the email itself, so just confirm the message sent' ); +}; -- 2.39.2