From ea670561977db4355eb8ab05e19ee61e3c45c5bd Mon Sep 17 00:00:00 2001 From: Olivia Reynolds Date: Tue, 9 Jun 2026 09:41:48 +0000 Subject: [PATCH] Bug 38621: Use GetPreparedLetter in EmailReport As with guided reports through the admin interface, this patch switches EmailReport to use the same function for processing templates. This gives patron emails sent via cronjobs access to the same variables as other report locations. Test plan: 1. Under Tools > Notices and Slips, create a new notice in the Reports module. Choose a code and a name, and under Email assign a subject and the following body: Date: [% today %] 2. Under Reports, create a new report from SQL with a name and the following query: SELECT 1 AS borrowernumber, 'to@example.com' AS email 3. Run the following command from the base of Koha, replacing CODE with the notice code created earlier, and ID with the report ID created earlier: misc/cronjobs/patron_emailer.pl -module report -from from@example.com -notice CODE -report ID 4. Verify that the report runs successfully, and the console output includes "Date:" on a line with nothing following it. 5. Apply patch. 6. Run the following command from the base of Koha, replacing CODE with the notice code created earlier, and ID with the report ID created earlier: misc/cronjobs/patron_emailer.pl -module report -from from@example.com -notice CODE -report ID 7. Verify that the report runs successfully, and the console output includes "Date:" on a line with the current time following it. 8. Run `prove t/db_dependent/Reports/Guided.t`, and verify it passes. Sponsored-by: Open Fifth Signed-off-by: David Nind Signed-off-by: Lisette Scheer Signed-off-by: Pedro Amorim --- C4/Reports/Guided.pm | 15 +++++++++------ t/db_dependent/Reports/Guided.t | 24 +++++++++++++++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/C4/Reports/Guided.pm b/C4/Reports/Guided.pm index 639608dacc..865d2ce370 100644 --- a/C4/Reports/Guided.pm +++ b/C4/Reports/Guided.pm @@ -58,6 +58,7 @@ use Koha::Report; use Koha::Reports; use Koha::SharedContent; use Koha::TemplateUtils qw( process_tt ); +use C4::Letters qw( GetPreparedLetter ); =head1 NAME @@ -1170,8 +1171,7 @@ sub EmailReport { my ( $sth, $errors ) = execute_query( { sql => $sql, report_id => $report_id } ); return ( undef, [ { FATAL => "REPORT_FAIL" } ] ) if $errors; - my $counter = 1; - my $template = $letter->{content}; + my $counter = 1; while ( my $row = $sth->fetchrow_hashref() ) { my $email; @@ -1184,13 +1184,16 @@ sub EmailReport { my $from_address = $from || $row->{from}; my $to_address = $row->{$email_col}; - push( @errors, { NOT_PARSE => $counter } ) unless my $content = process_tt( $template, $row ); + my $letter = {%$letter}; # GetPreparedLetter mutates, so make a local copy beforehand + push( @errors, { NOT_PARSE => $counter } ) unless my $rendered = GetPreparedLetter( + letter => $letter, + objects => $row + ); $counter++; - next if scalar @errors > $err_count; #If any problems, try next + next if scalar @errors > $err_count; #If any problems, try next - $letter->{content} = $content; $email->{borrowernumber} = $row->{borrowernumber}; - $email->{letter} = {%$letter}; + $email->{letter} = $rendered; $email->{from_address} = $from_address; $email->{to_address} = $to_address; diff --git a/t/db_dependent/Reports/Guided.t b/t/db_dependent/Reports/Guided.t index d09fcc7311..aa196b5631 100755 --- a/t/db_dependent/Reports/Guided.t +++ b/t/db_dependent/Reports/Guided.t @@ -21,6 +21,7 @@ use Modern::Perl; use Test::NoWarnings; +use Test::MockModule qw(strict); use Test::More tests => 14; use Test::Warn; @@ -440,7 +441,7 @@ count(h.reservedate) AS 'holds' subtest 'Email report test' => sub { - plan tests => 14; + plan tests => 15; my $dbh = C4::Context->dbh; my $id1 = $builder->build( @@ -499,6 +500,18 @@ subtest 'Email report test' => sub { } ); + my $letter4 = $builder->build( + { + source => 'Letter', + value => { + content => "[% today %]", + branchcode => "", + message_transport_type => 'email', + is_html => 0 + } + } + ); + my $message_count = Koha::Notice::Messages->search( {} )->count; my ( $emails, $errors ) = C4::Reports::Guided::EmailReport(); @@ -551,6 +564,15 @@ subtest 'Email report test' => sub { { report_id => $report1, module => $letter3->{module}, code => $letter3->{code}, from => 'the@future.ooh' } ); is( $emails->[0]{letter}->{'content-type'}, 'text/html; charset="UTF-8"', "Message has expected content type" ); + { + # Mock formatting dates to fixed output + my $mock = Test::MockModule->new('C4::Letters'); + $mock->redefine( 'output_pref', '1999-09-06 13:23' ); + ($emails) = C4::Reports::Guided::EmailReport( + { report_id => $report1, module => $letter4->{module}, code => $letter4->{code}, from => 'the@future.ooh' } + ); + } + is( $emails->[0]{letter}->{content}, '1999-09-06 13:23', "Message has expected content" ); }; $schema->storage->txn_rollback;