3 # Copyright 2020 Koha Development team
5 # This file is part of Koha
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.
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.
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>.
22 use Test::More tests => 2;
30 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
37 subtest 'DB constraints' => sub {
40 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
41 my $item = $builder->build_sample_item;
43 branchcode => $patron->branchcode,
44 borrowernumber => $patron->borrowernumber,
45 biblionumber => $item->biblionumber,
47 title => "title for fee",
48 itemnumber => $item->itemnumber,
51 my $reserve_id = C4::Reserves::AddReserve($hold_info);
52 my $hold = Koha::Holds->find( $reserve_id );
55 eval { $hold->priority(undef)->store }
57 qr{.*DBD::mysql::st execute failed: Column 'priority' cannot be null.*},
58 'DBD should have raised an error about priority that cannot be null';
61 subtest 'cancel' => sub {
63 my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
64 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
65 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
67 biblionumber => $biblioitem->biblionumber,
68 biblioitemnumber => $biblioitem->biblioitemnumber,
69 homebranch => $library->branchcode,
70 holdingbranch => $library->branchcode,
71 itype => $itemtype->itemtype,
73 my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } );
74 my $manager = $builder->build_object({ class => "Koha::Patrons" });
75 t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
77 my ( @patrons, @holds );
78 for my $i ( 0 .. 2 ) {
79 my $priority = $i + 1;
80 my $patron = $builder->build_object(
82 class => 'Koha::Patrons',
83 value => { branchcode => $library->branchcode, }
86 my $reserve_id = C4::Reserves::AddReserve(
88 branchcode => $library->branchcode,
89 borrowernumber => $patron->borrowernumber,
90 biblionumber => $item->biblionumber,
91 priority => $priority,
92 title => "title for fee",
93 itemnumber => $item->itemnumber,
96 my $hold = Koha::Holds->find($reserve_id);
97 push @patrons, $patron;
101 # There are 3 holds on this records
103 Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
105 'There should have 3 holds placed on this biblio record' );
106 my $first_hold = $holds[0];
107 my $second_hold = $holds[1];
108 my $third_hold = $holds[2];
109 is( ref($second_hold), 'Koha::Hold',
110 'We should play with Koha::Hold objects' );
111 is( $second_hold->priority, 2,
112 'Second hold should have a priority set to 3' );
114 # Remove the second hold, only 2 should still exist in DB and priorities must have been updated
115 my $is_cancelled = $second_hold->cancel;
116 is( ref($is_cancelled), 'Koha::Hold',
117 'Koha::Hold->cancel should return the Koha::Hold (?)' )
118 ; # This is can reconsidered
119 is( $second_hold->in_storage, 0,
120 'The hold has been cancelled and does not longer exist in DB' );
122 Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
124 'a hold has been cancelled, there should have only 2 holds placed on this biblio record'
127 # discard_changes to refetch
128 is( $first_hold->discard_changes->priority, 1, 'First hold should still be first' );
129 is( $third_hold->discard_changes->priority, 2, 'Third hold should now be second' );
131 subtest 'charge_cancel_fee parameter' => sub {
133 my $patron_category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { reservefee => 0 } } );
134 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $patron_category->categorycode } });
135 is( $patron->account->balance, 0, 'A new patron does not have any charges' );
138 branchcode => $library->branchcode,
139 borrowernumber => $patron->borrowernumber,
140 biblionumber => $item->biblionumber,
142 title => "title for fee",
143 itemnumber => $item->itemnumber,
146 # First, test cancelling a reserve when there's no charge configured.
147 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
148 my $reserve_id = C4::Reserves::AddReserve( $hold_info );
149 Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
150 is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=0 - The patron should not have been charged' );
152 # Then, test cancelling a reserve when there's no charge desired.
153 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
154 $reserve_id = C4::Reserves::AddReserve( $hold_info );
155 Koha::Holds->find( $reserve_id )->cancel(); # charge_cancel_fee => 0
156 is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=42, but charge_cancel_fee => 0, The patron should not have been charged' );
159 # Finally, test cancelling a reserve when there's a charge desired and configured.
160 t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
161 $reserve_id = C4::Reserves::AddReserve( $hold_info );
162 Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
163 is( int($patron->account->balance), 42, 'ExpireReservesMaxPickUpDelayCharge=42 and charge_cancel_fee => 1, The patron should have been charged!' );
166 subtest 'waiting hold' => sub {
168 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
169 my $reserve_id = C4::Reserves::AddReserve(
171 branchcode => $library->branchcode,
172 borrowernumber => $patron->borrowernumber,
173 biblionumber => $item->biblionumber,
175 title => "title for fee",
176 itemnumber => $item->itemnumber,
180 Koha::Holds->find( $reserve_id )->cancel;
181 my $hold_old = Koha::Old::Holds->find( $reserve_id );
182 is( $hold_old->found, 'W', 'The found column should have been kept and a hold is cancelled' );
185 subtest 'HoldsLog' => sub {
187 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
189 branchcode => $library->branchcode,
190 borrowernumber => $patron->borrowernumber,
191 biblionumber => $item->biblionumber,
193 title => "title for fee",
194 itemnumber => $item->itemnumber,
197 t::lib::Mocks::mock_preference('HoldsLog', 0);
198 my $reserve_id = C4::Reserves::AddReserve($hold_info);
199 Koha::Holds->find( $reserve_id )->cancel;
200 my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'HOLDS', action => 'CANCEL', object => $reserve_id } )->count;
201 is( $number_of_logs, 0, 'Without HoldsLog, Koha::Hold->cancel should not have logged' );
203 t::lib::Mocks::mock_preference('HoldsLog', 1);
204 $reserve_id = C4::Reserves::AddReserve($hold_info);
205 Koha::Holds->find( $reserve_id )->cancel;
206 $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'HOLDS', action => 'CANCEL', object => $reserve_id } )->count;
207 is( $number_of_logs, 1, 'With HoldsLog, Koha::Hold->cancel should have logged' );
210 subtest 'rollback' => sub {
212 my $patron_category = $builder->build_object(
214 class => 'Koha::Patron::Categories',
215 value => { reservefee => 0 }
218 my $patron = $builder->build_object(
220 class => 'Koha::Patrons',
221 value => { categorycode => $patron_category->categorycode }
225 branchcode => $library->branchcode,
226 borrowernumber => $patron->borrowernumber,
227 biblionumber => $item->biblionumber,
229 title => "title for fee",
230 itemnumber => $item->itemnumber,
233 t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelayCharge',42 );
234 my $reserve_id = C4::Reserves::AddReserve($hold_info);
235 my $hold = Koha::Holds->find($reserve_id);
237 # Add a row with the same id to make the cancel fails
238 Koha::Old::Hold->new( $hold->unblessed )->store;
241 eval { $hold->cancel( { charge_cancel_fee => 1 } ) };
243 qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
244 'DBD should have raised an error about dup primary key';
246 $hold = Koha::Holds->find($reserve_id);
247 is( ref($hold), 'Koha::Hold', 'The hold should not have been deleted' );
248 is( $patron->account->balance, 0,
249 'If the hold has not been cancelled, the patron should not have been charged'
255 $schema->storage->txn_rollback;