Bug 17776: (QA follow-up) Consistent regex for Plack detection
[koha.git] / t / db_dependent / Illrequests.t
blob76de8fa1fba9acdb8a5371120feda6b7be09eb53
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use File::Basename qw/basename/;
21 use Koha::Database;
22 use Koha::Illrequestattributes;
23 use Koha::Illrequest::Config;
24 use Koha::Patrons;
25 use t::lib::Mocks;
26 use t::lib::TestBuilder;
27 use Test::MockObject;
28 use Test::MockModule;
29 use Test::Exception;
31 use Test::More tests => 11;
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35 use_ok('Koha::Illrequest');
36 use_ok('Koha::Illrequests');
38 subtest 'Basic object tests' => sub {
40 plan tests => 21;
42 $schema->storage->txn_begin;
44 Koha::Illrequests->search->delete;
45 my $illrq = $builder->build({ source => 'Illrequest' });
46 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
48 isa_ok($illrq_obj, 'Koha::Illrequest',
49 "Correctly create and load an illrequest object.");
50 isa_ok($illrq_obj->_config, 'Koha::Illrequest::Config',
51 "Created a config object as part of Illrequest creation.");
53 is($illrq_obj->illrequest_id, $illrq->{illrequest_id},
54 "Illrequest_id getter works.");
55 is($illrq_obj->borrowernumber, $illrq->{borrowernumber},
56 "Borrowernumber getter works.");
57 is($illrq_obj->biblio_id, $illrq->{biblio_id},
58 "Biblio_Id getter works.");
59 is($illrq_obj->branchcode, $illrq->{branchcode},
60 "Branchcode getter works.");
61 is($illrq_obj->status, $illrq->{status},
62 "Status getter works.");
63 is($illrq_obj->placed, $illrq->{placed},
64 "Placed getter works.");
65 is($illrq_obj->replied, $illrq->{replied},
66 "Replied getter works.");
67 is($illrq_obj->updated, $illrq->{updated},
68 "Updated getter works.");
69 is($illrq_obj->completed, $illrq->{completed},
70 "Completed getter works.");
71 is($illrq_obj->medium, $illrq->{medium},
72 "Medium getter works.");
73 is($illrq_obj->accessurl, $illrq->{accessurl},
74 "Accessurl getter works.");
75 is($illrq_obj->cost, $illrq->{cost},
76 "Cost getter works.");
77 is($illrq_obj->notesopac, $illrq->{notesopac},
78 "Notesopac getter works.");
79 is($illrq_obj->notesstaff, $illrq->{notesstaff},
80 "Notesstaff getter works.");
81 is($illrq_obj->orderid, $illrq->{orderid},
82 "Orderid getter works.");
83 is($illrq_obj->backend, $illrq->{backend},
84 "Backend getter works.");
86 isnt($illrq_obj->status, 'COMP',
87 "ILL is not currently marked complete.");
88 $illrq_obj->mark_completed;
89 is($illrq_obj->status, 'COMP',
90 "ILL is now marked complete.");
92 $illrq_obj->delete;
94 is(Koha::Illrequests->search->count, 0,
95 "No illrequest found after delete.");
97 $schema->storage->txn_rollback;
100 subtest 'Working with related objects' => sub {
102 plan tests => 5;
104 $schema->storage->txn_begin;
106 Koha::Illrequests->search->delete;
108 my $patron = $builder->build({ source => 'Borrower' });
109 my $illrq = $builder->build({
110 source => 'Illrequest',
111 value => { borrowernumber => $patron->{borrowernumber} }
113 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
115 isa_ok($illrq_obj->patron, 'Koha::Patron',
116 "OK accessing related patron.");
118 $builder->build({
119 source => 'Illrequestattribute',
120 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'X' }
122 $builder->build({
123 source => 'Illrequestattribute',
124 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Y' }
126 $builder->build({
127 source => 'Illrequestattribute',
128 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Z' }
131 is($illrq_obj->illrequestattributes->count, Koha::Illrequestattributes->search->count,
132 "Fetching expected number of Illrequestattributes for our request.");
134 my $illrq1 = $builder->build({ source => 'Illrequest' });
135 $builder->build({
136 source => 'Illrequestattribute',
137 value => { illrequest_id => $illrq1->{illrequest_id}, type => 'X' }
140 is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
141 "Fetching expected number of Illrequestattributes for our request.");
143 $illrq_obj->delete;
144 is(Koha::Illrequestattributes->search->count, 1,
145 "Correct number of illrequestattributes after delete.");
147 isa_ok(Koha::Patrons->find($patron->{borrowernumber}), 'Koha::Patron',
148 "Borrower was not deleted after illrq delete.");
150 $schema->storage->txn_rollback;
153 subtest 'Status Graph tests' => sub {
155 plan tests => 4;
157 $schema->storage->txn_begin;
159 my $illrq = $builder->build({source => 'Illrequest'});
160 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
162 # _core_status_graph tests: it's just a constant, so here we just make
163 # sure it returns a hashref.
164 is(ref $illrq_obj->_core_status_graph, "HASH",
165 "_core_status_graph returns a hash.");
167 # _status_graph_union: let's try different merge operations.
168 # Identity operation
169 is_deeply(
170 $illrq_obj->_status_graph_union($illrq_obj->_core_status_graph, {}),
171 $illrq_obj->_core_status_graph,
172 "core_status_graph + null = core_status_graph"
175 # Simple addition
176 is_deeply(
177 $illrq_obj->_status_graph_union({}, $illrq_obj->_core_status_graph),
178 $illrq_obj->_core_status_graph,
179 "null + core_status_graph = core_status_graph"
182 # Correct merge behaviour
183 is_deeply(
184 $illrq_obj->_status_graph_union({
185 REQ => {
186 prev_actions => [ ],
187 id => 'REQ',
188 next_actions => [ ],
190 }, {
191 QER => {
192 prev_actions => [ 'REQ' ],
193 id => 'QER',
194 next_actions => [ 'REQ' ],
198 REQ => {
199 prev_actions => [ 'QER' ],
200 id => 'REQ',
201 next_actions => [ 'QER' ],
203 QER => {
204 prev_actions => [ 'REQ' ],
205 id => 'QER',
206 next_actions => [ 'REQ' ],
209 "REQ atom + linking QER = cyclical status graph"
212 $schema->storage->txn_rollback;
215 subtest 'Backend testing (mocks)' => sub {
217 plan tests => 13;
219 $schema->storage->txn_begin;
221 # testing load_backend & available_backends requires that we have at least
222 # the Dummy plugin installed. load_backend & available_backends don't
223 # currently have tests as a result.
225 t::lib::Mocks->mock_config('interlibrary_loans', { backend_dir => 'a_dir' } );
226 my $backend = Test::MockObject->new;
227 $backend->set_isa('Koha::Illbackends::Mock');
228 $backend->set_always('name', 'Mock');
230 my $patron = $builder->build({ source => 'Borrower' });
231 my $illrq = $builder->build_object({
232 class => 'Koha::Illrequests',
233 value => { borrowernumber => $patron->{borrowernumber} }
236 $illrq->_backend($backend);
238 isa_ok($illrq->_backend, 'Koha::Illbackends::Mock',
239 "OK accessing mocked backend.");
241 # _backend_capability tests:
242 # We need to test whether this optional feature of a mocked backend
243 # behaves as expected.
244 # 3 scenarios: feature not implemented, feature implemented, but requested
245 # capability is not provided by backend, & feature is implemented &
246 # capability exists. This method can be used to implement custom backend
247 # functionality, such as unmediated in the BLDSS backend (also see
248 # bugzilla 18837).
249 $backend->set_always('capabilities', undef);
250 is($illrq->_backend_capability('Test'), 0,
251 "0 returned on Mock not implementing capabilities.");
253 $backend->set_always('capabilities', 0);
254 is($illrq->_backend_capability('Test'), 0,
255 "0 returned on Mock not implementing Test capability.");
257 $backend->set_always('capabilities', sub { return 'bar'; } );
258 is($illrq->_backend_capability('Test'), 'bar',
259 "'bar' returned on Mock implementing Test capability.");
261 # metadata test: we need to be sure that we return the arbitrary values
262 # from the backend.
263 $backend->mock(
264 'metadata',
265 sub {
266 my ( $self, $rq ) = @_;
267 return {
268 ID => $rq->illrequest_id,
269 Title => $rq->patron->borrowernumber
274 is_deeply(
275 $illrq->metadata,
277 ID => $illrq->illrequest_id,
278 Title => $illrq->patron->borrowernumber
280 "Test metadata."
283 # capabilities:
285 # No backend graph extension
286 $backend->set_always('status_graph', {});
287 is_deeply($illrq->capabilities('COMP'),
289 prev_actions => [ 'REQ' ],
290 id => 'COMP',
291 name => 'Completed',
292 ui_method_name => 'Mark completed',
293 method => 'mark_completed',
294 next_actions => [ ],
295 ui_method_icon => 'fa-check',
297 "Dummy status graph for COMP.");
298 is($illrq->capabilities('UNKNOWN'), undef,
299 "Dummy status graph for UNKNOWN.");
300 is_deeply($illrq->capabilities(),
301 $illrq->_core_status_graph,
302 "Dummy full status graph.");
303 # Simple backend graph extension
304 $backend->set_always('status_graph',
306 QER => {
307 prev_actions => [ 'REQ' ],
308 id => 'QER',
309 next_actions => [ 'REQ' ],
312 is_deeply($illrq->capabilities('QER'),
314 prev_actions => [ 'REQ' ],
315 id => 'QER',
316 next_actions => [ 'REQ' ],
318 "Simple status graph for QER.");
319 is($illrq->capabilities('UNKNOWN'), undef,
320 "Simple status graph for UNKNOWN.");
321 is_deeply($illrq->capabilities(),
322 $illrq->_status_graph_union(
323 $illrq->_core_status_graph,
325 QER => {
326 prev_actions => [ 'REQ' ],
327 id => 'QER',
328 next_actions => [ 'REQ' ],
332 "Simple full status graph.");
334 # custom_capability:
336 # No backend graph extension
337 $backend->set_always('status_graph', {});
338 is($illrq->custom_capability('unknown', {}), 0,
339 "Unknown candidate.");
341 # Simple backend graph extension
342 $backend->set_always('status_graph',
344 ID => {
345 prev_actions => [ 'REQ' ],
346 id => 'ID',
347 method => 'identity',
348 next_actions => [ 'REQ' ],
351 $backend->mock('identity',
352 sub { my ( $self, $params ) = @_; return $params->{other}; });
353 is($illrq->custom_capability('identity', { test => 1, method => 'blah' })->{test}, 1,
354 "Resolve identity custom_capability");
356 $schema->storage->txn_rollback;
360 subtest 'Backend core methods' => sub {
362 plan tests => 17;
364 $schema->storage->txn_begin;
366 # Build infrastructure
367 my $backend = Test::MockObject->new;
368 $backend->set_isa('Koha::Illbackends::Mock');
369 $backend->set_always('name', 'Mock');
371 my $config = Test::MockObject->new;
372 $config->set_always('backend_dir', "/tmp");
373 $config->set_always('getLimitRules',
374 { default => { count => 0, method => 'active' } });
376 my $illrq = $builder->build_object({
377 class => 'Koha::Illrequests',
378 value => { backend => undef }
380 $illrq->_config($config);
382 # Test error conditions (no backend)
383 throws_ok { $illrq->load_backend; }
384 'Koha::Exceptions::Ill::InvalidBackendId',
385 'Exception raised correctly';
387 throws_ok { $illrq->load_backend(''); }
388 'Koha::Exceptions::Ill::InvalidBackendId',
389 'Exception raised correctly';
391 # Now load the mocked backend
392 $illrq->_backend($backend);
394 # expandTemplate:
395 is_deeply($illrq->expandTemplate({ test => 1, method => "bar" }),
397 test => 1,
398 method => "bar",
399 template => "/tmp/Mock/intra-includes/bar.inc",
400 opac_template => "/tmp/Mock/opac-includes/bar.inc",
402 "ExpandTemplate");
404 # backend_create
405 # we are testing simple cases.
406 $backend->set_series('create',
407 { stage => 'bar', method => 'create' },
408 { stage => 'commit', method => 'create' },
409 { stage => 'commit', method => 'create' });
410 # Test non-commit
411 is_deeply($illrq->backend_create({test => 1}),
413 stage => 'bar', method => 'create',
414 template => "/tmp/Mock/intra-includes/create.inc",
415 opac_template => "/tmp/Mock/opac-includes/create.inc",
417 "Backend create: arbitrary stage.");
418 # Test commit
419 is_deeply($illrq->backend_create({test => 1}),
421 stage => 'commit', method => 'create', permitted => 0,
422 template => "/tmp/Mock/intra-includes/create.inc",
423 opac_template => "/tmp/Mock/opac-includes/create.inc",
425 "Backend create: arbitrary stage, not permitted.");
426 is($illrq->status, "QUEUED", "Backend create: queued if restricted.");
427 $config->set_always('getLimitRules', {});
428 $illrq->status('NEW');
429 is_deeply($illrq->backend_create({test => 1}),
431 stage => 'commit', method => 'create', permitted => 1,
432 template => "/tmp/Mock/intra-includes/create.inc",
433 opac_template => "/tmp/Mock/opac-includes/create.inc",
435 "Backend create: arbitrary stage, permitted.");
436 is($illrq->status, "NEW", "Backend create: not-queued.");
438 # backend_renew
439 $backend->set_series('renew', { stage => 'bar', method => 'renew' });
440 is_deeply($illrq->backend_renew({test => 1}),
442 stage => 'bar', method => 'renew',
443 template => "/tmp/Mock/intra-includes/renew.inc",
444 opac_template => "/tmp/Mock/opac-includes/renew.inc",
446 "Backend renew: arbitrary stage.");
448 # backend_cancel
449 $backend->set_series('cancel', { stage => 'bar', method => 'cancel' });
450 is_deeply($illrq->backend_cancel({test => 1}),
452 stage => 'bar', method => 'cancel',
453 template => "/tmp/Mock/intra-includes/cancel.inc",
454 opac_template => "/tmp/Mock/opac-includes/cancel.inc",
456 "Backend cancel: arbitrary stage.");
458 # backend_update_status
459 $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
460 is_deeply($illrq->backend_update_status({test => 1}),
462 stage => 'bar', method => 'update_status',
463 template => "/tmp/Mock/intra-includes/update_status.inc",
464 opac_template => "/tmp/Mock/opac-includes/update_status.inc",
466 "Backend update_status: arbitrary stage.");
468 # backend_confirm
469 $backend->set_series('confirm', { stage => 'bar', method => 'confirm' });
470 is_deeply($illrq->backend_confirm({test => 1}),
472 stage => 'bar', method => 'confirm',
473 template => "/tmp/Mock/intra-includes/confirm.inc",
474 opac_template => "/tmp/Mock/opac-includes/confirm.inc",
476 "Backend confirm: arbitrary stage.");
478 $config->set_always('partner_code', "ILLTSTLIB");
479 $backend->set_always('metadata', { Test => "Foobar" });
480 my $illbrn = $builder->build({
481 source => 'Branch',
482 value => { branchemail => "", branchreplyto => "" }
484 my $partner1 = $builder->build({
485 source => 'Borrower',
486 value => { categorycode => "ILLTSTLIB" },
488 my $partner2 = $builder->build({
489 source => 'Borrower',
490 value => { categorycode => "ILLTSTLIB" },
492 my $gen_conf = $illrq->generic_confirm({
493 current_branchcode => $illbrn->{branchcode}
495 isnt(index($gen_conf->{value}->{draft}->{body}, $backend->metadata->{Test}), -1,
496 "Generic confirm: draft contains metadata."
498 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner1->{borrowernumber},
499 "Generic cofnirm: partner 1 is correct."
501 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner2->{borrowernumber},
502 "Generic confirm: partner 2 is correct."
505 dies_ok { $illrq->generic_confirm({
506 current_branchcode => $illbrn->{branchcode},
507 stage => 'draft'
508 }) }
509 "Generic confirm: missing to dies OK.";
511 dies_ok { $illrq->generic_confirm({
512 current_branchcode => $illbrn->{branchcode},
513 partners => $partner1->{email},
514 stage => 'draft'
515 }) }
516 "Generic confirm: missing from dies OK.";
518 $schema->storage->txn_rollback;
522 subtest 'Helpers' => sub {
524 plan tests => 9;
526 $schema->storage->txn_begin;
528 # Build infrastructure
529 my $backend = Test::MockObject->new;
530 $backend->set_isa('Koha::Illbackends::Mock');
531 $backend->set_always('name', 'Mock');
533 my $config = Test::MockObject->new;
534 $config->set_always('backend_dir', "/tmp");
536 my $patron = $builder->build({
537 source => 'Borrower',
538 value => { categorycode => "A" }
540 my $illrq = $builder->build({
541 source => 'Illrequest',
542 value => { branchcode => "CPL", borrowernumber => $patron->{borrowernumber} }
544 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
545 $illrq_obj->_config($config);
546 $illrq_obj->_backend($backend);
548 # getPrefix
549 $config->set_series('getPrefixes',
550 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
551 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
552 is($illrq_obj->getPrefix({ brw_cat => "C", branch => "CPL" }), "CBAR",
553 "getPrefix: brw_cat");
554 $config->set_series('getPrefixes',
555 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
556 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
557 is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "CPL" }), "TEST",
558 "getPrefix: branch");
559 $config->set_series('getPrefixes',
560 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
561 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
562 is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "UNKNOWN" }), "DEFAULT",
563 "getPrefix: default");
564 $config->set_always('getPrefixes', {});
565 is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "UNKNOWN" }), "",
566 "getPrefix: the empty prefix");
568 # id_prefix
569 $config->set_series('getPrefixes',
570 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
571 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
572 is($illrq_obj->id_prefix, "ATEST-", "id_prefix: brw_cat");
573 $config->set_series('getPrefixes',
574 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
575 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
576 is($illrq_obj->id_prefix, "TEST-", "id_prefix: branch");
577 $config->set_series('getPrefixes',
578 { CPLT => "TEST", TSLT => "BAR", default => "DEFAULT" },
579 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
580 is($illrq_obj->id_prefix, "DEFAULT-", "id_prefix: default");
582 # requires_moderation
583 $illrq_obj->status('NEW')->store;
584 is($illrq_obj->requires_moderation, undef, "requires_moderation: No.");
585 $illrq_obj->status('CANCREQ')->store;
586 is($illrq_obj->requires_moderation, 'CANCREQ', "requires_moderation: Yes.");
588 $schema->storage->txn_rollback;
592 subtest 'Censorship' => sub {
594 plan tests => 2;
596 $schema->storage->txn_begin;
598 # Build infrastructure
599 my $backend = Test::MockObject->new;
600 $backend->set_isa('Koha::Illbackends::Mock');
601 $backend->set_always('name', 'Mock');
603 my $config = Test::MockObject->new;
604 $config->set_always('backend_dir', "/tmp");
606 my $illrq = $builder->build({source => 'Illrequest'});
607 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
608 $illrq_obj->_config($config);
609 $illrq_obj->_backend($backend);
611 $config->set_always('censorship', { censor_notes_staff => 1, censor_reply_date => 0 });
613 my $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564 });
614 is_deeply($censor_out, { foo => 'bar', baz => 564, display_reply_date => 1 },
615 "_censor: not OPAC, reply_date = 1");
617 $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564, opac => 1 });
618 is_deeply($censor_out, {
619 foo => 'bar', baz => 564, censor_notes_staff => 1,
620 display_reply_date => 1, opac => 1
621 }, "_censor: notes_staff = 0, reply_date = 0");
623 $schema->storage->txn_rollback;
626 subtest 'Checking Limits' => sub {
628 plan tests => 30;
630 $schema->storage->txn_begin;
632 # Build infrastructure
633 my $backend = Test::MockObject->new;
634 $backend->set_isa('Koha::Illbackends::Mock');
635 $backend->set_always('name', 'Mock');
637 my $config = Test::MockObject->new;
638 $config->set_always('backend_dir', "/tmp");
640 my $illrq = $builder->build({source => 'Illrequest'});
641 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
642 $illrq_obj->_config($config);
643 $illrq_obj->_backend($backend);
645 # getLimits
646 $config->set_series('getLimitRules',
647 { CPL => { count => 1, method => 'test' } },
648 { default => { count => 0, method => 'active' } });
649 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
650 { count => 1, method => 'test' },
651 "getLimits: by value.");
652 is_deeply($illrq_obj->getLimits({ type => 'branch' }),
653 { count => 0, method => 'active' },
654 "getLimits: by default.");
655 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
656 { count => -1, method => 'active' },
657 "getLimits: by hard-coded.");
659 #_limit_counter
660 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
661 1, "_limit_counter: Initial branch annual count.");
662 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
663 1, "_limit_counter: Initial branch active count.");
664 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
665 1, "_limit_counter: Initial patron annual count.");
666 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
667 1, "_limit_counter: Initial patron active count.");
668 $builder->build({
669 source => 'Illrequest',
670 value => {
671 branchcode => $illrq_obj->branchcode,
672 borrowernumber => $illrq_obj->borrowernumber,
675 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
676 2, "_limit_counter: Add a qualifying request for branch annual count.");
677 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
678 2, "_limit_counter: Add a qualifying request for branch active count.");
679 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
680 2, "_limit_counter: Add a qualifying request for patron annual count.");
681 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
682 2, "_limit_counter: Add a qualifying request for patron active count.");
683 $builder->build({
684 source => 'Illrequest',
685 value => {
686 branchcode => $illrq_obj->branchcode,
687 borrowernumber => $illrq_obj->borrowernumber,
688 placed => "2005-05-31",
691 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
692 2, "_limit_counter: Add an out-of-date branch request.");
693 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
694 3, "_limit_counter: Add a qualifying request for branch active count.");
695 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
696 2, "_limit_counter: Add an out-of-date patron request.");
697 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
698 3, "_limit_counter: Add a qualifying request for patron active count.");
699 $builder->build({
700 source => 'Illrequest',
701 value => {
702 branchcode => $illrq_obj->branchcode,
703 borrowernumber => $illrq_obj->borrowernumber,
704 status => "COMP",
707 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
708 3, "_limit_counter: Add a qualifying request for branch annual count.");
709 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
710 3, "_limit_counter: Add a completed request for branch active count.");
711 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
712 3, "_limit_counter: Add a qualifying request for patron annual count.");
713 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
714 3, "_limit_counter: Add a completed request for patron active count.");
716 # check_limits:
718 # We've tested _limit_counter, so all we need to test here is whether the
719 # current counts of 3 for each work as they should against different
720 # configuration declarations.
722 # No limits
723 $config->set_always('getLimitRules', undef);
724 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
725 librarycode => $illrq_obj->branchcode}),
726 1, "check_limits: no configuration => no limits.");
728 # Branch tests
729 $config->set_always('getLimitRules',
730 { $illrq_obj->branchcode => { count => 1, method => 'active' } });
731 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
732 librarycode => $illrq_obj->branchcode}),
733 0, "check_limits: branch active limit exceeded.");
734 $config->set_always('getLimitRules',
735 { $illrq_obj->branchcode => { count => 1, method => 'annual' } });
736 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
737 librarycode => $illrq_obj->branchcode}),
738 0, "check_limits: branch annual limit exceeded.");
739 $config->set_always('getLimitRules',
740 { $illrq_obj->branchcode => { count => 4, method => 'active' } });
741 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
742 librarycode => $illrq_obj->branchcode}),
743 1, "check_limits: branch active limit OK.");
744 $config->set_always('getLimitRules',
745 { $illrq_obj->branchcode => { count => 4, method => 'annual' } });
746 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
747 librarycode => $illrq_obj->branchcode}),
748 1, "check_limits: branch annual limit OK.");
750 # Patron tests
751 $config->set_always('getLimitRules',
752 { $illrq_obj->patron->categorycode => { count => 1, method => 'active' } });
753 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
754 librarycode => $illrq_obj->branchcode}),
755 0, "check_limits: patron category active limit exceeded.");
756 $config->set_always('getLimitRules',
757 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
758 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
759 librarycode => $illrq_obj->branchcode}),
760 0, "check_limits: patron category annual limit exceeded.");
761 $config->set_always('getLimitRules',
762 { $illrq_obj->patron->categorycode => { count => 4, method => 'active' } });
763 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
764 librarycode => $illrq_obj->branchcode}),
765 1, "check_limits: patron category active limit OK.");
766 $config->set_always('getLimitRules',
767 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
768 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
769 librarycode => $illrq_obj->branchcode}),
770 1, "check_limits: patron category annual limit OK.");
772 # One rule cancels the other
773 $config->set_series('getLimitRules',
774 # Branch rules allow request
775 { $illrq_obj->branchcode => { count => 4, method => 'active' } },
776 # Patron rule forbids it
777 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
778 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
779 librarycode => $illrq_obj->branchcode}),
780 0, "check_limits: patron category veto overrides branch OK.");
781 $config->set_series('getLimitRules',
782 # Branch rules allow request
783 { $illrq_obj->branchcode => { count => 1, method => 'active' } },
784 # Patron rule forbids it
785 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
786 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
787 librarycode => $illrq_obj->branchcode}),
788 0, "check_limits: branch veto overrides patron category OK.");
790 $schema->storage->txn_rollback;
793 subtest 'TO_JSON() tests' => sub {
795 plan tests => 10;
797 my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
799 # Mock ->capabilities
800 $illreqmodule->mock( 'capabilities', sub { return 'capable'; } );
802 # Mock ->metadata
803 $illreqmodule->mock( 'metadata', sub { return 'metawhat?'; } );
805 $schema->storage->txn_begin;
807 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
808 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
809 my $illreq = $builder->build_object(
811 class => 'Koha::Illrequests',
812 value => {
813 branchcode => $library->branchcode,
814 borrowernumber => $patron->borrowernumber
818 my $illreq_json = $illreq->TO_JSON;
819 is( $illreq_json->{patron},
820 undef, '%embed not passed, no \'patron\' attribute' );
821 is( $illreq_json->{metadata},
822 undef, '%embed not passed, no \'metadata\' attribute' );
823 is( $illreq_json->{capabilities},
824 undef, '%embed not passed, no \'capabilities\' attribute' );
825 is( $illreq_json->{library},
826 undef, '%embed not passed, no \'library\' attribute' );
828 $illreq_json = $illreq->TO_JSON(
829 { patron => 1, metadata => 1, capabilities => 1, library => 1 } );
830 is( $illreq_json->{patron}->{firstname},
831 $patron->firstname,
832 '%embed passed, \'patron\' attribute correct (firstname)' );
833 is( $illreq_json->{patron}->{surname},
834 $patron->surname,
835 '%embed passed, \'patron\' attribute correct (surname)' );
836 is( $illreq_json->{patron}->{cardnumber},
837 $patron->cardnumber,
838 '%embed passed, \'patron\' attribute correct (cardnumber)' );
839 is( $illreq_json->{metadata},
840 'metawhat?', '%embed passed, \'metadata\' attribute correct' );
841 is( $illreq_json->{capabilities},
842 'capable', '%embed passed, \'capabilities\' attribute correct' );
843 is( $illreq_json->{library}->{branchcode},
844 $library->branchcode, '%embed not passed, no \'library\' attribute' );
846 $schema->storage->txn_rollback;