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 => 14;
25 use t::lib::TestBuilder;
28 use C4::Reserves qw( AddReserve );
29 use C4::Circulation qw( AddIssue );
30 use Koha::CirculationRules;
36 use_ok('C4::SIP::ILS');
39 my $schema = Koha::Database->new->schema;
40 $schema->storage->txn_begin;
42 my $builder = t::lib::TestBuilder->new();
44 my $class = 'C4::SIP::ILS';
45 my $institution = { id => 'CPL', };
47 my $ils = $class->new( $institution );
49 isa_ok( $ils, $class );
51 # Check all methods required for interface are there
53 find_patron find_item checkout_ok checkin_ok offline_ok status_update_ok
54 offline_ok checkout checkin end_patron_session pay_fee add_hold cancel_hold
55 alter_hold renew renew_all
58 can_ok( $ils, @methods );
60 is( $ils->institution(), 'CPL', 'institution method returns id' );
62 is( $ils->institution_id(), 'CPL', 'institution_id method returns id' );
64 is( $ils->supports('checkout'), 1, 'checkout supported' );
66 is( $ils->supports('security_inhibit'),
67 q{}, 'unsupported feature returns false' );
69 is( $ils->test_cardnumber_compare( 'A1234', 'a1234' ),
70 1, 'borrower bc test is case insensitive' );
72 is( $ils->test_cardnumber_compare( 'A1234', 'b1234' ),
73 q{}, 'borrower bc test identifies difference' );
75 subtest add_hold => sub {
78 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
79 my $patron = $builder->build_object(
81 class => 'Koha::Patrons',
83 branchcode => $library->branchcode,
87 t::lib::Mocks::mock_userenv(
88 { branchcode => $library->branchcode, flags => 1 } );
90 my $item = $builder->build_sample_item(
92 library => $library->branchcode,
96 Koha::CirculationRules->set_rules(
98 categorycode => $patron->categorycode,
99 branchcode => $library->branchcode,
100 itemtype => $item->effective_itemtype,
103 reservesallowed => 3,
104 holds_per_record => 3,
106 lengthunit => 'days',
111 my $ils = C4::SIP::ILS->new( { id => $library->branchcode } );
113 # Send empty AD segments (i.e. empty string for patron_pwd)
114 my $transaction = $ils->add_hold( $patron->cardnumber, "", $item->barcode, undef );
116 $transaction->{screen_msg},
117 'Invalid patron password.',
118 "Empty password succeeds"
120 ok( $transaction->{ok}, "Transaction returned success");
121 is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
122 # FIXME: Should we not allow for item-level holds when we're passed an item barcode...
123 is( $item->holds->count(),0,"Hold was placed at bib level");
126 subtest cancel_hold => sub {
129 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
130 my $patron = $builder->build_object(
132 class => 'Koha::Patrons',
134 branchcode => $library->branchcode,
138 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
140 my $item = $builder->build_sample_item({
141 library => $library->branchcode,
144 Koha::CirculationRules->set_rules(
146 categorycode => $patron->categorycode,
147 branchcode => $library->branchcode,
148 itemtype => $item->effective_itemtype,
151 reservesallowed => 3,
152 holds_per_record => 3,
154 lengthunit => 'days',
159 my $reserve1 = AddReserve(
161 branchcode => $library->branchcode,
162 borrowernumber => $patron->borrowernumber,
163 biblionumber => $item->biblio->biblionumber,
164 itemnumber => $item->itemnumber,
167 is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
168 is( $item->holds->count(),1,"Hold was placed on specific item");
170 my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
171 my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
172 my $transaction = $ils->cancel_hold($patron->cardnumber,"",$item->barcode,undef);
174 isnt( $transaction->{screen_msg}, 'Invalid patron password.', "Empty password succeeds" );
175 is( $transaction->{screen_msg},"Hold Cancelled.","We get a success message when hold cancelled");
177 is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
178 is( $item->holds->count(), 0, "Item has 0 holds remaining");
181 subtest cancel_waiting_hold => sub {
184 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
185 my $patron = $builder->build_object(
187 class => 'Koha::Patrons',
189 branchcode => $library->branchcode,
193 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
195 my $item = $builder->build_sample_item({
196 library => $library->branchcode,
199 Koha::CirculationRules->set_rules(
201 categorycode => $patron->categorycode,
202 branchcode => $library->branchcode,
203 itemtype => $item->effective_itemtype,
206 reservesallowed => 3,
207 holds_per_record => 3,
209 lengthunit => 'days',
214 my $reserve_id = AddReserve(
216 branchcode => $library->branchcode,
217 borrowernumber => $patron->borrowernumber,
218 biblionumber => $item->biblio->biblionumber,
219 itemnumber => $item->itemnumber,
222 is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
223 is( $item->holds->count(),1,"Hold was placed on specific item");
225 my $hold = Koha::Holds->find( $reserve_id );
226 ok( $hold, 'Get hold object' );
227 $hold->update({ found => 'W' });
228 $hold->get_from_storage;
230 is( $hold->found, 'W', "Hold was correctly set to waiting." );
232 my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
233 my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
234 my $transaction = $ils->cancel_hold($patron->cardnumber,undef,$item->barcode,undef);
236 is( $transaction->{screen_msg},"Hold Cancelled.","We get a success message when hold cancelled");
238 is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
239 is( $item->holds->count(), 0, "Item has 0 holds remaining");
242 subtest checkout => sub {
245 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
246 my $patron = $builder->build_object(
248 class => 'Koha::Patrons',
250 branchcode => $library->branchcode,
254 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
256 my $item = $builder->build_sample_item({
257 library => $library->branchcode,
260 Koha::CirculationRules->set_rules(
262 categorycode => $patron->categorycode,
263 branchcode => $library->branchcode,
264 itemtype => $item->effective_itemtype,
267 reservesallowed => 3,
268 holds_per_record => 3,
270 lengthunit => 'days',
271 renewalsallowed => 6,
276 AddIssue( $patron->unblessed, $item->barcode, undef, 0 );
277 my $checkout = $item->checkout;
278 ok( defined($checkout), "Checkout added");
279 is( $checkout->renewals, 0, "Correct renewals");
281 my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
282 my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
283 my $transaction = $ils->checkout($patron->cardnumber,$item->barcode,undef,undef);
285 is( $transaction->{screen_msg},"Item already checked out to you: renewing item.","We get a success message when issue is renewed");
287 $checkout->discard_changes();
288 is( $checkout->renewals, 1, "Renewals has been reduced");
291 subtest renew_all => sub {
294 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
295 my $patron = $builder->build_object(
297 class => 'Koha::Patrons',
299 branchcode => $library->branchcode,
303 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
305 my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
307 # Send empty AD segments (i.e. empty string for patron_pwd)
308 my $transaction = $ils->renew_all( $patron->cardnumber, "", undef );
309 isnt( $transaction->{screen_msg}, 'Invalid patron password.', "Empty password succeeds" );
312 $schema->storage->txn_rollback;