13dbc0089ca884ae1e7c45f418541b10093a37d2
[koha-equinox.git] / t / db_dependent / ILSDI_Services.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 CGI qw ( -utf8 );
21
22 use Test::More tests => 8;
23 use Test::MockModule;
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Items qw( ModItemTransfer );
28
29 use Koha::AuthUtils;
30
31 BEGIN {
32     use_ok('C4::ILSDI::Services');
33 }
34
35 my $schema  = Koha::Database->schema;
36 my $dbh     = C4::Context->dbh;
37 my $builder = t::lib::TestBuilder->new;
38
39 subtest 'AuthenticatePatron test' => sub {
40
41     plan tests => 14;
42
43     $schema->storage->txn_begin;
44
45     my $plain_password = 'tomasito';
46
47     $builder->build({
48         source => 'Borrower',
49         value => {
50             cardnumber => undef,
51         }
52     });
53
54     my $borrower = $builder->build({
55         source => 'Borrower',
56         value  => {
57             cardnumber => undef,
58             password => Koha::AuthUtils::hash_password( $plain_password )
59         }
60     });
61
62     my $query = new CGI;
63     $query->param( 'username', $borrower->{userid});
64     $query->param( 'password', $plain_password);
65
66     my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
67     is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
68     is( $reply->{code}, undef, "Error code undef");
69
70     $query->param('password','ilsdi-passworD');
71     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
72     is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" );
73     is( $reply->{id}, undef, "id undef");
74
75     $query->param( 'password', $plain_password );
76     $query->param( 'username', 'wrong-ilsdi-useriD' );
77     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
78     is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" );
79     is( $reply->{id}, undef, "id undef");
80
81     $query->param( 'username', uc( $borrower->{userid} ));
82     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
83     is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" );
84     is( $reply->{code}, undef, "Error code undef");
85
86     $query->param( 'username', $borrower->{cardnumber} );
87     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
88     is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" );
89     is( $reply->{code}, undef, "Error code undef" );
90
91     $query->param( 'password', 'ilsdi-passworD' );
92     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
93     is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" );
94     is( $reply->{id}, undef, "id undef" );
95
96     $query->param( 'username', 'randomcardnumber1234' );
97     $query->param( 'password', $plain_password );
98     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
99     is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
100     is( $reply->{id}, undef, "id undef");
101
102     $schema->storage->txn_rollback;
103 };
104
105
106 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
107
108     plan tests => 5;
109
110     $schema->storage->txn_begin;
111
112     $schema->resultset( 'Issue' )->delete_all;
113     $schema->resultset( 'Borrower' )->delete_all;
114     $schema->resultset( 'BorrowerAttribute' )->delete_all;
115     $schema->resultset( 'BorrowerAttributeType' )->delete_all;
116     $schema->resultset( 'Category' )->delete_all;
117     $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
118     $schema->resultset( 'Club' )->delete_all;
119     $schema->resultset( 'Branch' )->delete_all;
120
121     # Configure Koha to enable ILS-DI server and extended attributes:
122     t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
123     t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
124
125     # Set up a library/branch for our user to belong to:
126     my $lib = $builder->build( {
127         source => 'Branch',
128         value => {
129             branchcode => 'T_ILSDI',
130         }
131     } );
132
133     # Create a new category for user to belong to:
134     my $cat = $builder->build( {
135         source => 'Category',
136         value  => {
137             category_type                 => 'A',
138             BlockExpiredPatronOpacActions => -1,
139         }
140     } );
141
142     # Create a new attribute type:
143     my $attr_type = $builder->build( {
144         source => 'BorrowerAttributeType',
145         value  => {
146             code                      => 'HIDEME',
147             opac_display              => 0,
148             authorised_value_category => '',
149             class                     => '',
150         }
151     } );
152     my $attr_type_visible = $builder->build( {
153         source => 'BorrowerAttributeType',
154         value  => {
155             code                      => 'SHOWME',
156             opac_display              => 1,
157             authorised_value_category => '',
158             class                     => '',
159         }
160     } );
161
162     # Create a new user:
163     my $brwr = $builder->build( {
164         source => 'Borrower',
165         value  => {
166             categorycode => $cat->{'categorycode'},
167             branchcode   => $lib->{'branchcode'},
168         }
169     } );
170
171     # Authorised value:
172     my $auth = $builder->build( {
173         source => 'AuthorisedValue',
174         value  => {
175             category => $cat->{'categorycode'}
176         }
177     } );
178
179     # Set the new attribute for our user:
180     my $attr_hidden = $builder->build( {
181         source => 'BorrowerAttribute',
182         value  => {
183             borrowernumber => $brwr->{'borrowernumber'},
184             code           => $attr_type->{'code'},
185             attribute      => '1337 hidden',
186         }
187     } );
188     my $attr_shown = $builder->build( {
189         source => 'BorrowerAttribute',
190         value  => {
191             borrowernumber => $brwr->{'borrowernumber'},
192             code           => $attr_type_visible->{'code'},
193             attribute      => '1337 shown',
194         }
195     } );
196
197     my $fine = $builder->build(
198         {
199             source => 'Accountline',
200             value  => {
201                 borrowernumber    => $brwr->{borrowernumber},
202                 accounttype       => 'xxx',
203                 amountoutstanding => 10
204             }
205         }
206     );
207
208     # Prepare and send web request for IL-SDI server:
209     my $query = new CGI;
210     $query->param( 'service', 'GetPatronInfo' );
211     $query->param( 'patron_id', $brwr->{'borrowernumber'} );
212     $query->param( 'show_attributes', '1' );
213     $query->param( 'show_fines', '1' );
214
215     my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
216
217     # Build a structure for comparison:
218     my $cmp = {
219         category_code     => $attr_type_visible->{'category_code'},
220         class             => $attr_type_visible->{'class'},
221         code              => $attr_shown->{'code'},
222         description       => $attr_type_visible->{'description'},
223         display_checkout  => $attr_type_visible->{'display_checkout'},
224         value             => $attr_shown->{'attribute'},
225         value_description => undef,
226     };
227
228     is( $reply->{'charges'}, '10.00',
229         'The \'charges\' attribute should be correctly filled (bug 17836)' );
230
231     is( scalar( @{$reply->{fines}->{fine}}), 1, 'There should be only 1 account line');
232     is(
233         $reply->{fines}->{fine}->[0]->{accountlines_id},
234         $fine->{accountlines_id},
235         "The accountline should be the correct one"
236     );
237
238     # Check results:
239     is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
240
241     ok( exists $reply->{is_expired}, 'There should be the is_expired information');
242
243     # Cleanup
244     $schema->storage->txn_rollback;
245 };
246
247 subtest 'LookupPatron test' => sub {
248
249     plan tests => 9;
250
251     $schema->storage->txn_begin;
252
253     $schema->resultset( 'Issue' )->delete_all;
254     $schema->resultset( 'Borrower' )->delete_all;
255     $schema->resultset( 'BorrowerAttribute' )->delete_all;
256     $schema->resultset( 'BorrowerAttributeType' )->delete_all;
257     $schema->resultset( 'Category' )->delete_all;
258     $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
259     $schema->resultset( 'Branch' )->delete_all;
260
261     my $borrower = $builder->build({
262         source => 'Borrower',
263     });
264
265     my $query = CGI->new();
266     my $bad_result = C4::ILSDI::Services::LookupPatron($query);
267     is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
268
269     $query->delete_all();
270     $query->param( 'id', $borrower->{firstname} );
271     my $optional_result = C4::ILSDI::Services::LookupPatron($query);
272     is(
273         $optional_result->{id},
274         $borrower->{borrowernumber},
275         'Valid Firstname only'
276     );
277
278     $query->delete_all();
279     $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' );
280     my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query);
281     is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' );
282
283     foreach my $id_type (
284         'cardnumber',
285         'userid',
286         'email',
287         'borrowernumber',
288         'surname',
289         'firstname'
290     ) {
291         $query->delete_all();
292         $query->param( 'id_type', $id_type );
293         $query->param( 'id', $borrower->{$id_type} );
294         my $result = C4::ILSDI::Services::LookupPatron($query);
295         is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" );
296     }
297
298     # Cleanup
299     $schema->storage->txn_rollback;
300 };
301
302 subtest 'Holds test' => sub {
303
304     plan tests => 5;
305
306     $schema->storage->txn_begin;
307
308     t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
309
310     my $patron = $builder->build({
311         source => 'Borrower',
312     });
313
314     my $biblio = $builder->build({
315         source => 'Biblio',
316     });
317
318     my $biblioitems = $builder->build({
319         source => 'Biblioitem',
320         value => {
321             biblionumber => $biblio->{biblionumber},
322         }
323     });
324
325     my $item = $builder->build({
326         source => 'Item',
327         value => {
328             biblionumber => $biblio->{biblionumber},
329             damaged => 1
330         }
331     });
332
333     my $query = new CGI;
334     $query->param( 'patron_id', $patron->{borrowernumber});
335     $query->param( 'bib_id', $biblio->{biblionumber});
336
337     my $reply = C4::ILSDI::Services::HoldTitle( $query );
338     is( $reply->{code}, 'damaged', "Item damaged" );
339
340     my $item_o = Koha::Items->find($item->{itemnumber});
341     $item_o->damaged(0)->store;
342
343     my $hold = $builder->build({
344         source => 'Reserve',
345         value => {
346             borrowernumber => $patron->{borrowernumber},
347             biblionumber => $biblio->{biblionumber},
348             itemnumber => $item->{itemnumber}
349         }
350     });
351
352     $reply = C4::ILSDI::Services::HoldTitle( $query );
353     is( $reply->{code}, 'itemAlreadyOnHold', "Item already on hold" );
354
355     my $biblio_with_no_item = $builder->build({
356         source => 'Biblio',
357     });
358
359     $query = new CGI;
360     $query->param( 'patron_id', $patron->{borrowernumber});
361     $query->param( 'bib_id', $biblio_with_no_item->{biblionumber});
362
363     $reply = C4::ILSDI::Services::HoldTitle( $query );
364     is( $reply->{code}, 'NoItems', 'Biblio has no item' );
365
366     my $biblio2 = $builder->build({
367         source => 'Biblio',
368     });
369
370     my $biblioitems2 = $builder->build({
371         source => 'Biblioitem',
372         value => {
373             biblionumber => $biblio2->{biblionumber},
374         }
375     });
376
377     my $item2 = $builder->build({
378         source => 'Item',
379         value => {
380             biblionumber => $biblio2->{biblionumber},
381             damaged => 0
382         }
383     });
384
385     t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
386     my $issuingrule = $builder->build({
387         source => 'Issuingrule',
388         value => {
389             categorycode => $patron->{categorycode},
390             itemtype => $item2->{itype},
391             branchcode => $patron->{branchcode},
392             reservesallowed => 0,
393         }
394     });
395
396     $query = new CGI;
397     $query->param( 'patron_id', $patron->{borrowernumber});
398     $query->param( 'bib_id', $biblio2->{biblionumber});
399     $query->param( 'item_id', $item2->{itemnumber});
400
401     $reply = C4::ILSDI::Services::HoldItem( $query );
402     is( $reply->{code}, 'tooManyReserves', "Too many reserves" );
403
404     my $biblio3 = $builder->build({
405         source => 'Biblio',
406     });
407
408     my $biblioitems3 = $builder->build({
409         source => 'Biblioitem',
410         value => {
411             biblionumber => $biblio3->{biblionumber},
412         }
413     });
414
415     # Adding a holdable item to biblio 3.
416     my $item3 = $builder->build({
417         source => 'Item',
418         value => {
419             biblionumber => $biblio3->{biblionumber},
420             damaged => 0,
421         }
422     });
423
424     my $item4 = $builder->build({
425         source => 'Item',
426         value => {
427             biblionumber => $biblio3->{biblionumber},
428             damaged => 1,
429         }
430     });
431
432     my $issuingrule2 = $builder->build({
433         source => 'Issuingrule',
434         value => {
435             categorycode => $patron->{categorycode},
436             itemtype => $item3->{itype},
437             branchcode => $patron->{branchcode},
438             reservesallowed => 10,
439         }
440     });
441
442     $query = new CGI;
443     $query->param( 'patron_id', $patron->{borrowernumber});
444     $query->param( 'bib_id', $biblio3->{biblionumber});
445     $query->param( 'item_id', $item4->{itemnumber});
446
447     $reply = C4::ILSDI::Services::HoldItem( $query );
448     is( $reply->{code}, 'damaged', "Item is damaged" );
449
450     $schema->storage->txn_rollback;
451 };
452
453 subtest 'Holds test for branch transfer limits' => sub {
454
455     plan tests => 4;
456
457     $schema->storage->txn_begin;
458
459     # Test enforement of branch transfer limits
460     t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
461     t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
462
463     my $patron = $builder->build({
464         source => 'Borrower',
465     });
466
467     my $origin_branch = $builder->build(
468         {
469             source => 'Branch',
470             value  => {
471                 pickup_location => 1,
472             }
473         }
474     );
475     my $pickup_branch = $builder->build(
476         {
477             source => 'Branch',
478             value  => {
479                 pickup_location => 1,
480             }
481         }
482     );
483
484     my $biblio = $builder->build({
485         source => 'Biblio',
486     });
487     my $biblioitem = $builder->build({
488         source => 'Biblioitem',
489         value => {
490             biblionumber => $biblio->{biblionumber},
491         }
492     });
493     my $item = $builder->build({
494         source => 'Item',
495         value => {
496             homebranch => $origin_branch->{branchcode},
497             holdingbranch => $origin_branch->{branchcode},
498             biblionumber => $biblio->{biblionumber},
499             damaged => 0,
500             itemlost => 0,
501         }
502     });
503
504     Koha::IssuingRules->search()->delete();
505     my $issuingrule = $builder->build({
506         source => 'Issuingrule',
507         value => {
508             categorycode => '*',
509             itemtype => '*',
510             branchcode => '*',
511             reservesallowed => 99,
512         }
513     });
514
515     my $limit = Koha::Item::Transfer::Limit->new({
516         toBranch => $pickup_branch->{branchcode},
517         fromBranch => $item->{holdingbranch},
518         itemtype => $item->{itype},
519     })->store();
520
521     my $query = new CGI;
522     $query->param( 'pickup_location', $pickup_branch->{branchcode} );
523     $query->param( 'patron_id', $patron->{borrowernumber});
524     $query->param( 'bib_id', $biblio->{biblionumber});
525     $query->param( 'item_id', $item->{itemnumber});
526
527     my $reply = C4::ILSDI::Services::HoldItem( $query );
528     is( $reply->{code}, 'cannotBeTransferred', "Item hold, Item cannot be transferred" );
529
530     $reply = C4::ILSDI::Services::HoldTitle( $query );
531     is( $reply->{code}, 'cannotBeTransferred', "Record hold, Item cannot be transferred" );
532
533     t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
534
535     $reply = C4::ILSDI::Services::HoldItem( $query );
536     is( $reply->{code}, undef, "Item hold, Item can be transferred" );
537
538     Koha::Holds->search()->delete();
539
540     $reply = C4::ILSDI::Services::HoldTitle( $query );
541     is( $reply->{code}, undef, "Record hold, Item con be transferred" );
542
543     $schema->storage->txn_rollback;
544 };
545
546 subtest 'GetRecords' => sub {
547
548     plan tests => 1;
549
550     $schema->storage->txn_begin;
551
552     t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
553
554     my $branch1 = $builder->build({
555         source => 'Branch',
556     });
557     my $branch2 = $builder->build({
558         source => 'Branch',
559     });
560
561     my $biblio = $builder->build({
562         source => 'Biblio',
563     });
564     my $biblioitem = $builder->build({
565         source => 'Biblioitem',
566         value => {
567             biblionumber => $biblio->{biblionumber},
568         },
569     });
570     my $item = $builder->build_object({
571         class => 'Koha::Items',
572         value => {
573             biblionumber => $biblio->{biblionumber},
574             biblioitemnumber => $biblioitem->{biblioitemnumber},
575             homebranch => $branch1->{branchcode},
576             holdingbranch => $branch1->{branchcode},
577         },
578     });
579
580     ModItemTransfer($item->itemnumber, $branch1->{branchcode}, $branch2->{branchcode});
581
582     my $cgi = new CGI;
583     $cgi->param(service => 'GetRecords');
584     $cgi->param(id => $biblio->{biblionumber});
585
586     my $reply = C4::ILSDI::Services::GetRecords($cgi);
587
588     my $transfer = $item->get_transfer;
589     my $expected = {
590         datesent => $transfer->datesent,
591         frombranch => $transfer->frombranch,
592         tobranch => $transfer->tobranch,
593     };
594     is_deeply($reply->{record}->[0]->{items}->{item}->[0]->{transfer}, $expected,
595         'GetRecords returns transfer informations');
596
597     $schema->storage->txn_rollback;
598 };
599
600 subtest 'RenewHold' => sub {
601     plan tests => 4;
602
603     $schema->storage->txn_begin;
604
605     my $cgi    = new CGI;
606     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
607     my $item   = $builder->build_object( { class => 'Koha::Items' } );
608     $cgi->param( patron_id => $patron->borrowernumber );
609     $cgi->param( item_id   => $item->itemnumber );
610
611     t::lib::Mocks::mock_userenv( { patron => $patron } );    # For AddIssue
612     my $checkout = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
613
614     # Everything is ok
615     my $reply = C4::ILSDI::Services::RenewLoan($cgi);
616     is( exists $reply->{date_due}, 1, 'If the item is checked out, the date_due key should exist' );
617
618     # The item is not checked out
619     $checkout->delete;
620     $reply = C4::ILSDI::Services::RenewLoan($cgi);
621     is( $reply, undef, 'If the item is not checked out, we should not explode.');    # FIXME We should return an error code instead
622
623     # The item does not exist
624     $item->delete;
625     $reply = C4::ILSDI::Services::RenewLoan($cgi);
626     is( $reply->{code}, 'RecordNotFound', 'If the item does not exist, RecordNotFound should be returned');
627
628     $patron->delete;
629     $reply = C4::ILSDI::Services::RenewLoan($cgi);
630     is( $reply->{code}, 'PatronNotFound', 'If the patron does not exist, PatronNotFound should be returned');
631
632     $schema->storage->txn_rollback;
633 };