1 # Copyright 2012 Catalyst IT Ltd.
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>.
21 use Test::More tests => 19; # last test to print
23 use_ok('C4::Reports::Guided');
25 # This is the query I found that triggered bug 8594.
26 my $sql = "SELECT aqorders.ordernumber, biblio.title, biblio.biblionumber, items.homebranch,
27 aqorders.entrydate, aqorders.datereceived,
28 (SELECT DATE(datetime) FROM statistics
29 WHERE itemnumber=items.itemnumber AND
30 (type='return' OR type='issue') LIMIT 1)
33 (SELECT DATE(datetime) FROM statistics
34 WHERE itemnumber=items.itemnumber AND
35 (type='return' OR type='issue') LIMIT 1),
36 aqorders.datereceived), aqorders.entrydate) AS totaldays
38 LEFT JOIN biblio USING (biblionumber)
39 LEFT JOIN items ON (items.biblionumber = biblio.biblionumber
40 AND dateaccessioned=aqorders.datereceived)
41 WHERE (entrydate >= '2011-01-01' AND (datereceived < '2011-02-01' OR datereceived IS NULL))
42 AND items.homebranch LIKE 'INFO'
45 my ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql);
46 is($res_sql, $sql, "Not breaking subqueries");
47 is($res_lim1, 0, "Returns correct default offset");
48 is($res_lim2, undef, "Returns correct default LIMIT");
50 # Now the same thing, but we want it to remove the LIMIT from the end
52 my $test_sql = $res_sql . " LIMIT 242";
53 ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
54 # The replacement drops a ' ' where the limit was
55 is(trim($res_sql), $sql, "Correctly removes only final LIMIT");
56 is($res_lim1, 0, "Returns correct default offset");
57 is($res_lim2, 242, "Returns correct extracted LIMIT");
59 $test_sql = $res_sql . " LIMIT 13,242";
60 ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
61 # The replacement drops a ' ' where the limit was
62 is(trim($res_sql), $sql, "Correctly removes only final LIMIT (with offset)");
63 is($res_lim1, 13, "Returns correct extracted offset");
64 is($res_lim2, 242, "Returns correct extracted LIMIT");
66 # After here is the simpler case, where there isn't a WHERE clause to worry
69 # First case with nothing to change
70 $sql = "SELECT * FROM items";
71 ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql);
72 is($res_sql, $sql, "Not breaking simple queries");
73 is($res_lim1, 0, "Returns correct default offset");
74 is($res_lim2, undef, "Returns correct default LIMIT");
76 $test_sql = $sql . " LIMIT 242";
77 ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
78 is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case");
79 is($res_lim1, 0, "Returns correct default offset");
80 is($res_lim2, 242, "Returns correct extracted LIMIT");
82 $test_sql = $sql . " LIMIT 13,242";
83 ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
84 is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case (with offset)");
85 is($res_lim1, 13, "Returns correct extracted offset");
86 is($res_lim2, 242, "Returns correct extracted LIMIT");
90 $s =~ s/^\s*(.*?)\s*$/$1/s;