Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright Koha-Suomi Oy 2016 |
4 |
# |
5 |
# This file is part of Koha |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope t1hat it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
use Test::More tests => 10; |
22 |
use t::lib::Mocks; |
23 |
use t::lib::TestBuilder; |
24 |
require t::db_dependent::Koha::Availability::Helpers; |
25 |
|
26 |
use Koha::Database; |
27 |
use Koha::IssuingRules; |
28 |
use Koha::Items; |
29 |
use Koha::ItemTypes; |
30 |
|
31 |
use Koha::Item::Availability::Search; |
32 |
|
33 |
my $schema = Koha::Database->new->schema; |
34 |
$schema->storage->txn_begin; |
35 |
|
36 |
my $builder = t::lib::TestBuilder->new; |
37 |
|
38 |
set_default_system_preferences(); |
39 |
set_default_circulation_rules(); |
40 |
|
41 |
subtest 'Given item is in a good state for availability' => \&t_ok_availability; |
42 |
sub t_ok_availability { |
43 |
plan tests => 3; |
44 |
|
45 |
my $patron = build_a_test_patron(); |
46 |
my $item = build_a_test_item(); |
47 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
48 |
|
49 |
ok($availability->available, 'When I request availability, then the item is available.'); |
50 |
ok(!$availability->confirm, 'Then nothing needs to be confirmed.'); |
51 |
ok(!$availability->unavailable, 'Then there are no reasons to be unavailable.'); |
52 |
} |
53 |
|
54 |
subtest 'Given item is damaged' => sub { |
55 |
plan tests => 2; |
56 |
|
57 |
subtest 'Given AllowHoldsOnDamagedItems is disabled' => \&t_damaged_item_allow_disabled; |
58 |
subtest 'Given AllowHoldsOnDamagedItems is enabled' => \&t_damaged_item_allow_enabled; |
59 |
sub t_damaged_item_allow_disabled { |
60 |
plan tests => 4; |
61 |
|
62 |
t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0); |
63 |
|
64 |
my $patron = build_a_test_patron(); |
65 |
my $item = build_a_test_item()->set({damaged=>1})->store; |
66 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
67 |
my $expecting = 'Koha::Exceptions::Item::Damaged'; |
68 |
|
69 |
is($item->damaged, 1, 'When I look at the item, I see that it is damaged.'); |
70 |
ok(!$availability->available, 'When I request availability, then the item is not available.'); |
71 |
is($availability->unavailable, 1, 'Then there is only one unavailability reason.'); |
72 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, |
73 |
'Then there is an unavailability status indicating damaged item.'); |
74 |
}; |
75 |
sub t_damaged_item_allow_enabled { |
76 |
plan tests => 4; |
77 |
|
78 |
t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1); |
79 |
|
80 |
my $patron = build_a_test_patron(); |
81 |
my $item = build_a_test_item()->set({damaged=>1})->store; |
82 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
83 |
|
84 |
is($item->damaged, 1, 'When I look at the item, I see that it is damaged.'); |
85 |
ok($availability->available, 'When I request availability, then the item is available.'); |
86 |
ok(!$availability->unavailable, 'Then there are no statuses for unavailability.'); |
87 |
ok(!$availability->confirm, 'Then there is no reason to have availability confirmed.'); |
88 |
}; |
89 |
}; |
90 |
|
91 |
subtest 'Given item is lost' => \&t_lost; |
92 |
sub t_lost { |
93 |
plan tests => 4; |
94 |
|
95 |
my $patron = build_a_test_patron(); |
96 |
my $item = build_a_test_item()->set({itemlost=>1})->store; |
97 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
98 |
my $expecting = 'Koha::Exceptions::Item::Lost'; |
99 |
|
100 |
is($item->itemlost, 1, 'When I try to look at the item, I find out that it is lost.'); |
101 |
ok(!$availability->available, 'When I request availability, then the item is not available.'); |
102 |
is($availability->unavailable, 1, 'Then there is only one unavailability reason.'); |
103 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, |
104 |
'Then there is an unavailability status indicating lost item.'); |
105 |
}; |
106 |
|
107 |
subtest 'Given item is not for loan' => \&t_notforloan; |
108 |
sub t_notforloan { |
109 |
plan tests => 4; |
110 |
|
111 |
my $patron = build_a_test_patron(); |
112 |
my $item = build_a_test_item()->set({notforloan=>1})->store; |
113 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
114 |
my $expecting = 'Koha::Exceptions::Item::NotForLoan'; |
115 |
|
116 |
is($item->notforloan, 1, 'When I look at the item, I see that it is not for loan.'); |
117 |
ok(!$availability->available, 'When I request availability, then the item is not available.'); |
118 |
is($availability->unavailable, 1, 'Then there is only one unavailability reason.'); |
119 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, |
120 |
'Then there is an unavailability status indicating the item is not for loan.'); |
121 |
}; |
122 |
|
123 |
subtest 'Given item type is not for loan' => sub { |
124 |
|
125 |
subtest 'Given item-level_itypes is on (item-itemtype)' => \&t_itemlevel_itemtype_notforloan_item_level_itypes_on; |
126 |
subtest 'Given item-level_itypes is off (biblioitem-itemtype)' => \&t_itemlevel_itemtype_notforloan_item_level_itypes_off; |
127 |
sub t_itemlevel_itemtype_notforloan_item_level_itypes_on { |
128 |
plan tests => 5; |
129 |
|
130 |
t::lib::Mocks::mock_preference('item-level_itypes', 1); |
131 |
|
132 |
my $patron = build_a_test_patron(); |
133 |
my $item = build_a_test_item(); |
134 |
my $biblioitem = Koha::Biblioitems->find($item->biblioitemnumber); |
135 |
my $itemtype = Koha::ItemTypes->find($item->itype); |
136 |
$itemtype->set({notforloan=>1})->store; |
137 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
138 |
my $expecting = 'Koha::Exceptions::ItemType::NotForLoan'; |
139 |
|
140 |
is(Koha::ItemTypes->find($biblioitem->itemtype)->notforloan, 0, 'Biblioitem itemtype is for loan.'); |
141 |
is(Koha::ItemTypes->find($item->itype)->notforloan, 1, 'Item itemtype is not for loan.'); |
142 |
ok(!$availability->available, 'When I request availability, then the item is not available.'); |
143 |
is($availability->unavailable, 1, 'Then there is only one unavailability reason.'); |
144 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, |
145 |
"Then there is an unavailability status indicating the itemtype is not forloan."); |
146 |
}; |
147 |
sub t_itemlevel_itemtype_notforloan_item_level_itypes_off { |
148 |
plan tests => 5; |
149 |
|
150 |
t::lib::Mocks::mock_preference('item-level_itypes', 0); |
151 |
|
152 |
my $patron = build_a_test_patron(); |
153 |
my $item = build_a_test_item(); |
154 |
my $biblioitem = Koha::Biblioitems->find($item->biblioitemnumber); |
155 |
my $itemtype = Koha::ItemTypes->find($biblioitem->itemtype); |
156 |
$itemtype->set({notforloan=>1})->store; |
157 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
158 |
my $expecting = 'Koha::Exceptions::ItemType::NotForLoan'; |
159 |
|
160 |
is(Koha::ItemTypes->find($biblioitem->itemtype)->notforloan, 1, 'Biblioitem itemtype is not for loan.'); |
161 |
is(Koha::ItemTypes->find($item->itype)->notforloan, 0, 'Item itemtype is for loan.'); |
162 |
ok(!$availability->available, 'When I request availability, then the item is not available.'); |
163 |
is($availability->unavailable, 1, 'Then there is only one unavailability reason.'); |
164 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, |
165 |
"Then there is an unavailability status indicating the itemtype is not forloan."); |
166 |
}; |
167 |
}; |
168 |
|
169 |
subtest 'Given item is ordered' => \&t_ordered; |
170 |
sub t_ordered { |
171 |
plan tests => 4; |
172 |
|
173 |
my $patron = build_a_test_patron(); |
174 |
my $item = build_a_test_item()->set({notforloan=>-1})->store; |
175 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
176 |
my $expecting = 'Koha::Exceptions::Item::NotForLoan'; |
177 |
|
178 |
ok(!$availability->available, 'When I request availability, then the item is not available.'); |
179 |
ok($availability->unavailable, 'Then there is one reason for unavailability.'); |
180 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, |
181 |
'Then the unavailability status is indicating not for loan status.'); |
182 |
is($availability->unavailabilities->{$expecting}->code, 'Ordered', 'Not for loan code says the item is ordered.') |
183 |
}; |
184 |
|
185 |
subtest 'Given item is restricted' => \&t_restricted; |
186 |
sub t_restricted { |
187 |
plan tests => 4; |
188 |
|
189 |
my $patron = build_a_test_patron(); |
190 |
my $item = build_a_test_item()->set({restricted=>1})->store; |
191 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
192 |
my $expecting = 'Koha::Exceptions::Item::Restricted'; |
193 |
|
194 |
is($item->restricted, 1, 'When I look at the item, I see that it is restricted.'); |
195 |
ok(!$availability->available, 'When I request availability, then the item is not available.'); |
196 |
is($availability->unavailable, 1, 'Then there is only one unavailability reason.'); |
197 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, |
198 |
'Then there is an unavailability status indicating restricted item.'); |
199 |
}; |
200 |
|
201 |
subtest 'Given item has no barcode' => \&t_unknown_barcode; |
202 |
sub t_unknown_barcode { |
203 |
plan tests => 4; |
204 |
|
205 |
my $patron = build_a_test_patron(); |
206 |
my $item = build_a_test_item()->set({barcode=>undef})->store; |
207 |
my $expecting = 'Koha::Exceptions::Item::UnknownBarcode'; |
208 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
209 |
|
210 |
is($item->barcode, undef, 'When I look at the item, we see that it has undefined barcode.'); |
211 |
ok($availability->unavailable, 'When I request availability, then the item is not available.'); |
212 |
is($availability->unavailable, 1, 'Then there is only one unavailability reason.'); |
213 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, |
214 |
'Then there is an unavailability status indicating unknown barcode.'); |
215 |
}; |
216 |
|
217 |
subtest 'Given item is withdrawn' => \&t_withdrawn; |
218 |
sub t_withdrawn { |
219 |
plan tests => 4; |
220 |
|
221 |
my $patron = build_a_test_patron(); |
222 |
my $item = build_a_test_item()->set({restricted=>1})->store; |
223 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
224 |
my $expecting = 'Koha::Exceptions::Item::Restricted'; |
225 |
|
226 |
is($item->restricted, 1, 'When I look at the item, I see that it is restricted.'); |
227 |
ok(!$availability->available, 'When I request availability, then the item is not available.'); |
228 |
is($availability->unavailable, 1, 'Then there is only one unavailability reason.'); |
229 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, |
230 |
'Then there is an unavailability status indicating restricted item.'); |
231 |
}; |
232 |
|
233 |
subtest 'Held' => \&t_held; |
234 |
sub t_held { |
235 |
plan tests => 8; |
236 |
|
237 |
my $patron = build_a_test_patron(); |
238 |
my $item = build_a_test_item(); |
239 |
my $reserve_id = add_item_level_hold($item, $patron, $item->homebranch); |
240 |
my $hold = Koha::Holds->find({ borrowernumber => $patron->borrowernumber }); |
241 |
Koha::IssuingRules->search->delete; |
242 |
my $rule = Koha::IssuingRule->new({ |
243 |
branchcode => $item->homebranch, |
244 |
itemtype => $item->effective_itemtype, |
245 |
categorycode => $patron->categorycode, |
246 |
holds_per_record => 9001, |
247 |
reservesallowed => 9001, |
248 |
opacitemholds => 'Y', |
249 |
})->store; |
250 |
my $availability = Koha::Item::Availability::Search->new({item => $item})->in_opac; |
251 |
my $expecting = 'Koha::Exceptions::Item::Held'; |
252 |
|
253 |
is($rule->reservesallowed, 9001, 'As I look at circulation rules, I can see that many reserves are allowed.'); |
254 |
ok($reserve_id, 'I have placed a hold on an item.'); |
255 |
is($hold->itemnumber, $item->itemnumber, 'The item I have hold for is the same item I will check availability for.'); |
256 |
ok(!$availability->available, 'When I request availability, then the item is not available.'); |
257 |
ok(!$availability->confirm, 'Then there is nothing to be confirmed.'); |
258 |
ok(!$availability->note, 'Then there are no additional notes.'); |
259 |
is($availability->unavailable, 1, 'Then there is only one reason for unavailability.'); |
260 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, |
261 |
'Then there is an unavailability status indicating that I have already held this.'); |
262 |
}; |
263 |
|
264 |
$schema->storage->txn_rollback; |
265 |
|
266 |
1; |