3 # This file is part of Koha
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.
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.
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>.
20 use Test::More tests => 6;
26 use t::lib::TestBuilder;
28 my $schema = Koha::Database->new->schema;
29 $schema->storage->txn_begin;
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"',
37 my $new_report_2 = Koha::Report->new({
38 report_name => 'report_name_for_test_1',
39 savedsql => 'SELECT "Oops, I did it again"',
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' );
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' );
48 $retrieved_report_1->delete;
49 is( Koha::Reports->search->count, $nb_of_reports + 1, 'Delete should have deleted the report' );
51 subtest 'prep_report' => sub {
54 my $report = Koha::Report->new({
55 report_name => 'report_name_for_test_1',
56 savedsql => 'SELECT * FROM items WHERE itemnumber IN <<Test|list>>',
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');
63 $report->savedsql('SELECT * FROM items WHERE itemnumber IN <<Test|list>> AND <<Another>> AND <<Test|list>>')->store;
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');
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');
73 $schema->storage->txn_rollback;
75 subtest 'is_sql_valid' => sub {
76 plan tests => 3 + 6 * 2;
77 my @badwords = ( 'UPDATE', 'DELETE', 'DROP', 'INSERT', 'SHOW', 'CREATE' );
79 [ Koha::Report->new( { savedsql => '' } )->is_sql_valid ],
80 [ 0, [ { queryerr => 'Missing SELECT' } ] ],
81 'Empty sql is missing SELECT'
84 [ Koha::Report->new( { savedsql => 'FOO' } )->is_sql_valid ],
85 [ 0, [ { queryerr => 'Missing SELECT' } ] ],
86 'Nonsense sql is missing SELECT'
89 [ Koha::Report->new( { savedsql => 'select FOO' } )->is_sql_valid ],
93 foreach my $word (@badwords) {
97 { savedsql => 'select FOO;' . $word . ' BAR' }
100 [ 0, [ { sqlerr => $word } ] ],
101 'select FOO with ' . $word . ' BAR'
105 Koha::Report->new( { savedsql => $word . ' qux' } )
108 [ 0, [ { sqlerr => $word } ] ],