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 <david@davidnind.com>
Signed-off-by: Lisette Scheer <lisette@bywatersolutions.com>
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Olivia Reynolds 2026-06-09 09:41:48 +00:00 committed by Pedro Amorim
parent cbbfa8c8d2
commit ea67056197
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
2 changed files with 32 additions and 7 deletions

View File

@ -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;

View File

@ -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;