Bug 19437: (followup) Rearrange CancelExpiredReserves tests - fix typos
[koha.git] / t / db_dependent / Reserves / CancelExpiredReserves.t
blob5efd02dd35155684cc5971a64807fe072311f57a
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 2;
6 use t::lib::Mocks;
7 use t::lib::TestBuilder;
9 use C4::Members;
10 use C4::Reserves;
11 use Koha::Database;
12 use Koha::DateUtils;
13 use Koha::Holds;
15 my $schema = Koha::Database->new->schema;
16 $schema->storage->txn_begin;
18 subtest 'CancelExpiredReserves tests incl. holidays' => sub {
19 plan tests => 4;
21 my $builder = t::lib::TestBuilder->new();
23 t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 0);
24 # Waiting holds could be cancelled only if ExpireReservesMaxPickUpDelay is set to "allow", see bug 19260
25 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1);
28 my $today = dt_from_string();
29 my $reserve_reservedate = $today->clone;
30 $reserve_reservedate->subtract(days => 30);
32 my $reserve1_expirationdate = $today->clone;
33 $reserve1_expirationdate->add(days => 1);
35 # Reserve not expired
36 my $reserve1 = $builder->build({
37 source => 'Reserve',
38 value => {
39 reservedate => $reserve_reservedate,
40 expirationdate => $reserve1_expirationdate,
41 cancellationdate => undef,
42 priority => 0,
43 found => 'W',
45 });
47 CancelExpiredReserves();
48 my $r1 = Koha::Holds->find($reserve1->{reserve_id});
49 ok($r1, 'Reserve 1 should not be canceled.');
51 my $reserve2_expirationdate = $today->clone;
52 $reserve2_expirationdate->subtract(days => 1);
54 # Reserve expired
55 my $reserve2 = $builder->build({
56 source => 'Reserve',
57 value => {
58 reservedate => $reserve_reservedate,
59 expirationdate => $reserve2_expirationdate,
60 cancellationdate => undef,
61 priority => 0,
62 found => 'W',
64 });
66 CancelExpiredReserves();
67 my $r2 = Koha::Holds->find($reserve2->{reserve_id});
68 is($r2, undef,'reserve 2 should be canceled.');
70 # Reserve expired on holiday
71 my $reserve3 = $builder->build({
72 source => 'Reserve',
73 value => {
74 reservedate => $reserve_reservedate,
75 expirationdate => $reserve2_expirationdate,
76 branchcode => 'LIB1',
77 cancellationdate => undef,
78 priority => 0,
79 found => 'W',
81 });
83 Koha::Caches->get_instance()->flush_all();
84 my $holiday = $builder->build({
85 source => 'SpecialHoliday',
86 value => {
87 branchcode => 'LIB1',
88 day => $today->day,
89 month => $today->month,
90 year => $today->year,
91 title => 'My holiday',
92 isexception => 0
94 });
96 CancelExpiredReserves();
97 my $r3 = Koha::Holds->find($reserve3->{reserve_id});
98 ok($r3,'Reserve 3 should not be canceled.');
100 t::lib::Mocks::mock_preference('ExpireReservesOnHolidays', 1);
101 CancelExpiredReserves();
102 $r3 = Koha::Holds->find($reserve3->{reserve_id});
103 is($r3, undef,'Reserve 3 should be canceled.');
106 subtest 'Test handling of waiting reserves by CancelExpiredReserves' => sub {
107 plan tests => 2;
109 Koha::Holds->delete;
111 my $builder = t::lib::TestBuilder->new();
112 my $category = $builder->build({ source => 'Category' });
113 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
114 my $biblio = $builder->build({ source => 'Biblio' });
115 my $bibnum = $biblio->{biblionumber};
116 my $item = $builder->build({ source => 'Item', value => { biblionumber => $bibnum }});
117 my $itemnumber = $item->{itemnumber};
118 my $borrowernumber = $builder->build({ source => 'Borrower', value => { categorycode => $category->{categorycode}, branchcode => $branchcode }})->{borrowernumber};
120 my $resdate = dt_from_string->add( days => -20 );
121 my $expdate = dt_from_string->add( days => -2 );
122 my $notexpdate = dt_from_string->add( days => 2 );
124 my $hold1 = Koha::Hold->new({
125 branchcode => $branchcode,
126 borrowernumber => $borrowernumber,
127 biblionumber => $bibnum,
128 priority => 1,
129 reservedate => $resdate,
130 expirationdate => $notexpdate,
131 found => undef,
132 })->store;
134 my $hold2 = Koha::Hold->new({
135 branchcode => $branchcode,
136 borrowernumber => $borrowernumber,
137 biblionumber => $bibnum,
138 priority => 2,
139 reservedate => $resdate,
140 expirationdate => $expdate,
141 found => undef,
142 })->store;
144 my $hold3 = Koha::Hold->new({
145 branchcode => $branchcode,
146 borrowernumber => $borrowernumber,
147 biblionumber => $bibnum,
148 itemnumber => $itemnumber,
149 priority => 0,
150 reservedate => $resdate,
151 expirationdate => $expdate,
152 found => 'W',
153 })->store;
155 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 0 );
156 CancelExpiredReserves();
157 my $count1 = Koha::Holds->search->count;
158 is( $count1, 2, 'Only the non-waiting expired holds should be cancelled');
160 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 );
161 CancelExpiredReserves();
162 my $count2 = Koha::Holds->search->count;
163 is( $count2, 1, 'Also the waiting expired hold should be cancelled now');
166 $schema->storage->txn_rollback;