View | Details | Raw Unified | Return to bug 7317
Collapse All | Expand All

(-)a/Koha/Illrequest.pm (-41 / +69 lines)
Lines 166-171 This is a helper method to invoke optional capabilities in the backend. If Link Here
166
the capability named by $name is not supported, return 0, else invoke it,
166
the capability named by $name is not supported, return 0, else invoke it,
167
passing $args along with the invocation, and return its return value.
167
passing $args along with the invocation, and return its return value.
168
168
169
NOTE: this module suffers from a confusion in termninology:
170
171
in _backend_capability, the notion of capability refers to an optional feature
172
that is implemented in core, but might not be supported by a given backend.
173
174
in capabilities & custom_capability, capability refers to entries in the
175
status_graph (after union between backend and core).
176
177
The easiest way to fix this would be to fix the terminology in
178
capabilities & custom_capability and their callers.
179
169
=cut
180
=cut
170
181
171
sub _backend_capability {
182
sub _backend_capability {
Lines 177-183 sub _backend_capability { Link Here
177
        return 0;
188
        return 0;
178
    };
189
    };
179
    if ( $capability ) {
190
    if ( $capability ) {
180
        return $capability($args);
191
        return &{$capability}($args);
181
    } else {
192
    } else {
182
        return 0;
193
        return 0;
183
    }
194
    }
Lines 213-218 sub metadata { Link Here
213
224
214
=head3 _core_status_graph
225
=head3 _core_status_graph
215
226
227
    my $core_status_graph = $illrequest->_core_status_graph;
228
229
Returns ILL module's default status graph.  A status graph defines the list of
230
available actions at any stage in the ILL workflow.  This is for instance used
231
by the perl script & template to generate the correct buttons to display to
232
the end user at any given point.
233
216
=cut
234
=cut
217
235
218
sub _core_status_graph {
236
sub _core_status_graph {
Lines 296-301 sub _core_status_graph { Link Here
296
    };
314
    };
297
}
315
}
298
316
317
=head3 _core_status_graph
318
319
    my $status_graph = $illrequest->_core_status_graph($origin, $new_graph);
320
321
Return a new status_graph, the result of merging $origin & new_graph.  This is
322
operation is a union over the sets defied by the two graphs.
323
324
Each entry in $new_graph is added to $origin.  We do not provide a syntax for
325
'subtraction' of entries from $origin.
326
327
Whilst it is not intended that this works, you can override entries in $origin
328
with entries with the same key in $new_graph.  This can lead to problematic
329
behaviour when $new_graph adds an entry, which modifies a dependent entry in
330
$origin, only for the entry in $origin to be replaced later with a new entry
331
from $new_graph.
332
333
NOTE: this procedure does not "re-link" entries in $origin or $new_graph,
334
i.e. each of the graphs need to be correct at the outset of the operation.
335
336
=cut
337
299
sub _status_graph_union {
338
sub _status_graph_union {
300
    my ( $self, $core_status_graph, $backend_status_graph ) = @_;
339
    my ( $self, $core_status_graph, $backend_status_graph ) = @_;
301
    # Create new status graph with:
340
    # Create new status graph with:
Lines 354-359 Example return value: Link Here
354
393
355
    { create => "Create Request", confirm => "Progress Request" }
394
    { create => "Create Request", confirm => "Progress Request" }
356
395
396
NOTE: this module suffers from a confusion in termninology:
397
398
in _backend_capability, the notion of capability refers to an optional feature
399
that is implemented in core, but might not be supported by a given backend.
400
401
in capabilities & custom_capability, capability refers to entries in the
402
status_graph (after union between backend and core).
403
404
The easiest way to fix this would be to fix the terminology in
405
capabilities & custom_capability and their callers.
406
357
=cut
407
=cut
358
408
359
sub capabilities {
409
sub capabilities {
Lines 377-382 sub capabilities { Link Here
377
Return the result of invoking $CANDIDATE on this request's backend with
427
Return the result of invoking $CANDIDATE on this request's backend with
378
$PARAMS, or 0 if $CANDIDATE is an unknown method on backend.
428
$PARAMS, or 0 if $CANDIDATE is an unknown method on backend.
379
429
430
NOTE: this module suffers from a confusion in termninology:
431
432
in _backend_capability, the notion of capability refers to an optional feature
433
that is implemented in core, but might not be supported by a given backend.
434
435
in capabilities & custom_capability, capability refers to entries in the
436
status_graph (after union between backend and core).
437
438
The easiest way to fix this would be to fix the terminology in
439
capabilities & custom_capability and their callers.
440
380
=cut
441
=cut
381
442
382
sub custom_capability {
443
sub custom_capability {
Lines 413-419 sub available_actions { Link Here
413
474
414
sub mark_completed {
475
sub mark_completed {
415
    my ( $self ) = @_;
476
    my ( $self ) = @_;
416
    $self->status('COMP')->store unless ( $permitted );
477
    $self->status('COMP')->store;
417
    return {
478
    return {
418
        error   => 0,
479
        error   => 0,
419
        status  => '',
480
        status  => '',
Lines 585-620 sub expandTemplate { Link Here
585
646
586
#### Abstract Imports
647
#### Abstract Imports
587
648
588
=head3 getCensorNotesStaff
589
590
    my $bool = $abstract->getCensorNotesStaff;
591
592
Return a boolean indicating whether we should be censoring staff notes or not,
593
as determined by our configuration file.
594
595
=cut
596
597
sub getCensorNotesStaff {
598
    my ( $self ) = @_;
599
    my $censorship = $self->_config->censorship;
600
    return $censorship->{censor_notes_staff};
601
}
602
603
=head3 getDisplayReplyDate
604
605
    my 0 = $abstract->getDisplayReplyDate;
606
607
Return a 0 if we want to hide it or 1 if not.
608
609
=cut
610
611
sub getDisplayReplyDate {
612
    my ( $self ) = @_;
613
    my $censorship = $self->_config->censorship;
614
    # If censor is yes, don't display and vice versa.
615
    return ( $censorship->{censor_reply_date} ) ? 0 : 1;
616
}
617
618
=head3 getLimits
649
=head3 getLimits
619
650
620
    my $limit_rules = $abstract->getLimits( {
651
    my $limit_rules = $abstract->getLimits( {
Lines 629-635 or for the default, we must define fall-back values here. Link Here
629
660
630
=cut
661
=cut
631
662
632
# FIXME: Needs unit tests!
633
sub getLimits {
663
sub getLimits {
634
    my ( $self, $params ) = @_;
664
    my ( $self, $params ) = @_;
635
    my $limits = $self->_config->getLimitRules($params->{type});
665
    my $limits = $self->_config->getLimitRules($params->{type});
Lines 683-695 LimitRules are derived from koha-conf.xml: Link Here
683
713
684
=cut
714
=cut
685
715
686
# FIXME: Needs unit tests!
687
sub check_limits {
716
sub check_limits {
688
    my ( $self, $params ) = @_;
717
    my ( $self, $params ) = @_;
689
    my $patron     = $params->{patron};
718
    my $patron     = $params->{patron};
690
    my $branchcode = $params->{librarycode} || $patron->branchcode;
719
    my $branchcode = $params->{librarycode} || $patron->branchcode;
691
720
692
    # Establish rules
721
    # Establish maximum number of allowed requests
693
    my ( $branch_rules, $brw_rules ) = (
722
    my ( $branch_rules, $brw_rules ) = (
694
        $self->getLimits( {
723
        $self->getLimits( {
695
            type => 'branch',
724
            type => 'branch',
Lines 700-708 sub check_limits { Link Here
700
            value => $patron->categorycode,
729
            value => $patron->categorycode,
701
        } ),
730
        } ),
702
    );
731
    );
703
    # Almost there, but category code didn't quite work.
704
    my ( $branch_limit, $brw_limit )
732
    my ( $branch_limit, $brw_limit )
705
        = ( $branch_rules->{count}, $brw_rules->{count} );
733
        = ( $branch_rules->{count}, $brw_rules->{count} );
734
    # Establish currently existing requests
706
    my ( $branch_count, $brw_count ) = (
735
    my ( $branch_count, $brw_count ) = (
707
        $self->_limit_counter(
736
        $self->_limit_counter(
708
            $branch_rules->{method}, { branchcode => $branchcode }
737
            $branch_rules->{method}, { branchcode => $branchcode }
Lines 723-729 sub check_limits { Link Here
723
    }
752
    }
724
}
753
}
725
754
726
# FIXME: Needs unit tests!
727
sub _limit_counter {
755
sub _limit_counter {
728
    my ( $self, $method, $target ) = @_;
756
    my ( $self, $method, $target ) = @_;
729
757
Lines 737-743 sub _limit_counter { Link Here
737
            ]
765
            ]
738
        });
766
        });
739
    } else {                    # assume 'active'
767
    } else {                    # assume 'active'
740
        # FIXME: This status list is ugly. There should be a method in config
768
        # XXX: This status list is ugly. There should be a method in config
741
        # to return these.
769
        # to return these.
742
        $where = { status => { -not_in => [ 'QUEUED', 'COMP' ] } };
770
        $where = { status => { -not_in => [ 'QUEUED', 'COMP' ] } };
743
        $resultset = Koha::Illrequests->search({ %{$target}, %{$where} });
771
        $resultset = Koha::Illrequests->search({ %{$target}, %{$where} });
Lines 888-894 file. Link Here
888
916
889
sub id_prefix {
917
sub id_prefix {
890
    my ( $self ) = @_;
918
    my ( $self ) = @_;
891
    # FIXME: can we automatically use borrowernumber as borrower?
892
    my $brw = $self->patron;
919
    my $brw = $self->patron;
893
    my $brw_cat = "dummy";
920
    my $brw_cat = "dummy";
894
    $brw_cat = $brw->categorycode
921
    $brw_cat = $brw->categorycode
Lines 911-919 Return $params, modified to reflect our censorship requirements. Link Here
911
938
912
sub _censor {
939
sub _censor {
913
    my ( $self, $params ) = @_;
940
    my ( $self, $params ) = @_;
914
    $params->{censor_notes_staff} = $self->getCensorNotesStaff
941
    my $censorship = $self->_config->censorship;
942
    $params->{censor_notes_staff} = $censorship->{censor_notes_staff}
915
        if ( $params->{opac} );
943
        if ( $params->{opac} );
916
    $params->{display_reply_date} = $self->getDisplayReplyDate;
944
    $params->{display_reply_date} = ( $censorship->{censor_reply_date} ) ? 0 : 1;
917
945
918
    return $params;
946
    return $params;
919
}
947
}
(-)a/Koha/Illrequest/Config.pm (-4 / +4 lines)
Lines 62-74 ILL configuration file. Link Here
62
=cut
62
=cut
63
63
64
sub new {
64
sub new {
65
    my ( $class, $test ) = @_;
65
    my ( $class ) = @_;
66
    my $self  = {};
66
    my $self  = {};
67
67
68
    $self->{configuration} = _load_configuration(
68
    $self->{configuration} = _load_configuration(
69
        C4::Context->config("interlibrary_loans"),
69
        C4::Context->config("interlibrary_loans"),
70
        C4::Context->preference("UnmediatedILL")
70
        C4::Context->preference("UnmediatedILL")
71
      ) unless ( $test );
71
      );
72
72
73
    bless $self, $class;
73
    bless $self, $class;
74
74
Lines 107-113 sub backend_dir { Link Here
107
107
108
=head3 partner_code
108
=head3 partner_code
109
109
110
    $partner_code = $config->partner_code($new_path);
110
    $partner_code = $config->partner_code($new_code);
111
    $partner_code = $config->partner_code;
111
    $partner_code = $config->partner_code;
112
112
113
Standard setter/accessor for our partner_code.
113
Standard setter/accessor for our partner_code.
Lines 289-295 sub _load_configuration { Link Here
289
    my $staff_comments = $xml_config->{staff_request_comments};
289
    my $staff_comments = $xml_config->{staff_request_comments};
290
    $configuration->{censorship}->{censor_notes_staff} = 1
290
    $configuration->{censorship}->{censor_notes_staff} = 1
291
        if ( $staff_comments && 'hide' eq $staff_comments );
291
        if ( $staff_comments && 'hide' eq $staff_comments );
292
    my $reply_date = $xml_config->{reply_date};
292
    my $reply_date = $xml_config->{reply_date} || 0;
293
    if ( 'hide' eq $reply_date ) {
293
    if ( 'hide' eq $reply_date ) {
294
        $configuration->{censorship}->{censor_reply_date} = 1;
294
        $configuration->{censorship}->{censor_reply_date} = 1;
295
    } else {
295
    } else {
(-)a/t/db_dependent/Illrequest/Config.t (-316 / +84 lines)
Lines 17-344 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Koha::Database;
21
use t::lib::Mocks;
22
use t::lib::TestBuilder;
23
use Test::MockObject;
20
use Test::Exception;
24
use Test::Exception;
21
use Test::More;
22
use Test::Warn;
23
25
24
# Some data structures that will be repeatedly referenced
26
use Test::More tests => 2;
25
my $defaults  = {
26
    api_key  => "564euie",
27
    api_auth => "unidaenudvnled",
28
};
29
my $application = {
30
    key  => "6546uedrun",
31
    auth => "edutrineadue",
32
};
33
# Simulate $from_xml
34
my $params = {
35
    application            => $application,
36
    backend                => "Dummy",
37
    configuration          => $defaults,
38
    reply_date             => "hide",
39
    staff_request_comments => "hide",
40
};
41
my $first_branch = {
42
    code => "test", api_key => "dphügnpgüffq", api_auth => "udrend"
43
};
44
my $second_branch = {
45
    code          => "second",
46
    api_key       => "eduirn",
47
    api_auth      => "eudtireand",
48
    request_limit => { count => "5" },
49
};
50
51
BEGIN {
52
    use_ok('Koha::Illrequest::Config');
53
}
54
55
my $config = Koha::Illrequest::Config->new(1); # with test_mode enabled.
56
isa_ok($config, 'Koha::Illrequest::Config');
57
58
# _load_configuration
59
is_deeply(
60
    Koha::Illrequest::Config::_load_configuration($params),
61
    {
62
        api_url         => 'http://apitest.bldss.bl.uk',
63
        backend         => 'Dummy',
64
        censorship      => {
65
            censor_notes_staff => 1,
66
            censor_reply_date => 1,
67
        },
68
        credentials     => {
69
            api_application => $application,
70
            api_keys        => { default => $defaults },
71
        },
72
        default_formats => {},
73
        digital_recipients => {},
74
        library_privileges => {},
75
        limits          => {},
76
        prefixes           => {},
77
        spec_file       => undef,
78
    },
79
    "Basic _load_configuration"
80
);
81
82
$params->{configuration}->{request_limit}->{count} = 10;
83
is_deeply(
84
    Koha::Illrequest::Config::_load_configuration($params),
85
    {
86
        api_url            => 'http://apitest.bldss.bl.uk',
87
        backend            => 'Dummy',
88
        censorship         => {
89
            censor_notes_staff => 1,
90
            censor_reply_date => 1,
91
        },
92
        credentials        => {
93
            api_application => $application,
94
            api_keys        => {
95
                default => {
96
                    api_auth => $defaults->{api_auth},
97
                    api_key  => $defaults->{api_key},
98
                }
99
            },
100
        },
101
        default_formats    => {},
102
        digital_recipients => {},
103
        library_privileges => {},
104
        limits             => { default => { count => 10 } },
105
        prefixes           => {},
106
        spec_file          => undef,
107
    },
108
    "Basic _load_configuration, with limit"
109
);
110
27
111
$params->{configuration}->{branch} = $first_branch;
28
my $schema = Koha::Database->new->schema;
112
is_deeply(
29
my $builder = t::lib::TestBuilder->new;
113
    Koha::Illrequest::Config::_load_configuration($params),
30
use_ok('Koha::Illrequest::Config');
114
    {
115
        api_url         => 'http://apitest.bldss.bl.uk',
116
        backend         => 'Dummy',
117
        censorship      => {
118
            censor_notes_staff => 1,
119
            censor_reply_date => 1,
120
        },
121
        credentials     => {
122
            api_keys        => {
123
                default => {
124
                    api_key  => $defaults->{api_key},
125
                    api_auth => $defaults->{api_auth},
126
                },
127
                $first_branch->{code} => {
128
                    api_key  => $first_branch->{api_key},
129
                    api_auth => $first_branch->{api_auth},
130
                },
131
            },
132
            api_application => $application,
133
        },
134
        default_formats => {},
135
        digital_recipients => {},
136
        library_privileges => {},
137
        limits          => { default => { count => 10 } },
138
        prefixes           => {},
139
        spec_file       => undef,
140
    },
141
    "Single Branch _load_configuration"
142
);
143
31
144
$params->{configuration}->{branch} = [ $first_branch, $second_branch ];
32
my $base_limits = {
145
is_deeply(
33
    branch => { CPL => { count => 1, method => 'annual' } },
146
    Koha::Illrequest::Config::_load_configuration($params),
34
    brw_cat => { A => { count => -1, method => 'active' } },
147
    {
35
    default => { count => 10, method => 'annual' },
148
        api_url         => 'http://apitest.bldss.bl.uk',
149
        credentials     => {
150
            api_keys        => {
151
                default => {
152
                    api_key  => $defaults->{api_key},
153
                    api_auth => $defaults->{api_auth},
154
                },
155
                $first_branch->{code} => {
156
                    api_key  => $first_branch->{api_key},
157
                    api_auth => $first_branch->{api_auth},
158
                },
159
                $second_branch->{code} => {
160
                    api_key  => $second_branch->{api_key},
161
                    api_auth => $second_branch->{api_auth},
162
                },
163
            },
164
            api_application => $application,
165
        },
166
        backend         => 'Dummy',
167
        censorship      => {
168
            censor_notes_staff => 1,
169
            censor_reply_date => 1,
170
        },
171
        default_formats => {},
172
        digital_recipients => {},
173
        library_privileges => {},
174
        limits          => {
175
            default => {count => 10 },
176
            branch  => {
177
                $second_branch->{code} => {count => 5 },
178
            },
179
        },
180
        prefixes           => {},
181
        spec_file       => undef,
182
    },
183
    "Multi Branch _load_configuration"
184
);
185
186
dies_ok { Koha::Illrequest::Config::_load_configuration($params, 1) }
187
    "Unmediated, missing config _load_configuration";
188
189
$params->{configuration}->{default_formats} = {
190
    format => 1, quality => 1, quantity => 1, service => 1, speed => 1
191
};
36
};
192
is_deeply(
193
    Koha::Illrequest::Config::_load_configuration($params, 1),
194
    {
195
        api_url         => 'http://apitest.bldss.bl.uk',
196
        backend         => 'Dummy',
197
        censorship      => {
198
            censor_notes_staff => 1,
199
            censor_reply_date => 1,
200
        },
201
        credentials     => {
202
            api_keys        => {
203
                default => {
204
                    api_key  => $defaults->{api_key},
205
                    api_auth => $defaults->{api_auth},
206
                },
207
                $first_branch->{code} => {
208
                    api_key  => $first_branch->{api_key},
209
                    api_auth => $first_branch->{api_auth},
210
                },
211
                $second_branch->{code} => {
212
                    api_key  => $second_branch->{api_key},
213
                    api_auth => $second_branch->{api_auth},
214
                },
215
            },
216
            api_application => $application,
217
        },
218
        default_formats => {
219
            default => {
220
                format => 1,
221
                quality => 1,
222
                quantity => 1,
223
                service => 1,
224
                speed => 1
225
            }
226
        },
227
        digital_recipients => {},
228
        library_privileges => {},
229
        limits          => {
230
            default => {count => 10 },
231
            branch  => {
232
                $second_branch->{code} => {count => 5 },
233
            },
234
        },
235
        prefixes           => {},
236
        spec_file       => undef,
237
    },
238
    "default_formats, default only _load_configuration"
239
);
240
241
# getDefaultFormats
242
dies_ok { $config->getLimitRules('wrongType') }
243
    "Faulty getDefaultFormats";
244
245
$config->{configuration} =
246
    Koha::Illrequest::Config::_load_configuration($params);
247
is_deeply(
248
    $config->getDefaultFormats('brw_cat'),
249
    {
250
        default => {
251
            format => 1,
252
            quality => 1,
253
            quantity => 1,
254
            service => 1,
255
            speed => 1
256
        }
257
    },
258
    "Default getDefaultFormats"
259
);
260
261
# getLimitRules
262
dies_ok { $config->getLimitRules('wrongType') }
263
    "Faulty getLimitRules";
264
37
265
$config->{configuration} =
38
my $base_censorship = { censor_notes_staff => 1, censor_reply_date => 1 };
266
    Koha::Illrequest::Config::_load_configuration($params);
39
267
is_deeply(
40
subtest 'Basics' => sub {
268
    $config->getLimitRules('branch'),
41
269
    {
42
    plan tests => 25;
270
        default => { count => 10 },
43
271
        second  => { count => 5 },
44
    $schema->storage->txn_begin;
272
    },
45
273
    "second branch getLimitRules"
46
    t::lib::Mocks::mock_preference("UnmediatedILL", 0);
274
);
47
    t::lib::Mocks::mock_config("interlibrary_loans", {});
275
48
276
is_deeply(
49
    my $config = Koha::Illrequest::Config->new;
277
    $config->getLimitRules('brw_cat'),
50
    isa_ok($config, "Koha::Illrequest::Config",
278
    {
51
           "Correctly create and load a config object.");
279
        default => { count => 10 },
52
280
    },
53
    # backend:
281
    "empty brw_cat getLimitRules"
54
    is($config->backend, undef, "backend: Undefined backend is undefined.");
282
);
55
    is($config->backend("Mock"), "Mock", "backend: setter works.");
283
56
    is($config->backend, "Mock", "backend: setter is persistent.");
284
# getCredentials
57
285
$params = {
58
    # backend_dir:
286
    application            => $application,
59
    is($config->backend_dir, undef, "backend_dir: Undefined backend_dir is undefined.");
287
    backend                => 'Dummy',
60
    is($config->backend_dir("/tmp/"), "/tmp/", "backend_dir: setter works.");
288
    configuration          => {},
61
    is($config->backend_dir, "/tmp/", "backend_dir: setter is persistent.");
289
    reply_date             => "hide",
62
290
    staff_request_comments => "hide",
63
    # partner_code:
64
    is($config->partner_code, "ILLLIBS", "partner_code: Undefined partner_code is undefined.");
65
    is($config->partner_code("ILLLIBSTST"), "ILLLIBSTST", "partner_code: setter works.");
66
    is($config->partner_code, "ILLLIBSTST", "partner_code: setter is persistent.");
67
68
    # limits:
69
    is_deeply($config->limits, {}, "limits: Undefined limits is empty hash.");
70
    is_deeply($config->limits($base_limits), $base_limits, "limits: setter works.");
71
    is_deeply($config->limits, $base_limits, "limits: setter is persistent.");
72
73
    # censorship:
74
    is_deeply($config->censorship, { censor_notes_staff => 0, censor_reply_date => 0 },
75
              "censorship: Undefined censorship is default values.");
76
    is_deeply($config->censorship($base_censorship), $base_censorship, "censorship: setter works.");
77
    is_deeply($config->censorship, $base_censorship, "censorship: setter is persistent.");
78
79
    # getLimitRules
80
    dies_ok( sub { $config->getLimitRules("FOO") }, "getLimitRules: die if not correct type.");
81
    is_deeply($config->getLimitRules("brw_cat"), {
82
        A => { count => -1, method => 'active' },
83
        default => { count => 10, method => 'annual' },
84
    }, "getLimitRules: fetch brw_cat limits.");
85
    is_deeply($config->getLimitRules("branch"), {
86
        CPL => { count => 1, method => 'annual' },
87
        default => { count => 10, method => 'annual' },
88
    }, "getLimitRules: fetch brw_cat limits.");
89
90
    # getPrefixes:
91
    # FIXME: Need to test returning actual prefixes
92
    dies_ok( sub { $config->getPrefixes("FOO") }, "getPrefixes: die if not correct type.");
93
    is_deeply($config->getPrefixes("brw_cat"), { default => undef},
94
              "getPrefixes: Undefined brw_cat prefix is undefined.");
95
    is_deeply($config->getPrefixes("branch"), { default => undef},
96
              "getPrefixes: Undefined branch prefix is undefined.");
97
98
    # getDigitalRecipients
99
    # FIXME: Need to test returning actual recipients
100
    dies_ok( sub { $config->getDigitalRecipients("FOO") },
101
             "getDigitalRecipients: die if not correct type.");
102
    is_deeply($config->getDigitalRecipients("brw_cat"), { default => undef},
103
              "getDigitalRecipients: Undefined brw_cat dig rec is undefined.");
104
    is_deeply($config->getDigitalRecipients("branch"), { default => undef},
105
              "getDigitalRecipients: Undefined branch dig rec is undefined.");
106
107
    $schema->storage->txn_rollback;
291
};
108
};
292
$config->{configuration} =
293
    Koha::Illrequest::Config::_load_configuration($params);
294
is_deeply(
295
    $config->getCredentials,
296
    {
297
        api_key              => 0,
298
        api_key_auth         => 0,
299
        api_application      => $application->{key},
300
        api_application_auth => $application->{auth},
301
    },
302
    "getCredentials, no creds, just App."
303
);
304
305
$params->{configuration} = $defaults;
306
$config->{configuration} =
307
    Koha::Illrequest::Config::_load_configuration($params),
308
is_deeply(
309
    $config->getCredentials,
310
    {
311
        api_key              => $defaults->{api_key},
312
        api_key_auth         => $defaults->{api_auth},
313
        api_application      => $application->{key},
314
        api_application_auth => $application->{auth},
315
    },
316
    "getCredentials, default creds & App."
317
);
318
319
$params->{configuration}->{branch} = $first_branch;
320
$config->{configuration} =
321
    Koha::Illrequest::Config::_load_configuration($params),
322
is_deeply(
323
    $config->getCredentials($first_branch->{code}),
324
    {
325
        api_key              => $first_branch->{api_key},
326
        api_key_auth         => $first_branch->{api_auth},
327
        api_application      => $application->{key},
328
        api_application_auth => $application->{auth},
329
    },
330
    "getCredentials, $first_branch->{code} creds & App."
331
);
332
109
333
is_deeply(
110
# FIXME: need to test _load_configuration & _load_unit_config
334
    $config->getCredentials("random"),
335
    {
336
        api_key              => $defaults->{api_key},
337
        api_key_auth         => $defaults->{api_auth},
338
        api_application      => $application->{key},
339
        api_application_auth => $application->{auth},
340
    },
341
    "getCredentials, fallback creds & app."
342
);
343
111
344
done_testing;
112
1;
(-)a/t/db_dependent/Illrequestattributes.t (+63 lines)
Line 0 Link Here
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::Patrons;
24
use t::lib::TestBuilder;
25
26
use Test::More tests => 3;
27
28
my $schema = Koha::Database->new->schema;
29
use_ok('Koha::Illrequestattribute');
30
use_ok('Koha::Illrequestattributes');
31
32
subtest 'Basic object tests' => sub {
33
34
    plan tests => 5;
35
36
    $schema->storage->txn_begin;
37
38
    my $builder = t::lib::TestBuilder->new;
39
40
    my $illrqattr = $builder->build({ source => 'Illrequestattribute' });
41
42
    my $illrqattr_obj = Koha::Illrequestattributes->find(
43
        $illrqattr->{illrequest_id},
44
        $illrqattr->{type}
45
    );
46
    isa_ok($illrqattr_obj, 'Koha::Illrequestattribute',
47
        "Correctly create and load an illrequestattribute object.");
48
    is($illrqattr_obj->illrequest_id, $illrqattr->{illrequest_id},
49
       "Illrequest_id getter works.");
50
    is($illrqattr_obj->type, $illrqattr->{type},
51
       "Type getter works.");
52
    is($illrqattr_obj->value, $illrqattr->{value},
53
       "Value getter works.");
54
55
    $illrqattr_obj->delete;
56
57
    is(Koha::Illrequestattributes->search->count, 0,
58
        "No attributes found after delete.");
59
60
    $schema->storage->txn_rollback;
61
};
62
63
1;
(-)a/t/db_dependent/Illrequests.t (-1 / +792 lines)
Line 0 Link Here
0
- 
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;

Return to bug 7317