Lines 1-544
Link Here
|
1 |
#!/usr/bin/perl |
|
|
2 |
# |
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 2 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along |
15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17 |
# |
18 |
|
19 |
use Modern::Perl; |
20 |
|
21 |
use File::Basename qw/basename/; |
22 |
use Koha::Database; |
23 |
use Koha::Illrequestattributes; |
24 |
use Koha::Patrons; |
25 |
use t::lib::TestBuilder; |
26 |
|
27 |
use Test::More tests => 44; |
28 |
|
29 |
# We want to test the Koha IllRequest object. At its core it's a simple |
30 |
# Koha::Object, mapping to the ill_request table. |
31 |
# |
32 |
# This object will supersede the Status object in ILLModule. |
33 |
# |
34 |
# We must ensure perfect backward compatibility between the current model and |
35 |
# the Status less model. |
36 |
|
37 |
use_ok('Koha::Illrequest'); |
38 |
use_ok('Koha::Illrequests'); |
39 |
|
40 |
my $schema = Koha::Database->new->schema; |
41 |
$schema->storage->txn_begin; |
42 |
|
43 |
my $builder = t::lib::TestBuilder->new; |
44 |
|
45 |
my $patron = $builder->build({ source => 'Borrower' }); |
46 |
my $branch = $builder->build({ source => 'Branch' }); |
47 |
|
48 |
my $illRequest = $builder->build({ |
49 |
source => 'Illrequest', |
50 |
value => { |
51 |
borrowernumber => $patron->{borrowernumber}, |
52 |
branch => $branch->{branchcode}, |
53 |
biblionumber => 0, |
54 |
status => 'NEW', |
55 |
completion_date => 0, |
56 |
reqtype => 'book', |
57 |
} |
58 |
}); |
59 |
|
60 |
my $illObject = Koha::Illrequests->find($illRequest->{illrequest_id}); |
61 |
|
62 |
isa_ok($illObject, "Koha::Illrequest"); |
63 |
|
64 |
# Test delete works correctly. |
65 |
my $illRequestDelete = $builder->build({ |
66 |
source => 'Illrequest', |
67 |
value => { |
68 |
borrowernumber => $patron->{borrowernumber}, |
69 |
branch => $branch->{branchcode}, |
70 |
biblionumber => 0, |
71 |
status => 'NEW', |
72 |
completion_date => 0, |
73 |
reqtype => 'book', |
74 |
} |
75 |
}); |
76 |
sub ill_req_search { |
77 |
return Koha::Illrequestattributes->search({ |
78 |
illrequest_id => $illRequestDelete->{illrequest_id} |
79 |
})->count; |
80 |
} |
81 |
|
82 |
is(ill_req_search, 0, "Correctly not found matching Illrequestattributes."); |
83 |
# XXX: For some reason test builder can't build Illrequestattributes. |
84 |
my $illReqAttr = Koha::Illrequestattribute->new({ |
85 |
illrequest_id => $illRequestDelete->{illrequest_id}, |
86 |
type => "test", |
87 |
value => "Hello World" |
88 |
})->store; |
89 |
is(ill_req_search, 1, "We have found a matching Illrequestattribute."); |
90 |
|
91 |
Koha::Illrequests->find($illRequestDelete->{illrequest_id})->delete; |
92 |
is( |
93 |
Koha::Illrequests->find($illRequestDelete->{illrequest_id}), |
94 |
undef, |
95 |
"Correctly deleted Illrequest." |
96 |
); |
97 |
is(ill_req_search, 0, "Correctly deleted Illrequestattributes."); |
98 |
|
99 |
# Test Accessing of related records. |
100 |
|
101 |
# # TODO the conclusion from being able to use one_to_many? we no longer need |
102 |
# # the Record object: simply pass the `ill_request_attributes` resultset |
103 |
# # whenever one would pass a Record. |
104 |
|
105 |
my $illRequest2 = $builder->build({ |
106 |
source => 'Illrequest', |
107 |
value => { |
108 |
borrower_id => $patron->{borrowernumber}, |
109 |
branch_id => $branch->{branchcode}, |
110 |
biblio_id => 0, |
111 |
status => 'NEW', |
112 |
completed => 0, |
113 |
medium => 'book', |
114 |
} |
115 |
}); |
116 |
my $illReqAttr2 = Koha::Illrequestattribute->new({ |
117 |
illrequest_id => $illRequest2->{illrequest_id}, |
118 |
type => "test2", |
119 |
value => "Hello World" |
120 |
})->store; |
121 |
my $illReqAttr3 = Koha::Illrequestattribute->new({ |
122 |
illrequest_id => $illRequest2->{illrequest_id}, |
123 |
type => "test3", |
124 |
value => "Hello Space" |
125 |
})->store; |
126 |
|
127 |
my $illRequestAttributes = Koha::Illrequests |
128 |
->find($illRequest2->{illrequest_id})->illrequestattributes; |
129 |
|
130 |
isa_ok($illRequestAttributes, "Koha::Illrequestattributes"); |
131 |
|
132 |
is($illRequestAttributes->count, 2, "Able to search related."); |
133 |
|
134 |
# Test loading of 'Config'. |
135 |
|
136 |
my $rqConfigTest = Koha::Illrequest->new({ |
137 |
borrower_id => $patron->{borrowernumber}, |
138 |
branch_id => $branch->{branchcode}, |
139 |
}); |
140 |
|
141 |
isa_ok($rqConfigTest->_config, "Koha::Illrequest::Config"); |
142 |
|
143 |
# Test loading of 'Dummy' backend. |
144 |
|
145 |
my $rqBackendTest = Koha::Illrequest->new({ |
146 |
borrower_id => $patron->{borrowernumber}, |
147 |
branch_id => $branch->{branchcode}, |
148 |
})->store; |
149 |
|
150 |
$rqBackendTest->_config->backend("Dummy"); |
151 |
$rqBackendTest->_config->limits({ default => { count => -1 } }); |
152 |
isa_ok($rqBackendTest->_backend, "Koha::Illbackends::Dummy::Base"); |
153 |
|
154 |
# Test use of 'Dummy' Backend. |
155 |
|
156 |
## Test backend_update_status |
157 |
|
158 |
# FIXME: This breaks transparancy of ->status method! |
159 |
eval { $rqBackendTest->status("ERR") }; |
160 |
ok($@, "status: Test for status error on hook fail."); |
161 |
|
162 |
# FIXME: Will need to test this on new illRequest to not pollute rest of |
163 |
# tests. |
164 |
|
165 |
# is($rqBackendTest->status("NEW")->status, "NEW", "status: Setter works |
166 |
# OK."); |
167 |
# is($rqBackendTest->status(null), null, "status: Unsetter works OK."); |
168 |
|
169 |
## Test backend_create |
170 |
|
171 |
is( |
172 |
$rqBackendTest->status, |
173 |
undef, |
174 |
"backend_create: Test our status initiates correctly." |
175 |
); |
176 |
|
177 |
# Request a search form |
178 |
my $created_rq = $rqBackendTest->backend_create({ |
179 |
stage => "search_form", |
180 |
method => "create", |
181 |
}); |
182 |
|
183 |
is( $created_rq->{stage}, 'search_results', |
184 |
"backend_create: search_results stage." ); |
185 |
|
186 |
# Request search results |
187 |
# FIXME: fails because of missing patron / branch info. |
188 |
# $created_rq = $rqBackendTest->backend_create({ |
189 |
# stage => "search_results", |
190 |
# method => "create", |
191 |
# other => { search => "interlibrary loans" }, |
192 |
# }); |
193 |
|
194 |
# is_deeply( |
195 |
# $created_rq, |
196 |
# { |
197 |
# error => 0, |
198 |
# status => '', |
199 |
# message => '', |
200 |
# method => 'create', |
201 |
# stage => 'search_results', |
202 |
# template => 'ill/Dummy/create.inc', |
203 |
# value => [ |
204 |
# { |
205 |
# id => 1234, |
206 |
# title => "Ordering ILLs using Koha", |
207 |
# author => "A.N. Other", |
208 |
# }, |
209 |
# { |
210 |
# id => 5678, |
211 |
# title => "Interlibrary loans in Koha", |
212 |
# author => "A.N. Other", |
213 |
# }, |
214 |
# ], |
215 |
# } |
216 |
# , |
217 |
# "backend_create: search_results stage." |
218 |
# ); |
219 |
|
220 |
# # Create the request |
221 |
# $created_rq = $rqBackendTest->backend_create({ |
222 |
# stage => "commit", |
223 |
# method => "create", |
224 |
# other => { |
225 |
# id => 1234, |
226 |
# title => "Ordering ILLs using Koha", |
227 |
# author => "A.N. Other", |
228 |
# }, |
229 |
# }); |
230 |
|
231 |
# while ( my ( $field, $value ) = each %{$created_rq} ) { |
232 |
# isnt($value, undef, "backend_create: key '$field' exists"); |
233 |
# }; |
234 |
|
235 |
# is( |
236 |
# $rqBackendTest->status, |
237 |
# "NEW", |
238 |
# "backend_create: Test our status was updated." |
239 |
# ); |
240 |
|
241 |
# cmp_ok( |
242 |
# $rqBackendTest->illrequestattributes->count, |
243 |
# "==", |
244 |
# 3, |
245 |
# "backend_create: Ensure we have correctly stored our new attributes." |
246 |
# ); |
247 |
|
248 |
# ## Test backend_list |
249 |
|
250 |
# is_deeply( |
251 |
# $rqBackendTest->backend_list->{value}, |
252 |
# { |
253 |
# 1 => { |
254 |
# id => 1234, |
255 |
# title => "Ordering ILLs using Koha", |
256 |
# author => "A.N. Other", |
257 |
# status => "On order", |
258 |
# cost => "30 GBP", |
259 |
# } |
260 |
# }, |
261 |
# "backend_list: Retrieve our list of requested requests." |
262 |
# ); |
263 |
|
264 |
# ## Test backend_renew |
265 |
|
266 |
# ok( |
267 |
# $rqBackendTest->backend_renew->{error}, |
268 |
# "backend_renew: Error for invalid request." |
269 |
# ); |
270 |
# is_deeply( |
271 |
# $rqBackendTest->backend_renew->{value}, |
272 |
# { |
273 |
# id => 1234, |
274 |
# title => "Ordering ILLs using Koha", |
275 |
# author => "A.N. Other", |
276 |
# }, |
277 |
# "backend_renew: Renew request." |
278 |
# ); |
279 |
|
280 |
# ## Test backend_confirm |
281 |
|
282 |
# my $rqBackendTestConfirmed = $rqBackendTest->backend_confirm; |
283 |
# is( |
284 |
# $rqBackendTest->status, |
285 |
# "REQ", |
286 |
# "backend_commit: Confirm status update correct." |
287 |
# ); |
288 |
# is( |
289 |
# $rqBackendTest->orderid, |
290 |
# 1234, |
291 |
# "backend_commit: Confirm orderid populated correctly." |
292 |
# ); |
293 |
|
294 |
# ## Test backend_status |
295 |
|
296 |
# is( |
297 |
# $rqBackendTest->backend_status->{error}, |
298 |
# 0, |
299 |
# "backend_status: error for invalid request." |
300 |
# ); |
301 |
# is_deeply( |
302 |
# $rqBackendTest->backend_status->{value}, |
303 |
# { |
304 |
# id => 1234, |
305 |
# status => "On order", |
306 |
# title => "Ordering ILLs using Koha", |
307 |
# author => "A.N. Other", |
308 |
# }, |
309 |
# "backend_status: Retrieve the status of request." |
310 |
# ); |
311 |
|
312 |
# # Now test trying to get status on non-confirmed request. |
313 |
my $rqBackendTestUnconfirmed = Koha::Illrequest->new({ |
314 |
borrower_id => $patron->{borrowernumber}, |
315 |
branch_id => $branch->{branchcode}, |
316 |
})->store; |
317 |
$rqBackendTestUnconfirmed->_config->backend("Dummy"); |
318 |
$rqBackendTestUnconfirmed->_config->limits({ default => { count => -1 } }); |
319 |
|
320 |
$rqBackendTestUnconfirmed->backend_create({ |
321 |
stage => "commit", |
322 |
method => "create", |
323 |
other => { |
324 |
id => 1234, |
325 |
title => "Ordering ILLs using Koha", |
326 |
author => "A.N. Other", |
327 |
}, |
328 |
}); |
329 |
is( |
330 |
$rqBackendTestUnconfirmed->backend_status->{error}, |
331 |
1, |
332 |
"backend_status: error for invalid request." |
333 |
); |
334 |
|
335 |
## Test backend_cancel |
336 |
|
337 |
# is( |
338 |
# $rqBackendTest->backend_cancel->{error}, |
339 |
# 0, |
340 |
# "backend_cancel: Successfully cancelling request." |
341 |
# ); |
342 |
# is_deeply( |
343 |
# $rqBackendTest->backend_cancel->{value}, |
344 |
# { |
345 |
# id => 1234, |
346 |
# title => "Ordering ILLs using Koha", |
347 |
# author => "A.N. Other", |
348 |
# }, |
349 |
# "backend_cancel: Cancel request." |
350 |
# ); |
351 |
|
352 |
# Now test trying to cancel non-confirmed request. |
353 |
is( |
354 |
$rqBackendTestUnconfirmed->backend_cancel->{error}, |
355 |
1, |
356 |
"backend_cancel: error for invalid request." |
357 |
); |
358 |
is_deeply( |
359 |
$rqBackendTestUnconfirmed->backend_cancel->{value}, |
360 |
{}, |
361 |
"backend_cancel: Cancel request." |
362 |
); |
363 |
|
364 |
# Test Helpers |
365 |
|
366 |
## Test getCensorNotesStaff |
367 |
|
368 |
is($rqBackendTest->getCensorNotesStaff, 1, "getCensorNotesStaff: Public."); |
369 |
$rqBackendTest->_config->censorship({ |
370 |
censor_notes_staff => 0, |
371 |
censor_reply_date => 0, |
372 |
}); |
373 |
is($rqBackendTest->getCensorNotesStaff, 0, "getCensorNotesStaff: Censored."); |
374 |
|
375 |
## Test getCensorNotesStaff |
376 |
|
377 |
is($rqBackendTest->getDisplayReplyDate, 1, "getDisplayReplyDate: Yes."); |
378 |
$rqBackendTest->_config->censorship({ |
379 |
censor_notes_staff => 0, |
380 |
censor_reply_date => 1, |
381 |
}); |
382 |
is($rqBackendTest->getDisplayReplyDate, 0, "getDisplayReplyDate: No."); |
383 |
|
384 |
# FIXME: These should be handled by the templates. |
385 |
# # Test Output Helpers |
386 |
|
387 |
# ## Test getStatusSummary |
388 |
|
389 |
# $rqBackendTest->medium("Book")->store; |
390 |
# is_deeply( |
391 |
# $rqBackendTest->getStatusSummary({brw => 0}), |
392 |
# { |
393 |
# biblionumber => ["Biblio Number", undef], |
394 |
# borrowernumber => ["Borrower Number", $patron->{borrowernumber}], |
395 |
# id => ["Request Number", $rqBackendTest->illrequest_id], |
396 |
# prefix_id => ["Request Number", $rqBackendTest->illrequest_id], |
397 |
# reqtype => ["Request Type", "Book"], |
398 |
# status => ["Status", "REQREV"], |
399 |
# }, |
400 |
# "getStatusSummary: Without Borrower." |
401 |
# ); |
402 |
|
403 |
# is_deeply( |
404 |
# $rqBackendTest->getStatusSummary({brw => 1}), |
405 |
# { |
406 |
# biblionumber => ["Biblio Number", undef], |
407 |
# borrower => ["Borrower", Koha::Patrons->find($patron->{borrowernumber})], |
408 |
# id => ["Request Number", $rqBackendTest->illrequest_id], |
409 |
# prefix_id => ["Request Number", $rqBackendTest->illrequest_id], |
410 |
# reqtype => ["Request Type", "Book"], |
411 |
# status => ["Status", "REQREV"], |
412 |
# }, |
413 |
# "getStatusSummary: With Borrower." |
414 |
# ); |
415 |
|
416 |
# ## Test getFullStatus |
417 |
|
418 |
# is_deeply( |
419 |
# $rqBackendTest->getFullStatus({brw => 0}), |
420 |
# { |
421 |
# biblionumber => ["Biblio Number", undef], |
422 |
# borrowernumber => ["Borrower Number", $patron->{borrowernumber}], |
423 |
# id => ["Request Number", $rqBackendTest->illrequest_id], |
424 |
# prefix_id => ["Request Number", $rqBackendTest->illrequest_id], |
425 |
# reqtype => ["Request Type", "Book"], |
426 |
# status => ["Status", "REQREV"], |
427 |
# placement_date => ["Placement Date", $rqBackendTest->placed], |
428 |
# completion_date => ["Completion Date", $rqBackendTest->completed], |
429 |
# ts => ["Timestamp", $rqBackendTest->updated], |
430 |
# branch => ["Branch", $rqBackendTest->branch_id], |
431 |
# }, |
432 |
# "getFullStatus: Without Borrower." |
433 |
# ); |
434 |
|
435 |
# is_deeply( |
436 |
# $rqBackendTest->getFullStatus({brw => 1}), |
437 |
# { |
438 |
# biblionumber => ["Biblio Number", undef], |
439 |
# borrower => ["Borrower", Koha::Patrons->find($patron->{borrowernumber})], |
440 |
# id => ["Request Number", $rqBackendTest->illrequest_id], |
441 |
# prefix_id => ["Request Number", $rqBackendTest->illrequest_id], |
442 |
# reqtype => ["Request Type", "Book"], |
443 |
# status => ["Status", "REQREV"], |
444 |
# placement_date => ["Placement Date", $rqBackendTest->placed], |
445 |
# completion_date => ["Completion Date", $rqBackendTest->completed], |
446 |
# ts => ["Timestamp", $rqBackendTest->updated], |
447 |
# branch => ["Branch", $rqBackendTest->branch_id], |
448 |
# }, |
449 |
# "getFullStatus: With Borrower." |
450 |
# ); |
451 |
|
452 |
## Test available_backends |
453 |
subtest 'available_backends' => sub { |
454 |
plan tests => 1; |
455 |
|
456 |
my $rq = Koha::Illrequest->new({ |
457 |
borrower_id => $patron->{borrowernumber}, |
458 |
branch_id => $branch->{branchcode}, |
459 |
})->store; |
460 |
|
461 |
my @backends = (); |
462 |
my $backenddir = $rq->_config->backend_dir; |
463 |
@backends = <$backenddir/*> if ( $backenddir ); |
464 |
@backends = map { basename($_) } @backends; |
465 |
is_deeply(\@backends, $rq->available_backends, |
466 |
"Correctly identify available backends."); |
467 |
|
468 |
}; |
469 |
|
470 |
## Test capabilities |
471 |
|
472 |
my $rqCapTest = Koha::Illrequest->new({ |
473 |
borrower_id => $patron->{borrowernumber}, |
474 |
branch_id => $branch->{branchcode}, |
475 |
})->store; |
476 |
|
477 |
is( keys %{$rqCapTest->_core_status_graph}, |
478 |
@{[ 'NEW', 'REQ', 'REVREQ', 'QUEUED', 'CANCREQ', 'COMP', 'KILL' ]}, |
479 |
"Complete list of core statuses." ); |
480 |
|
481 |
my $union = $rqCapTest->_status_graph_union( |
482 |
$rqCapTest->_core_status_graph, |
483 |
{ |
484 |
TEST => { |
485 |
prev_actions => [ 'COMP' ], |
486 |
id => 'TEST', |
487 |
name => "Test", |
488 |
ui_method_name => "Perform test", |
489 |
method => 'test', |
490 |
next_actions => [ 'NEW' ] |
491 |
}, |
492 |
BLAH => { |
493 |
prev_actions => [ 'COMP' ], |
494 |
id => 'BLAH', |
495 |
name => "BLAH", |
496 |
ui_method_name => "Perform test", |
497 |
method => 'test', |
498 |
next_actions => [ 'NEW' ] |
499 |
}, |
500 |
} |
501 |
); |
502 |
ok( ( grep 'BLAH', @{$union->{COMP}->{next_actions}} and |
503 |
grep 'TEST', @{$union->{COMP}->{next_actions}} ), |
504 |
"next_actions: updated." ); |
505 |
ok( ( grep 'BLAH', @{$union->{NEW}->{prev_actions}} and |
506 |
grep 'TEST', @{$union->{NEW}->{prev_actions}} ), |
507 |
"next_actions: updated." ); |
508 |
|
509 |
## Test available_backends |
510 |
subtest 'available_actions' => sub { |
511 |
plan tests => 1; |
512 |
|
513 |
my $rq = Koha::Illrequest->new({ |
514 |
borrower_id => $patron->{borrowernumber}, |
515 |
branch_id => $branch->{branchcode}, |
516 |
status => 'NEW', |
517 |
})->store; |
518 |
|
519 |
is_deeply( |
520 |
$rq->available_actions, |
521 |
[ |
522 |
{ |
523 |
prev_actions => [ 'NEW', 'REQREV', 'QUEUED' ], |
524 |
id => 'REQ', |
525 |
name => 'Requested', |
526 |
ui_method_name => 'Create request', |
527 |
method => 'confirm', |
528 |
next_actions => [ 'REQREV' ], |
529 |
}, |
530 |
{ |
531 |
prev_actions => [ 'CANCREQ', 'QUEUED', 'REQREV', 'NEW' ], |
532 |
id => 'KILL', |
533 |
name => 0, |
534 |
ui_method_name => 'Delete request', |
535 |
method => 'delete', |
536 |
next_actions => [ ], |
537 |
} |
538 |
] |
539 |
); |
540 |
}; |
541 |
|
542 |
$schema->storage->txn_rollback; |
543 |
|
544 |
1; |