|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright 2017 Koha Development team |
| 4 |
# |
| 5 |
# This file is part of Koha |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 1; |
| 23 |
|
| 24 |
use C4::Reserves; |
| 25 |
use Koha::Holds; |
| 26 |
use Koha::Database; |
| 27 |
|
| 28 |
use t::lib::Mocks; |
| 29 |
use t::lib::TestBuilder; |
| 30 |
|
| 31 |
my $schema = Koha::Database->new->schema; |
| 32 |
$schema->storage->txn_begin; |
| 33 |
|
| 34 |
my $builder = t::lib::TestBuilder->new; |
| 35 |
|
| 36 |
subtest 'cancel' => sub { |
| 37 |
plan tests => 10; |
| 38 |
my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } ); |
| 39 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
| 40 |
my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } ); |
| 41 |
my $item_info = { |
| 42 |
biblionumber => $biblioitem->biblionumber, |
| 43 |
biblioitemnumber => $biblioitem->biblioitemnumber, |
| 44 |
homebranch => $library->branchcode, |
| 45 |
holdingbranch => $library->branchcode, |
| 46 |
itype => $itemtype->itemtype, |
| 47 |
}; |
| 48 |
my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } ); |
| 49 |
|
| 50 |
my ( @patrons, @holds ); |
| 51 |
for my $i ( 0 .. 2 ) { |
| 52 |
my $priority = $i + 1; |
| 53 |
my $patron = $builder->build_object( |
| 54 |
{ |
| 55 |
class => 'Koha::Patrons', |
| 56 |
value => { branchcode => $library->branchcode, } |
| 57 |
} |
| 58 |
); |
| 59 |
my $reserve_id = C4::Reserves::AddReserve( |
| 60 |
$library->branchcode, $patron->borrowernumber, |
| 61 |
$item->biblionumber, '', |
| 62 |
$priority, undef, |
| 63 |
undef, '', |
| 64 |
"title for fee", $item->itemnumber, |
| 65 |
); |
| 66 |
my $hold = Koha::Holds->find($reserve_id); |
| 67 |
push @patrons, $patron; |
| 68 |
push @holds, $hold; |
| 69 |
} |
| 70 |
|
| 71 |
# There are 3 holds on this records |
| 72 |
my $nb_of_holds = |
| 73 |
Koha::Holds->search( { biblionumber => $item->biblionumber } )->count; |
| 74 |
is( $nb_of_holds, 3, |
| 75 |
'There should have 3 holds placed on this biblio record' ); |
| 76 |
my $first_hold = $holds[0]; |
| 77 |
my $second_hold = $holds[1]; |
| 78 |
my $third_hold = $holds[2]; |
| 79 |
is( ref($second_hold), 'Koha::Hold', |
| 80 |
'We should play with Koha::Hold objects' ); |
| 81 |
is( $second_hold->priority, 2, |
| 82 |
'Second hold should have a priority set to 3' ); |
| 83 |
|
| 84 |
# Remove the second hold, only 2 should still exist in DB and priorities must have been updated |
| 85 |
my $is_cancelled = $second_hold->cancel; |
| 86 |
is( ref($is_cancelled), 'Koha::Hold', |
| 87 |
'Koha::Hold->cancel should return the Koha::Hold (?)' ) |
| 88 |
; # This is can reconsidered |
| 89 |
is( $second_hold->in_storage, 0, |
| 90 |
'The hold has been cancelled and does not longer exist in DB' ); |
| 91 |
$nb_of_holds = |
| 92 |
Koha::Holds->search( { biblionumber => $item->biblionumber } )->count; |
| 93 |
is( $nb_of_holds, 2, |
| 94 |
'a hold has been cancelled, there should have only 2 holds placed on this biblio record' |
| 95 |
); |
| 96 |
|
| 97 |
# discard_changes to refetch |
| 98 |
is( $first_hold->discard_changes->priority, 1, 'First hold should still be first' ); |
| 99 |
is( $third_hold->discard_changes->priority, 2, 'Third hold should now be second' ); |
| 100 |
|
| 101 |
subtest 'charge_cancel_fee parameter' => sub { |
| 102 |
plan tests => 4; |
| 103 |
my $patron_category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { reservefee => 0 } } ); |
| 104 |
my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $patron_category->categorycode } }); |
| 105 |
is( $patron->account->balance, 0, 'A new patron does not have any charges' ); |
| 106 |
|
| 107 |
my @hold_info = ( |
| 108 |
$library->branchcode, $patron->borrowernumber, |
| 109 |
$item->biblionumber, '', |
| 110 |
1, undef, |
| 111 |
undef, '', |
| 112 |
"title for fee", $item->itemnumber, |
| 113 |
); |
| 114 |
|
| 115 |
# First, test cancelling a reserve when there's no charge configured. |
| 116 |
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0); |
| 117 |
my $reserve_id = C4::Reserves::AddReserve( @hold_info ); |
| 118 |
Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } ); |
| 119 |
is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=0 - The patron should not have been charged' ); |
| 120 |
|
| 121 |
# Then, test cancelling a reserve when there's no charge desired. |
| 122 |
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42); |
| 123 |
$reserve_id = C4::Reserves::AddReserve( @hold_info ); |
| 124 |
Koha::Holds->find( $reserve_id )->cancel(); # charge_cancel_fee => 0 |
| 125 |
is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=42, but charge_cancel_fee => 0, The patron should not have been charged' ); |
| 126 |
|
| 127 |
|
| 128 |
# Finally, test cancelling a reserve when there's a charge desired and configured. |
| 129 |
t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42); |
| 130 |
$reserve_id = C4::Reserves::AddReserve( @hold_info ); |
| 131 |
Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } ); |
| 132 |
is( int($patron->account->balance), 42, 'ExpireReservesMaxPickUpDelayCharge=42 and charge_cancel_fee => 1, The patron should have been charged!' ); |
| 133 |
}; |
| 134 |
|
| 135 |
subtest 'waiting hold' => sub { |
| 136 |
plan tests => 1; |
| 137 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 138 |
my $reserve_id = C4::Reserves::AddReserve( |
| 139 |
$library->branchcode, $patron->borrowernumber, |
| 140 |
$item->biblionumber, '', |
| 141 |
1, undef, |
| 142 |
undef, '', |
| 143 |
"title for fee", $item->itemnumber, |
| 144 |
'W', |
| 145 |
); |
| 146 |
Koha::Holds->find( $reserve_id )->cancel; |
| 147 |
my $hold_old = Koha::Old::Holds->find( $reserve_id ); |
| 148 |
is( $hold_old->found, 'W', 'The found column should have been kept and a hold is cancelled' ); |
| 149 |
}; |
| 150 |
}; |
| 151 |
|
| 152 |
$schema->storage->txn_rollback; |
| 153 |
|
| 154 |
1; |