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

(-)a/t/db_dependent/Circulation/CanBookBeIssued.t (-1 / +398 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
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 9;
21
22
use DateTime;
23
use C4::Circulation qw( CanBookBeIssued AddIssue CheckValidBarcode AddReturn );
24
use C4::Biblio qw( AddBiblio );
25
use C4::Items;
26
use Koha::Database;
27
use Koha::Library;
28
use Koha::CirculationRules;
29
use Koha::DateUtils qw( dt_from_string );
30
use t::lib::TestBuilder;
31
use t::lib::Mocks;
32
33
my $builder = t::lib::TestBuilder->new;
34
35
sub set_userenv {
36
    my ($library) = @_;
37
    my $staff = $builder->build_object( { class => "Koha::Patrons" } );
38
    t::lib::Mocks::mock_userenv( { patron => $staff, branchcode => $library->{branchcode} } );
39
}
40
41
my $schema = Koha::Database->schema;
42
$schema->storage->txn_begin;
43
my $dbh = C4::Context->dbh;
44
45
# Start with a clean slate
46
$dbh->do(q|DELETE FROM issues|);
47
$dbh->do(q|DELETE FROM items|);
48
$dbh->do(q|DELETE FROM borrowers|);
49
$dbh->do(q|DELETE FROM biblio|);
50
$dbh->do(q|DELETE FROM action_logs|);
51
52
my $now            = dt_from_string();
53
my $two_days_later = $now->clone->add( days => 2 );
54
my $formatted_date = $two_days_later->ymd('');
55
56
my $library = $builder->build(
57
    {
58
        source => 'Branch',
59
    }
60
);
61
62
my $patron_category = $builder->build(
63
    {
64
        source => 'Category',
65
        value  => {
66
            category_type => 'P',
67
        }
68
    }
69
);
70
71
my $patron1 = $builder->build_object(
72
    { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
73
74
my $biblio = $builder->build_sample_biblio(
75
    {
76
        branchcode => $library->{branchcode},
77
    }
78
);
79
80
my $biblionumber = $biblio->biblionumber;
81
82
set_userenv($library);
83
84
my ( $error, $question, $alerts, $messages, $message_log );
85
86
#Test check out an item that has a “not for loan” status
87
t::lib::Mocks::mock_preference( 'AllowNotForLoanOverride', 1 );
88
89
my $item_1 = $builder->build_sample_item(
90
    {
91
        library      => $library->{branchcode},
92
        biblionumber => $biblionumber,
93
        notforloan   => 1,
94
    }
95
);
96
97
( $error, $question, $alerts, $messages, $message_log ) = CanBookBeIssued(
98
    $patron1,
99
    $item_1->barcode,
100
    undef,
101
    undef,
102
    undef,
103
    {
104
        issueconfirmed => 1,
105
    }
106
);
107
is( $message_log->[0], 'item not for loan', 'Item not for loan message displayed' );
108
109
#Test check out an item that has a “lost” status
110
t::lib::Mocks::mock_preference( 'IssueLostItem', 'require confirmation' );
111
112
my $item_2 = $builder->build_sample_item(
113
    {
114
        library      => $library->{branchcode},
115
        biblionumber => $biblionumber,
116
        itemlost     => 1,
117
    }
118
);
119
120
( $error, $question, $alerts, $messages, $message_log ) = CanBookBeIssued(
121
    $patron1,
122
    $item_2->barcode,
123
    undef,
124
    undef,
125
    undef,
126
    {
127
        issueconfirmed => 1,
128
    }
129
);
130
is( $message_log->[0], 'item lost', 'Item lost message displayed' );
131
132
#Test check out an item to a patron who has reached the checkout limit
133
t::lib::Mocks::mock_preference( 'AllowTooManyOverride', 1 );
134
135
Koha::CirculationRules->set_rules(
136
    {
137
        categorycode => undef,
138
        branchcode   => undef,
139
        itemtype     => undef,
140
        rules        => {
141
            maxissueqty         => 0,
142
            reservesallowed     => 25,
143
            issuelength         => 14,
144
            lengthunit          => 'days',
145
            renewalsallowed     => 1,
146
            renewalperiod       => 7,
147
            norenewalbefore     => undef,
148
            noautorenewalbefore => undef,
149
            auto_renew          => 0,
150
            fine                => .10,
151
            chargeperiod        => 1,
152
        }
153
    }
154
);
155
156
my $item_3 = $builder->build_sample_item(
157
    {
158
        library      => $library->{branchcode},
159
        biblionumber => $biblionumber,
160
    }
161
);
162
163
( $error, $question, $alerts, $messages, $message_log ) = CanBookBeIssued(
164
    $patron1,
165
    $item_3->barcode,
166
    undef,
167
    undef,
168
    undef,
169
    {
170
        issueconfirmed => 1,
171
    }
172
);
173
is( $message_log->[0], 'too many checkout', 'Checkout limit reached message displayed' );
174
175
#Test check out an item to a patron who has unpaid fines
176
t::lib::Mocks::mock_preference( 'AllFinesNeedOverride', 0 );
177
t::lib::Mocks::mock_preference( 'AllowFineOverride',    1 );
178
t::lib::Mocks::mock_preference( 'noissuescharge',       5 );
179
180
$dbh->do(q|DELETE FROM circulation_rules|);
181
182
Koha::CirculationRules->set_rules(
183
    {
184
        categorycode => undef,
185
        branchcode   => undef,
186
        itemtype     => undef,
187
        rules        => {
188
            maxissueqty         => 5,
189
            reservesallowed     => 25,
190
            issuelength         => 14,
191
            lengthunit          => 'days',
192
            renewalsallowed     => 1,
193
            renewalperiod       => 7,
194
            norenewalbefore     => undef,
195
            noautorenewalbefore => undef,
196
            auto_renew          => 0,
197
            fine                => .10,
198
            chargeperiod        => 1,
199
        }
200
    }
201
);
202
203
my $patron2 = $builder->build_object(
204
    { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
205
206
$patron2->account->add_debit(
207
    {
208
        amount    => 10,
209
        interface => C4::Context->interface,
210
        type      => 'ARTICLE_REQUEST',
211
    }
212
);
213
214
my $item_4 = $builder->build_sample_item(
215
    {
216
        library      => $library->{branchcode},
217
        biblionumber => $biblionumber,
218
    }
219
);
220
221
( $error, $question, $alerts, $messages, $message_log ) = CanBookBeIssued(
222
    $patron2,
223
    $item_4->barcode,
224
    undef,
225
    undef,
226
    undef,
227
    {
228
        issueconfirmed => 1,
229
    }
230
);
231
is( $message_log->[0], 'borrower had amend', 'Borrower had amend message displayed' );
232
233
#Test check out an item on hold for someone else
234
my $patron3 = $builder->build_object(
235
    { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
236
237
my $item_5 = $builder->build_sample_item(
238
    {
239
        library      => $library->{branchcode},
240
        biblionumber => $biblionumber,
241
    }
242
);
243
244
C4::Reserves::AddReserve(
245
    {
246
        borrowernumber => $patron3->borrowernumber,
247
        biblionumber   => $biblionumber,
248
        itemnumber     => $item_5->itemnumber,
249
        branchcode     => $library->{branchcode},
250
    }
251
);
252
253
( $error, $question, $alerts, $messages, $message_log ) = CanBookBeIssued(
254
    $patron1,
255
    $item_5->barcode,
256
    undef,
257
    undef,
258
    undef,
259
    {
260
        issueconfirmed => 1,
261
    }
262
);
263
is( $message_log->[0], 'item is on reserve for someone else', 'Item on hold by someone else message displayed' );
264
265
#Test check out an item that is age-restricted
266
t::lib::Mocks::mock_preference( 'AgeRestrictionOverride', 1 );
267
t::lib::Mocks::mock_preference( 'AgeRestrictionMarker',   'PEGI' );
268
269
my $patron4 = $builder->build_object(
270
    {
271
        class => 'Koha::Patrons',
272
        value => {
273
            categorycode => 'K',
274
            dateofbirth  => $formatted_date,
275
        }
276
    }
277
);
278
279
my $item_6 = $builder->build_sample_item(
280
    {
281
        library      => $library->{branchcode},
282
        biblionumber => $biblionumber,
283
    }
284
);
285
286
my $biblioitem = $biblio->biblioitem;
287
$biblioitem->agerestriction('PEGI 6');
288
$biblioitem->update;
289
290
( $error, $question, $alerts, $messages, $message_log ) = CanBookBeIssued(
291
    $patron4,
292
    $item_6->barcode,
293
    undef,
294
    undef,
295
    undef,
296
    {
297
        issueconfirmed => 1,
298
    }
299
);
300
is( $message_log->[0], 'age restriction', 'Item is age restricted message displayed' );
301
302
#Test check out an item already checked out to someone else
303
t::lib::Mocks::mock_preference( 'AutoReturnCheckedOutItems', 0 );
304
305
my $patron5 = $builder->build_object(
306
    { class => 'Koha::Patrons', value => { categorycode => $patron_category->{categorycode} } } );
307
308
my $item_7 = $builder->build_sample_item(
309
    {
310
        biblionumber => $biblionumber,
311
        homebranch   => $patron5->branchcode
312
    }
313
);
314
315
AddIssue( $patron5, $item_7->barcode );
316
317
( $error, $question, $alerts, $messages, $message_log ) = CanBookBeIssued(
318
    $patron1,
319
    $item_7->barcode,
320
    undef,
321
    undef,
322
    undef,
323
    {
324
        issueconfirmed => 1,
325
    }
326
);
327
is( $message_log->[0], 'item is checked out for someone else', 'Item already checked out message displayed' );
328
329
#Test check out to a patron who has restrictions
330
my $patron6 = $builder->build_object(
331
    {
332
        class => 'Koha::Patrons',
333
        value => {
334
            categorycode => $patron_category->{categorycode},
335
            debarred     => $formatted_date,
336
        }
337
    }
338
);
339
340
my $item_8 = $builder->build_sample_item(
341
    {
342
        library      => $library->{branchcode},
343
        biblionumber => $biblionumber,
344
    }
345
);
346
347
( $error, $question, $alerts, $messages, $message_log ) = CanBookBeIssued(
348
    $patron6,
349
    $item_8->barcode,
350
    undef,
351
    undef,
352
    undef,
353
    {
354
        issueconfirmed => 1,
355
    }
356
);
357
is( $message_log->[0], 'borrower is restricted', 'Borrower is restricted message displayed' );
358
359
#Test check out an item to a patron who is not allowed to borrow this item type
360
Koha::CirculationRules->set_rules(
361
    {
362
        categorycode => undef,
363
        branchcode   => undef,
364
        itemtype     => 'BK',
365
        rules        => {
366
            maxissueqty         => 5,
367
            reservesallowed     => 25,
368
            issuelength         => 14,
369
            lengthunit          => 'days',
370
            renewalsallowed     => 1,
371
            renewalperiod       => 7,
372
            norenewalbefore     => undef,
373
            noautorenewalbefore => undef,
374
            auto_renew          => 0,
375
            fine                => .10,
376
            chargeperiod        => 1,
377
        }
378
    }
379
);
380
381
my $item_9 = $builder->build_sample_item(
382
    {
383
        library      => $library->{branchcode},
384
        biblionumber => $biblionumber,
385
    }
386
);
387
388
( $error, $question, $alerts, $messages, $message_log ) = CanBookBeIssued(
389
    $patron6,
390
    $item_9->barcode,
391
    undef,
392
    undef,
393
    undef,
394
    {
395
        issueconfirmed => 1,
396
    }
397
);
398
is( $message_log->[0], 'borrower is restricted', 'Borrower is restricted for this item type message displayed' );

Return to bug 9762