Bug 24722: Add test
[srvgit] / t / db_dependent / Koha / Holds.t
1 #!/usr/bin/perl
2
3 # Copyright 2020 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 => 2;
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 'DB constraints' => sub {
38     plan tests => 1;
39
40     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
41     my $item = $builder->build_sample_item;
42     my $hold_info = {
43         branchcode     => $patron->branchcode,
44         borrowernumber => $patron->borrowernumber,
45         biblionumber   => $item->biblionumber,
46         priority       => 1,
47         title          => "title for fee",
48         itemnumber     => $item->itemnumber,
49     };
50
51     my $reserve_id = C4::Reserves::AddReserve($hold_info);
52     my $hold = Koha::Holds->find( $reserve_id );
53
54     warning_like {
55         eval { $hold->priority(undef)->store }
56     }
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';
59 };
60
61 subtest 'cancel' => sub {
62     plan tests => 12;
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 } } );
66     my $item_info  = {
67         biblionumber     => $biblioitem->biblionumber,
68         biblioitemnumber => $biblioitem->biblioitemnumber,
69         homebranch       => $library->branchcode,
70         holdingbranch    => $library->branchcode,
71         itype            => $itemtype->itemtype,
72     };
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 });
76
77     my ( @patrons, @holds );
78     for my $i ( 0 .. 2 ) {
79         my $priority = $i + 1;
80         my $patron   = $builder->build_object(
81             {
82                 class => 'Koha::Patrons',
83                 value => { branchcode => $library->branchcode, }
84             }
85         );
86         my $reserve_id = C4::Reserves::AddReserve(
87             {
88                 branchcode     => $library->branchcode,
89                 borrowernumber => $patron->borrowernumber,
90                 biblionumber   => $item->biblionumber,
91                 priority       => $priority,
92                 title          => "title for fee",
93                 itemnumber     => $item->itemnumber,
94             }
95         );
96         my $hold = Koha::Holds->find($reserve_id);
97         push @patrons, $patron;
98         push @holds,   $hold;
99     }
100
101     # There are 3 holds on this records
102     my $nb_of_holds =
103       Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
104     is( $nb_of_holds, 3,
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' );
113
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' );
121     $nb_of_holds =
122       Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
123     is( $nb_of_holds, 2,
124         'a hold has been cancelled, there should have only 2 holds placed on this biblio record'
125     );
126
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' );
130
131     subtest 'charge_cancel_fee parameter' => sub {
132         plan tests => 4;
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' );
136
137         my $hold_info = {
138             branchcode     => $library->branchcode,
139             borrowernumber => $patron->borrowernumber,
140             biblionumber   => $item->biblionumber,
141             priority       => 1,
142             title          => "title for fee",
143             itemnumber     => $item->itemnumber,
144         };
145
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' );
151
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' );
157
158
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!' );
164     };
165
166     subtest 'waiting hold' => sub {
167         plan tests => 1;
168         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
169         my $reserve_id = C4::Reserves::AddReserve(
170             {
171                 branchcode     => $library->branchcode,
172                 borrowernumber => $patron->borrowernumber,
173                 biblionumber   => $item->biblionumber,
174                 priority       => 1,
175                 title          => "title for fee",
176                 itemnumber     => $item->itemnumber,
177                 found          => 'W',
178             }
179         );
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' );
183     };
184
185     subtest 'HoldsLog' => sub {
186         plan tests => 2;
187         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
188         my $hold_info = {
189             branchcode     => $library->branchcode,
190             borrowernumber => $patron->borrowernumber,
191             biblionumber   => $item->biblionumber,
192             priority       => 1,
193             title          => "title for fee",
194             itemnumber     => $item->itemnumber,
195         };
196
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' );
202
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' );
208     };
209
210     subtest 'rollback' => sub {
211         plan tests => 3;
212         my $patron_category = $builder->build_object(
213             {
214                 class => 'Koha::Patron::Categories',
215                 value => { reservefee => 0 }
216             }
217         );
218         my $patron = $builder->build_object(
219             {
220                 class => 'Koha::Patrons',
221                 value => { categorycode => $patron_category->categorycode }
222             }
223         );
224         my $hold_info = {
225             branchcode     => $library->branchcode,
226             borrowernumber => $patron->borrowernumber,
227             biblionumber   => $item->biblionumber,
228             priority       => 1,
229             title          => "title for fee",
230             itemnumber     => $item->itemnumber,
231         };
232
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);
236
237         # Add a row with the same id to make the cancel fails
238         Koha::Old::Hold->new( $hold->unblessed )->store;
239
240         warning_like {
241             eval { $hold->cancel( { charge_cancel_fee => 1 } ) };
242         }
243         qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
244           'DBD should have raised an error about dup primary key';
245
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'
250         );
251     };
252
253 };
254
255 $schema->storage->txn_rollback;
256
257 1;