Bug 33214: (follow-up) Consistency on key names
[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     my $id = $report->id;
59
60     my ($sql, undef) = $report->prep_report( ['Test|list'],["1\n12\n\r243"] );
61     is( $sql, qq{SELECT * FROM items WHERE itemnumber IN ('1','12','243') /* saved_sql.id: $id */},'Expected sql generated correctly with single param and name');
62
63     $report->savedsql('SELECT * FROM items WHERE itemnumber IN <<Test|list>> AND <<Another>> AND <<Test|list>>')->store;
64
65     ($sql, undef) = $report->prep_report( ['Test|list','Another'],["1\n12\n\r243",'the other'] );
66     is( $sql, qq{SELECT * FROM items WHERE itemnumber IN ('1','12','243') AND 'the other' AND ('1','12','243') /* saved_sql.id: $id */},'Expected sql generated correctly with multiple params and names');
67
68     ($sql, undef) = $report->prep_report( [],["1\n12\n\r243",'the other',"42\n32\n22\n12"] );
69     is( $sql, qq{SELECT * FROM items WHERE itemnumber IN ('1','12','243') AND 'the other' AND ('42','32','22','12') /* saved_sql.id: $id */},'Expected sql generated correctly with multiple params and no names');
70
71 };
72
73 $schema->storage->txn_rollback;
74
75 subtest 'is_sql_valid' => sub {
76     plan tests => 3 + 6 * 2;
77     my @badwords = ( 'UPDATE', 'DELETE', 'DROP', 'INSERT', 'SHOW', 'CREATE' );
78     is_deeply(
79         [ Koha::Report->new( { savedsql => '' } )->is_sql_valid ],
80         [ 0, [ { queryerr => 'Missing SELECT' } ] ],
81         'Empty sql is missing SELECT'
82     );
83     is_deeply(
84         [ Koha::Report->new( { savedsql => 'FOO' } )->is_sql_valid ],
85         [ 0, [ { queryerr => 'Missing SELECT' } ] ],
86         'Nonsense sql is missing SELECT'
87     );
88     is_deeply(
89         [ Koha::Report->new( { savedsql => 'select FOO' } )->is_sql_valid ],
90         [ 1, [] ],
91         'select FOO is good'
92     );
93     foreach my $word (@badwords) {
94         is_deeply(
95             [
96                 Koha::Report->new(
97                     { savedsql => 'select FOO;' . $word . ' BAR' }
98                 )->is_sql_valid
99             ],
100             [ 0, [ { sqlerr => $word } ] ],
101             'select FOO with ' . $word . ' BAR'
102         );
103         is_deeply(
104             [
105                 Koha::Report->new( { savedsql => $word . ' qux' } )
106                   ->is_sql_valid
107             ],
108             [ 0, [ { sqlerr => $word } ] ],
109             $word . ' qux'
110         );
111     }
112   }