Bug 20581: (follow-up) Fix unit test
[koha-equinox.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 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;
32
33 use Test::More tests => 11;
34
35 my $schema = Koha::Database->new->schema;
36 my $builder = t::lib::TestBuilder->new;
37 use_ok('Koha::Illrequest');
38 use_ok('Koha::Illrequests');
39
40 subtest 'Basic object tests' => sub {
41
42     plan tests => 24;
43
44     $schema->storage->txn_begin;
45
46     Koha::Illrequests->search->delete;
47     my $illrq = $builder->build({ source => 'Illrequest' });
48     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
49
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.");
54
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.");
89
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'
98         }
99     });
100     is($illrq_obj->get_type, 'book',
101         'get_type() returns correct type if set');
102
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.");
108
109     $illrq_obj->delete;
110
111     is(Koha::Illrequests->search->count, 0,
112        "No illrequest found after delete.");
113
114     $schema->storage->txn_rollback;
115 };
116
117 subtest 'Working with related objects' => sub {
118
119     plan tests => 5;
120
121     $schema->storage->txn_begin;
122
123     Koha::Illrequests->search->delete;
124
125     my $patron = $builder->build({ source => 'Borrower' });
126     my $illrq = $builder->build({
127         source => 'Illrequest',
128         value => { borrowernumber => $patron->{borrowernumber} }
129     });
130     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
131
132     isa_ok($illrq_obj->patron, 'Koha::Patron',
133            "OK accessing related patron.");
134
135     $builder->build({
136         source => 'Illrequestattribute',
137         value  => { illrequest_id => $illrq_obj->illrequest_id, type => 'X' }
138     });
139     $builder->build({
140         source => 'Illrequestattribute',
141         value  => { illrequest_id => $illrq_obj->illrequest_id, type => 'Y' }
142     });
143     $builder->build({
144         source => 'Illrequestattribute',
145         value  => { illrequest_id => $illrq_obj->illrequest_id, type => 'Z' }
146     });
147
148     is($illrq_obj->illrequestattributes->count, Koha::Illrequestattributes->search->count,
149        "Fetching expected number of Illrequestattributes for our request.");
150
151     my $illrq1 = $builder->build({ source => 'Illrequest' });
152     $builder->build({
153         source => 'Illrequestattribute',
154         value  => { illrequest_id => $illrq1->{illrequest_id}, type => 'X' }
155     });
156
157     is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
158        "Fetching expected number of Illrequestattributes for our request.");
159
160     $illrq_obj->delete;
161     is(Koha::Illrequestattributes->search->count, 1,
162        "Correct number of illrequestattributes after delete.");
163
164     isa_ok(Koha::Patrons->find($patron->{borrowernumber}), 'Koha::Patron',
165            "Borrower was not deleted after illrq delete.");
166
167     $schema->storage->txn_rollback;
168 };
169
170 subtest 'Status Graph tests' => sub {
171
172     plan tests => 4;
173
174     $schema->storage->txn_begin;
175
176     my $illrq = $builder->build({source => 'Illrequest'});
177     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
178
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.");
183
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"
190     );
191
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"
197     );
198
199     # Correct merge behaviour
200     is_deeply(
201         $illrq_obj->_status_graph_union({
202             REQ => {
203                 prev_actions   => [ ],
204                 id             => 'REQ',
205                 next_actions   => [ ],
206             },
207         }, {
208             QER => {
209                 prev_actions   => [ 'REQ' ],
210                 id             => 'QER',
211                 next_actions   => [ 'REQ' ],
212             },
213         }),
214         {
215             REQ => {
216                 prev_actions   => [ 'QER' ],
217                 id             => 'REQ',
218                 next_actions   => [ 'QER' ],
219             },
220             QER => {
221                 prev_actions   => [ 'REQ' ],
222                 id             => 'QER',
223                 next_actions   => [ 'REQ' ],
224             },
225         },
226         "REQ atom + linking QER = cyclical status graph"
227     );
228
229     $schema->storage->txn_rollback;
230 };
231
232 subtest 'Backend testing (mocks)' => sub {
233
234     plan tests => 13;
235
236     $schema->storage->txn_begin;
237
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.
241
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');
246
247     my $patron = $builder->build({ source => 'Borrower' });
248     my $illrq = $builder->build_object({
249         class => 'Koha::Illrequests',
250         value => { borrowernumber => $patron->{borrowernumber} }
251     });
252
253     $illrq->_backend($backend);
254
255     isa_ok($illrq->_backend, 'Koha::Illbackends::Mock',
256            "OK accessing mocked backend.");
257
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.");
269
270     $backend->set_always('capabilities', 0);
271     is($illrq->_backend_capability('Test'), 0,
272        "0 returned on Mock not implementing Test capability.");
273
274     $backend->set_always('capabilities', sub { return 'bar'; } );
275     is($illrq->_backend_capability('Test'), 'bar',
276        "'bar' returned on Mock implementing Test capability.");
277
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
287             }
288         }
289     );
290
291     is_deeply(
292         $illrq->metadata,
293         {
294             ID => $illrq->illrequest_id,
295             Title => $illrq->patron->borrowernumber
296         },
297         "Test metadata."
298     );
299
300     # capabilities:
301
302     # No backend graph extension
303     $backend->set_always('status_graph', {});
304     is_deeply($illrq->capabilities('COMP'),
305               {
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',
313               },
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',
322                          {
323                              QER => {
324                                  prev_actions   => [ 'REQ' ],
325                                  id             => 'QER',
326                                  next_actions   => [ 'REQ' ],
327                              },
328                          });
329     is_deeply($illrq->capabilities('QER'),
330               {
331                   prev_actions   => [ 'REQ' ],
332                   id             => 'QER',
333                   next_actions   => [ 'REQ' ],
334               },
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,
341                   {
342                       QER => {
343                           prev_actions   => [ 'REQ' ],
344                           id             => 'QER',
345                           next_actions   => [ 'REQ' ],
346                       },
347                   }
348               ),
349               "Simple full status graph.");
350
351     # custom_capability:
352
353     # No backend graph extension
354     $backend->set_always('status_graph', {});
355     is($illrq->custom_capability('unknown', {}), 0,
356        "Unknown candidate.");
357
358     # Simple backend graph extension
359     $backend->set_always('status_graph',
360                          {
361                              ID => {
362                                  prev_actions   => [ 'REQ' ],
363                                  id             => 'ID',
364                                  method         => 'identity',
365                                  next_actions   => [ 'REQ' ],
366                              },
367                          });
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");
372
373     $schema->storage->txn_rollback;
374 };
375
376
377 subtest 'Backend core methods' => sub {
378
379     plan tests => 17;
380
381     $schema->storage->txn_begin;
382
383     # Build infrastructure
384     my $backend = Test::MockObject->new;
385     $backend->set_isa('Koha::Illbackends::Mock');
386     $backend->set_always('name', 'Mock');
387
388     my $config = Test::MockObject->new;
389     $config->set_always('backend_dir', "/tmp");
390     $config->set_always('getLimitRules',
391                         { default => { count => 0, method => 'active' } });
392
393     my $illrq = $builder->build_object({
394         class => 'Koha::Illrequests',
395         value => { backend => undef }
396     });
397     $illrq->_config($config);
398
399     # Test error conditions (no backend)
400     throws_ok { $illrq->load_backend; }
401         'Koha::Exceptions::Ill::InvalidBackendId',
402         'Exception raised correctly';
403
404     throws_ok { $illrq->load_backend(''); }
405         'Koha::Exceptions::Ill::InvalidBackendId',
406         'Exception raised correctly';
407
408     # Now load the mocked backend
409     $illrq->_backend($backend);
410
411     # expandTemplate:
412     is_deeply($illrq->expandTemplate({ test => 1, method => "bar" }),
413               {
414                   test => 1,
415                   method => "bar",
416                   template => "/tmp/Mock/intra-includes/bar.inc",
417                   opac_template => "/tmp/Mock/opac-includes/bar.inc",
418               },
419               "ExpandTemplate");
420
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}),
429               {
430                   stage => 'bar', method => 'create',
431                   template => "/tmp/Mock/intra-includes/create.inc",
432                   opac_template => "/tmp/Mock/opac-includes/create.inc",
433               },
434               "Backend create: arbitrary stage.");
435     # Test commit
436     is_deeply($illrq->backend_create({test => 1}),
437               {
438                   stage => 'commit', method => 'create', permitted => 0,
439                   template => "/tmp/Mock/intra-includes/create.inc",
440                   opac_template => "/tmp/Mock/opac-includes/create.inc",
441               },
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}),
447               {
448                   stage => 'commit', method => 'create', permitted => 1,
449                   template => "/tmp/Mock/intra-includes/create.inc",
450                   opac_template => "/tmp/Mock/opac-includes/create.inc",
451               },
452               "Backend create: arbitrary stage, permitted.");
453     is($illrq->status, "NEW", "Backend create: not-queued.");
454
455     # backend_renew
456     $backend->set_series('renew', { stage => 'bar', method => 'renew' });
457     is_deeply($illrq->backend_renew({test => 1}),
458               {
459                   stage => 'bar', method => 'renew',
460                   template => "/tmp/Mock/intra-includes/renew.inc",
461                   opac_template => "/tmp/Mock/opac-includes/renew.inc",
462               },
463               "Backend renew: arbitrary stage.");
464
465     # backend_cancel
466     $backend->set_series('cancel', { stage => 'bar', method => 'cancel' });
467     is_deeply($illrq->backend_cancel({test => 1}),
468               {
469                   stage => 'bar', method => 'cancel',
470                   template => "/tmp/Mock/intra-includes/cancel.inc",
471                   opac_template => "/tmp/Mock/opac-includes/cancel.inc",
472               },
473               "Backend cancel: arbitrary stage.");
474
475     # backend_update_status
476     $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
477     is_deeply($illrq->backend_update_status({test => 1}),
478               {
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",
482               },
483               "Backend update_status: arbitrary stage.");
484
485     # backend_confirm
486     $backend->set_series('confirm', { stage => 'bar', method => 'confirm' });
487     is_deeply($illrq->backend_confirm({test => 1}),
488               {
489                   stage => 'bar', method => 'confirm',
490                   template => "/tmp/Mock/intra-includes/confirm.inc",
491                   opac_template => "/tmp/Mock/opac-includes/confirm.inc",
492               },
493               "Backend confirm: arbitrary stage.");
494
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 => "" }
500     });
501     my $partner1 = $builder->build({
502         source => 'Borrower',
503         value => { categorycode => "ILLTSTLIB" },
504     });
505     my $partner2 = $builder->build({
506         source => 'Borrower',
507         value => { categorycode => "ILLTSTLIB" },
508     });
509     my $gen_conf = $illrq->generic_confirm({
510         current_branchcode => $illbrn->{branchcode}
511     });
512     isnt(index($gen_conf->{value}->{draft}->{body}, $backend->metadata->{Test}), -1,
513          "Generic confirm: draft contains metadata."
514     );
515     is($gen_conf->{value}->{partners}->next->borrowernumber, $partner1->{borrowernumber},
516        "Generic cofnirm: partner 1 is correct."
517     );
518     is($gen_conf->{value}->{partners}->next->borrowernumber, $partner2->{borrowernumber},
519        "Generic confirm: partner 2 is correct."
520     );
521
522     dies_ok { $illrq->generic_confirm({
523         current_branchcode => $illbrn->{branchcode},
524         stage => 'draft'
525     }) }
526         "Generic confirm: missing to dies OK.";
527
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.";
534
535     $schema->storage->txn_rollback;
536 };
537
538
539 subtest 'Helpers' => sub {
540
541     plan tests => 7;
542
543     $schema->storage->txn_begin;
544
545     # Build infrastructure
546     my $backend = Test::MockObject->new;
547     $backend->set_isa('Koha::Illbackends::Mock');
548     $backend->set_always('name', 'Mock');
549
550     my $config = Test::MockObject->new;
551     $config->set_always('backend_dir', "/tmp");
552
553     my $patron = $builder->build({
554         source => 'Borrower',
555         value => { categorycode => "A" }
556     });
557     my $illrq = $builder->build({
558         source => 'Illrequest',
559         value => { branchcode => "CPL", borrowernumber => $patron->{borrowernumber} }
560     });
561     my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
562     $illrq_obj->_config($config);
563     $illrq_obj->_backend($backend);
564
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");
579
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");
589
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.");
595
596     $schema->storage->txn_rollback;
597 };
598
599
600 subtest 'Censorship' => sub {
601
602     plan tests => 2;
603
604     $schema->storage->txn_begin;
605
606     # Build infrastructure
607     my $backend = Test::MockObject->new;
608     $backend->set_isa('Koha::Illbackends::Mock');
609     $backend->set_always('name', 'Mock');
610
611     my $config = Test::MockObject->new;
612     $config->set_always('backend_dir', "/tmp");
613
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);
618
619     $config->set_always('censorship', { censor_notes_staff => 1, censor_reply_date => 0 });
620
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");
624
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");
630
631     $schema->storage->txn_rollback;
632 };
633
634 subtest 'Checking Limits' => sub {
635
636     plan tests => 30;
637
638     $schema->storage->txn_begin;
639
640     # Build infrastructure
641     my $backend = Test::MockObject->new;
642     $backend->set_isa('Koha::Illbackends::Mock');
643     $backend->set_always('name', 'Mock');
644
645     my $config = Test::MockObject->new;
646     $config->set_always('backend_dir', "/tmp");
647
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);
652
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.");
666
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,
681         }
682     });
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",
697         }
698     });
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",
713         }
714     });
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.");
723
724     # check_limits:
725
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.
729
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.");
735
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.");
757
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.");
779
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.");
797
798     $schema->storage->txn_rollback;
799 };
800
801 subtest 'Custom statuses' => sub {
802
803     plan tests => 3;
804
805     $schema->storage->txn_begin;
806
807     my $cat = Koha::AuthorisedValueCategories->search(
808         {
809             category_name => 'ILLSTATUS'
810         }
811     );
812
813     if ($cat->count == 0) {
814         $cat  = $builder->build_object(
815             {
816                 class => 'Koha::AuthorisedValueCategory',
817                 value => {
818                     category_name => 'ILLSTATUS'
819                 }
820             }
821         );
822     };
823
824     my $av = $builder->build_object(
825         {
826             class => 'Koha::AuthorisedValues',
827             value => {
828                 category => 'ILLSTATUS'
829             }
830         }
831     );
832
833     is($av->category, 'ILLSTATUS',
834        "Successfully created authorised value for custom status");
835
836     my $ill_req = $builder->build_object(
837         {
838             class => 'Koha::Illrequests',
839             value => {
840                 status_alias => $av->authorised_value
841             }
842         }
843     );
844     isa_ok($ill_req->statusalias, 'Koha::AuthorisedValue',
845            "statusalias correctly returning Koha::AuthorisedValue object");
846
847     $ill_req->status("COMP");
848     is($ill_req->statusalias, undef,
849         "Koha::Illrequest->status overloading resetting status_alias");
850
851     $schema->storage->txn_rollback;
852 };