Bug 15756: Some tests for haspermission in C4::Auth
[koha.git] / t / db_dependent / Reports_Guided.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 => 18;
21 use Test::Warn;
22 use t::lib::TestBuilder;
23
24 use C4::Context;
25 use Koha::Database;
26
27 BEGIN {
28     use_ok('C4::Reports::Guided');
29 }
30 can_ok(
31     'C4::Reports::Guided',
32     qw(save_report delete_report execute_query)
33 );
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37 my $builder = t::lib::TestBuilder->new;
38
39 my $dbh = C4::Context->dbh;
40 $dbh->do(q|DELETE FROM saved_sql|);
41 $dbh->do(q|DELETE FROM saved_reports|);
42
43 #Start tests
44
45 #Test save_report
46 my $count = scalar( @{ get_saved_reports() } );
47 is( $count, 0, "There is no report" );
48
49 my @report_ids;
50 foreach ( 1..3 ) {
51     my $id = $builder->build({ source => 'Borrower' })->{ borrowernumber };
52     push @report_ids, save_report({
53         borrowernumber => $id,
54         sql            => "SQL$id",
55         name           => "Name$id",
56         area           => "area$id",
57         group          => "group$id",
58         subgroup       => "subgroup$id",
59         type           => "type$id",
60         notes          => "note$id",
61         cache_expiry   => "null",
62         public         => "null"
63     });
64     $count++;
65 }
66 like( $report_ids[0], '/^\d+$/', "Save_report returns an id for first" );
67 like( $report_ids[1], '/^\d+$/', "Save_report returns an id for second" );
68 like( $report_ids[2], '/^\d+$/', "Save_report returns an id for third" );
69
70 is( scalar( @{ get_saved_reports() } ),
71     $count, "$count reports have been added" );
72
73 is( scalar( @{ get_saved_reports( $report_ids[0] ) } ),
74     1, "filter takes report id" );
75
76 #Test delete_report
77 is (delete_report(),undef, "Without id delete_report returns undef");
78
79 is( delete_report( $report_ids[0] ), 1, "report 1 is deleted" );
80 $count--;
81
82 is( scalar( @{ get_saved_reports() } ), $count, "Report1 has been deleted" );
83
84 is( delete_report( $report_ids[1], $report_ids[2] ), 2, "report 2 and 3 are deleted" );
85 $count -= 2;
86
87 is( scalar( @{ get_saved_reports() } ),
88     $count, "Report2 and report3 have been deleted" );
89
90 my $sth = execute_query('SELECT COUNT(*) FROM systempreferences', 0, 10);
91 my $results = $sth->fetchall_arrayref;
92 is(scalar(@$results), 1, 'running a query returned a result');
93
94 my $version = C4::Context->preference('Version');
95 $sth = execute_query(
96     'SELECT value FROM systempreferences WHERE variable = ?',
97     0,
98     10,
99     [ 'Version' ],
100 );
101 $results = $sth->fetchall_arrayref;
102 is_deeply(
103     $results,
104     [ [ $version ] ],
105     'running a query with a parameter returned the expected result'
106 );
107
108 # for next test, we want to let execute_query capture any SQL errors
109 $dbh->{RaiseError} = 0;
110 my $errors;
111 warning_like { ($sth, $errors) = execute_query(
112         'SELECT surname FRM borrowers',  # error in the query is intentional
113         0, 10 ) }
114         qr/^DBD::mysql::st execute failed: You have an error in your SQL syntax;/,
115         "Wrong SQL syntax raises warning";
116 ok(
117     defined($errors) && exists($errors->{queryerr}),
118     'attempting to run a report with an SQL syntax error returns error message (Bug 12214)'
119 );
120
121 is_deeply( get_report_areas(), [ 'CIRC', 'CAT', 'PAT', 'ACQ', 'ACC', 'SER' ],
122     "get_report_areas returns the correct array of report areas");
123
124 $schema->storage->txn_rollback;