79de7fea3a8cd1e23c434382b1d5a4f646ec3c11
[koha-ffzg.git] / t / db_dependent / Holds / CancelReserves.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Reserves;
6 use Koha::DateUtils;
7
8 use t::lib::Mocks;
9 use t::lib::TestBuilder;
10
11 use Test::More tests => 5;
12
13 use_ok('C4::Reserves');
14
15 my $schema  = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
17 my $builder = t::lib::TestBuilder->new();
18
19 t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 0);
20 # Waiting reservers could be canceled only if ExpireReservesMaxPickUpDelay is set to "allow", see bug 19260
21 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1);
22
23 my $today = dt_from_string();
24 my $reserve_reservedate = $today->clone;
25 $reserve_reservedate->subtract(days => 30);
26
27 my $reserve1_expirationdate = $today->clone;
28 $reserve1_expirationdate->add(days => 1);
29
30 # Reserve not expired
31 my $reserve1 = $builder->build({
32     source => 'Reserve',
33     value => {
34         reservedate => $reserve_reservedate,
35         expirationdate => $reserve1_expirationdate,
36         cancellationdate => undef,
37         priority => 0,
38         found => 'W',
39     },
40 });
41
42 CancelExpiredReserves();
43 my $r1 = Koha::Holds->find($reserve1->{reserve_id});
44 ok($r1, 'Reserve 1 should not be canceled.');
45
46 my $reserve2_expirationdate = $today->clone;
47 $reserve2_expirationdate->subtract(days => 1);
48
49 # Reserve expired
50 my $reserve2 = $builder->build({
51     source => 'Reserve',
52     value => {
53         reservedate => $reserve_reservedate,
54         expirationdate => $reserve2_expirationdate,
55         cancellationdate => undef,
56         priority => 0,
57         found => 'W',
58     },
59 });
60
61 CancelExpiredReserves();
62 my $r2 = Koha::Holds->find($reserve2->{reserve_id});
63 is($r2, undef,'reserve 2 should be canceled.');
64
65 # Reserve expired on holiday
66 my $reserve3 = $builder->build({
67     source => 'Reserve',
68     value => {
69         reservedate => $reserve_reservedate,
70         expirationdate => $reserve2_expirationdate,
71         branchcode => 'LIB1',
72         cancellationdate => undef,
73         priority => 0,
74         found => 'W',
75     },
76 });
77
78 Koha::Caches->get_instance()->flush_all();
79 my $holiday = $builder->build({
80     source => 'SpecialHoliday',
81     value => {
82         branchcode => 'LIB1',
83         day => $today->day,
84         month => $today->month,
85         year => $today->year,
86         title => 'My holiday',
87         isexception => 0
88     },
89 });
90
91 CancelExpiredReserves();
92 my $r3 = Koha::Holds->find($reserve3->{reserve_id});
93 ok($r3,'Reserve 3 should not be canceled.');
94
95 t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1);
96 CancelExpiredReserves();
97 $r3 = Koha::Holds->find($reserve3->{reserve_id});
98 is($r3, undef,'Reserve 3 should be canceled.');
99
100 $schema->storage->txn_rollback;