3 # Tests for C4::SIP::ILS
4 # Please help to extend them!
6 # This file is part of Koha.
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.
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.
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>.
23 use Test::More tests => 15;
25 use t::lib::TestBuilder;
28 use C4::Reserves qw( AddReserve );
29 use C4::Circulation qw( AddIssue );
30 use Koha::CirculationRules;
35 use_ok('C4::SIP::ILS');
38 my $schema = Koha::Database->new->schema;
39 $schema->storage->txn_begin;
41 my $builder = t::lib::TestBuilder->new();
43 my $class = 'C4::SIP::ILS';
44 my $institution = { id => 'CPL', };
46 my $ils = $class->new( $institution );
48 isa_ok( $ils, $class );
50 # Check all methods required for interface are there
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
57 can_ok( $ils, @methods );
59 is( $ils->institution(), 'CPL', 'institution method returns id' );
61 is( $ils->institution_id(), 'CPL', 'institution_id method returns id' );
63 is( $ils->supports('checkout'), 1, 'checkout supported' );
65 is( $ils->supports('security_inhibit'),
66 q{}, 'unsupported feature returns false' );
68 is( $ils->test_cardnumber_compare( 'A1234', 'a1234' ),
69 1, 'borrower bc test is case insensitive' );
71 is( $ils->test_cardnumber_compare( 'A1234', 'b1234' ),
72 q{}, 'borrower bc test identifies difference' );
74 subtest add_hold => sub {
77 my $library = $builder->build_object(
79 class => 'Koha::Libraries',
85 my $patron = $builder->build_object(
87 class => 'Koha::Patrons',
89 branchcode => $library->branchcode,
93 t::lib::Mocks::mock_userenv(
94 { branchcode => $library->branchcode, flags => 1 } );
96 my $item = $builder->build_sample_item(
98 library => $library->branchcode,
102 Koha::CirculationRules->set_rules(
104 categorycode => $patron->categorycode,
105 branchcode => $library->branchcode,
106 itemtype => $item->effective_itemtype,
109 reservesallowed => 3,
110 holds_per_record => 3,
112 lengthunit => 'days',
117 my $ils = C4::SIP::ILS->new( { id => $library->branchcode } );
119 # Send empty AD segments (i.e. empty string for patron_pwd)
120 my $transaction = $ils->add_hold( $patron->cardnumber, "", $item->barcode, undef );
122 $transaction->{screen_msg},
123 'Invalid patron password.',
124 "Empty password succeeds"
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");
132 subtest cancel_hold => sub {
135 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
136 my $patron = $builder->build_object(
138 class => 'Koha::Patrons',
140 branchcode => $library->branchcode,
144 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
146 my $item = $builder->build_sample_item({
147 library => $library->branchcode,
150 Koha::CirculationRules->set_rules(
152 categorycode => $patron->categorycode,
153 branchcode => $library->branchcode,
154 itemtype => $item->effective_itemtype,
157 reservesallowed => 3,
158 holds_per_record => 3,
160 lengthunit => 'days',
165 my $reserve1 = AddReserve(
167 branchcode => $library->branchcode,
168 borrowernumber => $patron->borrowernumber,
169 biblionumber => $item->biblio->biblionumber,
170 itemnumber => $item->itemnumber,
173 is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
174 is( $item->holds->count(),1,"Hold was placed on specific item");
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);
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");
183 is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
184 is( $item->holds->count(), 0, "Item has 0 holds remaining");
187 subtest cancel_waiting_hold => sub {
190 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
191 my $patron = $builder->build_object(
193 class => 'Koha::Patrons',
195 branchcode => $library->branchcode,
199 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
201 my $item = $builder->build_sample_item({
202 library => $library->branchcode,
205 Koha::CirculationRules->set_rules(
207 categorycode => $patron->categorycode,
208 branchcode => $library->branchcode,
209 itemtype => $item->effective_itemtype,
212 reservesallowed => 3,
213 holds_per_record => 3,
215 lengthunit => 'days',
220 my $reserve_id = AddReserve(
222 branchcode => $library->branchcode,
223 borrowernumber => $patron->borrowernumber,
224 biblionumber => $item->biblio->biblionumber,
225 itemnumber => $item->itemnumber,
228 is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
229 is( $item->holds->count(),1,"Hold was placed on specific item");
231 my $hold = Koha::Holds->find( $reserve_id );
232 ok( $hold, 'Get hold object' );
233 $hold->update({ found => 'W' });
234 $hold->get_from_storage;
236 is( $hold->found, 'W', "Hold was correctly set to waiting." );
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);
242 is( $transaction->{screen_msg},"Hold Cancelled.","We get a success message when hold cancelled");
244 is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
245 is( $item->holds->count(), 0, "Item has 0 holds remaining");
248 subtest checkout => sub {
251 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
252 my $patron = $builder->build_object(
254 class => 'Koha::Patrons',
256 branchcode => $library->branchcode,
260 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
262 my $item = $builder->build_sample_item({
263 library => $library->branchcode,
266 Koha::CirculationRules->set_rules(
268 categorycode => $patron->categorycode,
269 branchcode => $library->branchcode,
270 itemtype => $item->effective_itemtype,
273 reservesallowed => 3,
274 holds_per_record => 3,
276 lengthunit => 'days',
277 renewalsallowed => 6,
282 AddIssue( $patron->unblessed, $item->barcode, undef, 0 );
283 my $checkout = $item->checkout;
284 ok( defined($checkout), "Checkout added");
285 is( $checkout->renewals_count, 0, "Correct renewals");
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,undef,undef);
291 is( $transaction->{screen_msg},"Item already checked out to you: renewing item.","We get a success message when issue is renewed");
293 $checkout->discard_changes();
294 is( $checkout->renewals_count, 1, "Renewals has been reduced");
297 subtest renew_all => sub {
300 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
301 my $patron = $builder->build_object(
303 class => 'Koha::Patrons',
305 branchcode => $library->branchcode,
309 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
311 my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
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" );
318 subtest renew => sub {
321 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
322 my $patron = $builder->build_object(
324 class => 'Koha::Patrons',
326 branchcode => $library->branchcode,
330 t::lib::Mocks::mock_userenv( { branchcode => $library->branchcode } );
332 my $item = $builder->build_sample_item(
334 library => $library->branchcode,
338 AddIssue( $patron->unblessed, $item->barcode, undef, 0 );
339 my $checkout = $item->checkout;
340 ok( defined($checkout), "Successfully checked out an item prior to renewal" );
342 my $ils = C4::SIP::ILS->new( { id => $library->branchcode } );
344 my $transaction = $ils->renew( $patron->cardnumber, "", $item->barcode );
346 is( $transaction->{renewal_ok}, 1, "Renewal succeeded" );
350 $schema->storage->txn_rollback;