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

(-)a/Koha/Illrequest/Backend/Dummy/Base.pm (-576 lines)
Lines 1-576 Link Here
1
package Koha::Illrequest::Backend::Dummy::Base;
2
3
# Copyright PTFS Europe 2014
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
use DateTime;
22
use Koha::Illrequestattribute;
23
24
=head1 NAME
25
26
Koha::Illrequest::Backend::Dummy - Koha ILL Backend: Dummy
27
28
=head1 SYNOPSIS
29
30
Koha ILL implementation for the "Dummy" backend.
31
32
=head1 DESCRIPTION
33
34
=head2 Overview
35
36
We will be providing the Abstract interface which requires we implement the
37
following methods:
38
- create        -> initial placement of the request for an ILL order
39
- confirm       -> confirm placement of the ILL order
40
- list          -> list all ILL Requests currently placed with the backend
41
- renew         -> request a currently borrowed ILL be renewed in the backend
42
- update_status -> ILL module update hook: custom actions on status update
43
- cancel        -> request an already 'confirm'ed ILL order be cancelled
44
- status        -> request the current status of a confirmed ILL order
45
46
Each of the above methods will receive the following parameter from
47
Illrequest.pm:
48
49
  {
50
      request    => $request,
51
      other      => $other,
52
  }
53
54
where:
55
56
- $REQUEST is the Illrequest object in Koha.  It's associated
57
  Illrequestattributes can be accessed through the `illrequestattributes`
58
  method.
59
- $OTHER is any further data, generally provided through templates .INCs
60
61
Each of the above methods should return a hashref of the following format:
62
63
    return {
64
        error   => 0,
65
        # ^------- 0|1 to indicate an error
66
        status  => 'result_code',
67
        # ^------- Summary of the result of the operation
68
        message => 'Human readable message.',
69
        # ^------- Message, possibly to be displayed
70
        #          Normally messages are derived from status in INCLUDE.
71
        #          But can be used to pass API messages to the INCLUDE.
72
        method  => 'list',
73
        # ^------- Name of the current method invoked.
74
        #          Used to load the appropriate INCLUDE.
75
        stage   => 'commit',
76
        # ^------- The current stage of this method
77
        #          Used by INCLUDE to determine HTML to generate.
78
        #          'commit' will result in final processing by Illrequest.pm.
79
        next    => 'illview'|'illlist',
80
        # ^------- When stage is 'commit', should we move on to ILLVIEW the
81
        #          current request or ILLLIST all requests.
82
        value   => {},
83
        # ^------- A hashref containing an arbitrary return value that this
84
        #          backend wants to supply to its INCLUDE.
85
    };
86
87
=head2 On the Dummy backend
88
89
The Dummy backend is rather simple, but provides correctly formatted response
90
values, that other backends can model themselves after.
91
92
The code is not DRY -- primarily so that each method can be looked at in
93
isolation rather than having to familiarise oneself with helper procedures.
94
95
=head1 API
96
97
=head2 Class Methods
98
99
=cut
100
101
=head3 new
102
103
  my $backend = Koha::Illrequest::Backend::Dummy->new;
104
105
=cut
106
107
sub new {
108
    # -> instantiate the backend
109
    my ( $class ) = @_;
110
    my $self = {};
111
    bless( $self, $class );
112
    return $self;
113
}
114
115
=head3 _data_store
116
117
  my $request = $self->_data_store($id);
118
  my $requests = $self->_data_store;
119
120
A mock of a data store.  When passed no parameters it returns all entries.
121
When passed one it will return the entry matched by its id.
122
123
=cut
124
125
sub _data_store {
126
    my $data = {
127
        1234 => {
128
            id     => 1234,
129
            title  => "Ordering ILLs using Koha",
130
            author => "A.N. Other",
131
        },
132
        5678 => {
133
            id     => 5678,
134
            title  => "Interlibrary loans in Koha",
135
            author => "A.N. Other",
136
        },
137
    };
138
    # ID search
139
    my ( $self, $id ) = @_;
140
    return $data->{$id} if $id;
141
142
    # Full search
143
    my @entries;
144
    while ( my ( $k, $v ) = each %{$data} ) {
145
        push @entries, $v;
146
    }
147
    return \@entries;
148
}
149
150
=head3 create
151
152
  my $response = $backend->create({
153
      request    => $requestdetails,
154
      other      => $other,
155
  });
