Bug 28464: Add unit 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 => 14;
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::DateUtils;
33 use Koha::Holds;
34
35 BEGIN {
36     use_ok('C4::SIP::ILS');
37 }
38
39 my $schema = Koha::Database->new->schema;
40 $schema->storage->txn_begin;
41
42 my $builder = t::lib::TestBuilder->new();
43
44 my $class = 'C4::SIP::ILS';
45 my $institution = { id => 'CPL', };
46
47 my $ils = $class->new( $institution );
48
49 isa_ok( $ils, $class );
50
51 # Check all methods required for interface are there
52 my @methods = qw(
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
56 );
57
58 can_ok( $ils, @methods );
59
60 is( $ils->institution(), 'CPL', 'institution method returns id' );
61
62 is( $ils->institution_id(), 'CPL', 'institution_id method returns id' );
63
64 is( $ils->supports('checkout'), 1, 'checkout supported' );
65
66 is( $ils->supports('security_inhibit'),
67     q{}, 'unsupported feature returns false' );
68
69 is( $ils->test_cardnumber_compare( 'A1234', 'a1234' ),
70     1, 'borrower bc test is case insensitive' );
71
72 is( $ils->test_cardnumber_compare( 'A1234', 'b1234' ),
73     q{}, 'borrower bc test identifies difference' );
74
75 subtest add_hold => sub {
76     plan tests => 4;
77
78     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
79     my $patron = $builder->build_object(
80         {
81             class => 'Koha::Patrons',
82             value => {
83                 branchcode => $library->branchcode,
84             }
85         }
86     );
87     t::lib::Mocks::mock_userenv(
88         { branchcode => $library->branchcode, flags => 1 } );
89
90     my $item = $builder->build_sample_item(
91         {
92             library => $library->branchcode,
93         }
94     );
95
96     Koha::CirculationRules->set_rules(
97         {
98             categorycode => $patron->categorycode,
99             branchcode   => $library->branchcode,
100             itemtype     => $item->effective_itemtype,
101             rules        => {
102                 onshelfholds     => 1,
103                 reservesallowed  => 3,
104                 holds_per_record => 3,
105                 issuelength      => 5,
106                 lengthunit       => 'days',
107             }
108         }
109     );
110
111     my $ils = C4::SIP::ILS->new( { id => $library->branchcode } );
112
113     # Send empty AD segments (i.e. empty string for patron_pwd)
114     my $transaction = $ils->add_hold( $patron->cardnumber, "", $item->barcode, undef );
115     isnt(
116         $transaction->{screen_msg},
117         'Invalid patron password.',
118         "Empty password succeeds"
119     );
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");
124 };
125
126 subtest cancel_hold => sub {
127     plan tests => 6;
128
129     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
130     my $patron = $builder->build_object(
131         {
132             class => 'Koha::Patrons',
133             value => {
134                 branchcode => $library->branchcode,
135             }
136         }
137     );
138     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
139
140     my $item = $builder->build_sample_item({
141         library       => $library->branchcode,
142     });
143
144     Koha::CirculationRules->set_rules(
145         {
146             categorycode => $patron->categorycode,
147             branchcode   => $library->branchcode,
148             itemtype     => $item->effective_itemtype,
149             rules        => {
150                 onshelfholds     => 1,
151                 reservesallowed  => 3,
152                 holds_per_record => 3,
153                 issuelength      => 5,
154                 lengthunit       => 'days',
155             }
156         }
157     );
158
159     my $reserve1 = AddReserve(
160         {
161             branchcode     => $library->branchcode,
162             borrowernumber => $patron->borrowernumber,
163             biblionumber   => $item->biblio->biblionumber,
164             itemnumber     => $item->itemnumber,
165         }
166     );
167     is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
168     is( $item->holds->count(),1,"Hold was placed on specific item");
169
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);
173
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");
176
177     is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
178     is( $item->holds->count(), 0,  "Item has 0 holds remaining");
179 };
180
181 subtest cancel_waiting_hold => sub {
182     plan tests => 7;
183
184     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
185     my $patron = $builder->build_object(
186         {
187             class => 'Koha::Patrons',
188             value => {
189                 branchcode => $library->branchcode,
190             }
191         }
192     );
193     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
194
195     my $item = $builder->build_sample_item({
196         library       => $library->branchcode,
197     });
198
199     Koha::CirculationRules->set_rules(
200         {
201             categorycode => $patron->categorycode,
202             branchcode   => $library->branchcode,
203             itemtype     => $item->effective_itemtype,
204             rules        => {
205                 onshelfholds     => 1,
206                 reservesallowed  => 3,
207                 holds_per_record => 3,
208                 issuelength      => 5,
209                 lengthunit       => 'days',
210             }
211         }
212     );
213
214     my $reserve_id = AddReserve(
215         {
216             branchcode     => $library->branchcode,
217             borrowernumber => $patron->borrowernumber,
218             biblionumber   => $item->biblio->biblionumber,
219             itemnumber     => $item->itemnumber,
220         }
221     );
222     is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
223     is( $item->holds->count(),1,"Hold was placed on specific item");
224
225     my $hold = Koha::Holds->find( $reserve_id );
226     ok( $hold, 'Get hold object' );
227     $hold->update({ found => 'W' });
228     $hold->get_from_storage;
229
230     is( $hold->found, 'W', "Hold was correctly set to waiting." );
231
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);
235
236     is( $transaction->{screen_msg},"Hold Cancelled.","We get a success message when hold cancelled");
237
238     is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
239     is( $item->holds->count(), 0,  "Item has 0 holds remaining");
240 };
241
242 subtest checkout => sub {
243     plan tests => 4;
244
245     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
246     my $patron = $builder->build_object(
247         {
248             class => 'Koha::Patrons',
249             value => {
250                 branchcode => $library->branchcode,
251             }
252         }
253     );
254     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
255
256     my $item = $builder->build_sample_item({
257         library       => $library->branchcode,
258     });
259
260     Koha::CirculationRules->set_rules(
261         {
262             categorycode => $patron->categorycode,
263             branchcode   => $library->branchcode,
264             itemtype     => $item->effective_itemtype,
265             rules        => {
266                 onshelfholds     => 1,
267                 reservesallowed  => 3,
268                 holds_per_record => 3,
269                 issuelength      => 5,
270                 lengthunit       => 'days',
271                 renewalsallowed  => 6,
272             }
273         }
274     );
275
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");
280
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);
284
285     is( $transaction->{screen_msg},"Item already checked out to you: renewing item.","We get a success message when issue is renewed");
286
287     $checkout->discard_changes();
288     is( $checkout->renewals, 1, "Renewals has been reduced");
289 };
290
291 subtest renew_all => sub {
292     plan tests => 1;
293
294     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
295     my $patron = $builder->build_object(
296         {
297             class => 'Koha::Patrons',
298             value => {
299                 branchcode => $library->branchcode,
300             }
301         }
302     );
303     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
304
305     my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
306
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" );
310 };
311
312 $schema->storage->txn_rollback;