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;