Line 0
Link Here
|
|
|
1 |
#!/usr/bin/env perl |
2 |
|
3 |
# Copyright KohaSuomi 2016 |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Test::More tests => 165; |
23 |
use Test::Mojo; |
24 |
use t::lib::Mocks; |
25 |
use t::lib::TestBuilder; |
26 |
|
27 |
use Mojo::JSON; |
28 |
|
29 |
use C4::Auth; |
30 |
use C4::Circulation; |
31 |
use C4::Context; |
32 |
|
33 |
use Koha::Database; |
34 |
use Koha::Items; |
35 |
use Koha::Patron; |
36 |
|
37 |
my $builder = t::lib::TestBuilder->new(); |
38 |
|
39 |
my $dbh = C4::Context->dbh; |
40 |
$dbh->{AutoCommit} = 0; |
41 |
$dbh->{RaiseError} = 1; |
42 |
|
43 |
$ENV{REMOTE_ADDR} = '127.0.0.1'; |
44 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
45 |
|
46 |
my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; |
47 |
my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; |
48 |
|
49 |
my $borrower = $builder->build({ source => 'Borrower' }); |
50 |
my $biblio = $builder->build({ source => 'Biblio' }); |
51 |
my $biblio2 = $builder->build({ source => 'Biblio' }); |
52 |
my $biblionumber = $biblio->{biblionumber}; |
53 |
my $biblionumber2 = $biblio2->{biblionumber}; |
54 |
|
55 |
my $module = new Test::MockModule('C4::Context'); |
56 |
$module->mock( 'userenv', sub { { branch => $borrower->{branchcode} } } ); |
57 |
|
58 |
# $item = available, $item2 = unavailable |
59 |
my $items; |
60 |
$items->{available} = build_item($biblionumber); |
61 |
$items->{notforloan} = build_item($biblionumber2, { notforloan => 1 }); |
62 |
$items->{damaged} = build_item($biblionumber2, { damaged => 1 }); |
63 |
$items->{withdrawn} = build_item($biblionumber2, { withdrawn => 1 }); |
64 |
$items->{onloan} = build_item($biblionumber2, { onloan => undef }); |
65 |
$items->{itemlost} = build_item($biblionumber2, { itemlost => 1 }); |
66 |
$items->{reserved} = build_item($biblionumber2); |
67 |
my $reserve = Koha::Hold->new( |
68 |
{ |
69 |
biblionumber => $items->{reserved}->{biblionumber}, |
70 |
itemnumber => $items->{reserved}->{itemnumber}, |
71 |
waitingdate => '2000-01-01', |
72 |
borrowernumber => $borrower->{borrowernumber}, |
73 |
branchcode => $items->{reserved}->{homebranch}, |
74 |
suspend => 0, |
75 |
} |
76 |
)->store(); |
77 |
|
78 |
my $itemnumber = $items->{available}->{itemnumber}; |
79 |
|
80 |
$t->get_ok("/api/v1/availability/items?itemnumber=-500382") |
81 |
->status_is(404); |
82 |
|
83 |
$t->get_ok("/api/v1/availability/items?itemnumber=-500382+-500383") |
84 |
->status_is(404); |
85 |
|
86 |
$t->get_ok("/api/v1/availability/items?biblionumber=-500382") |
87 |
->status_is(404); |
88 |
|
89 |
$t->get_ok("/api/v1/availability/items?biblionumber=-500382+-500383") |
90 |
->status_is(404); |
91 |
|
92 |
t::lib::Mocks::mock_preference('OnSiteCheckouts', 0); |
93 |
t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0); |
94 |
# available item |
95 |
$t->get_ok("/api/v1/availability/items?itemnumber=$itemnumber") |
96 |
->status_is(200) |
97 |
->json_is('/0/itemnumber', $itemnumber) |
98 |
->json_is('/0/biblionumber', $biblionumber) |
99 |
->json_is('/0/checkout/available', Mojo::JSON->true) |
100 |
->json_is('/0/checkout/description', []) |
101 |
->json_is('/0/hold/available', Mojo::JSON->true) |
102 |
->json_is('/0/hold/description', []) |
103 |
->json_is('/0/local_use/available', Mojo::JSON->true) |
104 |
->json_is('/0/local_use/description', []) |
105 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->false) |
106 |
->json_is('/0/onsite_checkout/description', ["onsite_checkouts_disabled"]) |
107 |
->json_is('/0/hold_queue_length', 0); |
108 |
t::lib::Mocks::mock_preference('OnSiteCheckouts', 1); |
109 |
$t->get_ok("/api/v1/availability/items?biblionumber=$biblionumber") |
110 |
->status_is(200) |
111 |
->json_is('/0/itemnumber', $itemnumber) |
112 |
->json_is('/0/biblionumber', $biblionumber) |
113 |
->json_is('/0/checkout/available', Mojo::JSON->true) |
114 |
->json_is('/0/checkout/description', []) |
115 |
->json_is('/0/hold/available', Mojo::JSON->true) |
116 |
->json_is('/0/hold/description', []) |
117 |
->json_is('/0/local_use/available', Mojo::JSON->true) |
118 |
->json_is('/0/local_use/description', []) |
119 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->true) |
120 |
->json_is('/0/onsite_checkout/description', []) |
121 |
->json_is('/0/hold_queue_length', 0); |
122 |
|
123 |
# notforloan item |
124 |
$t->get_ok("/api/v1/availability/items?itemnumber=".$items->{notforloan}->{itemnumber}) |
125 |
->status_is(200) |
126 |
->json_is('/0/itemnumber', $items->{notforloan}->{itemnumber}) |
127 |
->json_is('/0/biblionumber', $biblionumber2) |
128 |
->json_is('/0/checkout/available', Mojo::JSON->false) |
129 |
->json_is('/0/checkout/description/0', "notforloan") |
130 |
->json_is('/0/hold/available', Mojo::JSON->false) |
131 |
->json_is('/0/hold/description', ["notforloan"]) |
132 |
->json_is('/0/local_use/available', Mojo::JSON->true) |
133 |
->json_is('/0/local_use/description', []) |
134 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->true) |
135 |
->json_is('/0/onsite_checkout/description', []) |
136 |
->json_is('/0/hold_queue_length', 0); |
137 |
t::lib::Mocks::mock_preference('OnSiteCheckouts', 0); |
138 |
$t->get_ok("/api/v1/availability/items?itemnumber=$items->{notforloan}->{itemnumber}") |
139 |
->status_is(200) |
140 |
->json_is('/0/itemnumber', $items->{notforloan}->{itemnumber}) |
141 |
->json_is('/0/biblionumber', $biblionumber2) |
142 |
->json_is('/0/checkout/available', Mojo::JSON->false) |
143 |
->json_is('/0/checkout/description', ["notforloan"]) |
144 |
->json_is('/0/hold/available', Mojo::JSON->false) |
145 |
->json_is('/0/hold/description', ["notforloan"]) |
146 |
->json_is('/0/local_use/available', Mojo::JSON->true) |
147 |
->json_is('/0/local_use/description', []) |
148 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->false) |
149 |
->json_is('/0/onsite_checkout/description', ["onsite_checkouts_disabled"]) |
150 |
->json_is('/0/hold_queue_length', 0); |
151 |
t::lib::Mocks::mock_preference('OnSiteCheckouts', 1); |
152 |
|
153 |
# damaged item |
154 |
$t->get_ok("/api/v1/availability/items?itemnumber=".$items->{damaged}->{itemnumber}) |
155 |
->status_is(200) |
156 |
->json_is('/0/itemnumber', $items->{damaged}->{itemnumber}) |
157 |
->json_is('/0/biblionumber', $biblionumber2) |
158 |
->json_is('/0/checkout/available', Mojo::JSON->false) |
159 |
->json_is('/0/checkout/description', ["damaged"]) |
160 |
->json_is('/0/hold/available', Mojo::JSON->false) |
161 |
->json_is('/0/hold/description', ["damaged"]) |
162 |
->json_is('/0/local_use/available', Mojo::JSON->false) |
163 |
->json_is('/0/local_use/description', ["damaged"]) |
164 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->false) |
165 |
->json_is('/0/onsite_checkout/description', ["damaged"]) |
166 |
->json_is('/0/hold_queue_length', 0); |
167 |
t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1); |
168 |
$t->get_ok("/api/v1/availability/items?itemnumber=".$items->{damaged}->{itemnumber}) |
169 |
->status_is(200) |
170 |
->json_is('/0/itemnumber', $items->{damaged}->{itemnumber}) |
171 |
->json_is('/0/biblionumber', $biblionumber2) |
172 |
->json_is('/0/checkout/available', Mojo::JSON->true) |
173 |
->json_is('/0/checkout/description', ["damaged"]) |
174 |
->json_is('/0/hold/available', Mojo::JSON->true) |
175 |
->json_is('/0/hold/description', ["damaged"]) |
176 |
->json_is('/0/local_use/available', Mojo::JSON->true) |
177 |
->json_is('/0/local_use/description', ["damaged"]) |
178 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->true) |
179 |
->json_is('/0/onsite_checkout/description', ["damaged"]) |
180 |
->json_is('/0/hold_queue_length', 0); |
181 |
|
182 |
# withdrawn item |
183 |
$t->get_ok("/api/v1/availability/items?itemnumber=".$items->{withdrawn}->{itemnumber}) |
184 |
->status_is(200) |
185 |
->json_is('/0/itemnumber', $items->{withdrawn}->{itemnumber}) |
186 |
->json_is('/0/biblionumber', $biblionumber2) |
187 |
->json_is('/0/checkout/available', Mojo::JSON->false) |
188 |
->json_is('/0/checkout/description', ["withdrawn"]) |
189 |
->json_is('/0/hold/available', Mojo::JSON->false) |
190 |
->json_is('/0/hold/description', ["withdrawn"]) |
191 |
->json_is('/0/local_use/available', Mojo::JSON->false) |
192 |
->json_is('/0/local_use/description', ["withdrawn"]) |
193 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->false) |
194 |
->json_is('/0/onsite_checkout/description', ["withdrawn"]) |
195 |
->json_is('/0/hold_queue_length', 0); |
196 |
|
197 |
# lost item |
198 |
$t->get_ok("/api/v1/availability/items?itemnumber=".$items->{itemlost}->{itemnumber}) |
199 |
->status_is(200) |
200 |
->json_is('/0/itemnumber', $items->{itemlost}->{itemnumber}) |
201 |
->json_is('/0/biblionumber', $biblionumber2) |
202 |
->json_is('/0/checkout/available', Mojo::JSON->false) |
203 |
->json_is('/0/checkout/description', ["itemlost"]) |
204 |
->json_is('/0/hold/available', Mojo::JSON->false) |
205 |
->json_is('/0/hold/description', ["itemlost"]) |
206 |
->json_is('/0/local_use/available', Mojo::JSON->false) |
207 |
->json_is('/0/local_use/description', ["itemlost"]) |
208 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->false) |
209 |
->json_is('/0/onsite_checkout/description', ["itemlost"]) |
210 |
->json_is('/0/hold_queue_length', 0); |
211 |
|
212 |
my $issue = AddIssue($borrower, $items->{onloan}->{barcode}, undef, 1); |
213 |
|
214 |
# issued item |
215 |
$t->get_ok("/api/v1/availability/items?itemnumber=".$items->{onloan}->{itemnumber}) |
216 |
->status_is(200) |
217 |
->json_is('/0/itemnumber', $items->{onloan}->{itemnumber}) |
218 |
->json_is('/0/biblionumber', $biblionumber2) |
219 |
->json_is('/0/checkout/available', Mojo::JSON->false) |
220 |
->json_is('/0/checkout/description', ["onloan"]) |
221 |
->json_is('/0/hold/available', Mojo::JSON->true) |
222 |
->json_is('/0/hold/description', []) |
223 |
->json_is('/0/local_use/available', Mojo::JSON->false) |
224 |
->json_is('/0/local_use/description', ["onloan"]) |
225 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->false) |
226 |
->json_is('/0/onsite_checkout/description', ["onloan"]) |
227 |
->json_is('/0/checkout/expected_available', $issue->date_due) |
228 |
->json_is('/0/local_use/expected_available', $issue->date_due) |
229 |
->json_is('/0/onsite_checkout/expected_available', $issue->date_due) |
230 |
->json_is('/0/hold_queue_length', 0); |
231 |
|
232 |
# reserved item |
233 |
$t->get_ok("/api/v1/availability/items?itemnumber=".$items->{reserved}->{itemnumber}) |
234 |
->status_is(200) |
235 |
->json_is('/0/itemnumber', $items->{reserved}->{itemnumber}) |
236 |
->json_is('/0/biblionumber', $biblionumber2) |
237 |
->json_is('/0/checkout/available', Mojo::JSON->false) |
238 |
->json_is('/0/checkout/description', ["reserved"]) |
239 |
->json_is('/0/hold/available', Mojo::JSON->true) |
240 |
->json_is('/0/hold/description', []) |
241 |
->json_is('/0/local_use/available', Mojo::JSON->false) |
242 |
->json_is('/0/local_use/description', ["reserved"]) |
243 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->false) |
244 |
->json_is('/0/onsite_checkout/description', ["reserved"]) |
245 |
->json_is('/0/hold_queue_length', 1); |
246 |
|
247 |
# multiple in one request |
248 |
$t->get_ok("/api/v1/availability/items?itemnumber=".$items->{notforloan}->{itemnumber}."+$itemnumber+-500382") |
249 |
->status_is(200) |
250 |
->json_is('/0/itemnumber', $items->{notforloan}->{itemnumber}) |
251 |
->json_is('/0/biblionumber', $biblionumber2) |
252 |
->json_is('/0/checkout/available', Mojo::JSON->false) |
253 |
->json_is('/0/checkout/description/0', "notforloan") |
254 |
->json_is('/0/hold/available', Mojo::JSON->false) |
255 |
->json_is('/0/hold/description', ["notforloan"]) |
256 |
->json_is('/0/local_use/available', Mojo::JSON->true) |
257 |
->json_is('/0/local_use/description', []) |
258 |
->json_is('/0/onsite_checkout/available', Mojo::JSON->true) |
259 |
->json_is('/0/onsite_checkout/description', []) |
260 |
->json_is('/0/hold_queue_length', 0) |
261 |
->json_is('/1/itemnumber', $itemnumber) |
262 |
->json_is('/1/biblionumber', $biblionumber) |
263 |
->json_is('/1/checkout/available', Mojo::JSON->true) |
264 |
->json_is('/1/checkout/description', []) |
265 |
->json_is('/1/hold/available', Mojo::JSON->true) |
266 |
->json_is('/1/hold/description', []) |
267 |
->json_is('/1/local_use/available', Mojo::JSON->true) |
268 |
->json_is('/1/local_use/description', []) |
269 |
->json_is('/1/onsite_checkout/available', Mojo::JSON->true) |
270 |
->json_is('/1/onsite_checkout/description', []) |
271 |
->json_is('/1/hold_queue_length', 0); |
272 |
|
273 |
sub build_item { |
274 |
my ($biblionumber, $field) = @_; |
275 |
|
276 |
return $builder->build({ |
277 |
source => 'Item', |
278 |
value => { |
279 |
biblionumber => $biblionumber, |
280 |
notforloan => $field->{notforloan} || 0, |
281 |
damaged => $field->{damaged} || 0, |
282 |
withdrawn => $field->{withdrawn} || 0, |
283 |
itemlost => $field->{itemlost} || 0, |
284 |
restricted => $field->{restricted} || undef, |
285 |
onloan => $field->{onloan} || undef, |
286 |
itype => $field->{itype} || undef, |
287 |
} |
288 |
}); |
289 |
} |