156
157
This is the initial creation of the request.  Generally this stage will be
158
some form of search with the backend.
159
160
By and large we will not have useful $requestdetails (borrowernumber,
161
branchcode, status, etc.).
162
163
$params is simply an additional slot for any further arbitrary values to pass
164
to the backend.
165
166
This is an example of a multi-stage method.
167
168
=cut
169
170
sub create {
171
    # -> initial placement of the request for an ILL order
172
    my ( $self, $params ) = @_;
173
    my $stage = $params->{other}->{stage};
174
    if ( !$stage || $stage eq 'init' ) {
175
        # We simply need our template .INC to produce a search form.
176
        return {
177
            error   => 0,
178
            status  => '',
179
            message => '',
180
            method  => 'create',
181
            stage   => 'search_form',
182
            value   => {},
183
        };
184
    } elsif ( $stage eq 'search_form' ) {
185
	# Received search query in 'other'; perform search...
186
187
        # No-op on Dummy
188
189
        # and return results.
190
        return {
191
            error   => 0,
192
            status  => '',
193
            message => '',
194
            method  => 'create',
195
            stage   => 'search_results',
196
            value   => {
197
                borrowernumber => $params->{other}->{borrowernumber},
198
                branchcode     => $params->{other}->{branchcode},
199
                medium         => $params->{other}->{medium},
200
                candidates     => $self->_data_store,
201
            }
202
        };
203
    } elsif ( $stage eq 'search_results' ) {
204
        # We have a selection
205
        my $id = $params->{other}->{id};
206
207
        # -> select from backend...
208
        my $request_details = $self->_data_store($id);
209
210
        # ...Populate Illrequest
211
        my $request = $params->{request};
212
        $request->borrower_id($params->{other}->{borrowernumber});
213
        $request->branch_id($params->{other}->{branchcode});
214
        $request->medium($params->{other}->{medium});
215
        $request->status('NEW');
216
        $request->placed(DateTime->now);
217
        $request->updated(DateTime->now);
218
        $request->store;
219
        # ...Populate Illrequestattributes
220
        while ( my ( $type, $value ) = each %{$request_details} ) {
221
            Koha::Illrequestattribute->new({
222
                illrequest_id => $request->illrequest_id,
223
                type          => $type,
224
                value         => $value,
225
            })->store;
226
        }
227
228
        # -> create response.
229
        return {
230
            error   => 0,
231
            status  => '',
232
            message => '',
233
            method  => 'create',
234
            stage   => 'commit',
235
            next    => 'illview',
236
            value   => $request_details,
237
        };
238
    } else {
239
	# Invalid stage, return error.
240
        return {
241
            error   => 1,
242
            status  => 'unknown_stage',
243
            message => '',
244
            method  => 'create',
245
            stage   => $params->{stage},
246
            value   => {},
247
        };
248
    }
249
}
250
251
=head3 confirm
252
253
  my $response = $backend->confirm({
254
      request    => $requestdetails,
255
      other      => $other,
256
  });
