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

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

Return to bug 40866