ebfdeea9c64046223a1ac006dbcf6cab0a64b1ad
[srvgit] / 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 => 13;
24
25 use t::lib::TestBuilder;
26 use t::lib::Mocks;
27
28 use C4::Reserves;
29 use C4::Circulation;
30 use Koha::CirculationRules;
31 use Koha::Database;
32 use Koha::DateUtils;
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( { class => 'Koha::Libraries' } );
78     my $patron = $builder->build_object(
79         {
80             class => 'Koha::Patrons',
81             value => {
82                 branchcode => $library->branchcode,
83             }
84         }
85     );
86     t::lib::Mocks::mock_userenv(
87         { branchcode => $library->branchcode, flags => 1 } );
88
89     my $item = $builder->build_sample_item(
90         {
91             library => $library->branchcode,
92         }
93     );
94
95     Koha::CirculationRules->set_rules(
96         {
97             categorycode => $patron->categorycode,
98             branchcode   => $library->branchcode,
99             itemtype     => $item->effective_itemtype,
100             rules        => {
101                 onshelfholds     => 1,
102                 reservesallowed  => 3,
103                 holds_per_record => 3,
104                 issuelength      => 5,
105                 lengthunit       => 'days',
106             }
107         }
108     );
109
110     my $ils = C4::SIP::ILS->new( { id => $library->branchcode } );
111
112     # Send empty AD segments (i.e. empty string for patron_pwd)
113     my $transaction = $ils->add_hold( $patron->cardnumber, "", $item->barcode, undef );
114     isnt(
115         $transaction->{screen_msg},
116         'Invalid patron password.',
117         "Empty password succeeds"
118     );
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");
123 };
124
125 subtest cancel_hold => sub {
126     plan tests => 6;
127
128     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
129     my $patron = $builder->build_object(
130         {
131             class => 'Koha::Patrons',
132             value => {
133                 branchcode => $library->branchcode,
134             }
135         }
136     );
137     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
138
139     my $item = $builder->build_sample_item({
140         library       => $library->branchcode,
141     });
142
143     Koha::CirculationRules->set_rules(
144         {
145             categorycode => $patron->categorycode,
146             branchcode   => $library->branchcode,
147             itemtype     => $item->effective_itemtype,
148             rules        => {
149                 onshelfholds     => 1,
150                 reservesallowed  => 3,
151                 holds_per_record => 3,
152                 issuelength      => 5,
153                 lengthunit       => 'days',
154             }
155         }
156     );
157
158     my $reserve1 = AddReserve(
159         {
160             branchcode     => $library->branchcode,
161             borrowernumber => $patron->borrowernumber,
162             biblionumber   => $item->biblio->biblionumber,
163             itemnumber     => $item->itemnumber,
164         }
165     );
166     is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
167     is( $item->holds->count(),1,"Hold was placed on specific item");
168
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);
172
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");
175
176     is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
177     is( $item->holds->count(), 0,  "Item has 0 holds remaining");
178 };
179
180 subtest checkout => sub {
181     plan tests => 4;
182
183     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
184     my $patron = $builder->build_object(
185         {
186             class => 'Koha::Patrons',
187             value => {
188                 branchcode => $library->branchcode,
189             }
190         }
191     );
192     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
193
194     my $item = $builder->build_sample_item({
195         library       => $library->branchcode,
196     });
197
198     Koha::CirculationRules->set_rules(
199         {
200             categorycode => $patron->categorycode,
201             branchcode   => $library->branchcode,
202             itemtype     => $item->effective_itemtype,
203             rules        => {
204                 onshelfholds     => 1,
205                 reservesallowed  => 3,
206                 holds_per_record => 3,
207                 issuelength      => 5,
208                 lengthunit       => 'days',
209                 renewalsallowed  => 6,
210             }
211         }
212     );
213
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");
218
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);
222
223     is( $transaction->{screen_msg},"Item already checked out to you: renewing item.","We get a success message when issue is renewed");
224
225     $checkout->discard_changes();
226     is( $checkout->renewals, 1, "Renewals has been reduced");
227 };
228
229 subtest renew_all => sub {
230     plan tests => 1;
231
232     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
233     my $patron = $builder->build_object(
234         {
235             class => 'Koha::Patrons',
236             value => {
237                 branchcode => $library->branchcode,
238             }
239         }
240     );
241     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
242
243     my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
244
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" );
248 };
249
250 $schema->storage->txn_rollback;