From 1c617cd42bcb57c4fc3306dc53f03c6cfe299f35 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 12 May 2017 17:13:07 -0400 Subject: [PATCH] [SIGNED-OFF] Bug 16149 - Unit tests To test: prove -v t/db_dependent/Reports/Guided.t Signed-off-by: Claire Gravely --- t/db_dependent/Reports/Guided.t | 59 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Reports/Guided.t b/t/db_dependent/Reports/Guided.t index 827cfc1..7b1ceca 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,62 @@ 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 => 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} == 1 && $result[1]{NO_EMAIL_COL} == 2 && $result[2]{NO_FROM_COL} == 2, 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}, 2, "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 { -- 2.1.4