1 # Copyright 2012 Catalyst IT Ltd.
2 # Copyright 2015 Koha Development team
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Test::More tests => 9;
24 use t::lib::TestBuilder;
28 use_ok('C4::Reports::Guided');
30 'C4::Reports::Guided',
31 qw(save_report delete_report execute_query)
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
38 subtest 'strip_limit' => sub {
39 # This is the query I found that triggered bug 8594.
40 my $sql = "SELECT aqorders.ordernumber, biblio.title, biblio.biblionumber, items.homebranch,
41 aqorders.entrydate, aqorders.datereceived,
42 (SELECT DATE(datetime) FROM statistics
43 WHERE itemnumber=items.itemnumber AND
44 (type='return' OR type='issue') LIMIT 1)
47 (SELECT DATE(datetime) FROM statistics
48 WHERE itemnumber=items.itemnumber AND
49 (type='return' OR type='issue') LIMIT 1),
50 aqorders.datereceived), aqorders.entrydate) AS totaldays
52 LEFT JOIN biblio USING (biblionumber)
53 LEFT JOIN items ON (items.biblionumber = biblio.biblionumber
54 AND dateaccessioned=aqorders.datereceived)
55 WHERE (entrydate >= '2011-01-01' AND (datereceived < '2011-02-01' OR datereceived IS NULL))
56 AND items.homebranch LIKE 'INFO'
59 my ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql);
60 is($res_sql, $sql, "Not breaking subqueries");
61 is($res_lim1, 0, "Returns correct default offset");
62 is($res_lim2, undef, "Returns correct default LIMIT");
64 # Now the same thing, but we want it to remove the LIMIT from the end
66 my $test_sql = $res_sql . " LIMIT 242";
67 ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
68 # The replacement drops a ' ' where the limit was
69 is(trim($res_sql), $sql, "Correctly removes only final LIMIT");
70 is($res_lim1, 0, "Returns correct default offset");
71 is($res_lim2, 242, "Returns correct extracted LIMIT");
73 $test_sql = $res_sql . " LIMIT 13,242";
74 ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
75 # The replacement drops a ' ' where the limit was
76 is(trim($res_sql), $sql, "Correctly removes only final LIMIT (with offset)");
77 is($res_lim1, 13, "Returns correct extracted offset");
78 is($res_lim2, 242, "Returns correct extracted LIMIT");
80 # After here is the simpler case, where there isn't a WHERE clause to worry
83 # First case with nothing to change
84 $sql = "SELECT * FROM items";
85 ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql);
86 is($res_sql, $sql, "Not breaking simple queries");
87 is($res_lim1, 0, "Returns correct default offset");
88 is($res_lim2, undef, "Returns correct default LIMIT");
90 $test_sql = $sql . " LIMIT 242";
91 ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
92 is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case");
93 is($res_lim1, 0, "Returns correct default offset");
94 is($res_lim2, 242, "Returns correct extracted LIMIT");
96 $test_sql = $sql . " LIMIT 13,242";
97 ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
98 is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case (with offset)");
99 is($res_lim1, 13, "Returns correct extracted offset");
100 is($res_lim2, 242, "Returns correct extracted LIMIT");
103 $_->delete for Koha::AuthorisedValues->search({ category => 'XXX' });
104 Koha::AuthorisedValue->new({category => 'LOC'})->store;
106 subtest 'GetReservedAuthorisedValues' => sub {
108 # This one will catch new reserved words not added
109 # to GetReservedAuthorisedValues
116 'biblio_framework' => 1,
119 my $reserved_authorised_values = GetReservedAuthorisedValues();
120 is_deeply(\%test_authval, $reserved_authorised_values,
121 'GetReservedAuthorisedValues returns a fixed list');
124 subtest 'IsAuthorisedValueValid' => sub {
126 ok( IsAuthorisedValueValid('LOC'),
127 'User defined authorised value category is valid');
129 ok( ! IsAuthorisedValueValid('XXX'),
130 'Not defined authorised value category is invalid');
132 # Loop through the reserved authorised values
133 foreach my $authorised_value ( keys %{GetReservedAuthorisedValues()} ) {
134 ok( IsAuthorisedValueValid($authorised_value),
135 '\''.$authorised_value.'\' is a reserved word, and thus a valid authorised value');
139 subtest 'GetParametersFromSQL+ValidateSQLParameters' => sub {
144 WHERE YEAR(timestamp) = <<Year|custom_list>> AND
145 branchcode = <<Branch|branches>> AND
146 borrowernumber = <<Borrower>>
149 my @test_parameters_with_custom_list = (
150 { 'name' => 'Year', 'authval' => 'custom_list' },
151 { 'name' => 'Branch', 'authval' => 'branches' },
152 { 'name' => 'Borrower', 'authval' => undef }
155 is_deeply( GetParametersFromSQL($test_query_1), \@test_parameters_with_custom_list,
156 'SQL params are correctly parsed');
158 my @problematic_parameters = ();
159 push @problematic_parameters, { 'name' => 'Year', 'authval' => 'custom_list' };
160 is_deeply( ValidateSQLParameters( $test_query_1 ),
161 \@problematic_parameters,
162 '\'custom_list\' not a valid category' );
167 WHERE YEAR(timestamp) = <<Year|date>> AND
168 branchcode = <<Branch|branches>> AND
169 borrowernumber = <<Borrower|LOC>>
172 is_deeply( ValidateSQLParameters( $test_query_2 ),
174 'All parameters valid, empty problematic authvals list'
178 subtest 'get_saved_reports' => sub {
180 my $dbh = C4::Context->dbh;
181 $dbh->do(q|DELETE FROM saved_sql|);
182 $dbh->do(q|DELETE FROM saved_reports|);
185 my $count = scalar @{ get_saved_reports() };
186 is( $count, 0, "There is no report" );
190 my $id = $builder->build({ source => 'Borrower' })->{ borrowernumber };
191 push @report_ids, save_report({
192 borrowernumber => $id,
197 subgroup => "subgroup$id",
200 cache_expiry => "null",
205 like( $report_ids[0], '/^\d+$/', "Save_report returns an id for first" );
206 like( $report_ids[1], '/^\d+$/', "Save_report returns an id for second" );
207 like( $report_ids[2], '/^\d+$/', "Save_report returns an id for third" );
209 is( scalar @{ get_saved_reports() },
210 $count, "$count reports have been added" );
212 ok( 0 < scalar @{ get_saved_reports( $report_ids[0] ) }, "filter takes report id" );
215 is (delete_report(),undef, "Without id delete_report returns undef");
217 is( delete_report( $report_ids[0] ), 1, "report 1 is deleted" );
220 is( scalar @{ get_saved_reports() }, $count, "Report1 has been deleted" );
222 is( delete_report( $report_ids[1], $report_ids[2] ), 2, "report 2 and 3 are deleted" );
225 is( scalar @{ get_saved_reports() },
226 $count, "Report2 and report3 have been deleted" );
228 my $sth = execute_query('SELECT COUNT(*) FROM systempreferences', 0, 10);
229 my $results = $sth->fetchall_arrayref;
230 is(scalar @$results, 1, 'running a query returned a result');
232 my $version = C4::Context->preference('Version');
233 $sth = execute_query(
234 'SELECT value FROM systempreferences WHERE variable = ?',
239 $results = $sth->fetchall_arrayref;
243 'running a query with a parameter returned the expected result'
246 # for next test, we want to let execute_query capture any SQL errors
247 $dbh->{RaiseError} = 0;
249 warning_like { ($sth, $errors) = execute_query(
250 'SELECT surname FRM borrowers', # error in the query is intentional
252 qr/^DBD::mysql::st execute failed: You have an error in your SQL syntax;/,
253 "Wrong SQL syntax raises warning";
255 defined($errors) && exists($errors->{queryerr}),
256 'attempting to run a report with an SQL syntax error returns error message (Bug 12214)'
259 is_deeply( get_report_areas(), [ 'CIRC', 'CAT', 'PAT', 'ACQ', 'ACC', 'SER' ],
260 "get_report_areas returns the correct array of report areas");
263 subtest 'Ensure last_run is populated' => sub {
266 my $rs = Koha::Database->new()->schema()->resultset('SavedSql');
268 my $report = $rs->new(
270 report_name => 'Test Report',
271 savedsql => 'SELECT * FROM branches',
276 is( $report->last_run, undef, 'Newly created report has null last_run ' );
278 execute_query( $report->savedsql, undef, undef, undef, $report->id );
279 $report->discard_changes();
281 isnt( $report->last_run, undef, 'First run of report populates last_run' );
283 my $previous_last_run = $report->last_run;
284 sleep(1); # last_run is stored to the second, so we need to ensure at least one second has passed between runs
285 execute_query( $report->savedsql, undef, undef, undef, $report->id );
286 $report->discard_changes();
288 isnt( $report->last_run, $previous_last_run, 'Second run of report updates last_run' );
291 subtest 'convert_sql' => sub {
295 SELECT biblionumber, ExtractValue(marcxml,
296 'count(//datafield[@tag="505"])') AS count505
298 HAVING count505 > 1|;
299 my $expected_converted_sql = q|
300 SELECT biblionumber, ExtractValue(metadata,
301 'count(//datafield[@tag="505"])') AS count505
303 HAVING count505 > 1|;
305 is( C4::Reports::Guided::convert_sql( $sql ), $expected_converted_sql, "Simple query should have been correctly converted");
308 SELECT biblionumber, substring(
309 ExtractValue(marcxml,'//controlfield[@tag="008"]'), 8,4 ) AS 'PUB DATE',
312 INNER JOIN biblio USING (biblionumber)
313 WHERE biblionumber = 14|;
315 $expected_converted_sql = q|
316 SELECT biblionumber, substring(
317 ExtractValue(metadata,'//controlfield[@tag="008"]'), 8,4 ) AS 'PUB DATE',
320 INNER JOIN biblio USING (biblionumber)
321 WHERE biblionumber = 14|;
322 is( C4::Reports::Guided::convert_sql( $sql ), $expected_converted_sql, "Query with biblio info should have been correctly converted");
325 SELECT concat(b.title, ' ', ExtractValue(m.marcxml,
326 '//datafield[@tag="245"]/subfield[@code="b"]')) AS title, b.author,
327 count(h.reservedate) AS 'holds'
329 LEFT JOIN biblioitems m USING (biblionumber)
330 LEFT JOIN reserves h ON (b.biblionumber=h.biblionumber)
331 GROUP BY b.biblionumber
332 HAVING count(h.reservedate) >= 42|;
334 $expected_converted_sql = q|
335 SELECT concat(b.title, ' ', ExtractValue(m.metadata,
336 '//datafield[@tag="245"]/subfield[@code="b"]')) AS title, b.author,
337 count(h.reservedate) AS 'holds'
339 LEFT JOIN biblio_metadata m USING (biblionumber)
340 LEFT JOIN reserves h ON (b.biblionumber=h.biblionumber)
341 GROUP BY b.biblionumber
342 HAVING count(h.reservedate) >= 42|;
343 is( C4::Reports::Guided::convert_sql( $sql ), $expected_converted_sql, "Query with 2 joins should have been correctly converted");
346 $schema->storage->txn_rollback;
350 $s =~ s/^\s*(.*?)\s*$/$1/s;