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