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