257
258
Confirm the placement of the previously "selected" request (by using the
259
'create' method).
260
261
In this case we will generally use $request.
262
This will be supplied at all times through Illrequest.  $other may be supplied
263
using templates.
264
265
=cut
266
267
sub confirm {
268
    # -> confirm placement of the ILL order
269
    my ( $self, $params ) = @_;
270
    # Turn Illrequestattributes into a plain hashref
271
    my $value = {};
272
    my $attributes = $params->{request}->illrequestattributes;
273
    foreach my $attr (@{$attributes->as_list}) {
274
        $value->{$attr->type} = $attr->value;
275
    };
276
    # Submit request to backend...
277
278
    # No-op for Dummy
279
280
    # ...parse response...
281
    $attributes->find_or_create({ type => "status", value => "On order" });
282
    my $request = $params->{request};
283
    $request->cost("30 GBP");
284
    $request->orderid($value->{id});
285
    $request->status("REQ");
286
    $request->accessurl("URL") if $value->{url};
287
    $request->store;
288
    $value->{status} = "On order";
289
    $value->{cost} = "30 GBP";
290
    # ...then return our result:
291
    return {
292
        error    => 0,
293
        status   => '',
294
        message  => '',
295
        method   => 'confirm',
296
        stage    => 'commit',
297
        next     => 'illview',
298
        value    => $value,
299
    };
300
}
301
302
=head3 list
303
304
  my $response = $backend->list({
305
      request    => $requestdetails,
306
      other      => $other,
307
  };
308
309
Attempt to get a list of the currently registered requests with the backend.
310
311
Parameters are optional for this request.  A backend may be supplied with
312
details of a specific request (or a group of requests in $other), but equally
313
no parameters might be provided at all.
314
315
Normally no parameters will be provided in the 'create' stage.  After this,
316
parameters may be provided using templates.
317
318
=cut
319
320
sub list {
321
    # -> list all ILL Requests currently placed with the backend
322
    #    (we ignore all params provided)
323
    my ( $self, $params ) = @_;
324
    my $stage = $params->{other}->{stage};
325
    if ( !$stage || $stage eq 'init' ) {
326
        return {
327
            error   => 0,
328
            status  => '',
329
            message => '',
330
            method  => 'list',
331
            stage   => 'list',
332
            value   => {
333
                1 => {
334
                    id     => 1234,
335
                    title  => "Ordering ILLs using Koha",
336
                    author => "A.N. Other",
337
                    status => "On order",
338
                    cost   => "30 GBP",
339
                },
340
            },
341
        };
342
    } elsif ( $stage eq 'list' ) {
343
        return {
344
            error   => 0,
345
            status  => '',
346
            message => '',
347
            method  => 'list',
348
            stage   => 'commit',
349
            value   => {},
350
        };
351
    } else {
352
        # Invalid stage, return error.
353
        return {
354
            error   => 1,
355
            status  => 'unknown_stage',
356
            message => '',
357
            method  => 'create',
358
            stage   => $params->{stage},
359
            value   => {},
360
        };
361
    }
362
}
363
364
=head3 renew
365
366
  my $response = $backend->renew({
367
      request    => $requestdetails,
368
      other      => $other,
369
  });
370
371
Attempt to renew a request that was supplied through backend and is currently
372
in use by us.
373
374
We will generally use $request.  This will be supplied at all times through
375
Illrequest.  $other may be supplied using templates.
376
377
=cut
378
379
sub renew {
380
    # -> request a currently borrowed ILL be renewed in the backend
381
    my ( $self, $params ) = @_;
382
    # Turn Illrequestattributes into a plain hashref
383
    my $value = {};
384
    my $attributes = $params->{request}->illrequestattributes;
385
    foreach my $attr (@{$attributes->as_list}) {
386
        $value->{$attr->type} = $attr->value;
387
    };
388
    # Submit request to backend, parse response...
389
    my ( $error, $status, $message ) = ( 0, '', '' );
390
    if ( !$value->{status} || $value->{status} eq 'On order' ) {
391
        $error = 1;
392
        $status = 'not_renewed';
393
        $message = 'Order not yet delivered.';
394
    } else {
395
        $value->{status} = "Renewed";
396
    }
397
    # ...then return our result:
398
    return {
399
        error   => $error,
400
        status  => $status,
401
        message => $message,
402
        method  => 'renew',
403
        stage   => 'commit',
404
        value   => $value,
405
    };
406
}
407
408
=head3 update_status
409
410
  my $response = $backend->update_status({
411
      request    => $requestdetails,
412
      other      => $other,
413
  });
414
415
Our Illmodule is handling a request to update the status of an Illrequest.  As
416
part of this we give the backend an opportunity to perform arbitrary actions
417
on update to a new status.
418
419
We will provide $request.  This will be supplied at all times through
420
Illrequest.  $other will contain entries for the old status and the new
421
status, as well as other information provided from templates.
422
423
$old_status, $new_status.
424
425
=cut
426
427
sub update_status {
428
    # -> ILL module update hook: custom actions on status update
429
    my ( $self, $params ) = @_;
430
    # Turn Illrequestattributes into a plain hashref
431
    my $value = {};
432
    my $attributes = $params->{request}->illrequestattributes;
433
    foreach my $attr (@{$attributes->as_list}) {
434
        $value->{$attr->type} = $attr->value;
435
    };
436
    # Submit request to backend, parse response...
437
    my ( $error, $status, $message ) = (0, '', '');
438
    my $old = $params->{other}->{old_status};
439
    my $new = $params->{other}->{new_status};
440
    if ( !$new || $new eq 'ERR' ) {
441
        ( $error, $status, $message ) = (
442
            1, 'failed_update_hook',
443
            'Fake reason for failing to perform update operation.'
444
        );
445
    }
446
    return {
447
        error   => $error,
448
        status  => $status,
449
        message => $message,
450
        method  => 'update_status',
451
        stage   => 'commit',
452
        value   => $value,
453
    };
454
}
455
456
=head3 cancel
457
458
  my $response = $backend->cancel({
459
      request    => $requestdetails,
460
      other      => $other,
461
  });
462
463
We will attempt to cancel a request that was confirmed.
464
465
We will generally use $request.  This will be supplied at all times through
466
Illrequest.  $other may be supplied using templates.
467
468
=cut
469
470
sub cancel {
471
    # -> request an already 'confirm'ed ILL order be cancelled
472
    my ( $self, $params ) = @_;
473
    # Turn Illrequestattributes into a plain hashref
474
    my $value = {};
475
    my $attributes = $params->{request}->illrequestattributes;
476
    foreach my $attr (@{$attributes->as_list}) {
477
        $value->{$attr->type} = $attr->value;
478
    };
479
    # Submit request to backend, parse response...
480
    my ( $error, $status, $message ) = (0, '', '');
481
    if ( !$value->{status} ) {
482
        ( $error, $status, $message ) = (
483
            1, 'unknown_request', 'Cannot cancel an unknown request.'
484
        );
485
    } else {
486
        $attributes->find({ type => "status" })->delete;
487
        $params->{request}->status("REQREV");
488
        $params->{request}->cost(undef);
489
        $params->{request}->orderid(undef);
490
        $params->{request}->store;
491
    }
492
    return {
493
        error   => $error,
494
        status  => $status,
495
        message => $message,
496
        method  => 'cancel',
497
        stage   => 'commit',
498
        value   => $value,
499
    };
500
}
501
502
=head3 status
503
504
  my $response = $backend->create({
505
      request    => $requestdetails,
506
      other      => $other,
507
  });
508
509
We will try to retrieve the status of a specific request.
510
511
We will generally use $request.  This will be supplied at all times through
512
Illrequest.  $other may be supplied using templates.
513
514
=cut
515
516
sub status {
517
    # -> request the current status of a confirmed ILL order
518
    my ( $self, $params ) = @_;
519
    my $value = {};
520
    my $stage = $params->{other}->{stage};
521
    my ( $error, $status, $message ) = (0, '', '');
522
    if ( !$stage || $stage eq 'init' ) {
523
        # Generate status result
524
        # Turn Illrequestattributes into a plain hashref
525
        my $attributes = $params->{request}->illrequestattributes;
526
        foreach my $attr (@{$attributes->as_list}) {
527
            $value->{$attr->type} = $attr->value;
528
        }
529
        ;
530
        # Submit request to backend, parse response...
531
        if ( !$value->{status} ) {
532
            ( $error, $status, $message ) = (
533
                1, 'unknown_request', 'Cannot query status of an unknown request.'
534
            );
535
        }
536
        return {
537
            error   => $error,
538
            status  => $status,
539
            message => $message,
540
            method  => 'status',
541
            stage   => 'status',
542
            value   => $value,
543
        };
544
545
    } elsif ( $stage eq 'status') {
546
        # No more to do for method.  Return to illlist.
547
        return {
548
            error   => $error,
549
            status  => $status,
550
            message => $message,
551
            method  => 'status',
552
            stage   => 'commit',
553
            next    => 'illlist',
554
            value   => {},
555
        };
556
557
    } else {
558
        # Invalid stage, return error.
559
        return {
560
            error   => 1,
561
            status  => 'unknown_stage',
562
            message => '',
563
            method  => 'create',
564
            stage   => $params->{stage},
565
            value   => {},
566
        };
567
    }
568
}
569
570
=head1 AUTHOR
571
572
Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
573
574
=cut
575
576
1;
(-)a/t/db_dependent/Illrequest.t (-544 lines)
Lines 1-544 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::Illrequestattributes;
24
use Koha::Patrons;
25
use t::lib::TestBuilder;
26
27
use Test::More tests => 44;
28
29
# We want to test the Koha IllRequest object.  At its core it's a simple
30
# Koha::Object, mapping to the ill_request table.
31
#
32
# This object will supersede the Status object in ILLModule.
33
#
34
# We must ensure perfect backward compatibility between the current model and
35
# the Status less model.
36
37
use_ok('Koha::Illrequest');
38
use_ok('Koha::Illrequests');
39
40
my $schema = Koha::Database->new->schema;
41
$schema->storage->txn_begin;
42
43
my $builder = t::lib::TestBuilder->new;
44
45
my $patron = $builder->build({ source => 'Borrower' });
46
my $branch = $builder->build({ source => 'Branch' });
47
48
my $illRequest = $builder->build({
49
    source => 'Illrequest',
50
    value => {
51
        borrowernumber  => $patron->{borrowernumber},
52
        branch          => $branch->{branchcode},
53
        biblionumber    => 0,
54
        status          => 'NEW',
55
        completion_date => 0,
56
        reqtype         => 'book',
57
    }
58
});
59
60
my $illObject = Koha::Illrequests->find($illRequest->{illrequest_id});
61
62
isa_ok($illObject, "Koha::Illrequest");
63
64
# Test delete works correctly.
65
my $illRequestDelete = $builder->build({
66
    source => 'Illrequest',
67
    value => {
68
        borrowernumber  => $patron->{borrowernumber},
69
        branch          => $branch->{branchcode},
70
        biblionumber    => 0,
71
        status          => 'NEW',
72
        completion_date => 0,
73
        reqtype         => 'book',
74
    }
75
});
76
sub ill_req_search {
77
    return Koha::Illrequestattributes->search({
78
        illrequest_id => $illRequestDelete->{illrequest_id}
79
    })->count;
80
}
81
82
is(ill_req_search, 0, "Correctly not found matching Illrequestattributes.");
83
# XXX: For some reason test builder can't build Illrequestattributes.
84
my $illReqAttr = Koha::Illrequestattribute->new({
85
    illrequest_id => $illRequestDelete->{illrequest_id},
86
    type => "test",
87
    value => "Hello World"
88
})->store;
89
is(ill_req_search, 1, "We have found a matching Illrequestattribute.");
90
91
Koha::Illrequests->find($illRequestDelete->{illrequest_id})->delete;
92
is(
93
    Koha::Illrequests->find($illRequestDelete->{illrequest_id}),
94
    undef,
95
    "Correctly deleted Illrequest."
96
);
97
is(ill_req_search, 0, "Correctly deleted Illrequestattributes.");
98
99
# Test Accessing of related records.
100
101
# # TODO the conclusion from being able to use one_to_many? we no longer need
102
# # the Record object: simply pass the `ill_request_attributes` resultset
103
# # whenever one would pass a Record.
104
105
my $illRequest2 = $builder->build({
106
    source => 'Illrequest',
107
    value  => {
108
        borrower_id => $patron->{borrowernumber},
109
        branch_id   => $branch->{branchcode},
110
        biblio_id   => 0,
111
        status      => 'NEW',
112
        completed   => 0,
113
        medium      => 'book',
114
    }
115
});
116
my $illReqAttr2 = Koha::Illrequestattribute->new({
117
    illrequest_id => $illRequest2->{illrequest_id},
118
    type          => "test2",
119
    value         => "Hello World"
120
})->store;
121
my $illReqAttr3 = Koha::Illrequestattribute->new({
122
    illrequest_id => $illRequest2->{illrequest_id},
123
    type          => "test3",
124
    value         => "Hello Space"
125
})->store;
126
127
my $illRequestAttributes = Koha::Illrequests
128
    ->find($illRequest2->{illrequest_id})->illrequestattributes;
129
130
isa_ok($illRequestAttributes, "Koha::Illrequestattributes");
131
132
is($illRequestAttributes->count, 2, "Able to search related.");
133
134
# Test loading of 'Config'.
135
136
my $rqConfigTest = Koha::Illrequest->new({
137
    borrower_id => $patron->{borrowernumber},
138
    branch_id   => $branch->{branchcode},
139
});
140
141
isa_ok($rqConfigTest->_config, "Koha::Illrequest::Config");
142
143
# Test loading of 'Dummy' backend.
144
145
my $rqBackendTest = Koha::Illrequest->new({
146
    borrower_id => $patron->{borrowernumber},
147
    branch_id   => $branch->{branchcode},
148
})->store;
149
150
$rqBackendTest->_config->backend("Dummy");
151
$rqBackendTest->_config->limits({ default => { count => -1 } });
152
isa_ok($rqBackendTest->_backend, "Koha::Illbackends::Dummy::Base");
153
154
# Test use of 'Dummy' Backend.
155
156
## Test backend_update_status
157
158
# FIXME: This breaks transparancy of ->status method!
159
eval { $rqBackendTest->status("ERR") };
160
ok($@, "status: Test for status error on hook fail.");
161
162
# FIXME: Will need to test this on new illRequest to not pollute rest of
163
# tests.
164
165
# is($rqBackendTest->status("NEW")->status, "NEW", "status: Setter works
166
# OK.");
167
# is($rqBackendTest->status(null), null, "status: Unsetter works OK.");
168
169
## Test backend_create
170
171
is(
172
    $rqBackendTest->status,
173
    undef,
174
    "backend_create: Test our status initiates correctly."
175
);
176
177
# Request a search form
178
my $created_rq = $rqBackendTest->backend_create({
179
    stage  => "search_form",
180
    method => "create",
181
});
182
183
is( $created_rq->{stage}, 'search_results',
184
    "backend_create: search_results stage." );
185
186
# Request search results
187
# FIXME: fails because of missing patron / branch info.
188
# $created_rq = $rqBackendTest->backend_create({
189
#     stage  => "search_results",
190
#     method => "create",
191
#     other  => { search => "interlibrary loans" },
192
# });
193
194
# is_deeply(
195
#     $created_rq,
196
#     {
197
#         error    => 0,
198
#         status   => '',
199
#         message  => '',
200
#         method   => 'create',
201
#         stage    => 'search_results',
202
#         template => 'ill/Dummy/create.inc',
203
#         value    => [
204
#             {
205
#                 id     => 1234,
206
#                 title  => "Ordering ILLs using Koha",
207
#                 author => "A.N. Other",
208
#             },
209
#             {
210
#                 id     => 5678,
211
#                 title  => "Interlibrary loans in Koha",
212
#                 author => "A.N. Other",
213
#             },
214
#         ],
215
#     }
216
#     ,
217
#     "backend_create: search_results stage."
218
# );
219
220
# # Create the request
221
# $created_rq = $rqBackendTest->backend_create({
222
#     stage  => "commit",
223
#     method => "create",
224
#     other  => {
225
#         id     => 1234,
226
#         title  => "Ordering ILLs using Koha",
227
#         author => "A.N. Other",
228
#     },
229
# });
230
231
# while ( my ( $field, $value ) = each %{$created_rq} ) {
232
#     isnt($value, undef, "backend_create: key '$field' exists");
233
# };
234
235
# is(
236
#     $rqBackendTest->status,
237
#     "NEW",
238
#     "backend_create: Test our status was updated."
239
# );
240
241
# cmp_ok(
242
#     $rqBackendTest->illrequestattributes->count,
243
#     "==",
244
#     3,
245
#     "backend_create: Ensure we have correctly stored our new attributes."
246
# );
247
248
# ## Test backend_list
249
250
# is_deeply(
251
#     $rqBackendTest->backend_list->{value},
252
#     {
253
#         1 => {
254
#             id     => 1234,
255
#             title  => "Ordering ILLs using Koha",
256
#             author => "A.N. Other",
257
#             status => "On order",
258
#             cost   => "30 GBP",
259
#         }
260
#     },
261
#     "backend_list: Retrieve our list of requested requests."
262
# );
263
264
# ## Test backend_renew
265
266
# ok(
267
#     $rqBackendTest->backend_renew->{error},
268
#     "backend_renew: Error for invalid request."
269
# );
270
# is_deeply(
271
#     $rqBackendTest->backend_renew->{value},
272
#     {
273
#         id     => 1234,
274
#         title  => "Ordering ILLs using Koha",
275
#         author => "A.N. Other",
276
#     },
277
#     "backend_renew: Renew request."
278
# );
279
280
# ## Test backend_confirm
281
282
# my $rqBackendTestConfirmed = $rqBackendTest->backend_confirm;
283
# is(
284
#     $rqBackendTest->status,
285
#     "REQ",
286
#     "backend_commit: Confirm status update correct."
287
# );
288
# is(
289
#     $rqBackendTest->orderid,
290
#     1234,
291
#     "backend_commit: Confirm orderid populated correctly."
292
# );
293
294
# ## Test backend_status
295
296
# is(
297
#     $rqBackendTest->backend_status->{error},
298
#     0,
299
#     "backend_status: error for invalid request."
300
# );
301
# is_deeply(
302
#     $rqBackendTest->backend_status->{value},
303
#     {
304
#         id     => 1234,
305
#         status => "On order",
306
#         title  => "Ordering ILLs using Koha",
307
#         author => "A.N. Other",
308
#     },
309
#     "backend_status: Retrieve the status of request."
310
# );
311
312
# # Now test trying to get status on non-confirmed request.
313
my $rqBackendTestUnconfirmed = Koha::Illrequest->new({
314
    borrower_id => $patron->{borrowernumber},
315
    branch_id   => $branch->{branchcode},
316
})->store;
317
$rqBackendTestUnconfirmed->_config->backend("Dummy");
318
$rqBackendTestUnconfirmed->_config->limits({ default => { count => -1 } });
319
320
$rqBackendTestUnconfirmed->backend_create({
321
    stage  => "commit",
322
    method => "create",
323
    other  => {
324
        id     => 1234,
325
        title  => "Ordering ILLs using Koha",
326
        author => "A.N. Other",
327
    },
328
});
329
is(
330
    $rqBackendTestUnconfirmed->backend_status->{error},
331
    1,
332
    "backend_status: error for invalid request."
333
);
334
335
## Test backend_cancel
336
337
# is(
338
#     $rqBackendTest->backend_cancel->{error},
339
#     0,
340
#     "backend_cancel: Successfully cancelling request."
341
# );
342
# is_deeply(
343
#     $rqBackendTest->backend_cancel->{value},
344
#     {
345
#         id     => 1234,
346
#         title  => "Ordering ILLs using Koha",
347
#         author => "A.N. Other",
348
#     },
349
#     "backend_cancel: Cancel request."
350
# );
351
352
# Now test trying to cancel non-confirmed request.
353
is(
354
    $rqBackendTestUnconfirmed->backend_cancel->{error},
355
    1,
356
    "backend_cancel: error for invalid request."
357
);
358
is_deeply(
359
    $rqBackendTestUnconfirmed->backend_cancel->{value},
360
    {},
361
    "backend_cancel: Cancel request."
362
);
363
364
# Test Helpers
365
366
## Test getCensorNotesStaff
367
368
is($rqBackendTest->getCensorNotesStaff, 1, "getCensorNotesStaff: Public.");
369
$rqBackendTest->_config->censorship({
370
    censor_notes_staff => 0,
371
    censor_reply_date  => 0,
372
});
373
is($rqBackendTest->getCensorNotesStaff, 0, "getCensorNotesStaff: Censored.");
374
375
## Test getCensorNotesStaff
376
377
is($rqBackendTest->getDisplayReplyDate, 1, "getDisplayReplyDate: Yes.");
378
$rqBackendTest->_config->censorship({
379
    censor_notes_staff => 0,
380
    censor_reply_date  => 1,
381
});
382
is($rqBackendTest->getDisplayReplyDate, 0, "getDisplayReplyDate: No.");
383
384
# FIXME: These should be handled by the templates.
385
# # Test Output Helpers
386
387
# ## Test getStatusSummary
388
389
# $rqBackendTest->medium("Book")->store;
390
# is_deeply(
391
#     $rqBackendTest->getStatusSummary({brw => 0}),
392
#     {
393
#         biblionumber => ["Biblio Number", undef],
394
#         borrowernumber => ["Borrower Number", $patron->{borrowernumber}],
395
#         id => ["Request Number", $rqBackendTest->illrequest_id],
396
#         prefix_id => ["Request Number", $rqBackendTest->illrequest_id],
397
#         reqtype => ["Request Type", "Book"],
398
#         status => ["Status", "REQREV"],
399
#     },
400
#     "getStatusSummary: Without Borrower."
401
# );
402
403
# is_deeply(
404
#     $rqBackendTest->getStatusSummary({brw => 1}),
405
#     {
406
#         biblionumber => ["Biblio Number", undef],
407
#         borrower => ["Borrower", Koha::Patrons->find($patron->{borrowernumber})],
408
#         id => ["Request Number", $rqBackendTest->illrequest_id],
409
#         prefix_id => ["Request Number", $rqBackendTest->illrequest_id],
410
#         reqtype => ["Request Type", "Book"],
411
#         status => ["Status", "REQREV"],
412
#     },
413
#     "getStatusSummary: With Borrower."
414
# );
415
416
# ## Test getFullStatus
417
418
# is_deeply(
419
#     $rqBackendTest->getFullStatus({brw => 0}),
420
#     {
421
#         biblionumber => ["Biblio Number", undef],
422
#         borrowernumber => ["Borrower Number", $patron->{borrowernumber}],
423
#         id => ["Request Number", $rqBackendTest->illrequest_id],
424
#         prefix_id => ["Request Number", $rqBackendTest->illrequest_id],
425
#         reqtype => ["Request Type", "Book"],
426
#         status => ["Status", "REQREV"],
427
#         placement_date => ["Placement Date", $rqBackendTest->placed],
428
#         completion_date => ["Completion Date", $rqBackendTest->completed],
429
#         ts => ["Timestamp", $rqBackendTest->updated],
430
#         branch => ["Branch", $rqBackendTest->branch_id],
431
#     },
432
#     "getFullStatus: Without Borrower."
433
# );
434
435
# is_deeply(
436
#     $rqBackendTest->getFullStatus({brw => 1}),
437
#     {
438
#         biblionumber => ["Biblio Number", undef],
439
#         borrower => ["Borrower", Koha::Patrons->find($patron->{borrowernumber})],
440
#         id => ["Request Number", $rqBackendTest->illrequest_id],
441
#         prefix_id => ["Request Number", $rqBackendTest->illrequest_id],
442
#         reqtype => ["Request Type", "Book"],
443
#         status => ["Status", "REQREV"],
444
#         placement_date => ["Placement Date", $rqBackendTest->placed],
445
#         completion_date => ["Completion Date", $rqBackendTest->completed],
446
#         ts => ["Timestamp", $rqBackendTest->updated],
447
#         branch => ["Branch", $rqBackendTest->branch_id],
448
#     },
449
#     "getFullStatus: With Borrower."
450
# );
451
452
## Test available_backends
453
subtest 'available_backends' => sub {
454
    plan tests => 1;
455
456
    my $rq = Koha::Illrequest->new({
457
        borrower_id => $patron->{borrowernumber},
458
        branch_id   => $branch->{branchcode},
459
    })->store;
460
461
    my @backends = ();
462
    my $backenddir = $rq->_config->backend_dir;
463
    @backends = <$backenddir/*> if ( $backenddir );
464
    @backends = map { basename($_) } @backends;
465
    is_deeply(\@backends, $rq->available_backends,
466
              "Correctly identify available backends.");
467
468
};
469
470
## Test capabilities
471
472
my $rqCapTest = Koha::Illrequest->new({
473
    borrower_id => $patron->{borrowernumber},
474
    branch_id   => $branch->{branchcode},
475
})->store;
476
477
is( keys %{$rqCapTest->_core_status_graph},
478
    @{[ 'NEW', 'REQ', 'REVREQ', 'QUEUED', 'CANCREQ', 'COMP', 'KILL' ]},
479
    "Complete list of core statuses." );
480
481
my $union = $rqCapTest->_status_graph_union(
482
    $rqCapTest->_core_status_graph,
483
    {
484
        TEST => {
485
            prev_actions => [ 'COMP' ],
486
            id           => 'TEST',
487
            name         => "Test",
488
            ui_method_name => "Perform test",
489
            method         => 'test',
490
            next_actions   => [ 'NEW' ]
491
        },
492
        BLAH => {
493
            prev_actions => [ 'COMP' ],
494
            id           => 'BLAH',
495
            name         => "BLAH",
496
            ui_method_name => "Perform test",
497
            method         => 'test',
498
            next_actions   => [ 'NEW' ]
499
        },
500
    }
501
);
502
ok( ( grep 'BLAH', @{$union->{COMP}->{next_actions}} and
503
          grep 'TEST', @{$union->{COMP}->{next_actions}} ),
504
    "next_actions: updated." );
505
ok( ( grep 'BLAH', @{$union->{NEW}->{prev_actions}} and
506
          grep 'TEST', @{$union->{NEW}->{prev_actions}} ),
507
    "next_actions: updated." );
508
509
## Test available_backends
510
subtest 'available_actions' => sub {
511
    plan tests => 1;
512
513
    my $rq = Koha::Illrequest->new({
514
        borrower_id => $patron->{borrowernumber},
515
        branch_id   => $branch->{branchcode},
516
        status      => 'NEW',
517
    })->store;
518
519
    is_deeply(
520
        $rq->available_actions,
521
        [
522
            {
523
                prev_actions   => [ 'NEW', 'REQREV', 'QUEUED' ],
524
                id             => 'REQ',
525
                name           => 'Requested',
526
                ui_method_name => 'Create request',
527
                method         => 'confirm',
528
                next_actions   => [ 'REQREV' ],
529
            },
530
            {
531
                prev_actions   => [ 'CANCREQ', 'QUEUED', 'REQREV', 'NEW' ],
532
                id             => 'KILL',
533
                name           => 0,
534
                ui_method_name => 'Delete request',
535
                method         => 'delete',
536
                next_actions   => [ ],
537
            }
538
        ]
539
    );
540
};
541
542
$schema->storage->txn_rollback;
543
544
1;
(-)a/t/db_dependent/Illrequest/Dummy.t (-379 lines)
Lines 1-378 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 Koha::Database;
22
use Koha::Illrequests;
23
use t::lib::TestBuilder;
24
25
use Test::More tests => 15;
26
27
# This is a set of basic tests for the Dummy backend, largely to provide
28
# sanity checks for testing at the higher level Illrequest.pm level.
29
#
30
# The Dummy backend is rather simple, but provides correctly formatted
31
# response values, that other backends can model themselves after.
32
33
use_ok('Koha::Illrequest::Backend::Dummy');
34
35
my $backend = Koha::Illrequest::Backend::Dummy->new;
36
37
isa_ok($backend, 'Koha::Illrequest::Backend::Dummy');
38
39
40
my $schema = Koha::Database->new->schema;
41
$schema->storage->txn_begin;
42
43
my $builder = t::lib::TestBuilder->new;
44
45
my $patron = $builder->build({ source => 'Borrower' });
46
my $branch = $builder->build({ source => 'Branch' });
47
48
my $illRequest = $builder->build({
49
    source => 'Illrequest',
50
    value => {
51
        borrowernumber  => $patron->{borrowernumber},
52
        branch          => $branch->{branchcode},
53
    }
54
});
55
my $mock_request = Koha::Illrequests->find($illRequest->{illrequest_id});
56
$mock_request->_config->backend("Dummy");
57
$mock_request->_config->limits({ default => { count => -1 } });
58
59
# Test Create
60
my $rq = $backend->create({
61
    request    => $mock_request,
62
    method     => 'create',
63
    stage      => 'search_form',
64
    other      => undef,
65
});
66
67
is_deeply(
68
    $rq,
69
    {
70
        error   => 0,
71
        status  => '',
72
        message => '',
73
        method  => 'create',
74
        stage   => 'search_form',
75
        value   => {},
76
    },
77
    "Search_Form stage of create method."
78
);
79
80
$rq = $backend->create({
81
    request    => $mock_request,
82
    method     => 'create',
83
    stage      => 'search_results',
84
    other      => { search => "interlibrary loans" },
85
});
86
87
is_deeply(
88
    $rq,
89
    {
90
        error   => 0,
91
        status  => '',
92
        message => '',
93
        method  => 'create',
94
        stage   => 'search_results',
95
        value   => [
96
            {
97
                id     => 1234,
98
                title  => "Ordering ILLs using Koha",
99
                author => "A.N. Other",
100
            },
101
            {
102
                id     => 5678,
103
                title  => "Interlibrary loans in Koha",
104
                author => "A.N. Other",
105
            },
106
        ],
107
    },
108
    "Search_Results stage of create method."
109
);
110
111
$rq = $backend->create({
112
    request    => $mock_request,
113
    method     => 'create',
114
    stage      => 'commit',
115
    other      => {
116
        id     => 1234,
117
        title  => "Ordering ILLs using Koha",
118
        author => "A.N. Other",
119
    },
120
});
121
122
is_deeply(
123
    $rq,
124
    {
125
        error   => 0,
126
        status  => '',
127
        message => '',
128
        method  => 'create',
129
        stage   => 'commit',
130
        value   => {
131
            id     => 1234,
132
            title  => "Ordering ILLs using Koha",
133
            author => "A.N. Other"
134
        },
135
    },
136
    "Commit stage of create method."
137
);
138
139
$rq = $backend->create({
140
    request    => $mock_request,
141
    method     => 'create',
142
    stage      => 'unknown_stage',
143
    other      => {
144
        id     => 1234,
145
        title  => "Ordering ILLs using Koha",
146
        author => "A.N. Other",
147
    },
148
});
149
150
is_deeply(
151
    $rq,
152
    {
153
        error   => 1,
154
        status  => 'unknown_stage',
155
        message => '',
156
        method  => 'create',
157
        stage   => 'unknown_stage',
158
        value   => {},
159
    },
160
    "Commit stage of create method."
161
);
162
163
# Test Confirm
164
165
$rq = $backend->confirm({
166
    request    => $mock_request,
167
    other      => undef,
168
});
169
170
is_deeply(
171
    $rq,
172
    {
173
        error   => 0,
174
        status  => '',
175
        message => '',
176
        method  => 'confirm',
177
        stage   => 'commit',
178
        value   => {
179
            id     => 1234,
180
            title  => "Ordering ILLs using Koha",
181
            author => "A.N. Other",
182
            status => "On order",
183
            cost   => "30 GBP",
184
        },
185
    },
186
    "Basic confirm method."
187
);
188
189
# Test List
190
191
is_deeply(
192
    $backend->list,
193
    {
194
        error   => 0,
195
        status  => '',
196
        message => '',
197
        method  => 'list',
198
        stage   => 'commit',
199
        value   => {
200
            1 => {
201
                id     => 1234,
202
                title  => "Ordering ILLs using Koha",
203
                author => "A.N. Other",
204
                status => "On order",
205
                cost   => "30 GBP",
206
            },
207
        },
208
    },
209
    "Basic list method."
210
);
211
212
# Test Renew
213
214
is_deeply(
215
    $backend->renew({
216
        request    => $mock_request,
217
        other      => undef,
218
    }),
219
    {
220
        error   => 1,
221
        status  => 'not_renewed',
222
        message => 'Order not yet delivered.',
223
        method  => 'renew',
224
        stage   => 'commit',
225
        value   => {
226
            id     => 1234,
227
            title  => "Ordering ILLs using Koha",
228
            author => "A.N. Other",
229
            status => "On order",
230
        },
231
    },
232
    "Basic renew method."
233
);
234
235
Koha::Illrequestattributes->find({
236
    illrequest_id => $mock_request->illrequest_id,
237
    type          => "status"
238
})->set({ value => "Delivered" })->store;
239
240
is_deeply(
241
    $backend->renew({
242
        request    => $mock_request,
243
        other      => undef,
244
    }),
245
    {
246
        error   => 0,
247
        status  => '',
248
        message => '',
249
        method  => 'renew',
250
        stage   => 'commit',
251
        value   => {
252
            id     => 1234,
253
            title  => "Ordering ILLs using Koha",
254
            author => "A.N. Other",
255
            status => "Renewed",
256
        },
257
    },
258
    "Modified renew method."
259
);
260
261
# Test Update_Status
262
263
is_deeply(
264
    $backend->update_status({
265
        request    => $mock_request,
266
        other      => undef,
267
    }),
268
    {
269
        error   => 1,
270
        status  => 'failed_update_hook',
271
        message => 'Fake reason for failing to perform update operation.',
272
        method  => 'update_status',
273
        stage   => 'commit',
274
        value   => {
275
            id     => 1234,
276
            title  => "Ordering ILLs using Koha",
277
            author => "A.N. Other",
278
            status => "Delivered",
279
        },
280
    },
281
    "Basic update_status method."
282
);
283
284
# FIXME: Perhaps we should add a test checking for specific status code
285
# transitions.
286
287
# Test Cancel
288
289
is_deeply(
290
    $backend->cancel({
291
        request    => $mock_request,
292
        other      => undef,
293
    }),
294
    {
295
        error   => 0,
296
        status  => '',
297
        message => '',
298
        method  => 'cancel',
299
        stage   => 'commit',
300
        value   => {
301
            id     => 1234,
302
            title  => "Ordering ILLs using Koha",
303
            author => "A.N. Other",
304
            status => "Delivered",
305
        },
306
    },
307
    "Basic cancel method."
308
);
309
310
is_deeply(
311
    $backend->cancel({
312
        request    => $mock_request,
313
        other      => undef,
314
    }),
315
    {
316
        error   => 1,
317
        status  => 'unknown_request',
318
        message => 'Cannot cancel an unknown request.',
319
        method  => 'cancel',
320
        stage   => 'commit',
321
        value   => {
322
            id     => 1234,
323
            title  => "Ordering ILLs using Koha",
324
            author => "A.N. Other",
325
        },
326
    },
327
    "Attempt to cancel an unconfirmed request."
328
);
329
330
# Test Status
331
332
is_deeply(
333
    $backend->status({
334
        request    => $mock_request,
335
        other      => undef,
336
    }),
337
    {
338
        error   => 1,
339
        status  => 'unknown_request',
340
        message => 'Cannot query status of an unknown request.',
341
        method  => 'status',
342
        stage   => 'commit',
343
        value   => {
344
            id     => 1234,
345
            title  => "Ordering ILLs using Koha",
346
            author => "A.N. Other",
347
        },
348
    },
349
    "Attempt to get status of an unconfirmed request."
350
);
351
352
$rq = $backend->confirm({
353
    request    => $mock_request,
354
    other      => undef,
355
});
356
357
is_deeply(
358
    $backend->status({
359
        request    => $mock_request,
360
        other      => undef,
361
    }),
362
    {
363
        error   => 0,
364
        status  => '',
365
        message => '',
366
        method  => 'status',
367
        stage   => 'commit',
368
        value   => {
369
            id     => 1234,
370
            title  => "Ordering ILLs using Koha",
371
            author => "A.N. Other",
372
            status => "On order",
373
        },
374
    },
375
    "Basic status method."
376
);
377
378
1;
379
- 

Return to bug 7317