Bug 32496: Fix tests
[koha.git] / t / db_dependent / SIP / ILS.t
1 #!/usr/bin/perl
2
3 # Tests for C4::SIP::ILS
4 # Please help to extend them!
5
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 use Test::More tests => 15;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use C4::Reserves qw( AddReserve );
29 use C4::Circulation qw( AddIssue );
30 use Koha::CirculationRules;
31 use Koha::Database;
32 use Koha::Holds;
33
34 BEGIN {
35     use_ok('C4::SIP::ILS');
36 }
37
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
40
41 my $builder = t::lib::TestBuilder->new();
42
43 my $class = 'C4::SIP::ILS';
44 my $institution = { id => 'CPL', };
45
46 my $ils = $class->new( $institution );
47
48 isa_ok( $ils, $class );
49
50 # Check all methods required for interface are there
51 my @methods = qw(
52     find_patron find_item checkout_ok checkin_ok offline_ok status_update_ok
53     offline_ok checkout checkin end_patron_session pay_fee add_hold cancel_hold
54     alter_hold renew renew_all
55 );
56
57 can_ok( $ils, @methods );
58
59 is( $ils->institution(), 'CPL', 'institution method returns id' );
60
61 is( $ils->institution_id(), 'CPL', 'institution_id method returns id' );
62
63 is( $ils->supports('checkout'), 1, 'checkout supported' );
64
65 is( $ils->supports('security_inhibit'),
66     q{}, 'unsupported feature returns false' );
67
68 is( $ils->test_cardnumber_compare( 'A1234', 'a1234' ),
69     1, 'borrower bc test is case insensitive' );
70
71 is( $ils->test_cardnumber_compare( 'A1234', 'b1234' ),
72     q{}, 'borrower bc test identifies difference' );
73
74 subtest add_hold => sub {
75     plan tests => 4;
76
77     my $library = $builder->build_object(
78         {
79             class => 'Koha::Libraries',
80             value => {
81                 pickup_location => 1
82             }
83         }
84     );
85     my $patron = $builder->build_object(
86         {
87             class => 'Koha::Patrons',
88             value => {
89                 branchcode => $library->branchcode,
90             }
91         }
92     );
93     t::lib::Mocks::mock_userenv(
94         { branchcode => $library->branchcode, flags => 1 } );
95
96     my $item = $builder->build_sample_item(
97         {
98             library => $library->branchcode,
99         }
100     );
101
102     Koha::CirculationRules->set_rules(
103         {
104             categorycode => $patron->categorycode,
105             branchcode   => $library->branchcode,
106             itemtype     => $item->effective_itemtype,
107             rules        => {
108                 onshelfholds     => 1,
109                 reservesallowed  => 3,
110                 holds_per_record => 3,
111                 issuelength      => 5,
112                 lengthunit       => 'days',
113             }
114         }
115     );
116
117     my $ils = C4::SIP::ILS->new( { id => $library->branchcode } );
118
119     # Send empty AD segments (i.e. empty string for patron_pwd)
120     my $transaction = $ils->add_hold( $patron->cardnumber, "", $item->barcode, undef );
121     isnt(
122         $transaction->{screen_msg},
123         'Invalid patron password.',
124         "Empty password succeeds"
125     );
126     ok( $transaction->{ok}, "Transaction returned success");
127     is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
128     # FIXME: Should we not allow for item-level holds when we're passed an item barcode...
129     is( $item->holds->count(),0,"Hold was placed at bib level");
130 };
131
132 subtest cancel_hold => sub {
133     plan tests => 6;
134
135     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
136     my $patron = $builder->build_object(
137         {
138             class => 'Koha::Patrons',
139             value => {
140                 branchcode => $library->branchcode,
141             }
142         }
143     );
144     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
145
146     my $item = $builder->build_sample_item({
147         library       => $library->branchcode,
148     });
149
150     Koha::CirculationRules->set_rules(
151         {
152             categorycode => $patron->categorycode,
153             branchcode   => $library->branchcode,
154             itemtype     => $item->effective_itemtype,
155             rules        => {
156                 onshelfholds     => 1,
157                 reservesallowed  => 3,
158                 holds_per_record => 3,
159                 issuelength      => 5,
160                 lengthunit       => 'days',
161             }
162         }
163     );
164
165     my $reserve1 = AddReserve(
166         {
167             branchcode     => $library->branchcode,
168             borrowernumber => $patron->borrowernumber,
169             biblionumber   => $item->biblio->biblionumber,
170             itemnumber     => $item->itemnumber,
171         }
172     );
173     is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
174     is( $item->holds->count(),1,"Hold was placed on specific item");
175
176     my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
177     my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
178     my $transaction = $ils->cancel_hold($patron->cardnumber,"",$item->barcode,undef);
179
180     isnt( $transaction->{screen_msg}, 'Invalid patron password.', "Empty password succeeds" );
181     is( $transaction->{screen_msg},"Hold Cancelled.","We get a success message when hold cancelled");
182
183     is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
184     is( $item->holds->count(), 0,  "Item has 0 holds remaining");
185 };
186
187 subtest cancel_waiting_hold => sub {
188     plan tests => 7;
189
190     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
191     my $patron = $builder->build_object(
192         {
193             class => 'Koha::Patrons',
194             value => {
195                 branchcode => $library->branchcode,
196             }
197         }
198     );
199     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
200
201     my $item = $builder->build_sample_item({
202         library => $library->branchcode,
203     });
204
205     Koha::CirculationRules->set_rules(
206         {
207             categorycode => $patron->categorycode,
208             branchcode   => $library->branchcode,
209             itemtype     => $item->effective_itemtype,
210             rules        => {
211                 onshelfholds     => 1,
212                 reservesallowed  => 3,
213                 holds_per_record => 3,
214                 issuelength      => 5,
215                 lengthunit       => 'days',
216             }
217         }
218     );
219
220     my $reserve_id = AddReserve(
221         {
222             branchcode     => $library->branchcode,
223             borrowernumber => $patron->borrowernumber,
224             biblionumber   => $item->biblio->biblionumber,
225             itemnumber     => $item->itemnumber,
226         }
227     );
228     is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
229     is( $item->holds->count(),1,"Hold was placed on specific item");
230
231     my $hold = Koha::Holds->find( $reserve_id );
232     ok( $hold, 'Get hold object' );
233     $hold->update({ found => 'W' });
234     $hold->get_from_storage;
235
236     is( $hold->found, 'W', "Hold was correctly set to waiting." );
237
238     my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
239     my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
240     my $transaction = $ils->cancel_hold($patron->cardnumber,undef,$item->barcode,undef);
241
242     is( $transaction->{screen_msg},"Hold Cancelled.","We get a success message when hold cancelled");
243
244     is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
245     is( $item->holds->count(), 0,  "Item has 0 holds remaining");
246 };
247
248 subtest checkout => sub {
249     plan tests => 4;
250
251     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
252     my $patron = $builder->build_object(
253         {
254             class => 'Koha::Patrons',
255             value => {
256                 branchcode => $library->branchcode,
257             }
258         }
259     );
260     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
261
262     my $item = $builder->build_sample_item({
263         library => $library->branchcode,
264     });
265
266     Koha::CirculationRules->set_rules(
267         {
268             categorycode => $patron->categorycode,
269             branchcode   => $library->branchcode,
270             itemtype     => $item->effective_itemtype,
271             rules        => {
272                 onshelfholds     => 1,
273                 reservesallowed  => 3,
274                 holds_per_record => 3,
275                 issuelength      => 5,
276                 lengthunit       => 'days',
277                 renewalsallowed  => 6,
278             }
279         }
280     );
281
282     AddIssue( $patron, $item->barcode, undef, 0 );
283     my $checkout = $item->checkout;
284     ok( defined($checkout), "Checkout added");
285     is( $checkout->renewals_count, 0, "Correct renewals");
286
287     my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
288     my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
289     my $transaction = $ils->checkout($patron->cardnumber, $item->barcode);
290
291     is( $transaction->{screen_msg},"Item already checked out to you: renewing item.", "We get a success message when issue is renewed");
292
293     $checkout->discard_changes();
294     is( $checkout->renewals_count, 1, "Renewals has been reduced");
295 };
296
297 subtest renew_all => sub {
298     plan tests => 1;
299
300     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
301     my $patron = $builder->build_object(
302         {
303             class => 'Koha::Patrons',
304             value => {
305                 branchcode => $library->branchcode,
306             }
307         }
308     );
309     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
310
311     my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
312
313     # Send empty AD segments (i.e. empty string for patron_pwd)
314     my $transaction = $ils->renew_all( $patron->cardnumber, "", undef );
315     isnt( $transaction->{screen_msg}, 'Invalid patron password.', "Empty password succeeds" );
316 };
317
318 subtest renew => sub {
319     plan tests => 2;
320
321     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
322     my $patron  = $builder->build_object(
323         {
324             class => 'Koha::Patrons',
325             value => {
326                 branchcode => $library->branchcode,
327             }
328         }
329     );
330     t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
331
332     Koha::CirculationRules->set_rule(
333         {
334             categorycode => undef,
335             itemtype     => undef,
336             branchcode   => undef,
337             rule_name    => 'renewalsallowed',
338             rule_value   => '5',
339         }
340     );
341
342     my $item = $builder->build_sample_item(
343         {
344             library => $library->branchcode,
345         }
346     );
347
348     AddIssue( $patron, $item->barcode, undef, 0 );
349     my $checkout = $item->checkout;
350     ok( defined($checkout), "Successfully checked out an item prior to renewal" );
351
352     my $ils = C4::SIP::ILS->new( { id => $library->branchcode } );
353
354     my $transaction = $ils->renew( $patron->cardnumber, "", $item->barcode );
355
356     is( $transaction->{renewal_ok}, 1, "Renewal succeeded" );
357
358 };
359
360 $schema->storage->txn_rollback;