Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Koha / Reports.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 6;
21
22 use Koha::Report;
23 use Koha::Reports;
24 use Koha::Database;
25
26 use t::lib::TestBuilder;
27
28 my $schema = Koha::Database->new->schema;
29 $schema->storage->txn_begin;
30
31 my $builder = t::lib::TestBuilder->new;
32 my $nb_of_reports = Koha::Reports->search->count;
33 my $new_report_1 = Koha::Report->new({
34     report_name => 'report_name_for_test_1',
35     savedsql => 'SELECT "I wrote a report"',
36 })->store;
37 my $new_report_2 = Koha::Report->new({
38     report_name => 'report_name_for_test_1',
39     savedsql => 'SELECT "Oops, I did it again"',
40 })->store;
41
42 like( $new_report_1->id, qr|^\d+$|, 'Adding a new report should have set the id');
43 is( Koha::Reports->search->count, $nb_of_reports + 2, 'The 2 reports should have been added' );
44
45 my $retrieved_report_1 = Koha::Reports->find( $new_report_1->id );
46 is( $retrieved_report_1->report_name, $new_report_1->report_name, 'Find a report by id should return the correct report' );
47
48 $retrieved_report_1->delete;
49 is( Koha::Reports->search->count, $nb_of_reports + 1, 'Delete should have deleted the report' );
50
51 subtest 'prep_report' => sub {
52     plan tests => 3;
53
54     my $report = Koha::Report->new({
55         report_name => 'report_name_for_test_1',
56         savedsql => 'SELECT * FROM items WHERE itemnumber IN <<Test|list>>',
57     })->store;
58
59     my ($sql, undef) = $report->prep_report( ['Test|list'],["1\n12\n\r243"] );
60     is( $sql, q{SELECT * FROM items WHERE itemnumber IN ('1','12','243')},'Expected sql generated correctly with single param and name');
61
62     $report->savedsql('SELECT * FROM items WHERE itemnumber IN <<Test|list>> AND <<Another>> AND <<Test|list>>')->store;
63
64     ($sql, undef) = $report->prep_report( ['Test|list','Another'],["1\n12\n\r243",'the other'] );
65     is( $sql, q{SELECT * FROM items WHERE itemnumber IN ('1','12','243') AND 'the other' AND ('1','12','243')},'Expected sql generated correctly with multiple params and names');
66
67     ($sql, undef) = $report->prep_report( [],["1\n12\n\r243",'the other',"42\n32\n22\n12"] );
68     is( $sql, q{SELECT * FROM items WHERE itemnumber IN ('1','12','243') AND 'the other' AND ('42','32','22','12')},'Expected sql generated correctly with multiple params and no names');
69
70 };
71
72 $schema->storage->txn_rollback;
73
74 subtest 'is_sql_valid' => sub {
75     plan tests => 3 + 6 * 2;
76     my @badwords = ( 'UPDATE', 'DELETE', 'DROP', 'INSERT', 'SHOW', 'CREATE' );
77     is_deeply(
78         [ Koha::Report->new( { savedsql => '' } )->is_sql_valid ],
79         [ 0, [ { queryerr => 'Missing SELECT' } ] ],
80         'Empty sql is missing SELECT'
81     );
82     is_deeply(
83         [ Koha::Report->new( { savedsql => 'FOO' } )->is_sql_valid ],
84         [ 0, [ { queryerr => 'Missing SELECT' } ] ],
85         'Nonsense sql is missing SELECT'
86     );
87     is_deeply(
88         [ Koha::Report->new( { savedsql => 'select FOO' } )->is_sql_valid ],
89         [ 1, [] ],
90         'select FOO is good'
91     );
92     foreach my $word (@badwords) {
93         is_deeply(
94             [
95                 Koha::Report->new(
96                     { savedsql => 'select FOO;' . $word . ' BAR' }
97                 )->is_sql_valid
98             ],
99             [ 0, [ { sqlerr => $word } ] ],
100             'select FOO with ' . $word . ' BAR'
101         );
102         is_deeply(
103             [
104                 Koha::Report->new( { savedsql => $word . ' qux' } )
105                   ->is_sql_valid
106             ],
107             [ 0, [ { sqlerr => $word } ] ],
108             $word . ' qux'
109         );
110     }
111   }