From f751423d155d25b1ba80b4c429191389b46b6bcb Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 12 May 2017 17:13:07 -0400 Subject: [PATCH] Bug 16149: Unit tests To test: prove -v t/db_dependent/Reports/Guided.t Signed-off-by: Claire Gravely Signed-off-by: Jessica Ofsa Signed-off-by: Tomas Cohen Arazi Signed-off-by: Nick Clemens --- t/db_dependent/Reports/Guided.t | 55 ++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Reports/Guided.t b/t/db_dependent/Reports/Guided.t index 827cfc17a6..9c87dcf531 100644 --- a/t/db_dependent/Reports/Guided.t +++ b/t/db_dependent/Reports/Guided.t @@ -18,13 +18,14 @@ use Modern::Perl; -use Test::More tests => 9; +use Test::More tests => 10; use Test::Warn; use t::lib::TestBuilder; use C4::Context; use Koha::Database; use Koha::Reports; +use Koha::Notice::Messages; use_ok('C4::Reports::Guided'); can_ok( @@ -355,6 +356,58 @@ count(h.reservedate) AS 'holds' is( C4::Reports::Guided::convert_sql( $sql ), $expected_converted_sql, "Query with multiple instances of marcxml and biblioitems should have them all replaced"); }; +subtest 'Email report test' => sub { + + plan tests => 8; + + 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 ( $emails, $errors ) = C4::Reports::Guided::EmailReport(); + is( $errors->[0]{FATAL}, 'MISSING_PARAMS', "Need to enter required params"); + + ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report1, module => $letter1->{module}, code => $letter2->{code}}); + is( $errors->[0]{FATAL}, 'NO_LETTER', "Must have a letter that exists"); + + warning_like { ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report2, module => $letter1->{module} , code => $letter1->{code} }) } + qr/^DBD::mysql::st execute failed/, + 'Error from bad report'; + is( $errors->[0]{FATAL}, 'REPORT_FAIL', "Bad report returns failure"); + + ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report1, module => $letter1->{module} , code => $letter1->{code} }); + is( $errors->[0]{NO_FROM_COL} == 1 && $errors->[1]{NO_EMAIL_COL} == 2 && $errors->[2]{NO_FROM_COL} == 2, 1, "Correct warnings from the routine"); + + ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report1, module => $letter1->{module} , code => $letter1->{code}, from => 'the@future.ooh' }); + is( $errors->[0]{NO_EMAIL_COL}, 2, "Warning only for patron with no email"); + + is( $message_count, Koha::Notice::Messages->search({})->count, "Messages not added without commit"); + + ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report1, module => $letter1->{module} , code => $letter1->{code}, from => 'the@future.ooh' }); + is( $emails->[0]{letter}->{content}, "mailer", "Message has expected content"); + +}; + $schema->storage->txn_rollback; sub trim { -- 2.39.5