Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[koha.git] / t / db_dependent / Hold.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use t::lib::Mocks;
21 use C4::Context;
22 use C4::Biblio qw( AddBiblio );
23 use Koha::Database;
24 use Koha::Libraries;
25 use C4::Calendar qw( new insert_single_holiday );
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::Item;
29 use Koha::DateUtils qw( dt_from_string );
30 use t::lib::TestBuilder;
31
32 use Test::More tests => 35;
33 use Test::Exception;
34 use Test::Warn;
35
36 use_ok('Koha::Hold');
37
38 my $schema = Koha::Database->new()->schema();
39 $schema->storage->txn_begin();
40
41 # add two branches and a borrower
42 my $builder = t::lib::TestBuilder->new;
43 my @branches;
44 foreach( 1..2 ) {
45     push @branches, $builder->build({ source => 'Branch' });
46 }
47 my $borrower = $builder->build({ source => 'Borrower' });
48
49 my $biblio = MARC::Record->new();
50 my $title  = 'Silence in the library';
51 $biblio->append_fields(
52     MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
53     MARC::Field->new( '245', ' ', ' ', a => $title ),
54 );
55 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
56
57 my $item = Koha::Item->new(
58     {
59         biblionumber     => $biblionumber,
60         biblioitemnumber => $biblioitemnumber,
61         holdingbranch    => $branches[0]->{branchcode},
62         homebranch       => $branches[0]->{branchcode},
63     }
64 );
65 $item->store();
66
67 my $hold = Koha::Hold->new(
68     {
69         biblionumber   => $biblionumber,
70         itemnumber     => $item->id(),
71         reservedate    => '2017-01-01',
72         waitingdate    => '2000-01-01',
73         borrowernumber => $borrower->{borrowernumber},
74         branchcode     => $branches[1]->{branchcode},
75         suspend        => 0,
76     }
77 );
78 $hold->store();
79
80 my $b1_cal = C4::Calendar->new( branchcode => $branches[1]->{branchcode} );
81 $b1_cal->insert_single_holiday( day => 2, month => 1, year => 2017, title => "Morty Day", description => "Rick" ); #Add a holiday
82 my $today = dt_from_string;
83 is( $hold->age(), $today->delta_days( dt_from_string( '2017-01-01' ) )->in_units( 'days')  , "Age of hold is days from reservedate to now if calendar ignored");
84 is( $hold->age(1), $today->delta_days( dt_from_string( '2017-01-01' ) )->in_units( 'days' ) - 1 , "Age of hold is days from reservedate to now minus 1 if calendar used");
85
86 is( $hold->suspend, 0, "Hold is not suspended" );
87 $hold->suspend_hold();
88 is( $hold->suspend, 1, "Hold is suspended" );
89 $hold->resume();
90 is( $hold->suspend, 0, "Hold is not suspended" );
91 my $dt = dt_from_string();
92 $hold->suspend_hold( $dt );
93 $dt->truncate( to => 'day' );
94 is( $hold->suspend, 1, "Hold is suspended" );
95 is( $hold->suspend_until, "$dt", "Hold is suspended with a date, truncation takes place automatically" );
96 $hold->suspend_hold;
97 is( $hold->suspend, 1, "Hold is suspended" );
98 is( $hold->suspend_until, undef, "Hold is suspended without a date" );
99 $hold->resume();
100 is( $hold->suspend, 0, "Hold is not suspended" );
101 is( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
102
103 $item = $hold->item();
104
105 my $hold_borrower = $hold->borrower();
106 ok( $hold_borrower, 'Got hold borrower' );
107 is( $hold_borrower->borrowernumber(), $borrower->{borrowernumber}, 'Hold borrower matches correct borrower' );
108
109 t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '5' );
110 $hold->found('T');
111 isnt( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
112 is( $hold->is_found, 1, 'The hold is found');
113 is( $hold->is_in_transit, 1, 'The hold is in transit' );
114
115 $hold->found('P');
116 is( $hold->is_found, 1, 'The hold is found');
117 is( $hold->is_in_processing, 1, 'The hold is in processing' );
118
119 $hold->found(q{});
120 isnt( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
121 is( $hold->is_found, 0, 'The hold is not found' );
122 ok( !$hold->is_in_transit, 'The hold is not in transit' );
123 ok( !$hold->is_in_processing, 'The hold is not in processing' );
124
125 # Test method is_cancelable_from_opac
126 $hold->found(undef);
127 is( $hold->is_cancelable_from_opac, 1, "Unfound hold is cancelable" );
128 $hold->found('W');
129 is( $hold->is_cancelable_from_opac, 0, "Waiting hold is not cancelable" );
130 $hold->found('T');
131 is( $hold->is_cancelable_from_opac, 0, "In transit hold is not cancelable" );
132 $hold->found('P');
133 is( $hold->is_cancelable_from_opac, 0, "In processing hold is not cancelable" );
134
135 # Test method is_at_destination
136 $hold->found(undef);
137 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
138 $hold->found('T');
139 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
140 $hold->found('W');
141 ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
142 $item->holdingbranch( $branches[1]->{branchcode} );
143 ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
144
145 $schema->storage->txn_rollback();
146
147 subtest "store() tests" => sub {
148
149     plan tests => 5;
150
151     $schema->storage->txn_begin();
152
153     my $item     = $builder->build_sample_item;
154     my $biblio   = $item->biblio;
155     my $borrower = $builder->build_object( { class => 'Koha::Patrons' } );
156     my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
157
158     # Test setting expiration date
159     t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdate',       1 );
160     t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdatePeriod', 2 );
161     t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdateUnitOfTime',
162         'years' );
163
164     my $hold = Koha::Hold->new(
165         {
166             biblionumber   => $biblio->biblionumber,
167             itemnumber     => $item->id,
168             reservedate    => '2020-11-30',
169             waitingdate    => '2020-11-30',
170             borrowernumber => $borrower->borrowernumber,
171             branchcode     => $library->branchcode,
172             suspend        => 0,
173         }
174     )->store;
175     $hold->discard_changes;
176
177     my $expected_date =
178       dt_from_string( $hold->reservedate )->add( years => 2 );
179     is( $hold->expirationdate,
180         $expected_date->ymd, 'Default expiration date for new hold is correctly set.' );
181
182     my $passed_date =
183       dt_from_string( $hold->reservedate )->add( months => 2 );
184     $hold = Koha::Hold->new(
185         {
186             biblionumber   => $biblio->biblionumber,
187             itemnumber     => $item->id,
188             reservedate    => '2020-11-30',
189             expirationdate => $passed_date->ymd,
190             waitingdate    => '2020-11-30',
191             borrowernumber => $borrower->borrowernumber,
192             branchcode     => $library->branchcode,
193             suspend        => 0,
194         }
195     )->store;
196     $hold->discard_changes;
197
198     is( $hold->expirationdate,
199         $passed_date->ymd, 'Passed expiration date for new hold is correctly set.' );
200
201     $passed_date->add( months => 1 );
202     $hold->expirationdate($passed_date->ymd)->store();
203     $hold->discard_changes;
204
205     is( $hold->expirationdate,
206         $passed_date->ymd, 'Passed expiration date when updating hold is correctly set.' );
207
208     $hold->reservedate($passed_date->ymd)->store();
209     $hold->discard_changes;
210
211     is( $hold->expirationdate,
212         $passed_date->add( years => 2 )->ymd, 'Default expiration date correctly set on reservedate update.' );
213
214     $hold->set(
215         {
216             reservedate    => $passed_date->ymd,
217             expirationdate => $passed_date->add( weeks => 2 )->ymd
218         }
219     )->store();
220     $hold->discard_changes;
221
222     is( $hold->expirationdate,
223         $passed_date->ymd, 'Passed expiration date when updating hold correctly set (Passing both reservedate and expirationdate.' );
224
225     $schema->storage->txn_rollback();
226 };
227
228 subtest "set_waiting() tests" => sub {
229
230     plan tests => 2;
231
232     $schema->storage->txn_begin();
233
234     my $hold = $builder->build_object({ class => 'Koha::Holds' });
235
236     $hold->waitingdate('2021-01-01');
237     $hold->set_waiting();
238     is($hold->waitingdate, '2021-01-01', "Setting waiting when already waiting should not update waitingdate");
239
240     $hold->waitingdate(undef)->store;
241     $hold->set_waiting();
242     isnt($hold->waitingdate, undef, "Setting waiting when not waiting already should update waitingdate");
243
244     $schema->storage->txn_rollback();
245 };
246
247 subtest "delete() tests" => sub {
248
249     plan tests => 6;
250
251     $schema->storage->txn_begin();
252
253     # Disable logging
254     t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
255
256     my $hold = $builder->build({ source => 'Reserve' });
257
258     my $hold_object = Koha::Holds->find( $hold->{ reserve_id } );
259     my $deleted = $hold_object->delete;
260     is( ref($deleted), 'Koha::Hold', 'Koha::Hold->delete should return the Koha::Hold object if the hold has been correctly deleted' );
261     is( Koha::Holds->search({ reserve_id => $hold->{ reserve_id } })->count, 0,
262         "Koha::Hold->delete should have deleted the hold" );
263
264     my $number_of_logs = $schema->resultset('ActionLog')->search(
265             { module => 'HOLDS', action => 'DELETE', object => $hold->{ reserve_id } } )->count;
266     is( $number_of_logs, 0, 'With HoldsLogs, Koha::Hold->delete shouldn\'t have been logged' );
267
268     # Enable logging
269     t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
270
271     $hold = $builder->build({ source => 'Reserve' });
272
273     $hold_object = Koha::Holds->find( $hold->{ reserve_id } );
274     $deleted = $hold_object->delete;
275     is( ref($deleted), 'Koha::Hold', 'Koha::Hold->delete should return a Koha::Hold object if the hold has been correctly deleted' );
276     is( Koha::Holds->search({ reserve_id => $hold->{ reserve_id } })->count, 0,
277         "Koha::Hold->delete should have deleted the hold" );
278
279     $number_of_logs = $schema->resultset('ActionLog')->search(
280             { module => 'HOLDS', action => 'DELETE', object => $hold->{ reserve_id } } )->count;
281     is( $number_of_logs, 1, 'With HoldsLogs, Koha::Hold->delete should have been logged' );
282
283     $schema->storage->txn_rollback();
284  };
285
286 subtest 'suspend() tests' => sub {
287
288     plan tests => 18;
289
290     $schema->storage->txn_begin;
291
292     # Disable logging
293     t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
294
295     my $hold = $builder->build_object(
296         {   class => 'Koha::Holds',
297             value => { found => undef, suspend => 0, suspend_until => undef, waitingdate => undef }
298         }
299     );
300
301     ok( !$hold->is_suspended, 'Hold is not suspended' );
302     my $suspended = $hold->suspend_hold;
303     is( ref($suspended) , 'Koha::Hold', 'suspend returns the Koha::Hold object' );
304     is( $suspended->id, $hold->id, 'suspend returns the same object' );
305     ok( $suspended->is_suspended, 'The hold is suspended' );
306     is( $suspended->suspend_until, undef, 'It is an indefinite suspension' );
307
308     # resume the hold
309     $suspended->resume;
310     $hold->discard_changes;
311
312     # create a DT
313     my $date = dt_from_string()->add( days => 1 );
314     $suspended = $hold->suspend_hold( $date );
315     is( ref($suspended) , 'Koha::Hold', 'suspend returns the Koha::Hold object' );
316     is( $suspended->id, $hold->id, 'suspend returns the same object' );
317     ok( $suspended->is_suspended, 'The hold is suspended' );
318     is( $suspended->suspend_until, $date->truncate( to => 'day' ), 'It is an indefinite suspension' );
319
320     # resume the hold
321     $suspended->resume;
322     $hold->discard_changes;
323
324     # set hold found=W
325     $hold->set_waiting;
326     throws_ok
327         { $hold->suspend_hold; }
328         'Koha::Exceptions::Hold::CannotSuspendFound',
329         'Exception is thrown when a found hold is tried to suspend';
330
331     is( $@->status, 'W', 'Exception gets the \'status\' parameter set correctly' );
332
333     # set hold found=T
334     $hold->set_transfer;
335     throws_ok
336         { $hold->suspend_hold; }
337         'Koha::Exceptions::Hold::CannotSuspendFound',
338         'Exception is thrown when a found hold is tried to suspend';
339
340     is( $@->status, 'T', 'Exception gets the \'status\' parameter set correctly' );
341
342     # set hold found=T
343     $hold->set_processing();
344     throws_ok
345         { $hold->suspend_hold; }
346         'Koha::Exceptions::Hold::CannotSuspendFound',
347         'Exception is thrown when a found hold is tried to suspend';
348
349     is( $@->status, 'P', 'Exception gets the \'status\' parameter set correctly' );
350
351     my $holds_module = Test::MockModule->new('Koha::Hold');
352     $holds_module->mock( 'is_found', 1 );
353
354     # bad data case
355     $hold->found('X');
356     throws_ok
357         { $hold->suspend_hold }
358         'Koha::Exceptions::Hold::CannotSuspendFound',
359         'Exception is thrown when a found hold is tried to suspend';
360
361     is( $@->error, 'Unhandled data exception on found hold (id='
362                     . $hold->id
363                     . ', found='
364                     . $hold->found
365                     . ')' , 'Exception gets the \'status\' parameter set correctly' );
366
367     $holds_module->unmock( 'is_found' );
368
369     # Enable logging
370     t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
371
372     my $logs_count = $schema->resultset('ActionLog')->search(
373             { module => 'HOLDS', action => 'SUSPEND', object => $hold->id } )->count;
374
375     $hold = $builder->build_object(
376         {   class => 'Koha::Holds',
377             value => { suspend => 0, suspend_until => undef, waitingdate => undef, found => undef }
378         }
379     );
380
381     $hold->suspend_hold;
382     my $new_logs_count = $schema->resultset('ActionLog')->search(
383             { module => 'HOLDS', action => 'SUSPEND', object => $hold->id } )->count;
384
385     is( $new_logs_count, $logs_count + 1, 'If logging is enabled, suspending a hold gets logged' );
386
387     $schema->storage->txn_rollback;
388 };