Bug 20750: Add unit tests
[koha.git] / t / db_dependent / Illrequests.t
blob9046852b4546246dcae355546029b6b80955c715
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 Koha::AuthorisedValueCategories;
26 use Koha::AuthorisedValues;
27 use t::lib::Mocks;
28 use t::lib::TestBuilder;
29 use Test::MockObject;
30 use Test::MockModule;
31 use Test::Exception;
33 use Test::More tests => 11;
35 my $schema = Koha::Database->new->schema;
36 my $builder = t::lib::TestBuilder->new;
37 use_ok('Koha::Illrequest');
38 use_ok('Koha::Illrequests');
40 subtest 'Basic object tests' => sub {
42 plan tests => 24;
44 $schema->storage->txn_begin;
46 Koha::Illrequests->search->delete;
47 my $illrq = $builder->build({ source => 'Illrequest' });
48 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
50 isa_ok($illrq_obj, 'Koha::Illrequest',
51 "Correctly create and load an illrequest object.");
52 isa_ok($illrq_obj->_config, 'Koha::Illrequest::Config',
53 "Created a config object as part of Illrequest creation.");
55 is($illrq_obj->illrequest_id, $illrq->{illrequest_id},
56 "Illrequest_id getter works.");
57 is($illrq_obj->borrowernumber, $illrq->{borrowernumber},
58 "Borrowernumber getter works.");
59 is($illrq_obj->biblio_id, $illrq->{biblio_id},
60 "Biblio_Id getter works.");
61 is($illrq_obj->branchcode, $illrq->{branchcode},
62 "Branchcode getter works.");
63 is($illrq_obj->status, $illrq->{status},
64 "Status getter works.");
65 is($illrq_obj->placed, $illrq->{placed},
66 "Placed getter works.");
67 is($illrq_obj->replied, $illrq->{replied},
68 "Replied getter works.");
69 is($illrq_obj->updated, $illrq->{updated},
70 "Updated getter works.");
71 is($illrq_obj->completed, $illrq->{completed},
72 "Completed getter works.");
73 is($illrq_obj->medium, $illrq->{medium},
74 "Medium getter works.");
75 is($illrq_obj->accessurl, $illrq->{accessurl},
76 "Accessurl getter works.");
77 is($illrq_obj->cost, $illrq->{cost},
78 "Cost getter works.");
79 is($illrq_obj->price_paid, $illrq->{price_paid},
80 "Price_paid getter works.");
81 is($illrq_obj->notesopac, $illrq->{notesopac},
82 "Notesopac getter works.");
83 is($illrq_obj->notesstaff, $illrq->{notesstaff},
84 "Notesstaff getter works.");
85 is($illrq_obj->orderid, $illrq->{orderid},
86 "Orderid getter works.");
87 is($illrq_obj->backend, $illrq->{backend},
88 "Backend getter works.");
90 is($illrq_obj->get_type, undef,
91 'get_type() returns undef if no type is set');
92 $builder->build({
93 source => 'Illrequestattribute',
94 value => {
95 illrequest_id => $illrq_obj->illrequest_id,
96 type => 'type',
97 value => 'book'
99 });
100 is($illrq_obj->get_type, 'book',
101 'get_type() returns correct type if set');
103 isnt($illrq_obj->status, 'COMP',
104 "ILL is not currently marked complete.");
105 $illrq_obj->mark_completed;
106 is($illrq_obj->status, 'COMP',
107 "ILL is now marked complete.");
109 $illrq_obj->delete;
111 is(Koha::Illrequests->search->count, 0,
112 "No illrequest found after delete.");
114 $schema->storage->txn_rollback;
117 subtest 'Working with related objects' => sub {
119 plan tests => 5;
121 $schema->storage->txn_begin;
123 Koha::Illrequests->search->delete;
125 my $patron = $builder->build({ source => 'Borrower' });
126 my $illrq = $builder->build({
127 source => 'Illrequest',
128 value => { borrowernumber => $patron->{borrowernumber} }
130 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
132 isa_ok($illrq_obj->patron, 'Koha::Patron',
133 "OK accessing related patron.");
135 $builder->build({
136 source => 'Illrequestattribute',
137 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'X' }
139 $builder->build({
140 source => 'Illrequestattribute',
141 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Y' }
143 $builder->build({
144 source => 'Illrequestattribute',
145 value => { illrequest_id => $illrq_obj->illrequest_id, type => 'Z' }
148 is($illrq_obj->illrequestattributes->count, Koha::Illrequestattributes->search->count,
149 "Fetching expected number of Illrequestattributes for our request.");
151 my $illrq1 = $builder->build({ source => 'Illrequest' });
152 $builder->build({
153 source => 'Illrequestattribute',
154 value => { illrequest_id => $illrq1->{illrequest_id}, type => 'X' }
157 is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
158 "Fetching expected number of Illrequestattributes for our request.");
160 $illrq_obj->delete;
161 is(Koha::Illrequestattributes->search->count, 1,
162 "Correct number of illrequestattributes after delete.");
164 isa_ok(Koha::Patrons->find($patron->{borrowernumber}), 'Koha::Patron',
165 "Borrower was not deleted after illrq delete.");
167 $schema->storage->txn_rollback;
170 subtest 'Status Graph tests' => sub {
172 plan tests => 4;
174 $schema->storage->txn_begin;
176 my $illrq = $builder->build({source => 'Illrequest'});
177 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
179 # _core_status_graph tests: it's just a constant, so here we just make
180 # sure it returns a hashref.
181 is(ref $illrq_obj->_core_status_graph, "HASH",
182 "_core_status_graph returns a hash.");
184 # _status_graph_union: let's try different merge operations.
185 # Identity operation
186 is_deeply(
187 $illrq_obj->_status_graph_union($illrq_obj->_core_status_graph, {}),
188 $illrq_obj->_core_status_graph,
189 "core_status_graph + null = core_status_graph"
192 # Simple addition
193 is_deeply(
194 $illrq_obj->_status_graph_union({}, $illrq_obj->_core_status_graph),
195 $illrq_obj->_core_status_graph,
196 "null + core_status_graph = core_status_graph"
199 # Correct merge behaviour
200 is_deeply(
201 $illrq_obj->_status_graph_union({
202 REQ => {
203 prev_actions => [ ],
204 id => 'REQ',
205 next_actions => [ ],
207 }, {
208 QER => {
209 prev_actions => [ 'REQ' ],
210 id => 'QER',
211 next_actions => [ 'REQ' ],
215 REQ => {
216 prev_actions => [ 'QER' ],
217 id => 'REQ',
218 next_actions => [ 'QER' ],
220 QER => {
221 prev_actions => [ 'REQ' ],
222 id => 'QER',
223 next_actions => [ 'REQ' ],
226 "REQ atom + linking QER = cyclical status graph"
229 $schema->storage->txn_rollback;
232 subtest 'Backend testing (mocks)' => sub {
234 plan tests => 13;
236 $schema->storage->txn_begin;
238 # testing load_backend & available_backends requires that we have at least
239 # the Dummy plugin installed. load_backend & available_backends don't
240 # currently have tests as a result.
242 t::lib::Mocks->mock_config('interlibrary_loans', { backend_dir => 'a_dir' } );
243 my $backend = Test::MockObject->new;
244 $backend->set_isa('Koha::Illbackends::Mock');
245 $backend->set_always('name', 'Mock');
247 my $patron = $builder->build({ source => 'Borrower' });
248 my $illrq = $builder->build_object({
249 class => 'Koha::Illrequests',
250 value => { borrowernumber => $patron->{borrowernumber} }
253 $illrq->_backend($backend);
255 isa_ok($illrq->_backend, 'Koha::Illbackends::Mock',
256 "OK accessing mocked backend.");
258 # _backend_capability tests:
259 # We need to test whether this optional feature of a mocked backend
260 # behaves as expected.
261 # 3 scenarios: feature not implemented, feature implemented, but requested
262 # capability is not provided by backend, & feature is implemented &
263 # capability exists. This method can be used to implement custom backend
264 # functionality, such as unmediated in the BLDSS backend (also see
265 # bugzilla 18837).
266 $backend->set_always('capabilities', undef);
267 is($illrq->_backend_capability('Test'), 0,
268 "0 returned on Mock not implementing capabilities.");
270 $backend->set_always('capabilities', 0);
271 is($illrq->_backend_capability('Test'), 0,
272 "0 returned on Mock not implementing Test capability.");
274 $backend->set_always('capabilities', sub { return 'bar'; } );
275 is($illrq->_backend_capability('Test'), 'bar',
276 "'bar' returned on Mock implementing Test capability.");
278 # metadata test: we need to be sure that we return the arbitrary values
279 # from the backend.
280 $backend->mock(
281 'metadata',
282 sub {
283 my ( $self, $rq ) = @_;
284 return {
285 ID => $rq->illrequest_id,
286 Title => $rq->patron->borrowernumber
291 is_deeply(
292 $illrq->metadata,
294 ID => $illrq->illrequest_id,
295 Title => $illrq->patron->borrowernumber
297 "Test metadata."
300 # capabilities:
302 # No backend graph extension
303 $backend->set_always('status_graph', {});
304 is_deeply($illrq->capabilities('COMP'),
306 prev_actions => [ 'REQ' ],
307 id => 'COMP',
308 name => 'Completed',
309 ui_method_name => 'Mark completed',
310 method => 'mark_completed',
311 next_actions => [ ],
312 ui_method_icon => 'fa-check',
314 "Dummy status graph for COMP.");
315 is($illrq->capabilities('UNKNOWN'), undef,
316 "Dummy status graph for UNKNOWN.");
317 is_deeply($illrq->capabilities(),
318 $illrq->_core_status_graph,
319 "Dummy full status graph.");
320 # Simple backend graph extension
321 $backend->set_always('status_graph',
323 QER => {
324 prev_actions => [ 'REQ' ],
325 id => 'QER',
326 next_actions => [ 'REQ' ],
329 is_deeply($illrq->capabilities('QER'),
331 prev_actions => [ 'REQ' ],
332 id => 'QER',
333 next_actions => [ 'REQ' ],
335 "Simple status graph for QER.");
336 is($illrq->capabilities('UNKNOWN'), undef,
337 "Simple status graph for UNKNOWN.");
338 is_deeply($illrq->capabilities(),
339 $illrq->_status_graph_union(
340 $illrq->_core_status_graph,
342 QER => {
343 prev_actions => [ 'REQ' ],
344 id => 'QER',
345 next_actions => [ 'REQ' ],
349 "Simple full status graph.");
351 # custom_capability:
353 # No backend graph extension
354 $backend->set_always('status_graph', {});
355 is($illrq->custom_capability('unknown', {}), 0,
356 "Unknown candidate.");
358 # Simple backend graph extension
359 $backend->set_always('status_graph',
361 ID => {
362 prev_actions => [ 'REQ' ],
363 id => 'ID',
364 method => 'identity',
365 next_actions => [ 'REQ' ],
368 $backend->mock('identity',
369 sub { my ( $self, $params ) = @_; return $params->{other}; });
370 is($illrq->custom_capability('identity', { test => 1, method => 'blah' })->{test}, 1,
371 "Resolve identity custom_capability");
373 $schema->storage->txn_rollback;
377 subtest 'Backend core methods' => sub {
379 plan tests => 17;
381 $schema->storage->txn_begin;
383 # Build infrastructure
384 my $backend = Test::MockObject->new;
385 $backend->set_isa('Koha::Illbackends::Mock');
386 $backend->set_always('name', 'Mock');
388 my $config = Test::MockObject->new;
389 $config->set_always('backend_dir', "/tmp");
390 $config->set_always('getLimitRules',
391 { default => { count => 0, method => 'active' } });
393 my $illrq = $builder->build_object({
394 class => 'Koha::Illrequests',
395 value => { backend => undef }
397 $illrq->_config($config);
399 # Test error conditions (no backend)
400 throws_ok { $illrq->load_backend; }
401 'Koha::Exceptions::Ill::InvalidBackendId',
402 'Exception raised correctly';
404 throws_ok { $illrq->load_backend(''); }
405 'Koha::Exceptions::Ill::InvalidBackendId',
406 'Exception raised correctly';
408 # Now load the mocked backend
409 $illrq->_backend($backend);
411 # expandTemplate:
412 is_deeply($illrq->expandTemplate({ test => 1, method => "bar" }),
414 test => 1,
415 method => "bar",
416 template => "/tmp/Mock/intra-includes/bar.inc",
417 opac_template => "/tmp/Mock/opac-includes/bar.inc",
419 "ExpandTemplate");
421 # backend_create
422 # we are testing simple cases.
423 $backend->set_series('create',
424 { stage => 'bar', method => 'create' },
425 { stage => 'commit', method => 'create' },
426 { stage => 'commit', method => 'create' });
427 # Test non-commit
428 is_deeply($illrq->backend_create({test => 1}),
430 stage => 'bar', method => 'create',
431 template => "/tmp/Mock/intra-includes/create.inc",
432 opac_template => "/tmp/Mock/opac-includes/create.inc",
434 "Backend create: arbitrary stage.");
435 # Test commit
436 is_deeply($illrq->backend_create({test => 1}),
438 stage => 'commit', method => 'create', permitted => 0,
439 template => "/tmp/Mock/intra-includes/create.inc",
440 opac_template => "/tmp/Mock/opac-includes/create.inc",
442 "Backend create: arbitrary stage, not permitted.");
443 is($illrq->status, "QUEUED", "Backend create: queued if restricted.");
444 $config->set_always('getLimitRules', {});
445 $illrq->status('NEW');
446 is_deeply($illrq->backend_create({test => 1}),
448 stage => 'commit', method => 'create', permitted => 1,
449 template => "/tmp/Mock/intra-includes/create.inc",
450 opac_template => "/tmp/Mock/opac-includes/create.inc",
452 "Backend create: arbitrary stage, permitted.");
453 is($illrq->status, "NEW", "Backend create: not-queued.");
455 # backend_renew
456 $backend->set_series('renew', { stage => 'bar', method => 'renew' });
457 is_deeply($illrq->backend_renew({test => 1}),
459 stage => 'bar', method => 'renew',
460 template => "/tmp/Mock/intra-includes/renew.inc",
461 opac_template => "/tmp/Mock/opac-includes/renew.inc",
463 "Backend renew: arbitrary stage.");
465 # backend_cancel
466 $backend->set_series('cancel', { stage => 'bar', method => 'cancel' });
467 is_deeply($illrq->backend_cancel({test => 1}),
469 stage => 'bar', method => 'cancel',
470 template => "/tmp/Mock/intra-includes/cancel.inc",
471 opac_template => "/tmp/Mock/opac-includes/cancel.inc",
473 "Backend cancel: arbitrary stage.");
475 # backend_update_status
476 $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
477 is_deeply($illrq->backend_update_status({test => 1}),
479 stage => 'bar', method => 'update_status',
480 template => "/tmp/Mock/intra-includes/update_status.inc",
481 opac_template => "/tmp/Mock/opac-includes/update_status.inc",
483 "Backend update_status: arbitrary stage.");
485 # backend_confirm
486 $backend->set_series('confirm', { stage => 'bar', method => 'confirm' });
487 is_deeply($illrq->backend_confirm({test => 1}),
489 stage => 'bar', method => 'confirm',
490 template => "/tmp/Mock/intra-includes/confirm.inc",
491 opac_template => "/tmp/Mock/opac-includes/confirm.inc",
493 "Backend confirm: arbitrary stage.");
495 $config->set_always('partner_code', "ILLTSTLIB");
496 $backend->set_always('metadata', { Test => "Foobar" });
497 my $illbrn = $builder->build({
498 source => 'Branch',
499 value => { branchemail => "", branchreplyto => "" }
501 my $partner1 = $builder->build({
502 source => 'Borrower',
503 value => { categorycode => "ILLTSTLIB" },
505 my $partner2 = $builder->build({
506 source => 'Borrower',
507 value => { categorycode => "ILLTSTLIB" },
509 my $gen_conf = $illrq->generic_confirm({
510 current_branchcode => $illbrn->{branchcode}
512 isnt(index($gen_conf->{value}->{draft}->{body}, $backend->metadata->{Test}), -1,
513 "Generic confirm: draft contains metadata."
515 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner1->{borrowernumber},
516 "Generic cofnirm: partner 1 is correct."
518 is($gen_conf->{value}->{partners}->next->borrowernumber, $partner2->{borrowernumber},
519 "Generic confirm: partner 2 is correct."
522 dies_ok { $illrq->generic_confirm({
523 current_branchcode => $illbrn->{branchcode},
524 stage => 'draft'
525 }) }
526 "Generic confirm: missing to dies OK.";
528 dies_ok { $illrq->generic_confirm({
529 current_branchcode => $illbrn->{branchcode},
530 partners => $partner1->{email},
531 stage => 'draft'
532 }) }
533 "Generic confirm: missing from dies OK.";
535 $schema->storage->txn_rollback;
539 subtest 'Helpers' => sub {
541 plan tests => 7;
543 $schema->storage->txn_begin;
545 # Build infrastructure
546 my $backend = Test::MockObject->new;
547 $backend->set_isa('Koha::Illbackends::Mock');
548 $backend->set_always('name', 'Mock');
550 my $config = Test::MockObject->new;
551 $config->set_always('backend_dir', "/tmp");
553 my $patron = $builder->build({
554 source => 'Borrower',
555 value => { categorycode => "A" }
557 my $illrq = $builder->build({
558 source => 'Illrequest',
559 value => { branchcode => "CPL", borrowernumber => $patron->{borrowernumber} }
561 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
562 $illrq_obj->_config($config);
563 $illrq_obj->_backend($backend);
565 # getPrefix
566 $config->set_series('getPrefixes',
567 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
568 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
569 is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "CPL" }), "TEST",
570 "getPrefix: branch");
571 $config->set_series('getPrefixes',
572 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
573 { A => "ATEST", C => "CBAR", default => "DEFAULT" });
574 is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
575 "getPrefix: default");
576 $config->set_always('getPrefixes', {});
577 is($illrq_obj->getPrefix({ branch => "UNKNOWN" }), "",
578 "getPrefix: the empty prefix");
580 # id_prefix
581 $config->set_series('getPrefixes',
582 { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
583 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
584 is($illrq_obj->id_prefix, "TEST-", "id_prefix: branch");
585 $config->set_series('getPrefixes',
586 { CPLT => "TEST", TSLT => "BAR", default => "DEFAULT" },
587 { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
588 is($illrq_obj->id_prefix, "", "id_prefix: default");
590 # requires_moderation
591 $illrq_obj->status('NEW')->store;
592 is($illrq_obj->requires_moderation, undef, "requires_moderation: No.");
593 $illrq_obj->status('CANCREQ')->store;
594 is($illrq_obj->requires_moderation, 'CANCREQ', "requires_moderation: Yes.");
596 $schema->storage->txn_rollback;
600 subtest 'Censorship' => sub {
602 plan tests => 2;
604 $schema->storage->txn_begin;
606 # Build infrastructure
607 my $backend = Test::MockObject->new;
608 $backend->set_isa('Koha::Illbackends::Mock');
609 $backend->set_always('name', 'Mock');
611 my $config = Test::MockObject->new;
612 $config->set_always('backend_dir', "/tmp");
614 my $illrq = $builder->build({source => 'Illrequest'});
615 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
616 $illrq_obj->_config($config);
617 $illrq_obj->_backend($backend);
619 $config->set_always('censorship', { censor_notes_staff => 1, censor_reply_date => 0 });
621 my $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564 });
622 is_deeply($censor_out, { foo => 'bar', baz => 564, display_reply_date => 1 },
623 "_censor: not OPAC, reply_date = 1");
625 $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564, opac => 1 });
626 is_deeply($censor_out, {
627 foo => 'bar', baz => 564, censor_notes_staff => 1,
628 display_reply_date => 1, opac => 1
629 }, "_censor: notes_staff = 0, reply_date = 0");
631 $schema->storage->txn_rollback;
634 subtest 'Checking Limits' => sub {
636 plan tests => 30;
638 $schema->storage->txn_begin;
640 # Build infrastructure
641 my $backend = Test::MockObject->new;
642 $backend->set_isa('Koha::Illbackends::Mock');
643 $backend->set_always('name', 'Mock');
645 my $config = Test::MockObject->new;
646 $config->set_always('backend_dir', "/tmp");
648 my $illrq = $builder->build({source => 'Illrequest'});
649 my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
650 $illrq_obj->_config($config);
651 $illrq_obj->_backend($backend);
653 # getLimits
654 $config->set_series('getLimitRules',
655 { CPL => { count => 1, method => 'test' } },
656 { default => { count => 0, method => 'active' } });
657 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
658 { count => 1, method => 'test' },
659 "getLimits: by value.");
660 is_deeply($illrq_obj->getLimits({ type => 'branch' }),
661 { count => 0, method => 'active' },
662 "getLimits: by default.");
663 is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
664 { count => -1, method => 'active' },
665 "getLimits: by hard-coded.");
667 #_limit_counter
668 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
669 1, "_limit_counter: Initial branch annual count.");
670 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
671 1, "_limit_counter: Initial branch active count.");
672 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
673 1, "_limit_counter: Initial patron annual count.");
674 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
675 1, "_limit_counter: Initial patron active count.");
676 $builder->build({
677 source => 'Illrequest',
678 value => {
679 branchcode => $illrq_obj->branchcode,
680 borrowernumber => $illrq_obj->borrowernumber,
683 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
684 2, "_limit_counter: Add a qualifying request for branch annual count.");
685 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
686 2, "_limit_counter: Add a qualifying request for branch active count.");
687 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
688 2, "_limit_counter: Add a qualifying request for patron annual count.");
689 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
690 2, "_limit_counter: Add a qualifying request for patron active count.");
691 $builder->build({
692 source => 'Illrequest',
693 value => {
694 branchcode => $illrq_obj->branchcode,
695 borrowernumber => $illrq_obj->borrowernumber,
696 placed => "2005-05-31",
699 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
700 2, "_limit_counter: Add an out-of-date branch request.");
701 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
702 3, "_limit_counter: Add a qualifying request for branch active count.");
703 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
704 2, "_limit_counter: Add an out-of-date patron request.");
705 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
706 3, "_limit_counter: Add a qualifying request for patron active count.");
707 $builder->build({
708 source => 'Illrequest',
709 value => {
710 branchcode => $illrq_obj->branchcode,
711 borrowernumber => $illrq_obj->borrowernumber,
712 status => "COMP",
715 is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
716 3, "_limit_counter: Add a qualifying request for branch annual count.");
717 is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
718 3, "_limit_counter: Add a completed request for branch active count.");
719 is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
720 3, "_limit_counter: Add a qualifying request for patron annual count.");
721 is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
722 3, "_limit_counter: Add a completed request for patron active count.");
724 # check_limits:
726 # We've tested _limit_counter, so all we need to test here is whether the
727 # current counts of 3 for each work as they should against different
728 # configuration declarations.
730 # No limits
731 $config->set_always('getLimitRules', undef);
732 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
733 librarycode => $illrq_obj->branchcode}),
734 1, "check_limits: no configuration => no limits.");
736 # Branch tests
737 $config->set_always('getLimitRules',
738 { $illrq_obj->branchcode => { count => 1, method => 'active' } });
739 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
740 librarycode => $illrq_obj->branchcode}),
741 0, "check_limits: branch active limit exceeded.");
742 $config->set_always('getLimitRules',
743 { $illrq_obj->branchcode => { count => 1, method => 'annual' } });
744 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
745 librarycode => $illrq_obj->branchcode}),
746 0, "check_limits: branch annual limit exceeded.");
747 $config->set_always('getLimitRules',
748 { $illrq_obj->branchcode => { count => 4, method => 'active' } });
749 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
750 librarycode => $illrq_obj->branchcode}),
751 1, "check_limits: branch active limit OK.");
752 $config->set_always('getLimitRules',
753 { $illrq_obj->branchcode => { count => 4, method => 'annual' } });
754 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
755 librarycode => $illrq_obj->branchcode}),
756 1, "check_limits: branch annual limit OK.");
758 # Patron tests
759 $config->set_always('getLimitRules',
760 { $illrq_obj->patron->categorycode => { count => 1, method => 'active' } });
761 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
762 librarycode => $illrq_obj->branchcode}),
763 0, "check_limits: patron category active limit exceeded.");
764 $config->set_always('getLimitRules',
765 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
766 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
767 librarycode => $illrq_obj->branchcode}),
768 0, "check_limits: patron category annual limit exceeded.");
769 $config->set_always('getLimitRules',
770 { $illrq_obj->patron->categorycode => { count => 4, method => 'active' } });
771 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
772 librarycode => $illrq_obj->branchcode}),
773 1, "check_limits: patron category active limit OK.");
774 $config->set_always('getLimitRules',
775 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
776 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
777 librarycode => $illrq_obj->branchcode}),
778 1, "check_limits: patron category annual limit OK.");
780 # One rule cancels the other
781 $config->set_series('getLimitRules',
782 # Branch rules allow request
783 { $illrq_obj->branchcode => { count => 4, method => 'active' } },
784 # Patron rule forbids it
785 { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
786 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
787 librarycode => $illrq_obj->branchcode}),
788 0, "check_limits: patron category veto overrides branch OK.");
789 $config->set_series('getLimitRules',
790 # Branch rules allow request
791 { $illrq_obj->branchcode => { count => 1, method => 'active' } },
792 # Patron rule forbids it
793 { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
794 is($illrq_obj->check_limits({patron => $illrq_obj->patron,
795 librarycode => $illrq_obj->branchcode}),
796 0, "check_limits: branch veto overrides patron category OK.");
798 $schema->storage->txn_rollback;
801 subtest 'Custom statuses' => sub {
803 plan tests => 3;
805 $schema->storage->txn_begin;
807 my $cat = Koha::AuthorisedValueCategories->search(
809 category_name => 'ILLSTATUS'
813 if ($cat->count == 0) {
814 $cat = $builder->build_object(
816 class => 'Koha::AuthorisedValueCategory',
817 value => {
818 category_name => 'ILLSTATUS'
824 my $av = $builder->build_object(
826 class => 'Koha::AuthorisedValues',
827 value => {
828 category => 'ILLSTATUS'
833 is($av->category, 'ILLSTATUS',
834 "Successfully created authorised value for custom status");
836 my $ill_req = $builder->build_object(
838 class => 'Koha::Illrequests',
839 value => {
840 status_alias => $av->authorised_value
844 isa_ok($ill_req->statusalias, 'Koha::AuthorisedValue',
845 "statusalias correctly returning Koha::AuthorisedValue object");
847 $ill_req->status("COMP");
848 is($ill_req->statusalias, undef,
849 "Koha::Illrequest->status overloading resetting status_alias");
851 $schema->storage->txn_rollback;