@@ -, +, @@ --- t/db_dependent/Reports/Guided.t | 59 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) --- a/t/db_dependent/Reports/Guided.t +++ a/t/db_dependent/Reports/Guided.t @@ -18,12 +18,13 @@ use Modern::Perl; -use Test::More tests => 8; +use Test::More tests => 9; use Test::Warn; use t::lib::TestBuilder; use C4::Context; use Koha::Database; +use Koha::Notice::Messages; use_ok('C4::Reports::Guided'); can_ok( @@ -288,6 +289,62 @@ subtest 'Ensure last_run is populated' => sub { isnt( $report->last_run, $previous_last_run, 'Second run of report updates last_run' ); }; +subtest 'Email report test' => sub { + + plan tests => 9; + + my $id1 = $builder->build({ source => 'Borrower',value => { surname => 'mailer', email => 'a@b.com' } })->{ borrowernumber }; + my $id2 = $builder->build({ source => 'Borrower',value => { surname => 'nomailer', email => undef } })->{ borrowernumber }; + my $report1 = $builder->build({ source => 'SavedSql', value => { savedsql => "SELECT surname,borrowernumber,email FROM borrowers WHERE borrowernumber IN ($id1,$id2)" } })->{ id }; + my $report2 = $builder->build({ source => 'SavedSql', value => { savedsql => "SELECT potato FROM mashed" } })->{ id }; + + my $letter1 = $builder->build({ + source => 'Letter', + value => { + content => "[% surname %]", + branchcode => "", + message_transport_type => 'email' + } + }); + my $letter2 = $builder->build({ + source => 'Letter', + value => { + content => "[% firstname %]", + branchcode => "", + message_transport_type => 'email' + } + }); + + my $message_count = Koha::Notice::Messages->search({})->count; + + my @result = C4::Reports::Guided::EmailReport(); + is( $result[0]{FATAL}, 'MISSING_PARAMS', "Need to enter required params"); + + @result = C4::Reports::Guided::EmailReport({report => $report1, module => $letter1->{module}, code => $letter2->{code}}); + is( $result[0]{FATAL}, 'NO_LETTER', "Must have a letter that exists"); + + warning_like { @result = C4::Reports::Guided::EmailReport({report => $report2, module => $letter1->{module} , code => $letter1->{code} }) } + qr/^DBD::mysql::st execute failed/, + 'Error from bad report'; + is( $result[0]{FATAL}, 'REPORT_FAIL', "Bad report returns failure"); + + @result = C4::Reports::Guided::EmailReport({report => $report1, module => $letter1->{module} , code => $letter1 ->{code} }); + is( $result[0]{NO_FROM_COL} == 0 && $result[1]{NO_EMAIL_COL} == 1 && $result[2]{NO_FROM_COL} == 1, 1, "Correct warnings from the routine"); + + @result = C4::Reports::Guided::EmailReport({report => $report1, module => $letter1->{module} , code => $letter1 ->{code}, from => 'the@future.ooh' }); + is( $result[0]{NO_EMAIL_COL}, 1, "Warning only for patron with no email"); + + is( $message_count, Koha::Notice::Messages->search({})->count, "Messages not added without commit"); + + @result = C4::Reports::Guided::EmailReport({report => $report1, module => $letter1->{module} , code => $letter1 ->{code}, from => 'the@future.ooh', commit => 1 }); + + my $notices = Koha::Notice::Messages->search( undef,{ order_by => { -desc => 'message_id' } } ); + is( $notices->count, $message_count+1, "Message added with commit option"); + + is( $notices->next()->content, "mailer", "Message has expected content"); + +}; + $schema->storage->txn_rollback; sub trim { --