Bug 26384: Fix executable flags
[koha.git] / 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 => 4;
23 use Test::Warn;
24
25 use C4::Reserves;
26 use Koha::AuthorisedValueCategory;
27 use Koha::Database;
28 use Koha::Holds;
29
30 use t::lib::Mocks;
31 use t::lib::TestBuilder;
32
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
35
36 my $builder = t::lib::TestBuilder->new;
37
38 subtest 'DB constraints' => sub {
39     plan tests => 1;
40
41     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
42     my $item = $builder->build_sample_item;
43     my $hold_info = {
44         branchcode     => $patron->branchcode,
45         borrowernumber => $patron->borrowernumber,
46         biblionumber   => $item->biblionumber,
47         priority       => 1,
48         title          => "title for fee",
49         itemnumber     => $item->itemnumber,
50     };
51
52     my $reserve_id = C4::Reserves::AddReserve($hold_info);
53     my $hold = Koha::Holds->find( $reserve_id );
54
55     warning_like {
56         eval { $hold->priority(undef)->store }
57     }
58     qr{.*DBD::mysql::st execute failed: Column 'priority' cannot be null.*},
59       'DBD should have raised an error about priority that cannot be null';
60 };
61
62 subtest 'cancel' => sub {
63     plan tests => 12;
64     my $biblioitem = $builder->build_object( { class => 'Koha::Biblioitems' } );
65     my $library    = $builder->build_object( { class => 'Koha::Libraries' } );
66     my $itemtype   = $builder->build_object( { class => 'Koha::ItemTypes', value => { rentalcharge => 0 } } );
67     my $item_info  = {
68         biblionumber     => $biblioitem->biblionumber,
69         biblioitemnumber => $biblioitem->biblioitemnumber,
70         homebranch       => $library->branchcode,
71         holdingbranch    => $library->branchcode,
72         itype            => $itemtype->itemtype,
73     };
74     my $item = $builder->build_object( { class => 'Koha::Items', value => $item_info } );
75     my $manager = $builder->build_object({ class => "Koha::Patrons" });
76     t::lib::Mocks::mock_userenv({ patron => $manager,branchcode => $manager->branchcode });
77
78     my ( @patrons, @holds );
79     for my $i ( 0 .. 2 ) {
80         my $priority = $i + 1;
81         my $patron   = $builder->build_object(
82             {
83                 class => 'Koha::Patrons',
84                 value => { branchcode => $library->branchcode, }
85             }
86         );
87         my $reserve_id = C4::Reserves::AddReserve(
88             {
89                 branchcode     => $library->branchcode,
90                 borrowernumber => $patron->borrowernumber,
91                 biblionumber   => $item->biblionumber,
92                 priority       => $priority,
93                 title          => "title for fee",
94                 itemnumber     => $item->itemnumber,
95             }
96         );
97         my $hold = Koha::Holds->find($reserve_id);
98         push @patrons, $patron;
99         push @holds,   $hold;
100     }
101
102     # There are 3 holds on this records
103     my $nb_of_holds =
104       Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
105     is( $nb_of_holds, 3,
106         'There should have 3 holds placed on this biblio record' );
107     my $first_hold  = $holds[0];
108     my $second_hold = $holds[1];
109     my $third_hold  = $holds[2];
110     is( ref($second_hold), 'Koha::Hold',
111         'We should play with Koha::Hold objects' );
112     is( $second_hold->priority, 2,
113         'Second hold should have a priority set to 3' );
114
115     # Remove the second hold, only 2 should still exist in DB and priorities must have been updated
116     my $is_cancelled = $second_hold->cancel;
117     is( ref($is_cancelled), 'Koha::Hold',
118         'Koha::Hold->cancel should return the Koha::Hold (?)' )
119       ;    # This is can reconsidered
120     is( $second_hold->in_storage, 0,
121         'The hold has been cancelled and does not longer exist in DB' );
122     $nb_of_holds =
123       Koha::Holds->search( { biblionumber => $item->biblionumber } )->count;
124     is( $nb_of_holds, 2,
125         'a hold has been cancelled, there should have only 2 holds placed on this biblio record'
126     );
127
128     # discard_changes to refetch
129     is( $first_hold->discard_changes->priority, 1, 'First hold should still be first' );
130     is( $third_hold->discard_changes->priority, 2, 'Third hold should now be second' );
131
132     subtest 'charge_cancel_fee parameter' => sub {
133         plan tests => 4;
134         my $patron_category = $builder->build_object({ class => 'Koha::Patron::Categories', value => { reservefee => 0 } } );
135         my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $patron_category->categorycode } });
136         is( $patron->account->balance, 0, 'A new patron does not have any charges' );
137
138         my $hold_info = {
139             branchcode     => $library->branchcode,
140             borrowernumber => $patron->borrowernumber,
141             biblionumber   => $item->biblionumber,
142             priority       => 1,
143             title          => "title for fee",
144             itemnumber     => $item->itemnumber,
145         };
146
147         # First, test cancelling a reserve when there's no charge configured.
148         t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 0);
149         my $reserve_id = C4::Reserves::AddReserve( $hold_info );
150         Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
151         is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=0 - The patron should not have been charged' );
152
153         # Then, test cancelling a reserve when there's no charge desired.
154         t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
155         $reserve_id = C4::Reserves::AddReserve( $hold_info );
156         Koha::Holds->find( $reserve_id )->cancel(); # charge_cancel_fee => 0
157         is( $patron->account->balance, 0, 'ExpireReservesMaxPickUpDelayCharge=42, but charge_cancel_fee => 0, The patron should not have been charged' );
158
159
160         # Finally, test cancelling a reserve when there's a charge desired and configured.
161         t::lib::Mocks::mock_preference('ExpireReservesMaxPickUpDelayCharge', 42);
162         $reserve_id = C4::Reserves::AddReserve( $hold_info );
163         Koha::Holds->find( $reserve_id )->cancel( { charge_cancel_fee => 1 } );
164         is( int($patron->account->balance), 42, 'ExpireReservesMaxPickUpDelayCharge=42 and charge_cancel_fee => 1, The patron should have been charged!' );
165     };
166
167     subtest 'waiting hold' => sub {
168         plan tests => 1;
169         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
170         my $reserve_id = C4::Reserves::AddReserve(
171             {
172                 branchcode     => $library->branchcode,
173                 borrowernumber => $patron->borrowernumber,
174                 biblionumber   => $item->biblionumber,
175                 priority       => 1,
176                 title          => "title for fee",
177                 itemnumber     => $item->itemnumber,
178                 found          => 'W',
179             }
180         );
181         Koha::Holds->find( $reserve_id )->cancel;
182         my $hold_old = Koha::Old::Holds->find( $reserve_id );
183         is( $hold_old->found, 'W', 'The found column should have been kept and a hold is cancelled' );
184     };
185
186     subtest 'HoldsLog' => sub {
187         plan tests => 2;
188         my $patron = $builder->build_object({ class => 'Koha::Patrons' });
189         my $hold_info = {
190             branchcode     => $library->branchcode,
191             borrowernumber => $patron->borrowernumber,
192             biblionumber   => $item->biblionumber,
193             priority       => 1,
194             title          => "title for fee",
195             itemnumber     => $item->itemnumber,
196         };
197
198         t::lib::Mocks::mock_preference('HoldsLog', 0);
199         my $reserve_id = C4::Reserves::AddReserve($hold_info);
200         Koha::Holds->find( $reserve_id )->cancel;
201         my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'HOLDS', action => 'CANCEL', object => $reserve_id } )->count;
202         is( $number_of_logs, 0, 'Without HoldsLog, Koha::Hold->cancel should not have logged' );
203
204         t::lib::Mocks::mock_preference('HoldsLog', 1);
205         $reserve_id = C4::Reserves::AddReserve($hold_info);
206         Koha::Holds->find( $reserve_id )->cancel;
207         $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'HOLDS', action => 'CANCEL', object => $reserve_id } )->count;
208         is( $number_of_logs, 1, 'With HoldsLog, Koha::Hold->cancel should have logged' );
209     };
210
211     subtest 'rollback' => sub {
212         plan tests => 3;
213         my $patron_category = $builder->build_object(
214             {
215                 class => 'Koha::Patron::Categories',
216                 value => { reservefee => 0 }
217             }
218         );
219         my $patron = $builder->build_object(
220             {
221                 class => 'Koha::Patrons',
222                 value => { categorycode => $patron_category->categorycode }
223             }
224         );
225         my $hold_info = {
226             branchcode     => $library->branchcode,
227             borrowernumber => $patron->borrowernumber,
228             biblionumber   => $item->biblionumber,
229             priority       => 1,
230             title          => "title for fee",
231             itemnumber     => $item->itemnumber,
232         };
233
234         t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelayCharge',42 );
235         my $reserve_id = C4::Reserves::AddReserve($hold_info);
236         my $hold       = Koha::Holds->find($reserve_id);
237
238         # Add a row with the same id to make the cancel fails
239         Koha::Old::Hold->new( $hold->unblessed )->store;
240
241         warning_like {
242             eval { $hold->cancel( { charge_cancel_fee => 1 } ) };
243         }
244         qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
245           'DBD should have raised an error about dup primary key';
246
247         $hold = Koha::Holds->find($reserve_id);
248         is( ref($hold), 'Koha::Hold', 'The hold should not have been deleted' );
249         is( $patron->account->balance, 0,
250 'If the hold has not been cancelled, the patron should not have been charged'
251         );
252     };
253
254 };
255
256 subtest 'cancel with reason' => sub {
257     plan tests => 7;
258     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
259     my $item = $builder->build_sample_item({ library => $library->branchcode });
260     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
261     t::lib::Mocks::mock_userenv( { patron => $manager, branchcode => $manager->branchcode } );
262
263     my $patron = $builder->build_object(
264         {
265             class => 'Koha::Patrons',
266             value => { branchcode => $library->branchcode, }
267         }
268     );
269
270     my $reserve_id = C4::Reserves::AddReserve(
271         {
272             branchcode     => $library->branchcode,
273             borrowernumber => $patron->borrowernumber,
274             biblionumber   => $item->biblionumber,
275             priority       => 1,
276             itemnumber     => $item->itemnumber,
277         }
278     );
279
280     my $hold = Koha::Holds->find($reserve_id);
281
282     ok($reserve_id, "Hold created");
283     ok($hold, "Hold found");
284
285     my $av = Koha::AuthorisedValue->new( { category => 'HOLD_CANCELLATION', authorised_value => 'TEST_REASON' } )->store;
286     Koha::Notice::Templates->search({ code => 'HOLD_CANCELLATION'})->delete();
287     my $notice = Koha::Notice::Template->new({
288         name                   => 'Hold cancellation',
289         module                 => 'reserves',
290         code                   => 'HOLD_CANCELLATION',
291         title                  => 'Hold cancelled',
292         content                => 'Your hold was cancelled.',
293         message_transport_type => 'email',
294         branchcode             => q{},
295     })->store();
296
297     $hold->cancel({cancellation_reason => 'TEST_REASON'});
298
299     $hold = Koha::Holds->find($reserve_id);
300     is( $hold, undef, 'Hold is not in the reserves table');
301     $hold = Koha::Old::Holds->find($reserve_id);
302     ok( $hold, 'Hold was found in the old reserves table');
303
304     my $message = Koha::Notice::Messages->find({ borrowernumber => $patron->id, letter_code => 'HOLD_CANCELLATION'});
305     ok( $message, 'Found hold cancellation message');
306     is( $message->subject, 'Hold cancelled', 'Message has correct title' );
307     is( $message->content, 'Your hold was cancelled.', 'Message has correct content');
308
309     $notice->delete;
310     $av->delete;
311     $message->delete;
312 };
313
314 subtest 'cancel all with reason' => sub {
315     plan tests => 7;
316     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
317     my $item = $builder->build_sample_item({ library => $library->branchcode });
318     my $manager = $builder->build_object( { class => "Koha::Patrons" } );
319     t::lib::Mocks::mock_userenv( { patron => $manager, branchcode => $manager->branchcode } );
320
321     my $patron = $builder->build_object(
322         {
323             class => 'Koha::Patrons',
324             value => { branchcode => $library->branchcode, }
325         }
326     );
327
328     my $reserve_id = C4::Reserves::AddReserve(
329         {
330             branchcode     => $library->branchcode,
331             borrowernumber => $patron->borrowernumber,
332             biblionumber   => $item->biblionumber,
333             priority       => 1,
334             itemnumber     => $item->itemnumber,
335         }
336     );
337
338     my $hold = Koha::Holds->find($reserve_id);
339
340     ok($reserve_id, "Hold created");
341     ok($hold, "Hold found");
342
343     my $av = Koha::AuthorisedValue->new( { category => 'HOLD_CANCELLATION', authorised_value => 'TEST_REASON' } )->store;
344     Koha::Notice::Templates->search({ code => 'HOLD_CANCELLATION'})->delete();
345     my $notice = Koha::Notice::Template->new({
346         name                   => 'Hold cancellation',
347         module                 => 'reserves',
348         code                   => 'HOLD_CANCELLATION',
349         title                  => 'Hold cancelled',
350         content                => 'Your hold was cancelled.',
351         message_transport_type => 'email',
352         branchcode             => q{},
353     })->store();
354
355     ModReserveCancelAll($item->id, $patron->id, 'TEST_REASON');
356
357     $hold = Koha::Holds->find($reserve_id);
358     is( $hold, undef, 'Hold is not in the reserves table');
359     $hold = Koha::Old::Holds->find($reserve_id);
360     ok( $hold, 'Hold was found in the old reserves table');
361
362     my $message = Koha::Notice::Messages->find({ borrowernumber => $patron->id, letter_code => 'HOLD_CANCELLATION'});
363     ok( $message, 'Found hold cancellation message');
364     is( $message->subject, 'Hold cancelled', 'Message has correct title' );
365     is( $message->content, 'Your hold was cancelled.', 'Message has correct content');
366
367     $av->delete;
368     $message->delete;
369 };
370
371 $schema->storage->txn_rollback;
372
373 1;