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 => 13;
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( { class => 'Koha::Libraries' } );
78 my $patron = $builder->build_object(
80 class => 'Koha::Patrons',
82 branchcode => $library->branchcode,
86 t::lib::Mocks::mock_userenv(
87 { branchcode => $library->branchcode, flags => 1 } );
89 my $item = $builder->build_sample_item(
91 library => $library->branchcode,
95 Koha::CirculationRules->set_rules(
97 categorycode => $patron->categorycode,
98 branchcode => $library->branchcode,
99 itemtype => $item->effective_itemtype,
102 reservesallowed => 3,
103 holds_per_record => 3,
105 lengthunit => 'days',
110 my $ils = C4::SIP::ILS->new( { id => $library->branchcode } );
112 # Send empty AD segments (i.e. empty string for patron_pwd)
113 my $transaction = $ils->add_hold( $patron->cardnumber, "", $item->barcode, undef );
115 $transaction->{screen_msg},
116 'Invalid patron password.',
117 "Empty password succeeds"
119 ok( $transaction->{ok}, "Transaction returned success");
120 is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
121 # FIXME: Should we not allow for item-level holds when we're passed an item barcode...
122 is( $item->holds->count(),0,"Hold was placed at bib level");
125 subtest cancel_hold => sub {
128 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
129 my $patron = $builder->build_object(
131 class => 'Koha::Patrons',
133 branchcode => $library->branchcode,
137 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
139 my $item = $builder->build_sample_item({
140 library => $library->branchcode,
143 Koha::CirculationRules->set_rules(
145 categorycode => $patron->categorycode,
146 branchcode => $library->branchcode,
147 itemtype => $item->effective_itemtype,
150 reservesallowed => 3,
151 holds_per_record => 3,
153 lengthunit => 'days',
158 my $reserve1 = AddReserve(
160 branchcode => $library->branchcode,
161 borrowernumber => $patron->borrowernumber,
162 biblionumber => $item->biblio->biblionumber,
163 itemnumber => $item->itemnumber,
166 is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
167 is( $item->holds->count(),1,"Hold was placed on specific item");
169 my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
170 my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
171 my $transaction = $ils->cancel_hold($patron->cardnumber,"",$item->barcode,undef);
173 isnt( $transaction->{screen_msg}, 'Invalid patron password.', "Empty password succeeds" );
174 is( $transaction->{screen_msg},"Hold Cancelled.","We get a success message when hold cancelled");
176 is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
177 is( $item->holds->count(), 0, "Item has 0 holds remaining");
180 subtest checkout => sub {
183 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
184 my $patron = $builder->build_object(
186 class => 'Koha::Patrons',
188 branchcode => $library->branchcode,
192 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
194 my $item = $builder->build_sample_item({
195 library => $library->branchcode,
198 Koha::CirculationRules->set_rules(
200 categorycode => $patron->categorycode,
201 branchcode => $library->branchcode,
202 itemtype => $item->effective_itemtype,
205 reservesallowed => 3,
206 holds_per_record => 3,
208 lengthunit => 'days',
209 renewalsallowed => 6,
214 AddIssue( $patron->unblessed, $item->barcode, undef, 0 );
215 my $checkout = $item->checkout;
216 ok( defined($checkout), "Checkout added");
217 is( $checkout->renewals, 0, "Correct renewals");
219 my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
220 my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
221 my $transaction = $ils->checkout($patron->cardnumber,$item->barcode,undef,undef);
223 is( $transaction->{screen_msg},"Item already checked out to you: renewing item.","We get a success message when issue is renewed");
225 $checkout->discard_changes();
226 is( $checkout->renewals, 1, "Renewals has been reduced");
229 subtest renew_all => sub {
232 my $library = $builder->build_object ({ class => 'Koha::Libraries' });
233 my $patron = $builder->build_object(
235 class => 'Koha::Patrons',
237 branchcode => $library->branchcode,
241 t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
243 my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
245 # Send empty AD segments (i.e. empty string for patron_pwd)
246 my $transaction = $ils->renew_all( $patron->cardnumber, "", undef );
247 isnt( $transaction->{screen_msg}, 'Invalid patron password.', "Empty password succeeds" );
250 $schema->storage->txn_rollback;