Bug 27600: Add unit test
[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 => 12;
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 cancel_hold => sub {
75     plan tests => 5;
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({ branchcode => $library->branchcode, flags => 1 });
87
88     my $item = $builder->build_sample_item({
89         library       => $library->branchcode,
90     });
91
92     Koha::CirculationRules->set_rules(
93         {
94             categorycode => $patron->categorycode,
95             branchcode   => $library->branchcode,
96             itemtype     => $item->effective_itemtype,
97             rules        => {
98                 onshelfholds     => 1,
99                 reservesallowed  => 3,
100                 holds_per_record => 3,
101                 issuelength      => 5,
102                 lengthunit       => 'days',
103             }
104         }
105     );
106
107     my $reserve1 = AddReserve(
108         {
109             branchcode     => $library->branchcode,
110             borrowernumber => $patron->borrowernumber,
111             biblionumber   => $item->biblio->biblionumber,
112             itemnumber     => $item->itemnumber,
113         }
114     );
115     is( $item->biblio->holds->count(), 1, "Hold was placed on bib");
116     is( $item->holds->count(),1,"Hold was placed on specific item");
117
118     my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
119     my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
120     my $transaction = $ils->cancel_hold($patron->cardnumber,undef,$item->barcode,undef);
121
122     is( $transaction->{screen_msg},"Hold Cancelled.","We get a success message when hold cancelled");
123
124     is( $item->biblio->holds->count(), 0, "Bib has 0 holds remaining");
125     is( $item->holds->count(), 0,  "Item has 0 holds remaining");
126 };
127
128 subtest checkout => sub {
129     plan tests => 4;
130
131     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
132     my $patron = $builder->build_object(
133         {
134             class => 'Koha::Patrons',
135             value => {
136                 branchcode => $library->branchcode,
137             }
138         }
139     );
140     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
141
142     my $item = $builder->build_sample_item({
143         library       => $library->branchcode,
144     });
145
146     Koha::CirculationRules->set_rules(
147         {
148             categorycode => $patron->categorycode,
149             branchcode   => $library->branchcode,
150             itemtype     => $item->effective_itemtype,
151             rules        => {
152                 onshelfholds     => 1,
153                 reservesallowed  => 3,
154                 holds_per_record => 3,
155                 issuelength      => 5,
156                 lengthunit       => 'days',
157                 renewalsallowed  => 6,
158             }
159         }
160     );
161
162     AddIssue( $patron->unblessed, $item->barcode, undef, 0 );
163     my $checkout = $item->checkout;
164     ok( defined($checkout), "Checkout added");
165     is( $checkout->renewals, 0, "Correct renewals");
166
167     my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
168     my $sip_patron = C4::SIP::ILS::Patron->new( $patron->cardnumber );
169     my $transaction = $ils->checkout($patron->cardnumber,$item->barcode,undef,undef);
170
171     is( $transaction->{screen_msg},"Item already checked out to you: renewing item.","We get a success message when issue is renewed");
172
173     $checkout->discard_changes();
174     is( $checkout->renewals, 1, "Renewals has been reduced");
175 };
176
177 subtest renew_all => sub {
178     plan tests => 1;
179
180     my $library = $builder->build_object ({ class => 'Koha::Libraries' });
181     my $patron = $builder->build_object(
182         {
183             class => 'Koha::Patrons',
184             value => {
185                 branchcode => $library->branchcode,
186             }
187         }
188     );
189     t::lib::Mocks::mock_userenv({ branchcode => $library->branchcode, flags => 1 });
190
191     my $ils = C4::SIP::ILS->new({ id => $library->branchcode });
192
193     # Send empty AD segments (i.e. empty string for patron_pwd)
194     my $transaction = $ils->renew_all( $patron->cardnumber, "", undef );
195     isnt( $transaction->{screen_msg}, 'Invalid patron password.', "Empty password succeeds" );
196 };
197
198 $schema->storage->txn_rollback;