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

(-)a/Koha/Illrequest.pm (-6 / +63 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 629-635 or for the default, we must define fall-back values here. Link Here
629
690
630
=cut
691
=cut
631
692
632
# FIXME: Needs unit tests!
633
sub getLimits {
693
sub getLimits {
634
    my ( $self, $params ) = @_;
694
    my ( $self, $params ) = @_;
635
    my $limits = $self->_config->getLimitRules($params->{type});
695
    my $limits = $self->_config->getLimitRules($params->{type});
Lines 683-689 LimitRules are derived from koha-conf.xml: Link Here
683
743
684
=cut
744
=cut
685
745
686
# FIXME: Needs unit tests!
687
sub check_limits {
746
sub check_limits {
688
    my ( $self, $params ) = @_;
747
    my ( $self, $params ) = @_;
689
    my $patron     = $params->{patron};
748
    my $patron     = $params->{patron};
Lines 723-729 sub check_limits { Link Here
723
    }
782
    }
724
}
783
}
725
784
726
# FIXME: Needs unit tests!
727
sub _limit_counter {
785
sub _limit_counter {
728
    my ( $self, $method, $target ) = @_;
786
    my ( $self, $method, $target ) = @_;
729
787
Lines 888-894 file. Link Here
888
946
889
sub id_prefix {
947
sub id_prefix {
890
    my ( $self ) = @_;
948
    my ( $self ) = @_;
891
    # FIXME: can we automatically use borrowernumber as borrower?
892
    my $brw = $self->patron;
949
    my $brw = $self->patron;
893
    my $brw_cat = "dummy";
950
    my $brw_cat = "dummy";
894
    $brw_cat = $brw->categorycode
951
    $brw_cat = $brw->categorycode
(-)a/t/db_dependent/Illrequest/Config.t (-7 / +3 lines)
Lines 17-25 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::Exception;
20
use Test::More tests => 1;
21
use Test::More;
21
22
use Test::Warn;
22
use_ok('Koha::Illrequest::Config');
23
23
24
# Some data structures that will be repeatedly referenced
24
# Some data structures that will be repeatedly referenced
25
my $defaults  = {
25
my $defaults  = {
Lines 48-57 my $second_branch = { Link Here
48
    request_limit => { count => "5" },
48
    request_limit => { count => "5" },
49
};
49
};
50
50
51
BEGIN {
52
    use_ok('Koha::Illrequest::Config');
53
}
54
55
my $config = Koha::Illrequest::Config->new(1); # with test_mode enabled.
51
my $config = Koha::Illrequest::Config->new(1); # with test_mode enabled.
56
isa_ok($config, 'Koha::Illrequest::Config');
52
isa_ok($config, 'Koha::Illrequest::Config');
57
53
(-)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 / +642 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
30
use Test::More tests => 10;
31
32
# We want to test the Koha IllRequest object.  At its core it's a simple
33
# Koha::Object, mapping to the ill_request table.
34
#
35
# This object will supersede the Status object in ILLModule.
36
#
37
# We must ensure perfect backward compatibility between the current model and
38
# the Status less model.
39
40
my $schema = Koha::Database->new->schema;
41
my $builder = t::lib::TestBuilder->new;
42
use_ok('Koha::Illrequest');
43
use_ok('Koha::Illrequests');
44
45
subtest 'Basic object tests' => sub {
46
47
    plan tests => 21;
48
49
    $schema->storage->txn_begin;
50
51
    my $illrq = $builder->build({ source => 'Illrequest' });
52
    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
53
54
    isa_ok($illrq_obj, 'Koha::Illrequest',
55
           "Correctly create and load an illrequest object.");
56
    isa_ok($illrq_obj->_config, 'Koha::Illrequest::Config',
57
           "Created a config object as part of Illrequest creation.");
58
59
    is($illrq_obj->illrequest_id, $illrq->{illrequest_id},
60
       "Illrequest_id getter works.");
61
    is($illrq_obj->borrowernumber, $illrq->{borrowernumber},
62
       "Borrowernumber getter works.");
63
    is($illrq_obj->biblio_id, $illrq->{biblio_id},
64
       "Biblio_Id getter works.");
65
    is($illrq_obj->branchcode, $illrq->{branchcode},
66
       "Branchcode getter works.");
67
    is($illrq_obj->status, $illrq->{status},
68
       "Status getter works.");
69
    is($illrq_obj->placed, $illrq->{placed},
70
       "Placed getter works.");
71
    is($illrq_obj->replied, $illrq->{replied},
72
       "Replied getter works.");
73
    is($illrq_obj->updated, $illrq->{updated},
74
       "Updated getter works.");
75
    is($illrq_obj->completed, $illrq->{completed},
76
       "Completed getter works.");
77
    is($illrq_obj->medium, $illrq->{medium},
78
       "Medium getter works.");
79
    is($illrq_obj->accessurl, $illrq->{accessurl},
80
       "Accessurl getter works.");
81
    is($illrq_obj->cost, $illrq->{cost},
82
       "Cost getter works.");
83
    is($illrq_obj->notesopac, $illrq->{notesopac},
84
       "Notesopac getter works.");
85
    is($illrq_obj->notesstaff, $illrq->{notesstaff},
86
       "Notesstaff getter works.");
87
    is($illrq_obj->orderid, $illrq->{orderid},
88
       "Orderid getter works.");
89
    is($illrq_obj->backend, $illrq->{backend},
90
       "Backend getter works.");
91
92
    isnt($illrq_obj->status, 'COMP',
93
         "ILL is not currently marked complete.");
94
    $illrq_obj->mark_completed;
95
    is($illrq_obj->status, 'COMP',
96
       "ILL is now marked complete.");
97
98
    $illrq_obj->delete;
99
100
    is(Koha::Illrequests->search->count, 0,
101
       "No illrequest found after delete.");
102
103
    $schema->storage->txn_rollback;
104
};
105
106
subtest 'Working with related objects' => sub {
107
108
    plan tests => 5;
109
110
    $schema->storage->txn_begin;
111
112
    my $patron = $builder->build({ source => 'Borrower' });
113
    my $illrq = $builder->build({
114
        source => 'Illrequest',
115
        value => { borrowernumber => $patron->{borrowernumber} }
116
    });
117
    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
118
119
    isa_ok($illrq_obj->patron, 'Koha::Patron',
120
           "OK accessing related patron.");
121
122
    $builder->build({
123
        source => 'Illrequestattribute',
124
        value  => { illrequest_id => $illrq_obj->illrequest_id, type => 'X' }
125
    });
126
    $builder->build({
127
        source => 'Illrequestattribute',
128
        value  => { illrequest_id => $illrq_obj->illrequest_id, type => 'Y' }
129
    });
130
    $builder->build({
131
        source => 'Illrequestattribute',
132
        value  => { illrequest_id => $illrq_obj->illrequest_id, type => 'Z' }
133
    });
134
135
    is($illrq_obj->illrequestattributes->count, Koha::Illrequestattributes->search->count,
136
       "Fetching expected number of Illrequestattributes for our request.");
137
138
    my $illrq1 = $builder->build({ source => 'Illrequest' });
139
    $builder->build({
140
        source => 'Illrequestattribute',
141
        value  => { illrequest_id => $illrq1->{illrequest_id}, type => 'X' }
142
    });
143
144
    is($illrq_obj->illrequestattributes->count + 1, Koha::Illrequestattributes->search->count,
145
       "Fetching expected number of Illrequestattributes for our request.");
146
147
    $illrq_obj->delete;
148
    is(Koha::Illrequestattributes->search->count, 1,
149
       "Correct number of illrequestattributes after delete.");
150
151
    isa_ok(Koha::Patrons->find($patron->{borrowernumber}), 'Koha::Patron',
152
           "Borrower was not deleted after illrq delete.");
153
154
    $schema->storage->txn_rollback;
155
};
156
157
subtest 'Status Graph tests' => sub {
158
159
    plan tests => 4;
160
161
    $schema->storage->txn_begin;
162
163
    my $illrq = $builder->build({source => 'Illrequest'});
164
    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
165
166
    # _core_status_graph tests: it's just a constant, so here we just make
167
    # sure it returns a hashref.
168
    is(ref $illrq_obj->_core_status_graph, "HASH",
169
       "_core_status_graph returns a hash.");
170
171
    # _status_graph_union: let's try different merge operations.
172
    # Identity operation
173
    is_deeply(
174
        $illrq_obj->_status_graph_union($illrq_obj->_core_status_graph, {}),
175
        $illrq_obj->_core_status_graph,
176
        "core_status_graph + null = core_status_graph"
177
    );
178
179
    # Simple addition
180
    is_deeply(
181
        $illrq_obj->_status_graph_union({}, $illrq_obj->_core_status_graph),
182
        $illrq_obj->_core_status_graph,
183
        "null + core_status_graph = core_status_graph"
184
    );
185
186
    # Correct merge behaviour
187
    is_deeply(
188
        $illrq_obj->_status_graph_union({
189
            REQ => {
190
                prev_actions   => [ ],
191
                id             => 'REQ',
192
                next_actions   => [ ],
193
            },
194
        }, {
195
            QER => {
196
                prev_actions   => [ 'REQ' ],
197
                id             => 'QER',
198
                next_actions   => [ 'REQ' ],
199
            },
200
        }),
201
        {
202
            REQ => {
203
                prev_actions   => [ 'QER' ],
204
                id             => 'REQ',
205
                next_actions   => [ 'QER' ],
206
            },
207
            QER => {
208
                prev_actions   => [ 'REQ' ],
209
                id             => 'QER',
210
                next_actions   => [ 'REQ' ],
211
            },
212
        },
213
        "REQ atom + linking QER = cyclical status graph"
214
    );
215
216
    $schema->storage->txn_rollback;
217
};
218
219
subtest 'Backend testing (mocks)' => sub {
220
221
    plan tests => 13;
222
223
    $schema->storage->txn_begin;
224
225
    # testing load_backend & available_backends requires that we have at least
226
    # the Dummy plugin installed.  load_backend & available_backends don't
227
    # currently have tests as a result.
228
229
    my $backend = Test::MockObject->new;
230
    $backend->set_isa('Koha::Illbackends::Mock');
231
    $backend->set_always('name', 'Mock');
232
233
    my $patron = $builder->build({ source => 'Borrower' });
234
    my $illrq = $builder->build({
235
        source => 'Illrequest',
236
        value => { borrowernumber => $patron->{borrowernumber} }
237
    });
238
    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
239
240
    $illrq_obj->_backend($backend);
241
242
    isa_ok($illrq_obj->_backend, 'Koha::Illbackends::Mock',
243
           "OK accessing mocked backend.");
244
245
    # _backend_capability tests:
246
    # We need to test whether this optional feature of a mocked backend
247
    # behaves as expected.
248
    # 3 scenarios: feature not implemented, feature implemented, but requested
249
    # capability is not provided by backend, & feature is implemented &
250
    # capability exists.  This method can be used to implement custom backend
251
    # functionality, such as unmediated in the BLDSS backend (also see
252
    # bugzilla 18837).
253
    $backend->set_always('capabilities', undef);
254
    is($illrq_obj->_backend_capability('Test'), 0,
255
       "0 returned on Mock not implementing capabilities.");
256
257
    $backend->set_always('capabilities', 0);
258
    is($illrq_obj->_backend_capability('Test'), 0,
259
       "0 returned on Mock not implementing Test capability.");
260
261
    $backend->set_always('capabilities', sub { return 'bar'; } );
262
    is($illrq_obj->_backend_capability('Test'), 'bar',
263
       "'bar' returned on Mock implementing Test capability.");
264
265
    # metadata test: we need to be sure that we return the arbitrary values
266
    # from the backend.
267
    $backend->mock(
268
        'metadata',
269
        sub {
270
            my ( $self, $rq ) = @_;
271
            return {
272
                ID => $rq->illrequest_id,
273
                Title => $rq->patron->borrowernumber
274
            }
275
        }
276
    );
277
278
    is_deeply(
279
        $illrq_obj->metadata,
280
        {
281
            ID => $illrq_obj->illrequest_id,
282
            Title => $illrq_obj->patron->borrowernumber
283
        },
284
        "Test metadata."
285
    );
286
287
    # capabilities:
288
289
    # No backend graph extension
290
    $backend->set_always('status_graph', {});
291
    is_deeply($illrq_obj->capabilities('COMP'),
292
              {
293
                  prev_actions   => [ 'REQ' ],
294
                  id             => 'COMP',
295
                  name           => 'Completed',
296
                  ui_method_name => 'Mark completed',
297
                  method         => 'mark_completed',
298
                  next_actions   => [ ],
299
                  ui_method_icon => 'fa-check',
300
              },
301
              "Dummy status graph for COMP.");
302
    is($illrq_obj->capabilities('UNKNOWN'), undef,
303
       "Dummy status graph for UNKNOWN.");
304
    is_deeply($illrq_obj->capabilities(),
305
              $illrq_obj->_core_status_graph,
306
              "Dummy full status graph.");
307
    # Simple backend graph extension
308
    $backend->set_always('status_graph',
309
                         {
310
                             QER => {
311
                                 prev_actions   => [ 'REQ' ],
312
                                 id             => 'QER',
313
                                 next_actions   => [ 'REQ' ],
314
                             },
315
                         });
316
    is_deeply($illrq_obj->capabilities('QER'),
317
              {
318
                  prev_actions   => [ 'REQ' ],
319
                  id             => 'QER',
320
                  next_actions   => [ 'REQ' ],
321
              },
322
              "Simple status graph for QER.");
323
    is($illrq_obj->capabilities('UNKNOWN'), undef,
324
       "Simple status graph for UNKNOWN.");
325
    is_deeply($illrq_obj->capabilities(),
326
              $illrq_obj->_status_graph_union(
327
                  $illrq_obj->_core_status_graph,
328
                  {
329
                      QER => {
330
                          prev_actions   => [ 'REQ' ],
331
                          id             => 'QER',
332
                          next_actions   => [ 'REQ' ],
333
                      },
334
                  }
335
              ),
336
              "Simple full status graph.");
337
338
    # custom_capability:
339
340
    # No backend graph extension
341
    $backend->set_always('status_graph', {});
342
    is($illrq_obj->custom_capability('unknown', {}), 0,
343
       "Unknown candidate.");
344
345
    # Simple backend graph extension
346
    $backend->set_always('status_graph',
347
                         {
348
                             ID => {
349
                                 prev_actions   => [ 'REQ' ],
350
                                 id             => 'ID',
351
                                 method         => 'identity',
352
                                 next_actions   => [ 'REQ' ],
353
                             },
354
                         });
355
    $backend->mock('identity',
356
                   sub { my ( $self, $params ) = @_; return $params->{other}; });
357
    is($illrq_obj->custom_capability('identity', { test => 1 })->{test}, 1,
358
       "Resolve identity custom_capability");
359
360
    $schema->storage->txn_rollback;
361
};
362
363
364
subtest 'Backend core methods' => sub {
365
366
    plan tests => 11;
367
368
    $schema->storage->txn_begin;
369
370
    # Build infrastructure
371
    my $backend = Test::MockObject->new;
372
    $backend->set_isa('Koha::Illbackends::Mock');
373
    $backend->set_always('name', 'Mock');
374
375
    my $config = Test::MockObject->new;
376
    $config->set_always('backend_dir', "/tmp");
377
    $config->set_always('getLimitRules',
378
                        { default => { count => 0, method => 'active' } });
379
380
    my $illrq = $builder->build({source => 'Illrequest'});
381
    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
382
    $illrq_obj->_config($config);
383
    $illrq_obj->_backend($backend);
384
385
    # expandTemplate:
386
    is_deeply($illrq_obj->expandTemplate({ test => 1, method => "bar" }),
387
              {
388
                  test => 1,
389
                  method => "bar",
390
                  template => "/tmp/Mock/intra-includes/bar.inc",
391
                  opac_template => "/tmp/Mock/opac-includes/bar.inc",
392
              },
393
              "ExpandTemplate");
394
395
    # backend_create
396
    # we are testing simple cases.
397
    $backend->set_series('create',
398
                         { stage => 'bar', method => 'create' },
399
                         { stage => 'commit', method => 'create' },
400
                         { stage => 'commit', method => 'create' });
401
    # Test Copyright Clearance
402
    t::lib::Mocks::mock_preference("ILLModuleCopyrightClearance", "Test Copyright Clearance.");
403
    is_deeply($illrq_obj->backend_create({test => 1}),
404
              {
405
                  error   => 0,
406
                  status  => '',
407
                  message => '',
408
                  method  => 'create',
409
                  stage   => 'copyrightclearance',
410
                  value   => {
411
                      backend => "Mock"
412
                  }
413
              },
414
              "Backend create: copyright clearance.");
415
    t::lib::Mocks::mock_preference("ILLModuleCopyrightClearance", "");
416
    # Test non-commit
417
    is_deeply($illrq_obj->backend_create({test => 1}),
418
              {
419
                  stage => 'bar', method => 'create',
420
                  template => "/tmp/Mock/intra-includes/create.inc",
421
                  opac_template => "/tmp/Mock/opac-includes/create.inc",
422
              },
423
              "Backend create: arbitrary stage.");
424
    # Test commit
425
    is_deeply($illrq_obj->backend_create({test => 1}),
426
              {
427
                  stage => 'commit', method => 'create', permitted => 0,
428
                  template => "/tmp/Mock/intra-includes/create.inc",
429
                  opac_template => "/tmp/Mock/opac-includes/create.inc",
430
              },
431
              "Backend create: arbitrary stage, not permitted.");
432
    is($illrq_obj->status, "QUEUED", "Backend create: queued if restricted.");
433
    $config->set_always('getLimitRules', {});
434
    $illrq_obj->status('NEW');
435
    is_deeply($illrq_obj->backend_create({test => 1}),
436
              {
437
                  stage => 'commit', method => 'create', permitted => 1,
438
                  template => "/tmp/Mock/intra-includes/create.inc",
439
                  opac_template => "/tmp/Mock/opac-includes/create.inc",
440
              },
441
              "Backend create: arbitrary stage, permitted.");
442
    is($illrq_obj->status, "NEW", "Backend create: not-queued.");
443
444
    # backend_renew
445
    $backend->set_series('renew', { stage => 'bar', method => 'renew' });
446
    is_deeply($illrq_obj->backend_renew({test => 1}),
447
              {
448
                  stage => 'bar', method => 'renew',
449
                  template => "/tmp/Mock/intra-includes/renew.inc",
450
                  opac_template => "/tmp/Mock/opac-includes/renew.inc",
451
              },
452
              "Backend renew: arbitrary stage.");
453
454
    # backend_cancel
455
    $backend->set_series('cancel', { stage => 'bar', method => 'cancel' });
456
    is_deeply($illrq_obj->backend_cancel({test => 1}),
457
              {
458
                  stage => 'bar', method => 'cancel',
459
                  template => "/tmp/Mock/intra-includes/cancel.inc",
460
                  opac_template => "/tmp/Mock/opac-includes/cancel.inc",
461
              },
462
              "Backend cancel: arbitrary stage.");
463
464
    # backend_update_status
465
    $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
466
    is_deeply($illrq_obj->backend_update_status({test => 1}),
467
              {
468
                  stage => 'bar', method => 'update_status',
469
                  template => "/tmp/Mock/intra-includes/update_status.inc",
470
                  opac_template => "/tmp/Mock/opac-includes/update_status.inc",
471
              },
472
              "Backend update_status: arbitrary stage.");
473
474
    # backend_confirm
475
    $backend->set_series('confirm', { stage => 'bar', method => 'confirm' });
476
    is_deeply($illrq_obj->backend_confirm({test => 1}),
477
              {
478
                  stage => 'bar', method => 'confirm',
479
                  template => "/tmp/Mock/intra-includes/confirm.inc",
480
                  opac_template => "/tmp/Mock/opac-includes/confirm.inc",
481
              },
482
              "Backend confirm: arbitrary stage.");
483
484
    # FIXME have to test generic_confirm.
485
486
    $schema->storage->txn_rollback;
487
};
488
489
490
subtest 'Helpers' => sub {
491
492
    plan tests => 9;
493
494
    $schema->storage->txn_begin;
495
496
    # Build infrastructure
497
    my $backend = Test::MockObject->new;
498
    $backend->set_isa('Koha::Illbackends::Mock');
499
    $backend->set_always('name', 'Mock');
500
501
    my $config = Test::MockObject->new;
502
    $config->set_always('backend_dir', "/tmp");
503
504
    my $patron = $builder->build({
505
        source => 'Borrower',
506
        value => { categorycode => "A" }
507
    });
508
    my $illrq = $builder->build({
509
        source => 'Illrequest',
510
        value => { branchcode => "CPL", borrowernumber => $patron->{borrowernumber} }
511
    });
512
    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
513
    $illrq_obj->_config($config);
514
    $illrq_obj->_backend($backend);
515
516
    # getPrefix
517
    $config->set_series('getPrefixes',
518
                        { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
519
                        { A => "ATEST", C => "CBAR", default => "DEFAULT" });
520
    is($illrq_obj->getPrefix({ brw_cat => "C", branch => "CPL" }), "CBAR",
521
       "getPrefix: brw_cat");
522
    $config->set_series('getPrefixes',
523
                        { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
524
                        { A => "ATEST", C => "CBAR", default => "DEFAULT" });
525
    is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "CPL" }), "TEST",
526
       "getPrefix: branch");
527
    $config->set_series('getPrefixes',
528
                        { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
529
                        { A => "ATEST", C => "CBAR", default => "DEFAULT" });
530
    is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "UNKNOWN" }), "DEFAULT",
531
       "getPrefix: default");
532
    $config->set_always('getPrefixes', {});
533
    is($illrq_obj->getPrefix({ brw_cat => "UNKNOWN", branch => "UNKNOWN" }), "",
534
       "getPrefix: the empty prefix");
535
536
    # id_prefix
537
    $config->set_series('getPrefixes',
538
                        { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
539
                        { A => "ATEST", C => "CBAR", default => "DEFAULT" });
540
    is($illrq_obj->id_prefix, "ATEST-", "id_prefix: brw_cat");
541
    $config->set_series('getPrefixes',
542
                        { CPL => "TEST", TSL => "BAR", default => "DEFAULT" },
543
                        { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
544
    is($illrq_obj->id_prefix, "TEST-", "id_prefix: branch");
545
    $config->set_series('getPrefixes',
546
                        { CPLT => "TEST", TSLT => "BAR", default => "DEFAULT" },
547
                        { AB => "ATEST", CD => "CBAR", default => "DEFAULT" });
548
    is($illrq_obj->id_prefix, "DEFAULT-", "id_prefix: default");
549
550
    # requires_moderation
551
    $illrq_obj->status('NEW')->store;
552
    is($illrq_obj->requires_moderation, undef, "requires_moderation: No.");
553
    $illrq_obj->status('CANCREQ')->store;
554
    is($illrq_obj->requires_moderation, 'CANCREQ', "requires_moderation: Yes.");
555
556
    $schema->storage->txn_rollback;
557
};
558
559
560
subtest 'Censorship' => sub {
561
562
    plan tests => 4;
563
564
    $schema->storage->txn_begin;
565
566
    # Build infrastructure
567
    my $backend = Test::MockObject->new;
568
    $backend->set_isa('Koha::Illbackends::Mock');
569
    $backend->set_always('name', 'Mock');
570
571
    my $config = Test::MockObject->new;
572
    $config->set_always('backend_dir', "/tmp");
573
574
    my $illrq = $builder->build({source => 'Illrequest'});
575
    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
576
    $illrq_obj->_config($config);
577
    $illrq_obj->_backend($backend);
578
579
    # getCensorNotesStaff
580
    $config->set_series('censorship',
581
                        { censor_notes_staff => 1 },
582
                        { censor_notes_staff => 0 });
583
    is($illrq_obj->getCensorNotesStaff, 1,
584
       "getCensorNotesStaff: on.");
585
    is($illrq_obj->getCensorNotesStaff, 0,
586
       "getCensorNotesStaff: off.");
587
588
    # getDisplayReplyDate
589
    $config->set_series('censorship',
590
                        { censor_reply_date => 1 },
591
                        { censor_reply_date => 0 });
592
    is($illrq_obj->getDisplayReplyDate, 0,
593
       "getDisplayReplyDate: off.");
594
    is($illrq_obj->getDisplayReplyDate, 1,
595
       "getDisplayReplyDate: on.");
596
597
    # FIXME need to test _censor
598
599
    $schema->storage->txn_rollback;
600
};
601
602
subtest 'Checking Limits' => sub {
603
604
    plan tests => 3;
605
606
    $schema->storage->txn_begin;
607
608
    # Build infrastructure
609
    my $backend = Test::MockObject->new;
610
    $backend->set_isa('Koha::Illbackends::Mock');
611
    $backend->set_always('name', 'Mock');
612
613
    my $config = Test::MockObject->new;
614
    $config->set_always('backend_dir', "/tmp");
615
616
    my $illrq = $builder->build({source => 'Illrequest'});
617
    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
618
    $illrq_obj->_config($config);
619
    $illrq_obj->_backend($backend);
620
621
    # getLimits
622
    $config->set_series('getLimitRules',
623
                        { CPL => { count => 1, method => 'test' } },
624
                        { default => { count => 0, method => 'active' } });
625
    is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
626
              { count => 1, method => 'test' },
627
              "getLimits: by value.");
628
    is_deeply($illrq_obj->getLimits({ type => 'branch' }),
629
              { count => 0, method => 'active' },
630
              "getLimits: by default.");
631
    is_deeply($illrq_obj->getLimits({ type => 'branch', value => "CPL" }),
632
              { count => -1, method => 'active' },
633
              "getLimits: by hard-coded.");
634
635
    # FIXME need to test _limit_counter & check_limits
636
637
    $schema->storage->txn_rollback;
638
};
639
640
641
642
1;

Return to bug 7317