Bug 7317: Interlibrary loans framework for Koha.
[koha.git] / t / db_dependent / Illrequests.t
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::Illrequest::Config;
25 use Koha::Patrons;
26 use t::lib::Mocks;
27 use t::lib::TestBuilder;
28 use Test::MockObject;
29 use Test::Exception;
30
31 use Test::More tests => 10;
32
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35 use_ok('Koha::Illrequest');
36 use_ok('Koha::Illrequests');
37
38 subtest 'Basic object tests' => sub {
39
40     plan tests => 21;
41
42     $schema->storage->txn_begin;
43
44     my $illrq = $builder->build({ source => 'Illrequest' });
45     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
46
47     isa_ok($illrq_obj, 'Koha::Illrequest',
48            "Correctly create and load an illrequest object.");
49     isa_ok($illrq_obj->_config, 'Koha::Illrequest::Config',
50            "Created a config object as part of Illrequest creation.");
51
52     is($illrq_obj->illrequest_id, $illrq->{illrequest_id},
53        "Illrequest_id getter works.");
54     is($illrq_obj->borrowernumber, $illrq->{borrowernumber},
55        "Borrowernumber getter works.");
56     is($illrq_obj->biblio_id, $illrq->{biblio_id},
57        "Biblio_Id getter works.");
58     is($illrq_obj->branchcode, $illrq->{branchcode},
59        "Branchcode getter works.");
60     is($illrq_obj->status, $illrq->{status},
61        "Status getter works.");
62     is($illrq_obj->placed, $illrq->{placed},
63        "Placed getter works.");
64     is($illrq_obj->replied, $illrq->{replied},
65        "Replied getter works.");
66     is($illrq_obj->updated, $illrq->{updated},
67        "Updated getter works.");
68     is($illrq_obj->completed, $illrq->{completed},
69        "Completed getter works.");
70     is($illrq_obj->medium, $illrq->{medium},
71        "Medium getter works.");
72     is($illrq_obj->accessurl, $illrq->{accessurl},
73        "Accessurl getter works.");
74     is($illrq_obj->cost, $illrq->{cost},
75        "Cost getter works.");
76     is($illrq_obj->notesopac, $illrq->{notesopac},
77        "Notesopac getter works.");
78     is($illrq_obj->notesstaff, $illrq->{notesstaff},
79        "Notesstaff getter works.");
80     is($illrq_obj->orderid, $illrq->{orderid},
81        "Orderid getter works.");
82     is($illrq_obj->backend, $illrq->{backend},
83        "Backend getter works.");
84
85     isnt($illrq_obj->status, 'COMP',
86          "ILL is not currently marked complete.");
87     $illrq_obj->mark_completed;
88     is($illrq_obj->status, 'COMP',
89        "ILL is now marked complete.");
90
91     $illrq_obj->delete;
92
93     is(Koha::Illrequests->search->count, 0,
94        "No illrequest found after delete.");
95
96     $schema->storage->txn_rollback;
97 };
98
99 subtest 'Working with related objects' => sub {
100
101     plan tests => 5;
102
103     $schema->storage->txn_begin;
104
105     my $patron = $builder->build({ source => 'Borrower' });
106     my $illrq = $builder->build({
107         source => 'Illrequest',
108         value => { borrowernumber => $patron->{borrowernumber} }
109     });
110     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
111
112     isa_ok($illrq_obj->patron, 'Koha::Patron',
113            "OK accessing related patron.");
114
115     $builder->build({
116         source => 'Illrequestattribute',
117         value  => { illrequest_id => $illrq_obj->illrequest_id, type => 'X' }
118     });
119     $builder->build({
120         source => 'Illrequestattribute',
121         value  => { illrequest_id => $illrq_obj->illrequest_id, type => 'Y' }
122     });
123     $builder->build({
124         source => 'Illrequestattribute',
125         value  => { illrequest_id => $illrq_obj->illrequest_id, type => 'Z' }
126     });
127
128     is($illrq_obj->illrequestattributes->count, Koha::Illrequestattributes->search->count,
129        "Fetching expected number of Illrequestattributes for our request.");
130
131     my $illrq1 = $builder->build({ source => 'Illrequest' });
132     $builder->build({
133         source => 'Illrequestattribute',
134         value  => { illrequest_id => $illrq1->{illrequest_id}, type => 'X' }
135     });
136
137     is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
138        "Fetching expected number of Illrequestattributes for our request.");
139
140     $illrq_obj->delete;
141     is(Koha::Illrequestattributes->search->count, 1,
142        "Correct number of illrequestattributes after delete.");
143
144     isa_ok(Koha::Patrons->find($patron->{borrowernumber}), 'Koha::Patron',
145            "Borrower was not deleted after illrq delete.");
146
147     $schema->storage->txn_rollback;
148 };
149
150 subtest 'Status Graph tests' => sub {
151
152     plan tests => 4;
153
154     $schema->storage->txn_begin;
155
156     my $illrq = $builder->build({source => 'Illrequest'});
157     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
158
159     # _core_status_graph tests: it's just a constant, so here we just make
160     # sure it returns a hashref.
161     is(ref $illrq_obj->_core_status_graph, "HASH",
162        "_core_status_graph returns a hash.");
163
164     # _status_graph_union: let's try different merge operations.
165     # Identity operation
166     is_deeply(
167         $illrq_obj->_status_graph_union($illrq_obj->_core_status_graph, {}),
168         $illrq_obj->_core_status_graph,
169         "core_status_graph + null = core_status_graph"
170     );
171
172     # Simple addition
173     is_deeply(
174         $illrq_obj->_status_graph_union({}, $illrq_obj->_core_status_graph),
175         $illrq_obj->_core_status_graph,
176         "null + core_status_graph = core_status_graph"
177     );
178
179     # Correct merge behaviour
180     is_deeply(
181         $illrq_obj->_status_graph_union({
182             REQ => {
183                 prev_actions   => [ ],
184                 id             => 'REQ',
185                 next_actions   => [ ],
186             },
187         }, {
188             QER => {
189                 prev_actions   => [ 'REQ' ],
190                 id             => 'QER',
191                 next_actions   => [ 'REQ' ],
192             },
193         }),
194         {
195             REQ => {
196                 prev_actions   => [ 'QER' ],
197                 id             => 'REQ',
198                 next_actions   => [ 'QER' ],
199             },
200             QER => {
201                 prev_actions   => [ 'REQ' ],
202                 id             => 'QER',
203                 next_actions   => [ 'REQ' ],
204             },
205         },
206         "REQ atom + linking QER = cyclical status graph"
207     );
208
209     $schema->storage->txn_rollback;
210 };
211
212 subtest 'Backend testing (mocks)' => sub {
213
214     plan tests => 13;
215
216     $schema->storage->txn_begin;
217
218     # testing load_backend & available_backends requires that we have at least
219     # the Dummy plugin installed.  load_backend & available_backends don't
220     # currently have tests as a result.
221
222     my $backend = Test::MockObject->new;
223     $backend->set_isa('Koha::Illbackends::Mock');
224     $backend->set_always('name', 'Mock');
225
226     my $patron = $builder->build({ source => 'Borrower' });
227     my $illrq = $builder->build({
228         source => 'Illrequest',
229         value => { borrowernumber => $patron->{borrowernumber} }
230     });
231     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
232
233     $illrq_obj->_backend($backend);
234
235     isa_ok($illrq_obj->_backend, 'Koha::Illbackends::Mock',
236            "OK accessing mocked backend.");
237
238     # _backend_capability tests:
239     # We need to test whether this optional feature of a mocked backend
240     # behaves as expected.
241     # 3 scenarios: feature not implemented, feature implemented, but requested
242     # capability is not provided by backend, & feature is implemented &
243     # capability exists.  This method can be used to implement custom backend
244     # functionality, such as unmediated in the BLDSS backend (also see
245     # bugzilla 18837).
246     $backend->set_always('capabilities', undef);
247     is($illrq_obj->_backend_capability('Test'), 0,
248        "0 returned on Mock not implementing capabilities.");
249
250     $backend->set_always('capabilities', 0);
251     is($illrq_obj->_backend_capability('Test'), 0,
252        "0 returned on Mock not implementing Test capability.");
253
254     $backend->set_always('capabilities', sub { return 'bar'; } );
255     is($illrq_obj->_backend_capability('Test'), 'bar',
256        "'bar' returned on Mock implementing Test capability.");
257
258     # metadata test: we need to be sure that we return the arbitrary values
259     # from the backend.
260     $backend->mock(
261         'metadata',
262         sub {
263             my ( $self, $rq ) = @_;
264             return {
265                 ID => $rq->illrequest_id,
266                 Title => $rq->patron->borrowernumber
267             }
268         }
269     );
270
271     is_deeply(
272         $illrq_obj->metadata,
273         {
274             ID => $illrq_obj->illrequest_id,
275             Title => $illrq_obj->patron->borrowernumber
276         },
277         "Test metadata."
278     );
279
280     # capabilities:
281
282     # No backend graph extension
283     $backend->set_always('status_graph', {});
284     is_deeply($illrq_obj->capabilities('COMP'),
285               {
286                   prev_actions   => [ 'REQ' ],
287                   id             => 'COMP',
288                   name           => 'Completed',
289                   ui_method_name => 'Mark completed',
290                   method         => 'mark_completed',
291                   next_actions   => [ ],
292                   ui_method_icon => 'fa-check',
293               },
294               "Dummy status graph for COMP.");
295     is($illrq_obj->capabilities('UNKNOWN'), undef,
296        "Dummy status graph for UNKNOWN.");
297     is_deeply($illrq_obj->capabilities(),
298               $illrq_obj->_core_status_graph,
299               "Dummy full status graph.");
300     # Simple backend graph extension
301     $backend->set_always('status_graph',
302                          {
303                              QER => {
304                                  prev_actions   => [ 'REQ' ],
305                                  id             => 'QER',
306                                  next_actions   => [ 'REQ' ],
307                              },
308                          });
309     is_deeply($illrq_obj->capabilities('QER'),
310               {
311                   prev_actions   => [ 'REQ' ],
312                   id             => 'QER',
313                   next_actions   => [ 'REQ' ],
314               },
315               "Simple status graph for QER.");
316     is($illrq_obj->capabilities('UNKNOWN'), undef,
317        "Simple status graph for UNKNOWN.");
318     is_deeply($illrq_obj->capabilities(),
319               $illrq_obj->_status_graph_union(
320                   $illrq_obj->_core_status_graph,
321                   {
322                       QER => {
323                           prev_actions   => [ 'REQ' ],
324                           id             => 'QER',
325                           next_actions   => [ 'REQ' ],
326                       },
327                   }
328               ),
329               "Simple full status graph.");
330
331     # custom_capability:
332
333     # No backend graph extension
334     $backend->set_always('status_graph', {});
335     is($illrq_obj->custom_capability('unknown', {}), 0,
336        "Unknown candidate.");
337
338     # Simple backend graph extension
339     $backend->set_always('status_graph',
340                          {
341                              ID => {
342                                  prev_actions   => [ 'REQ' ],
343                                  id             => 'ID',
344                                  method         => 'identity',
345                                  next_actions   => [ 'REQ' ],
346                              },
347                          });
348     $backend->mock('identity',
349                    sub { my ( $self, $params ) = @_; return $params->{other}; });
350     is($illrq_obj->custom_capability('identity', { test => 1 })->{test}, 1,
351        "Resolve identity custom_capability");
352
353     $schema->storage->txn_rollback;
354 };
355
356
357 subtest 'Backend core methods' => sub {
358
359     plan tests => 16;
360
361     $schema->storage->txn_begin;
362
363     # Build infrastructure
364     my $backend = Test::MockObject->new;
365     $backend->set_isa('Koha::Illbackends::Mock');
366     $backend->set_always('name', 'Mock');
367
368     my $config = Test::MockObject->new;
369     $config->set_always('backend_dir', "/tmp");
370     $config->set_always('getLimitRules',
371                         { default => { count => 0, method => 'active' } });
372
373     my $illrq = $builder->build({source => 'Illrequest'});
374     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
375     $illrq_obj->_config($config);
376     $illrq_obj->_backend($backend);
377
378     # expandTemplate:
379     is_deeply($illrq_obj->expandTemplate({ test => 1, method => "bar" }),
380               {
381                   test => 1,
382                   method => "bar",
383                   template => "/tmp/Mock/intra-includes/bar.inc",
384                   opac_template => "/tmp/Mock/opac-includes/bar.inc",
385               },
386               "ExpandTemplate");
387
388     # backend_create
389     # we are testing simple cases.
390     $backend->set_series('create',
391                          { stage => 'bar', method => 'create' },
392                          { stage => 'commit', method => 'create' },
393                          { stage => 'commit', method => 'create' });
394     # Test Copyright Clearance
395     t::lib::Mocks::mock_preference("ILLModuleCopyrightClearance", "Test Copyright Clearance.");
396     is_deeply($illrq_obj->backend_create({test => 1}),
397               {
398                   error   => 0,
399                   status  => '',
400                   message => '',
401                   method  => 'create',
402                   stage   => 'copyrightclearance',
403                   value   => {
404                       backend => "Mock"
405                   }
406               },
407               "Backend create: copyright clearance.");
408     t::lib::Mocks::mock_preference("ILLModuleCopyrightClearance", "");
409     # Test non-commit
410     is_deeply($illrq_obj->backend_create({test => 1}),
411               {
412                   stage => 'bar', method => 'create',
413                   template => "/tmp/Mock/intra-includes/create.inc",
414                   opac_template => "/tmp/Mock/opac-includes/create.inc",
415               },
416               "Backend create: arbitrary stage.");
417     # Test commit
418     is_deeply($illrq_obj->backend_create({test => 1}),
419               {
420                   stage => 'commit', method => 'create', permitted => 0,
421                   template => "/tmp/Mock/intra-includes/create.inc",
422                   opac_template => "/tmp/Mock/opac-includes/create.inc",
423               },
424               "Backend create: arbitrary stage, not permitted.");
425     is($illrq_obj->status, "QUEUED", "Backend create: queued if restricted.");
426     $config->set_always('getLimitRules', {});
427     $illrq_obj->status('NEW');
428     is_deeply($illrq_obj->backend_create({test => 1}),
429               {
430                   stage => 'commit', method => 'create', permitted => 1,
431                   template => "/tmp/Mock/intra-includes/create.inc",
432                   opac_template => "/tmp/Mock/opac-includes/create.inc",
433               },
434               "Backend create: arbitrary stage, permitted.");
435     is($illrq_obj->status, "NEW", "Backend create: not-queued.");
436
437     # backend_renew
438     $backend->set_series('renew', { stage => 'bar', method => 'renew' });
439     is_deeply($illrq_obj->backend_renew({test => 1}),
440               {
441                   stage => 'bar', method => 'renew',
442                   template => "/tmp/Mock/intra-includes/renew.inc",
443                   opac_template => "/tmp/Mock/opac-includes/renew.inc",
444               },
445               "Backend renew: arbitrary stage.");
446
447     # backend_cancel
448     $backend->set_series('cancel', { stage => 'bar', method => 'cancel' });
449     is_deeply($illrq_obj->backend_cancel({test => 1}),
450               {
451                   stage => 'bar', method => 'cancel',
452                   template => "/tmp/Mock/intra-includes/cancel.inc",
453                   opac_template => "/tmp/Mock/opac-includes/cancel.inc",
454               },
455               "Backend cancel: arbitrary stage.");
456
457     # backend_update_status
458     $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
459     is_deeply($illrq_obj->backend_update_status({test => 1}),
460               {
461                   stage => 'bar', method => 'update_status',
462                   template => "/tmp/Mock/intra-includes/update_status.inc",
463                   opac_template => "/tmp/Mock/opac-includes/update_status.inc",
464               },
465               "Backend update_status: arbitrary stage.");
466
467     # backend_confirm
468     $backend->set_series('confirm', { stage => 'bar', method => 'confirm' });
469     is_deeply($illrq_obj->backend_confirm({test => 1}),
470               {
471                   stage => 'bar', method => 'confirm',
472                   template => "/tmp/Mock/intra-includes/confirm.inc",
473                   opac_template => "/tmp/Mock/opac-includes/confirm.inc",
474               },
475               "Backend confirm: arbitrary stage.");
476
477     $config->set_always('partner_code', "ILLTSTLIB");
478     $backend->set_always('metadata', { Test => "Foobar" });
479     my $illbrn = $builder->build({
480         source => 'Branch',
481         value => { branchemail => "", branchreplyto => "" }
482     });
483     my $partner1 = $builder->build({
484         source => 'Borrower',
485         value => { categorycode => "ILLTSTLIB" },
486     });
487     my $partner2 = $builder->build({
488         source => 'Borrower',
489         value => { categorycode => "ILLTSTLIB" },
490     });
491     my $gen_conf = $illrq_obj->generic_confirm({
492         current_branchcode => $illbrn->{branchcode}
493     });
494     isnt(index($gen_conf->{value}->{draft}->{body}, $backend->metadata->{Test}), -1,
495          "Generic confirm: draft contains metadata."
496     );
497     is($gen_conf->{value}->{partners}->next->borrowernumber, $partner1->{borrowernumber},
498        "Generic cofnirm: partner 1 is correct."
499     );
500     is($gen_conf->{value}->{partners}->next->borrowernumber, $partner2->{borrowernumber},
501        "Generic confirm: partner 2 is correct."
502     );
503
504     dies_ok { $illrq_obj->generic_confirm({
505         current_branchcode => $illbrn->{branchcode},
506         stage => 'draft'
507     }) }
508         "Generic confirm: missing to dies OK.";
509
510     dies_ok { $illrq_obj->generic_confirm({
511         current_branchcode => $illbrn->{branchcode},
512         partners => $partner1->{email},
513         stage => 'draft'
514     }) }
515         "Generic confirm: missing from dies OK.";
516
517     $schema->storage->txn_rollback;
518 };
519
520
521 subtest 'Helpers' => sub {
522
523     plan tests => 9;
524
525     $schema->storage->txn_begin;
526
527     # Build infrastructure
528     my $backend = Test::MockObject->new;
529     $backend->set_isa('Koha::Illbackends::Mock');
530     $backend->set_always('name', 'Mock');
531
532     my $config = Test::MockObject->new;
533     $config->set_always('backend_dir', "/tmp");
534
535     my $patron = $builder->build({
536         source => 'Borrower',
537         value => { categorycode => "A" }
538     });
539     my $illrq = $builder->build({
540         source => 'Illrequest',
541         value => { branchcode => "CPL", borrowernumber => $patron->{borrowernumber} }
542     });
543     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
544     $illrq_obj->_config($config);
545     $illrq_obj->_backend($backend);
546
547     # getPrefix
548     $config->set_series('getPrefixes',
549                         { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
550                         { A => "ATEST", C => "CBAR", default => "DEFAULT" });
551     is($illrq_obj->getPrefix({ brw_cat => "C", branch => "CPL" }), "CBAR",
552        "getPrefix: brw_cat");
553     $config->set_series('getPrefixes',
554                         { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
555                         { A => "ATEST", C => "CBAR", default => "DEFAULT" });
556     is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "CPL" }), "TEST",
557        "getPrefix: branch");
558     $config->set_series('getPrefixes',
559                         { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
560                         { A => "ATEST", C => "CBAR", default => "DEFAULT" });
561     is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "UNKNOWN" }), "DEFAULT",
562        "getPrefix: default");
563     $config->set_always('getPrefixes', {});
564     is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "UNKNOWN" }), "",
565        "getPrefix: the empty prefix");
566
567     # id_prefix
568     $config->set_series('getPrefixes',
569                         { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
570                         { A => "ATEST", C => "CBAR", default => "DEFAULT" });
571     is($illrq_obj->id_prefix, "ATEST-", "id_prefix: brw_cat");
572     $config->set_series('getPrefixes',
573                         { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
574                         { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
575     is($illrq_obj->id_prefix, "TEST-", "id_prefix: branch");
576     $config->set_series('getPrefixes',
577                         { CPLT => "TEST", TSLT => "BAR", default => "DEFAULT" },
578                         { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
579     is($illrq_obj->id_prefix, "DEFAULT-", "id_prefix: default");
580
581     # requires_moderation
582     $illrq_obj->status('NEW')->store;
583     is($illrq_obj->requires_moderation, undef, "requires_moderation: No.");
584     $illrq_obj->status('CANCREQ')->store;
585     is($illrq_obj->requires_moderation, 'CANCREQ', "requires_moderation: Yes.");
586
587     $schema->storage->txn_rollback;
588 };
589
590
591 subtest 'Censorship' => sub {
592
593     plan tests => 2;
594
595     $schema->storage->txn_begin;
596
597     # Build infrastructure
598     my $backend = Test::MockObject->new;
599     $backend->set_isa('Koha::Illbackends::Mock');
600     $backend->set_always('name', 'Mock');
601
602     my $config = Test::MockObject->new;
603     $config->set_always('backend_dir', "/tmp");
604
605     my $illrq = $builder->build({source => 'Illrequest'});
606     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
607     $illrq_obj->_config($config);
608     $illrq_obj->_backend($backend);
609
610     $config->set_always('censorship', { censor_notes_staff => 1, censor_reply_date => 0 });
611
612     my $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564 });
613     is_deeply($censor_out, { foo => 'bar', baz => 564, display_reply_date => 1 },
614               "_censor: not OPAC, reply_date = 1");
615
616     $censor_out = $illrq_obj->_censor({ foo => 'bar', baz => 564, opac => 1 });
617     is_deeply($censor_out, {
618         foo => 'bar', baz => 564, censor_notes_staff => 1,
619         display_reply_date => 1, opac => 1
620     }, "_censor: notes_staff = 0, reply_date = 0");
621
622     $schema->storage->txn_rollback;
623 };
624
625 subtest 'Checking Limits' => sub {
626
627     plan tests => 30;
628
629     $schema->storage->txn_begin;
630
631     # Build infrastructure
632     my $backend = Test::MockObject->new;
633     $backend->set_isa('Koha::Illbackends::Mock');
634     $backend->set_always('name', 'Mock');
635
636     my $config = Test::MockObject->new;
637     $config->set_always('backend_dir', "/tmp");
638
639     my $illrq = $builder->build({source => 'Illrequest'});
640     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
641     $illrq_obj->_config($config);
642     $illrq_obj->_backend($backend);
643
644     # getLimits
645     $config->set_series('getLimitRules',
646                         { CPL => { count => 1, method => 'test' } },
647                         { default => { count => 0, method => 'active' } });
648     is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
649               { count => 1, method => 'test' },
650               "getLimits: by value.");
651     is_deeply($illrq_obj->getLimits({ type => 'branch' }),
652               { count => 0, method => 'active' },
653               "getLimits: by default.");
654     is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
655               { count => -1, method => 'active' },
656               "getLimits: by hard-coded.");
657
658     #_limit_counter
659     is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
660        1, "_limit_counter: Initial branch annual count.");
661     is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
662        1, "_limit_counter: Initial branch active count.");
663     is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
664        1, "_limit_counter: Initial patron annual count.");
665     is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
666        1, "_limit_counter: Initial patron active count.");
667     $builder->build({
668         source => 'Illrequest',
669         value => {
670             branchcode => $illrq_obj->branchcode,
671             borrowernumber => $illrq_obj->borrowernumber,
672         }
673     });
674     is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
675        2, "_limit_counter: Add a qualifying request for branch annual count.");
676     is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
677        2, "_limit_counter: Add a qualifying request for branch active count.");
678     is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
679        2, "_limit_counter: Add a qualifying request for patron annual count.");
680     is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
681        2, "_limit_counter: Add a qualifying request for patron active count.");
682     $builder->build({
683         source => 'Illrequest',
684         value => {
685             branchcode => $illrq_obj->branchcode,
686             borrowernumber => $illrq_obj->borrowernumber,
687             placed => "2005-05-31",
688         }
689     });
690     is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
691        2, "_limit_counter: Add an out-of-date branch request.");
692     is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
693        3, "_limit_counter: Add a qualifying request for branch active count.");
694     is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
695        2, "_limit_counter: Add an out-of-date patron request.");
696     is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
697        3, "_limit_counter: Add a qualifying request for patron active count.");
698     $builder->build({
699         source => 'Illrequest',
700         value => {
701             branchcode => $illrq_obj->branchcode,
702             borrowernumber => $illrq_obj->borrowernumber,
703             status => "COMP",
704         }
705     });
706     is($illrq_obj->_limit_counter('annual', { branchcode => $illrq_obj->branchcode }),
707        3, "_limit_counter: Add a qualifying request for branch annual count.");
708     is($illrq_obj->_limit_counter('active', { branchcode => $illrq_obj->branchcode }),
709        3, "_limit_counter: Add a completed request for branch active count.");
710     is($illrq_obj->_limit_counter('annual', { borrowernumber => $illrq_obj->borrowernumber }),
711        3, "_limit_counter: Add a qualifying request for patron annual count.");
712     is($illrq_obj->_limit_counter('active', { borrowernumber => $illrq_obj->borrowernumber }),
713        3, "_limit_counter: Add a completed request for patron active count.");
714
715     # check_limits:
716
717     # We've tested _limit_counter, so all we need to test here is whether the
718     # current counts of 3 for each work as they should against different
719     # configuration declarations.
720
721     # No limits
722     $config->set_always('getLimitRules', undef);
723     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
724                                  librarycode => $illrq_obj->branchcode}),
725        1, "check_limits: no configuration => no limits.");
726
727     # Branch tests
728     $config->set_always('getLimitRules',
729                         { $illrq_obj->branchcode => { count => 1, method => 'active' } });
730     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
731                                  librarycode => $illrq_obj->branchcode}),
732        0, "check_limits: branch active limit exceeded.");
733     $config->set_always('getLimitRules',
734                         { $illrq_obj->branchcode => { count => 1, method => 'annual' } });
735     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
736                                  librarycode => $illrq_obj->branchcode}),
737        0, "check_limits: branch annual limit exceeded.");
738     $config->set_always('getLimitRules',
739                         { $illrq_obj->branchcode => { count => 4, method => 'active' } });
740     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
741                                  librarycode => $illrq_obj->branchcode}),
742        1, "check_limits: branch active limit OK.");
743     $config->set_always('getLimitRules',
744                         { $illrq_obj->branchcode => { count => 4, method => 'annual' } });
745     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
746                                  librarycode => $illrq_obj->branchcode}),
747        1, "check_limits: branch annual limit OK.");
748
749     # Patron tests
750     $config->set_always('getLimitRules',
751                         { $illrq_obj->patron->categorycode => { count => 1, method => 'active' } });
752     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
753                                  librarycode => $illrq_obj->branchcode}),
754        0, "check_limits: patron category active limit exceeded.");
755     $config->set_always('getLimitRules',
756                         { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
757     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
758                                  librarycode => $illrq_obj->branchcode}),
759        0, "check_limits: patron category annual limit exceeded.");
760     $config->set_always('getLimitRules',
761                         { $illrq_obj->patron->categorycode => { count => 4, method => 'active' } });
762     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
763                                  librarycode => $illrq_obj->branchcode}),
764        1, "check_limits: patron category active limit OK.");
765     $config->set_always('getLimitRules',
766                         { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
767     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
768                                  librarycode => $illrq_obj->branchcode}),
769        1, "check_limits: patron category annual limit OK.");
770
771     # One rule cancels the other
772     $config->set_series('getLimitRules',
773                         # Branch rules allow request
774                         { $illrq_obj->branchcode => { count => 4, method => 'active' } },
775                         # Patron rule forbids it
776                         { $illrq_obj->patron->categorycode => { count => 1, method => 'annual' } });
777     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
778                                  librarycode => $illrq_obj->branchcode}),
779        0, "check_limits: patron category veto overrides branch OK.");
780     $config->set_series('getLimitRules',
781                         # Branch rules allow request
782                         { $illrq_obj->branchcode => { count => 1, method => 'active' } },
783                         # Patron rule forbids it
784                         { $illrq_obj->patron->categorycode => { count => 4, method => 'annual' } });
785     is($illrq_obj->check_limits({patron => $illrq_obj->patron,
786                                  librarycode => $illrq_obj->branchcode}),
787        0, "check_limits: branch veto overrides patron category OK.");
788
789     $schema->storage->txn_rollback;
790 };
791
792 1;