Bug 18925: (QA follow-up) Remove weird 'Null' values for integer column
[koha.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 => 5;
23 use Test::MockModule;
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use Koha::AuthUtils;
28
29 BEGIN {
30     use_ok('C4::ILSDI::Services');
31 }
32
33 my $schema  = Koha::Database->schema;
34 my $dbh     = C4::Context->dbh;
35 my $builder = t::lib::TestBuilder->new;
36
37 subtest 'AuthenticatePatron test' => sub {
38
39     plan tests => 14;
40
41     $schema->storage->txn_begin;
42
43     my $plain_password = 'tomasito';
44
45     $builder->build({
46         source => 'Borrower',
47         value => {
48             cardnumber => undef,
49         }
50     });
51
52     my $borrower = $builder->build({
53         source => 'Borrower',
54         value  => {
55             cardnumber => undef,
56             password => Koha::AuthUtils::hash_password( $plain_password )
57         }
58     });
59
60     my $query = new CGI;
61     $query->param( 'username', $borrower->{userid});
62     $query->param( 'password', $plain_password);
63
64     my $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
65     is( $reply->{id}, $borrower->{borrowernumber}, "userid and password - Patron authenticated" );
66     is( $reply->{code}, undef, "Error code undef");
67
68     $query->param('password','ilsdi-passworD');
69     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
70     is( $reply->{code}, 'PatronNotFound', "userid and wrong password - PatronNotFound" );
71     is( $reply->{id}, undef, "id undef");
72
73     $query->param( 'password', $plain_password );
74     $query->param( 'username', 'wrong-ilsdi-useriD' );
75     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
76     is( $reply->{code}, 'PatronNotFound', "non-existing userid - PatronNotFound" );
77     is( $reply->{id}, undef, "id undef");
78
79     $query->param( 'username', uc( $borrower->{userid} ));
80     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
81     is( $reply->{id}, $borrower->{borrowernumber}, "userid is not case sensitive - Patron authenticated" );
82     is( $reply->{code}, undef, "Error code undef");
83
84     $query->param( 'username', $borrower->{cardnumber} );
85     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
86     is( $reply->{id}, $borrower->{borrowernumber}, "cardnumber and password - Patron authenticated" );
87     is( $reply->{code}, undef, "Error code undef" );
88
89     $query->param( 'password', 'ilsdi-passworD' );
90     $reply = C4::ILSDI::Services::AuthenticatePatron( $query );
91     is( $reply->{code}, 'PatronNotFound', "cardnumber and wrong password - PatronNotFount" );
92     is( $reply->{id}, undef, "id undef" );
93
94     $query->param( 'username', 'randomcardnumber1234' );
95     $query->param( 'password', $plain_password );
96     $reply = C4::ILSDI::Services::AuthenticatePatron($query);
97     is( $reply->{code}, 'PatronNotFound', "non-existing cardnumer/userid - PatronNotFound" );
98     is( $reply->{id}, undef, "id undef");
99
100     $schema->storage->txn_rollback;
101 };
102
103
104 subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub {
105
106     plan tests => 4;
107
108     $schema->storage->txn_begin;
109
110     $schema->resultset( 'Issue' )->delete_all;
111     $schema->resultset( 'Borrower' )->delete_all;
112     $schema->resultset( 'BorrowerAttribute' )->delete_all;
113     $schema->resultset( 'BorrowerAttributeType' )->delete_all;
114     $schema->resultset( 'Category' )->delete_all;
115     $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
116     $schema->resultset( 'Club' )->delete_all;
117     $schema->resultset( 'Branch' )->delete_all;
118
119     # Configure Koha to enable ILS-DI server and extended attributes:
120     t::lib::Mocks::mock_preference( 'ILS-DI', 1 );
121     t::lib::Mocks::mock_preference( 'ExtendedPatronAttributes', 1 );
122
123     # Set up a library/branch for our user to belong to:
124     my $lib = $builder->build( {
125         source => 'Branch',
126         value => {
127             branchcode => 'T_ILSDI',
128         }
129     } );
130
131     # Create a new category for user to belong to:
132     my $cat = $builder->build( {
133         source => 'Category',
134         value  => {
135             category_type                 => 'A',
136             BlockExpiredPatronOpacActions => -1,
137         }
138     } );
139
140     # Create a new attribute type:
141     my $attr_type = $builder->build( {
142         source => 'BorrowerAttributeType',
143         value  => {
144             code                      => 'HIDEME',
145             opac_display              => 0,
146             authorised_value_category => '',
147             class                     => '',
148         }
149     } );
150     my $attr_type_visible = $builder->build( {
151         source => 'BorrowerAttributeType',
152         value  => {
153             code                      => 'SHOWME',
154             opac_display              => 1,
155             authorised_value_category => '',
156             class                     => '',
157         }
158     } );
159
160     # Create a new user:
161     my $brwr = $builder->build( {
162         source => 'Borrower',
163         value  => {
164             categorycode => $cat->{'categorycode'},
165             branchcode   => $lib->{'branchcode'},
166         }
167     } );
168
169     # Authorised value:
170     my $auth = $builder->build( {
171         source => 'AuthorisedValue',
172         value  => {
173             category => $cat->{'categorycode'}
174         }
175     } );
176
177     # Set the new attribute for our user:
178     my $attr_hidden = $builder->build( {
179         source => 'BorrowerAttribute',
180         value  => {
181             borrowernumber => $brwr->{'borrowernumber'},
182             code           => $attr_type->{'code'},
183             attribute      => '1337 hidden',
184         }
185     } );
186     my $attr_shown = $builder->build( {
187         source => 'BorrowerAttribute',
188         value  => {
189             borrowernumber => $brwr->{'borrowernumber'},
190             code           => $attr_type_visible->{'code'},
191             attribute      => '1337 shown',
192         }
193     } );
194
195     my $fine = $builder->build(
196         {
197             source => 'Accountline',
198             value  => {
199                 borrowernumber    => $brwr->{borrowernumber},
200                 accountno         => 1,
201                 accounttype       => 'xxx',
202                 amountoutstanding => 10
203             }
204         }
205     );
206
207     # Prepare and send web request for IL-SDI server:
208     my $query = new CGI;
209     $query->param( 'service', 'GetPatronInfo' );
210     $query->param( 'patron_id', $brwr->{'borrowernumber'} );
211     $query->param( 'show_attributes', '1' );
212     $query->param( 'show_fines', '1' );
213
214     my $reply = C4::ILSDI::Services::GetPatronInfo( $query );
215
216     # Build a structure for comparison:
217     my $cmp = {
218         category_code     => $attr_type_visible->{'category_code'},
219         class             => $attr_type_visible->{'class'},
220         code              => $attr_shown->{'code'},
221         description       => $attr_type_visible->{'description'},
222         display_checkout  => $attr_type_visible->{'display_checkout'},
223         value             => $attr_shown->{'attribute'},
224         value_description => undef,
225     };
226
227     is( $reply->{'charges'}, '10.00',
228         'The \'charges\' attribute should be correctly filled (bug 17836)' );
229
230     is( scalar( @{$reply->{fines}->{fine}}), 1, 'There should be only 1 account line');
231     is(
232         $reply->{fines}->{fine}->[0]->{accountlines_id},
233         $fine->{accountlines_id},
234         "The accountline should be the correct one"
235     );
236
237     # Check results:
238     is_deeply( $reply->{'attributes'}, [ $cmp ], 'Test GetPatronInfo - show_attributes parameter' );
239
240     # Cleanup
241     $schema->storage->txn_rollback;
242 };
243
244
245 subtest 'LookupPatron test' => sub {
246
247     plan tests => 9;
248
249     $schema->storage->txn_begin;
250
251     $schema->resultset( 'Issue' )->delete_all;
252     $schema->resultset( 'Borrower' )->delete_all;
253     $schema->resultset( 'BorrowerAttribute' )->delete_all;
254     $schema->resultset( 'BorrowerAttributeType' )->delete_all;
255     $schema->resultset( 'Category' )->delete_all;
256     $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this
257     $schema->resultset( 'Branch' )->delete_all;
258
259     my $borrower = $builder->build({
260         source => 'Borrower',
261     });
262
263     my $query = CGI->new();
264     my $bad_result = C4::ILSDI::Services::LookupPatron($query);
265     is( $bad_result->{message}, 'PatronNotFound', 'No parameters' );
266
267     $query->delete_all();
268     $query->param( 'id', $borrower->{firstname} );
269     my $optional_result = C4::ILSDI::Services::LookupPatron($query);
270     is(
271         $optional_result->{id},
272         $borrower->{borrowernumber},
273         'Valid Firstname only'
274     );
275
276     $query->delete_all();
277     $query->param( 'id', 'ThereIsNoWayThatThisCouldPossiblyBeValid' );
278     my $bad_optional_result = C4::ILSDI::Services::LookupPatron($query);
279     is( $bad_optional_result->{message}, 'PatronNotFound', 'Invalid ID' );
280
281     foreach my $id_type (
282         'cardnumber',
283         'userid',
284         'email',
285         'borrowernumber',
286         'surname',
287         'firstname'
288     ) {
289         $query->delete_all();
290         $query->param( 'id_type', $id_type );
291         $query->param( 'id', $borrower->{$id_type} );
292         my $result = C4::ILSDI::Services::LookupPatron($query);
293         is( $result->{'id'}, $borrower->{borrowernumber}, "Checking $id_type" );
294     }
295
296     # Cleanup
297     $schema->storage->txn_rollback;
298 };
299
300 subtest 'Holds test' => sub {
301
302     plan tests => 5;
303
304     $schema->storage->txn_begin;
305
306     t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
307
308     my $patron = $builder->build({
309         source => 'Borrower',
310     });
311
312     my $biblio = $builder->build({
313         source => 'Biblio',
314     });
315
316     my $biblioitems = $builder->build({
317         source => 'Biblioitem',
318         value => {
319             biblionumber => $biblio->{biblionumber},
320         }
321     });
322
323     my $item = $builder->build({
324         source => 'Item',
325         value => {
326             biblionumber => $biblio->{biblionumber},
327             damaged => 1
328         }
329     });
330
331     my $query = new CGI;
332     $query->param( 'patron_id', $patron->{borrowernumber});
333     $query->param( 'bib_id', $biblio->{biblionumber});
334
335     my $reply = C4::ILSDI::Services::HoldTitle( $query );
336     is( $reply->{code}, 'damaged', "Item damaged" );
337
338     my $item_o = Koha::Items->find($item->{itemnumber});
339     $item_o->damaged(0)->store;
340
341     my $hold = $builder->build({
342         source => 'Reserve',
343         value => {
344             borrowernumber => $patron->{borrowernumber},
345             biblionumber => $biblio->{biblionumber},
346             itemnumber => $item->{itemnumber}
347         }
348     });
349
350     $reply = C4::ILSDI::Services::HoldTitle( $query );
351     is( $reply->{code}, 'itemAlreadyOnHold', "Item already on hold" );
352
353     my $biblio_with_no_item = $builder->build({
354         source => 'Biblio',
355     });
356
357     $query = new CGI;
358     $query->param( 'patron_id', $patron->{borrowernumber});
359     $query->param( 'bib_id', $biblio_with_no_item->{biblionumber});
360
361     $reply = C4::ILSDI::Services::HoldTitle( $query );
362     is( $reply->{code}, 'NoItems', 'Biblio has no item' );
363
364     my $biblio2 = $builder->build({
365         source => 'Biblio',
366     });
367
368     my $biblioitems2 = $builder->build({
369         source => 'Biblioitem',
370         value => {
371             biblionumber => $biblio2->{biblionumber},
372         }
373     });
374
375     my $item2 = $builder->build({
376         source => 'Item',
377         value => {
378             biblionumber => $biblio2->{biblionumber},
379             damaged => 0
380         }
381     });
382
383     t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
384     my $issuingrule = $builder->build({
385         source => 'Issuingrule',
386         value => {
387             categorycode => $patron->{categorycode},
388             itemtype => $item2->{itype},
389             branchcode => $patron->{branchcode},
390             reservesallowed => 0,
391         }
392     });
393
394     $query = new CGI;
395     $query->param( 'patron_id', $patron->{borrowernumber});
396     $query->param( 'bib_id', $biblio2->{biblionumber});
397     $query->param( 'item_id', $item2->{itemnumber});
398
399     $reply = C4::ILSDI::Services::HoldItem( $query );
400     is( $reply->{code}, 'tooManyReserves', "Too many reserves" );
401
402     my $biblio3 = $builder->build({
403         source => 'Biblio',
404     });
405
406     my $biblioitems3 = $builder->build({
407         source => 'Biblioitem',
408         value => {
409             biblionumber => $biblio3->{biblionumber},
410         }
411     });
412
413     # Adding a holdable item to biblio 3.
414     my $item3 = $builder->build({
415         source => 'Item',
416         value => {
417             biblionumber => $biblio3->{biblionumber},
418             damaged => 0,
419         }
420     });
421
422     my $item4 = $builder->build({
423         source => 'Item',
424         value => {
425             biblionumber => $biblio3->{biblionumber},
426             damaged => 1,
427         }
428     });
429
430     my $issuingrule2 = $builder->build({
431         source => 'Issuingrule',
432         value => {
433             categorycode => $patron->{categorycode},
434             itemtype => $item3->{itype},
435             branchcode => $patron->{branchcode},
436             reservesallowed => 10,
437         }
438     });
439
440     $query = new CGI;
441     $query->param( 'patron_id', $patron->{borrowernumber});
442     $query->param( 'bib_id', $biblio3->{biblionumber});
443     $query->param( 'item_id', $item4->{itemnumber});
444
445     $reply = C4::ILSDI::Services::HoldItem( $query );
446     is( $reply->{code}, 'damaged', "Item is damaged" );
447
448     $schema->storage->txn_rollback;
449 };