Bug 14334: Remove AutoCommit from tests
[koha.git] / t / db_dependent / Koha / Holds.t
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 use Test::Warn;
24
25 use C4::Reserves;
26 use Koha::Holds;
27 use Koha::Database;
28
29 use t::lib::Mocks;
30 use t::lib::TestBuilder;
31
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 my $builder = t::lib::TestBuilder->new;
36
37 subtest 'cancel' => sub {
38     plan tests => 12;
39     my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
40     my $library    = $builder->build_object( { class => 'Koha::Libraries' } );
41     my $itemtype   = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
42     my $item_info  = {
43         biblionumber     => $biblioitem->biblionumber,
44         biblioitemnumber => $biblioitem->biblioitemnumber,
45         homebranch       => $library->branchcode,
46         holdingbranch    => $library->branchcode,
47         itype            => $itemtype->itemtype,
48     };
49     my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } );
50
51     my ( @patrons, @holds );
52     for my $i ( 0 .. 2 ) {
53         my $priority = $i + 1;
54         my $patron   = $builder->build_object(
55             {
56                 class => 'Koha::Patrons',
57                 value => { branchcode => $library->branchcode, }
58             }
59         );
60         my $reserve_id = C4::Reserves::AddReserve(
61             $library->branchcode, $patron->borrowernumber,
62             $item->biblionumber,  '',
63             $priority,            undef,
64             undef,                '',
65             "title for fee",      $item->itemnumber,
66         );
67         my $hold = Koha::Holds->find($reserve_id);
68         push @patrons, $patron;
69         push @holds,   $hold;
70     }
71
72     # There are 3 holds on this records
73     my $nb_of_holds =
74       Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
75     is( $nb_of_holds, 3,
76         'There should have 3 holds placed on this biblio record' );
77     my $first_hold  = $holds[0];
78     my $second_hold = $holds[1];
79     my $third_hold  = $holds[2];
80     is( ref($second_hold), 'Koha::Hold',
81         'We should play with Koha::Hold objects' );
82     is( $second_hold->priority, 2,
83         'Second hold should have a priority set to 3' );
84
85     # Remove the second hold, only 2 should still exist in DB and priorities must have been updated
86     my $is_cancelled = $second_hold->cancel;
87     is( ref($is_cancelled), 'Koha::Hold',
88         'Koha::Hold->cancel should return the Koha::Hold (?)' )
89       ;    # This is can reconsidered
90     is( $second_hold->in_storage, 0,
91         'The hold has been cancelled and does not longer exist in DB' );
92     $nb_of_holds =
93       Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
94     is( $nb_of_holds, 2,
95         'a hold has been cancelled, there should have only 2 holds placed on this biblio record'
96     );
97
98     # discard_changes to refetch
99     is( $first_hold->discard_changes->priority, 1, 'First hold should still be first' );
100     is( $third_hold->discard_changes->priority, 2, 'Third hold should now be second' );
101
102     subtest 'charge_cancel_fee parameter' => sub {
103         plan tests => 4;
104         my $patron_category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { reservefee => 0 } } );
105         my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $patron_category->categorycode } });
106         is( $patron->account->balance, 0, 'A new patron does not have any charges' );
107
108         my @hold_info = (
109             $library->branchcode, $patron->borrowernumber,
110             $item->biblionumber,  '',
111             1,                    undef,
112             undef,                '',
113             "title for fee",      $item->itemnumber,
114         );
115
116         # First, test cancelling a reserve when there's no charge configured.
117         t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
118         my $reserve_id = C4::Reserves::AddReserve( @hold_info );
119         Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
120         is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=0 - The patron should not have been charged' );
121
122         # Then, test cancelling a reserve when there's no charge desired.
123         t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
124         $reserve_id = C4::Reserves::AddReserve( @hold_info );
125         Koha::Holds->find( $reserve_id )->cancel(); # charge_cancel_fee => 0
126         is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=42, but charge_cancel_fee => 0, The patron should not have been charged' );
127
128
129         # Finally, test cancelling a reserve when there's a charge desired and configured.
130         t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
131         $reserve_id = C4::Reserves::AddReserve( @hold_info );
132         Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
133         is( int($patron->account->balance), 42, 'ExpireReservesMaxPickUpDelayCharge=42 and charge_cancel_fee => 1, The patron should have been charged!' );
134     };
135
136     subtest 'waiting hold' => sub {
137         plan tests => 1;
138         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
139         my $reserve_id = C4::Reserves::AddReserve(
140             $library->branchcode, $patron->borrowernumber,
141             $item->biblionumber,  '',
142             1,                    undef,
143             undef,                '',
144             "title for fee",      $item->itemnumber,
145             'W',
146         );
147         Koha::Holds->find( $reserve_id )->cancel;
148         my $hold_old = Koha::Old::Holds->find( $reserve_id );
149         is( $hold_old->found, 'W', 'The found column should have been kept and a hold is cancelled' );
150     };
151
152     subtest 'HoldsLog' => sub {
153         plan tests => 2;
154         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
155         my @hold_info = (
156             $library->branchcode, $patron->borrowernumber,
157             $item->biblionumber,  '',
158             1,                    undef,
159             undef,                '',
160             "title for fee",      $item->itemnumber,
161         );
162
163         t::lib::Mocks::mock_preference('HoldsLog', 0);
164         my $reserve_id = C4::Reserves::AddReserve(@hold_info);
165         Koha::Holds->find( $reserve_id )->cancel;
166         my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'HOLDS', action => 'CANCEL', object => $reserve_id } )->count;
167         is( $number_of_logs, 0, 'Without HoldsLog, Koha::Hold->cancel should not have logged' );
168
169         t::lib::Mocks::mock_preference('HoldsLog', 1);
170         $reserve_id = C4::Reserves::AddReserve(@hold_info);
171         Koha::Holds->find( $reserve_id )->cancel;
172         $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'HOLDS', action => 'CANCEL', object => $reserve_id } )->count;
173         is( $number_of_logs, 1, 'With HoldsLog, Koha::Hold->cancel should have logged' );
174     };
175
176     subtest 'rollback' => sub {
177         plan tests => 3;
178         my $patron_category = $builder->build_object(
179             {
180                 class => 'Koha::Patron::Categories',
181                 value => { reservefee => 0 }
182             }
183         );
184         my $patron = $builder->build_object(
185             {
186                 class => 'Koha::Patrons',
187                 value => { categorycode => $patron_category->categorycode }
188             }
189         );
190         my @hold_info = (
191             $library->branchcode, $patron->borrowernumber,
192             $item->biblionumber,  '',
193             1,                    undef,
194             undef,                '',
195             "title for fee",      $item->itemnumber,
196         );
197
198         t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelayCharge',42 );
199         my $reserve_id = C4::Reserves::AddReserve(@hold_info);
200         my $hold       = Koha::Holds->find($reserve_id);
201
202         # Add a row with the same id to make the cancel fails
203         Koha::Old::Hold->new( $hold->unblessed )->store;
204
205         warning_like {
206             eval { $hold->cancel( { charge_cancel_fee => 1 } ) };
207         }
208         qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
209           'DBD should have raised an error about dup primary key';
210
211         $hold = Koha::Holds->find($reserve_id);
212         is( ref($hold), 'Koha::Hold', 'The hold should not have been deleted' );
213         is( $patron->account->balance, 0,
214 'If the hold has not been cancelled, the patron should not have been charged'
215         );
216     };
217
218 };
219
220 $schema->storage->txn_rollback;
221
222 1;