28ef81dbfd446fcdebde97f148ec85789eead682
[srvgit] / t / db_dependent / Reports / Guided.t
1 # Copyright 2012 Catalyst IT Ltd.
2 # Copyright 2015 Koha Development team
3 #
4 # This file is part of Koha.
5 #
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.
10 #
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.
15 #
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>.
18
19 use Modern::Perl;
20
21 use Test::More tests => 11;
22 use Test::Warn;
23
24 use t::lib::TestBuilder;
25 use C4::Context;
26 use Koha::Database;
27 use Koha::Items;
28 use Koha::Reports;
29 use Koha::Notice::Messages;
30
31 use_ok('C4::Reports::Guided', qw( execute_query save_report delete_report strip_limit GetReservedAuthorisedValues IsAuthorisedValueValid GetParametersFromSQL ValidateSQLParameters get_saved_reports update_sql get_report_areas convert_sql EmailReport nb_rows ));
32 can_ok(
33     'C4::Reports::Guided',
34     qw(save_report delete_report execute_query)
35 );
36
37 my $schema = Koha::Database->new->schema;
38 $schema->storage->txn_begin;
39 my $builder = t::lib::TestBuilder->new;
40
41 subtest 'strip_limit' => sub {
42     # This is the query I found that triggered bug 8594.
43     my $sql = "SELECT aqorders.ordernumber, biblio.title, biblio.biblionumber, items.homebranch,
44         aqorders.entrydate, aqorders.datereceived,
45         (SELECT DATE(datetime) FROM statistics
46             WHERE itemnumber=items.itemnumber AND
47                 (type='return' OR type='issue') LIMIT 1)
48         AS shelvedate,
49         DATEDIFF(COALESCE(
50             (SELECT DATE(datetime) FROM statistics
51                 WHERE itemnumber=items.itemnumber AND
52                 (type='return' OR type='issue') LIMIT 1),
53         aqorders.datereceived), aqorders.entrydate) AS totaldays
54     FROM aqorders
55     LEFT JOIN biblio USING (biblionumber)
56     LEFT JOIN items ON (items.biblionumber = biblio.biblionumber
57         AND dateaccessioned=aqorders.datereceived)
58     WHERE (entrydate >= '2011-01-01' AND (datereceived < '2011-02-01' OR datereceived IS NULL))
59         AND items.homebranch LIKE 'INFO'
60     ORDER BY title";
61
62     my ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql);
63     is($res_sql, $sql, "Not breaking subqueries");
64     is($res_lim1, 0, "Returns correct default offset");
65     is($res_lim2, undef, "Returns correct default LIMIT");
66
67     # Now the same thing, but we want it to remove the LIMIT from the end
68
69     my $test_sql = $res_sql . " LIMIT 242";
70     ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
71     # The replacement drops a ' ' where the limit was
72     is(trim($res_sql), $sql, "Correctly removes only final LIMIT");
73     is($res_lim1, 0, "Returns correct default offset");
74     is($res_lim2, 242, "Returns correct extracted LIMIT");
75
76     $test_sql = $res_sql . " LIMIT 13,242";
77     ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
78     # The replacement drops a ' ' where the limit was
79     is(trim($res_sql), $sql, "Correctly removes only final LIMIT (with offset)");
80     is($res_lim1, 13, "Returns correct extracted offset");
81     is($res_lim2, 242, "Returns correct extracted LIMIT");
82
83     # After here is the simpler case, where there isn't a WHERE clause to worry
84     # about.
85
86     # First case with nothing to change
87     $sql = "SELECT * FROM items";
88     ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($sql);
89     is($res_sql, $sql, "Not breaking simple queries");
90     is($res_lim1, 0, "Returns correct default offset");
91     is($res_lim2, undef, "Returns correct default LIMIT");
92
93     $test_sql = $sql . " LIMIT 242";
94     ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
95     is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case");
96     is($res_lim1, 0, "Returns correct default offset");
97     is($res_lim2, 242, "Returns correct extracted LIMIT");
98
99     $test_sql = $sql . " LIMIT 13,242";
100     ($res_sql, $res_lim1, $res_lim2) = C4::Reports::Guided::strip_limit($test_sql);
101     is(trim($res_sql), $sql, "Correctly removes LIMIT in simple case (with offset)");
102     is($res_lim1, 13, "Returns correct extracted offset");
103     is($res_lim2, 242, "Returns correct extracted LIMIT");
104 };
105
106 $_->delete for Koha::AuthorisedValues->search({ category => 'XXX' });
107 Koha::AuthorisedValue->new({category => 'LOC'})->store;
108
109 subtest 'GetReservedAuthorisedValues' => sub {
110     plan tests => 1;
111     # This one will catch new reserved words not added
112     # to GetReservedAuthorisedValues
113     my %test_authval = (
114         'date' => 1,
115         'branches' => 1,
116         'itemtypes' => 1,
117         'cn_source' => 1,
118         'categorycode' => 1,
119         'biblio_framework' => 1,
120         'list' => 1,
121         'cash_registers' => 1,
122         'debit_types' => 1,
123         'credit_types' => 1
124     );
125
126     my $reserved_authorised_values = GetReservedAuthorisedValues();
127     is_deeply(\%test_authval, $reserved_authorised_values,
128                 'GetReservedAuthorisedValues returns a fixed list');
129 };
130
131 subtest 'IsAuthorisedValueValid' => sub {
132     plan tests => 12;
133     ok( IsAuthorisedValueValid('LOC'),
134         'User defined authorised value category is valid');
135
136     ok( ! IsAuthorisedValueValid('XXX'),
137         'Not defined authorised value category is invalid');
138
139     # Loop through the reserved authorised values
140     foreach my $authorised_value ( keys %{GetReservedAuthorisedValues()} ) {
141         ok( IsAuthorisedValueValid($authorised_value),
142             '\''.$authorised_value.'\' is a reserved word, and thus a valid authorised value');
143     }
144 };
145
146 subtest 'GetParametersFromSQL+ValidateSQLParameters' => sub  {
147     plan tests => 3;
148     my $test_query_1 = "
149         SELECT date_due
150         FROM old_issues
151         WHERE YEAR(timestamp) = <<Year|custom_list>> AND
152               branchcode = <<Branch|branches>> AND
153               borrowernumber = <<Borrower>> AND
154               itemtype = <<Item type|itemtypes:all>>
155     ";
156
157     my @test_parameters_with_custom_list = (
158         { 'name' => 'Year', 'authval' => 'custom_list' },
159         { 'name' => 'Branch', 'authval' => 'branches' },
160         { 'name' => 'Borrower', 'authval' => undef },
161         { 'name' => 'Item type', 'authval' => 'itemtypes' }
162     );
163
164     is_deeply( GetParametersFromSQL($test_query_1), \@test_parameters_with_custom_list,
165         'SQL params are correctly parsed');
166
167     my @problematic_parameters = ();
168     push @problematic_parameters, { 'name' => 'Year', 'authval' => 'custom_list' };
169     is_deeply( ValidateSQLParameters( $test_query_1 ),
170                \@problematic_parameters,
171                '\'custom_list\' not a valid category' );
172
173     my $test_query_2 = "
174         SELECT date_due
175         FROM old_issues
176         WHERE YEAR(timestamp) = <<Year|date>> AND
177               branchcode = <<Branch|branches>> AND
178               borrowernumber = <<Borrower|LOC>>
179     ";
180
181     is_deeply( ValidateSQLParameters( $test_query_2 ),
182         [],
183         'All parameters valid, empty problematic authvals list'
184     );
185 };
186
187 subtest 'get_saved_reports' => sub {
188     plan tests => 17;
189     my $dbh = C4::Context->dbh;
190     $dbh->do(q|DELETE FROM saved_sql|);
191     $dbh->do(q|DELETE FROM saved_reports|);
192
193     #Test save_report
194     my $count = scalar @{ get_saved_reports() };
195     is( $count, 0, "There is no report" );
196
197     my @report_ids;
198     foreach my $ii ( 1..3 ) {
199         my $id = $builder->build({ source => 'Borrower' })->{ borrowernumber };
200         push @report_ids, save_report({
201             borrowernumber => $id,
202             sql            => "SQL$id",
203             name           => "Name$id",
204             area           => "area$ii", # ii vs id area is varchar(6)
205             group          => "group$id",
206             subgroup       => "subgroup$id",
207             type           => "type$id",
208             notes          => "note$id",
209             cache_expiry   => undef,
210             public         => 0,
211         });
212         $count++;
213     }
214     like( $report_ids[0], '/^\d+$/', "Save_report returns an id for first" );
215     like( $report_ids[1], '/^\d+$/', "Save_report returns an id for second" );
216     like( $report_ids[2], '/^\d+$/', "Save_report returns an id for third" );
217
218     is( scalar @{ get_saved_reports() },
219         $count, "$count reports have been added" );
220
221     ok( 0 < scalar @{ get_saved_reports( $report_ids[0] ) }, "filter takes report id" );
222
223     my $r1 = Koha::Reports->find($report_ids[0]);
224     $r1 = update_sql($r1->id, { %{$r1->unblessed}, borrowernumber => $r1->borrowernumber, name => 'Just another report' });
225     is( $r1->cache_expiry, 300, 'cache_expiry has the correct default value, from DBMS' );
226
227     #Test delete_report
228     is (delete_report(),undef, "Without id delete_report returns undef");
229
230     is( delete_report( $report_ids[0] ), 1, "report 1 is deleted" );
231     $count--;
232
233     is( scalar @{ get_saved_reports() }, $count, "Report1 has been deleted" );
234
235     is( delete_report( $report_ids[1], $report_ids[2] ), 2, "report 2 and 3 are deleted" );
236     $count -= 2;
237
238     is( scalar @{ get_saved_reports() },
239         $count, "Report2 and report3 have been deleted" );
240
241     my $sth = execute_query('SELECT COUNT(*) FROM systempreferences', 0, 10);
242     my $results = $sth->fetchall_arrayref;
243     is(scalar @$results, 1, 'running a query returned a result');
244
245     my $version = C4::Context->preference('Version');
246     $sth = execute_query(
247         'SELECT value FROM systempreferences WHERE variable = ?',
248         0,
249         10,
250         [ 'Version' ],
251     );
252     $results = $sth->fetchall_arrayref;
253     is_deeply(
254         $results,
255         [ [ $version ] ],
256         'running a query with a parameter returned the expected result'
257     );
258
259     # for next test, we want to let execute_query capture any SQL errors
260     my $errors;
261     warning_like {local $dbh->{RaiseError} = 0; ($sth, $errors) = execute_query(
262             'SELECT surname FRM borrowers',  # error in the query is intentional
263             0, 10 ) }
264             qr/DBD::mysql::st execute failed: You have an error in your SQL syntax;/,
265             "Wrong SQL syntax raises warning";
266     ok(
267         defined($errors) && exists($errors->{queryerr}),
268         'attempting to run a report with an SQL syntax error returns error message (Bug 12214)'
269     );
270
271     is_deeply( get_report_areas(), [ 'CIRC', 'CAT', 'PAT', 'ACQ', 'ACC', 'SER' ],
272         "get_report_areas returns the correct array of report areas");
273 };
274
275 subtest 'Ensure last_run is populated' => sub {
276     plan tests => 3;
277
278     my $rs = Koha::Database->new()->schema()->resultset('SavedSql');
279
280     my $report = $rs->new(
281         {
282             report_name => 'Test Report',
283             savedsql    => 'SELECT * FROM branches',
284             notes       => undef,
285         }
286     )->insert();
287
288     is( $report->last_run, undef, 'Newly created report has null last_run ' );
289
290     execute_query( $report->savedsql, undef, undef, undef, $report->id );
291     $report->discard_changes();
292
293     isnt( $report->last_run, undef, 'First run of report populates last_run' );
294
295     my $previous_last_run = $report->last_run;
296     sleep(1); # last_run is stored to the second, so we need to ensure at least one second has passed between runs
297     execute_query( $report->savedsql, undef, undef, undef, $report->id );
298     $report->discard_changes();
299
300     isnt( $report->last_run, $previous_last_run, 'Second run of report updates last_run' );
301 };
302
303 subtest 'convert_sql' => sub {
304     plan tests => 4;
305
306     my $sql = q|
307     SELECT biblionumber, ExtractValue(marcxml,
308 'count(//datafield[@tag="505"])') AS count505
309     FROM biblioitems
310     HAVING count505 > 1|;
311     my $expected_converted_sql = q|
312     SELECT biblionumber, ExtractValue(metadata,
313 'count(//datafield[@tag="505"])') AS count505
314     FROM biblio_metadata
315     HAVING count505 > 1|;
316
317     is( C4::Reports::Guided::convert_sql( $sql ), $expected_converted_sql, "Simple query should have been correctly converted");
318
319     $sql = q|
320     SELECT biblionumber, substring(
321 ExtractValue(marcxml,'//controlfield[@tag="008"]'), 8,4 ) AS 'PUB DATE',
322 title
323     FROM biblioitems
324     INNER JOIN biblio USING (biblionumber)
325     WHERE biblionumber = 14|;
326
327     $expected_converted_sql = q|
328     SELECT biblionumber, substring(
329 ExtractValue(metadata,'//controlfield[@tag="008"]'), 8,4 ) AS 'PUB DATE',
330 title
331     FROM biblio_metadata
332     INNER JOIN biblio USING (biblionumber)
333     WHERE biblionumber = 14|;
334     is( C4::Reports::Guided::convert_sql( $sql ), $expected_converted_sql, "Query with biblio info should have been correctly converted");
335
336     $sql = q|
337     SELECT concat(b.title, ' ', ExtractValue(m.marcxml,
338 '//datafield[@tag="245"]/subfield[@code="b"]')) AS title, b.author,
339 count(h.reservedate) AS 'holds'
340     FROM biblio b
341     LEFT JOIN biblioitems m USING (biblionumber)
342     LEFT JOIN reserves h ON (b.biblionumber=h.biblionumber)
343     GROUP BY b.biblionumber
344     HAVING count(h.reservedate) >= 42|;
345
346     $expected_converted_sql = q|
347     SELECT concat(b.title, ' ', ExtractValue(m.metadata,
348 '//datafield[@tag="245"]/subfield[@code="b"]')) AS title, b.author,
349 count(h.reservedate) AS 'holds'
350     FROM biblio b
351     LEFT JOIN biblio_metadata m USING (biblionumber)
352     LEFT JOIN reserves h ON (b.biblionumber=h.biblionumber)
353     GROUP BY b.biblionumber
354     HAVING count(h.reservedate) >= 42|;
355     is( C4::Reports::Guided::convert_sql( $sql ), $expected_converted_sql, "Query with 2 joins should have been correctly converted");
356
357     $sql = q|
358     SELECT t1.marcxml AS first, t2.marcxml AS second,
359     FROM biblioitems t1
360     LEFT JOIN biblioitems t2 USING ( biblionumber )|;
361
362     $expected_converted_sql = q|
363     SELECT t1.metadata AS first, t2.metadata AS second,
364     FROM biblio_metadata t1
365     LEFT JOIN biblio_metadata t2 USING ( biblionumber )|;
366     is( C4::Reports::Guided::convert_sql( $sql ), $expected_converted_sql, "Query with multiple instances of marcxml and biblioitems should have them all replaced");
367 };
368
369 subtest 'Email report test' => sub {
370
371     plan tests => 14;
372     my $dbh = C4::Context->dbh;
373
374     my $id1 = $builder->build({ source => 'Borrower',value => { surname => 'mailer', email => 'a@b.com', emailpro => 'b@c.com' } })->{ borrowernumber };
375     my $id2 = $builder->build({ source => 'Borrower',value => { surname => 'nomailer', email => undef, emailpro => 'd@e.com' } })->{ borrowernumber };
376     my $id3 = $builder->build({ source => 'Borrower',value => { surname => 'norman', email => 'a@b.com', emailpro => undef } })->{ borrowernumber };
377     my $report1 = $builder->build({ source => 'SavedSql', value => { savedsql => "SELECT surname,borrowernumber,email,emailpro FROM borrowers WHERE borrowernumber IN ($id1,$id2,$id3)" } })->{ id };
378     my $report2 = $builder->build({ source => 'SavedSql', value => { savedsql => "SELECT potato FROM mashed" } })->{ id };
379
380     my $letter1 = $builder->build({
381             source => 'Letter',
382             value => {
383                 content => "[% surname %]",
384                 branchcode => "",
385                 message_transport_type => 'email',
386                 is_html => undef
387             }
388         });
389     my $letter2 = $builder->build({
390             source => 'Letter',
391             value => {
392                 content => "[% firstname %]",
393                 branchcode => "",
394                 message_transport_type => 'email',
395                 is_html => 0
396             }
397         });
398
399     my $letter3 = $builder->build({
400             source => 'Letter',
401             value => {
402                 content => "[% surname %]",
403                 branchcode => "",
404                 message_transport_type => 'email',
405                 is_html => 1
406             }
407         });
408
409     my $message_count = Koha::Notice::Messages->search({})->count;
410
411     my ( $emails, $errors ) = C4::Reports::Guided::EmailReport();
412     is( $errors->[0]{FATAL}, 'MISSING_PARAMS', "Need to enter required params");
413
414     ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report1, module => $letter1->{module}, code => $letter2->{code}});
415     is( $errors->[0]{FATAL}, 'NO_LETTER', "Must have a letter that exists");
416
417     # for next test, we want to let execute_query capture any SQL errors
418     warning_like { local $dbh->{RaiseError} = 0; ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report2, module => $letter1->{module} , code => $letter1->{code} }) }
419         qr/DBD::mysql::st execute failed/,
420         'Error from bad report';
421     is( $errors->[0]{FATAL}, 'REPORT_FAIL', "Bad report returns failure");
422
423     ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report1, module => $letter1->{module} , code => $letter1->{code} });
424     is( $errors->[0]{NO_FROM_COL} == 1 && $errors->[1]{NO_EMAIL_COL} == 2  && $errors->[2]{NO_FROM_COL} == 2, 1, "Correct warnings from the routine");
425
426     ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report1, module => $letter1->{module} , code => $letter1->{code}, from => 'the@future.ooh' });
427     is( $errors->[0]{NO_EMAIL_COL}, 2, "Warning only for patron with no email");
428
429     is( $message_count,  Koha::Notice::Messages->search({})->count, "Messages not added without commit");
430
431     ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report1, module => $letter1->{module} , code => $letter1->{code}, from => 'the@future.ooh' });
432     is( $emails->[0]{letter}->{content}, "mailer", "Message has expected content");
433     is( $emails->[1]{letter}->{content}, "norman", "Message has expected content");
434     is( $emails->[0]{letter}->{'content-type'}, undef, "Message content type is not set for plain text mail");
435
436     ($emails, $errors ) = C4::Reports::Guided::EmailReport({report_id => $report1, module => $letter1->{module} , code => $letter1->{code}, from => 'the@future.ooh', email => 'emailpro' });
437     is_deeply( $errors, [{'NO_EMAIL_COL'=>3}],"We report missing email in emailpro column");
438     is( $emails->[0]->{to_address}, 'b@c.com', "Message uses correct email");
439     is( $emails->[1]->{to_address}, 'd@e.com', "Message uses correct email");
440
441     ($emails) = C4::Reports::Guided::EmailReport({report_id => $report1, module => $letter3->{module} , code => $letter3->{code}, from => 'the@future.ooh' });
442     is( $emails->[0]{letter}->{'content-type'}, 'text/html; charset="UTF-8"', "Message has expected content type");
443
444 };
445
446 $schema->storage->txn_rollback;
447
448 subtest 'nb_rows() tests' => sub {
449
450     plan tests => 3;
451
452     my $dbh = C4::Context->dbh;
453     $schema->storage->txn_begin;
454
455     my $items_count = Koha::Items->search->count;
456     $builder->build_object({ class => 'Koha::Items' });
457     $builder->build_object({ class => 'Koha::Items' });
458     $items_count += 2;
459
460     my $query = q{
461         SELECT * FROM items xxx
462     };
463
464     my $nb_rows = nb_rows( $query );
465
466     is( $nb_rows, $items_count, 'nb_rows returns the right value' );
467
468     my $bad_query = q{
469         SELECT * items xxx
470     };
471
472     # for next test, we want to let execute_query capture any SQL errors
473     
474     warning_like
475         { $nb_rows = nb_rows( $bad_query ) }
476         qr/DBD::mysql::st execute failed:/,
477         'Bad queries raise a warning';
478
479     is( $nb_rows, 0, 'nb_rows returns 0 on bad queries' );
480
481     $schema->storage->txn_rollback;
482 };
483
484 sub trim {
485     my ($s) = @_;
486     $s =~ s/^\s*(.*?)\s*$/$1/s;
487     return $s;
488 }