|
Line 0
Link Here
|
| 0 |
- |
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 => 10; |
| 12 |
|
| 13 |
use_ok('C4::Reserves'); |
| 14 |
|
| 15 |
my $schema = Koha::Database->new->schema; |
| 16 |
$schema->storage->txn_begin; |
| 17 |
|
| 18 |
my $dbh = C4::Context->dbh; |
| 19 |
$dbh->{AutoCommit} = 0; |
| 20 |
$dbh->{RaiseError} = 1; |
| 21 |
$dbh->do(q{DELETE FROM special_holidays}); |
| 22 |
|
| 23 |
my $builder = t::lib::TestBuilder->new(); |
| 24 |
|
| 25 |
# Category, branch and patrons |
| 26 |
$builder->build({ |
| 27 |
source => 'Category', |
| 28 |
value => { |
| 29 |
categorycode => 'XYZ1', |
| 30 |
}, |
| 31 |
}); |
| 32 |
$builder->build({ |
| 33 |
source => 'Branch', |
| 34 |
value => { |
| 35 |
branchcode => 'LIB1', |
| 36 |
}, |
| 37 |
}); |
| 38 |
|
| 39 |
$builder->build({ |
| 40 |
source => 'Branch', |
| 41 |
value => { |
| 42 |
branchcode => 'LIB2', |
| 43 |
}, |
| 44 |
}); |
| 45 |
|
| 46 |
my $patron1 = $builder->build({ |
| 47 |
source => 'Borrower', |
| 48 |
value => { |
| 49 |
categorycode => 'XYZ1', |
| 50 |
branchcode => 'LIB1', |
| 51 |
}, |
| 52 |
}); |
| 53 |
|
| 54 |
my $patron2 = $builder->build({ |
| 55 |
source => 'Borrower', |
| 56 |
value => { |
| 57 |
categorycode => 'XYZ1', |
| 58 |
branchcode => 'LIB2', |
| 59 |
}, |
| 60 |
}); |
| 61 |
|
| 62 |
my $biblio = $builder->build({ |
| 63 |
source => 'Biblio', |
| 64 |
value => { |
| 65 |
title => 'Title 1', }, |
| 66 |
}); |
| 67 |
|
| 68 |
my $biblio2 = $builder->build({ |
| 69 |
source => 'Biblio', |
| 70 |
value => { |
| 71 |
title => 'Title 2', }, |
| 72 |
}); |
| 73 |
|
| 74 |
my $biblio3 = $builder->build({ |
| 75 |
source => 'Biblio', |
| 76 |
value => { |
| 77 |
title => 'Title 3', }, |
| 78 |
}); |
| 79 |
|
| 80 |
my $item1 = $builder->build({ |
| 81 |
source => 'Item', |
| 82 |
value => { |
| 83 |
biblionumber => $biblio->{biblionumber}, |
| 84 |
}, |
| 85 |
}); |
| 86 |
|
| 87 |
my $item2 = $builder->build({ |
| 88 |
source => 'Item', |
| 89 |
value => { |
| 90 |
biblionumber => $biblio2->{biblionumber}, |
| 91 |
}, |
| 92 |
}); |
| 93 |
|
| 94 |
my $item3 = $builder->build({ |
| 95 |
source => 'Item', |
| 96 |
value => { |
| 97 |
biblionumber => $biblio3->{biblionumber}, |
| 98 |
}, |
| 99 |
}); |
| 100 |
|
| 101 |
my $today = dt_from_string(); |
| 102 |
|
| 103 |
my $reserve1_reservedate = $today->clone; |
| 104 |
$reserve1_reservedate->subtract(days => 20); |
| 105 |
|
| 106 |
my $reserve1_expirationdate = $today->clone; |
| 107 |
$reserve1_expirationdate->add(days => 6); |
| 108 |
|
| 109 |
my $reserve1 = $builder->build({ |
| 110 |
source => 'Reserve', |
| 111 |
value => { |
| 112 |
borrowernumber => $patron1->{borrowernumber}, |
| 113 |
reservedate => $reserve1_reservedate->ymd, |
| 114 |
expirationdate => undef, |
| 115 |
biblionumber => $biblio->{biblionumber}, |
| 116 |
branchcode => 'LIB1', |
| 117 |
priority => 1, |
| 118 |
found => '', |
| 119 |
}, |
| 120 |
}); |
| 121 |
|
| 122 |
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelay', 1); |
| 123 |
t::lib::Mocks::mock_preference('ReservesMaxPickUpDelay', 6); |
| 124 |
|
| 125 |
ModReserveAffect( $item1->{itemnumber}, $patron1->{borrowernumber}, 0); |
| 126 |
my $r = Koha::Holds->find($reserve1->{reserve_id}); |
| 127 |
|
| 128 |
is($r->waitingdate, $today->ymd, 'Waiting date should be set to today' ); |
| 129 |
is($r->expirationdate, $reserve1_expirationdate->ymd, 'Expiration date should be set to today + 6' ); |
| 130 |
is($r->found, 'W', 'Reserve status is now "waiting"' ); |
| 131 |
is($r->priority, 0, 'Priority should be 0' ); |
| 132 |
is($r->itemnumber, $item1->{itemnumber}, 'Item number should be set correctly' ); |
| 133 |
|
| 134 |
my $reserve2 = $builder->build({ |
| 135 |
source => 'Reserve', |
| 136 |
value => { |
| 137 |
borrowernumber => $patron2->{borrowernumber}, |
| 138 |
reservedate => $reserve1_reservedate->ymd, |
| 139 |
expirationdate => undef, |
| 140 |
biblionumber => $biblio2->{biblionumber}, |
| 141 |
branchcode => 'LIB1', |
| 142 |
priority => 1, |
| 143 |
found => '', |
| 144 |
}, |
| 145 |
}); |
| 146 |
|
| 147 |
ModReserveAffect( $item2->{itemnumber}, $patron2->{borrowernumber}, 1); |
| 148 |
my $r2 = Koha::Holds->find($reserve2->{reserve_id}); |
| 149 |
|
| 150 |
is($r2->found, 'T', '2nd reserve - Reserve status is now "To transfer"' ); |
| 151 |
is($r2->priority, 0, '2nd reserve - Priority should be 0' ); |
| 152 |
is($r2->itemnumber, $item2->{itemnumber}, '2nd reserve - Item number should be set correctly' ); |
| 153 |
|
| 154 |
my $reserve3 = $builder->build({ |
| 155 |
source => 'Reserve', |
| 156 |
value => { |
| 157 |
borrowernumber => $patron2->{borrowernumber}, |
| 158 |
reservedate => $reserve1_reservedate->ymd, |
| 159 |
expirationdate => undef, |
| 160 |
biblionumber => $biblio3->{biblionumber}, |
| 161 |
branchcode => 'LIB1', |
| 162 |
priority => 1, |
| 163 |
found => '', |
| 164 |
}, |
| 165 |
}); |
| 166 |
|
| 167 |
my $special_holiday1_dt = $today->clone; |
| 168 |
$special_holiday1_dt->add(days => 2); |
| 169 |
|
| 170 |
Koha::Cache->get_instance()->flush_all(); |
| 171 |
my $holiday = $builder->build({ |
| 172 |
source => 'SpecialHoliday', |
| 173 |
value => { |
| 174 |
branchcode => 'LIB1', |
| 175 |
day => $special_holiday1_dt->day, |
| 176 |
month => $special_holiday1_dt->month, |
| 177 |
year => $special_holiday1_dt->year, |
| 178 |
title => 'My special holiday', |
| 179 |
isexception => 0 |
| 180 |
}, |
| 181 |
}); |
| 182 |
|
| 183 |
my $special_holiday2_dt = $today->clone; |
| 184 |
$special_holiday2_dt->add(days => 4); |
| 185 |
|
| 186 |
my $holiday2 = $builder->build({ |
| 187 |
source => 'SpecialHoliday', |
| 188 |
value => { |
| 189 |
branchcode => 'LIB1', |
| 190 |
day => $special_holiday2_dt->day, |
| 191 |
month => $special_holiday2_dt->month, |
| 192 |
year => $special_holiday2_dt->year, |
| 193 |
title => 'My special holiday 2', |
| 194 |
isexception => 0 |
| 195 |
}, |
| 196 |
}); |
| 197 |
|
| 198 |
t::lib::Mocks::mock_preference('ExcludeHolidaysFromMaxPickUpDelay', 1); |
| 199 |
ModReserveAffect( $item3->{itemnumber}, $patron2->{borrowernumber}, 0); |
| 200 |
|
| 201 |
# Add 6 days of pickup delay + 1 day of holiday. |
| 202 |
my $expected_expiration = $today->clone; |
| 203 |
$expected_expiration->add(days => 8); |
| 204 |
|
| 205 |
my $r3 = Koha::Holds->find($reserve3->{reserve_id}); |
| 206 |
is($r3->expirationdate, $expected_expiration->ymd, 'Expiration date should be set to today + 7' ); |
| 207 |
|
| 208 |
$dbh->rollback; |