Bug 21722: Tidy up tests and increase coverage
[koha.git] / t / db_dependent / Hold.t
blob2ba6f7bafb79edd070d28e20a43cb50eb170a2f2
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
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;
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::Item;
29 use Koha::DateUtils;
30 use t::lib::TestBuilder;
32 use Test::More tests => 29;
33 use Test::Exception;
34 use Test::Warn;
36 use_ok('Koha::Hold');
38 my $schema = Koha::Database->new()->schema();
39 $schema->storage->txn_begin();
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' });
47 my $borrower = $builder->build({ source => 'Borrower' });
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 ),
55 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
57 my $item = Koha::Item->new(
59 biblionumber => $biblionumber,
60 biblioitemnumber => $biblioitemnumber,
61 holdingbranch => $branches[0]->{branchcode},
62 homebranch => $branches[0]->{branchcode},
65 $item->store();
67 my $hold = Koha::Hold->new(
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,
78 $hold->store();
80 my $b1_cal = C4::Calendar->new( branchcode => $branches[1]->{branchcode} );
81 $b1_cal->insert_single_holiday( day => 02, month => 01, 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");
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" );
103 $item = $hold->item();
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' );
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' );
115 $hold->found(q{});
116 isnt( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
117 is( $hold->is_found, 0, 'The hold is not found' );
118 ok( !$hold->is_in_transit, 'The hold is not in transit' );
120 # Test method is_cancelable_from_opac
121 $hold->found(undef);
122 is( $hold->is_cancelable_from_opac, 1, "Unfound hold is cancelable" );
123 $hold->found('W');
124 is( $hold->is_cancelable_from_opac, 0, "Waiting hold is not cancelable" );
125 $hold->found('T');
126 is( $hold->is_cancelable_from_opac, 0, "In transit hold is not cancelable" );
128 # Test method is_at_destination
129 $hold->found(undef);
130 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
131 $hold->found('T');
132 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
133 $hold->found('W');
134 ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
135 $item->holdingbranch( $branches[1]->{branchcode} );
136 ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
138 $schema->storage->txn_rollback();
140 subtest "delete() tests" => sub {
142 plan tests => 6;
144 $schema->storage->txn_begin();
146 # Disable logging
147 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
149 my $hold = $builder->build({ source => 'Reserve' });
151 my $hold_object = Koha::Holds->find( $hold->{ reserve_id } );
152 my $deleted = $hold_object->delete;
153 is( $deleted, 1, 'Koha::Hold->delete should return 1 if the hold has been correctly deleted' );
154 is( Koha::Holds->search({ reserve_id => $hold->{ reserve_id } })->count, 0,
155 "Koha::Hold->delete should have deleted the hold" );
157 my $number_of_logs = $schema->resultset('ActionLog')->search(
158 { module => 'HOLDS', action => 'DELETE', object => $hold->{ reserve_id } } )->count;
159 is( $number_of_logs, 0, 'With HoldsLogs, Koha::Hold->delete shouldn\'t have been logged' );
161 # Enable logging
162 t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
164 $hold = $builder->build({ source => 'Reserve' });
166 $hold_object = Koha::Holds->find( $hold->{ reserve_id } );
167 $deleted = $hold_object->delete;
168 is( $deleted, 1, 'Koha::Hold->delete should return 1 if the hold has been correctly deleted' );
169 is( Koha::Holds->search({ reserve_id => $hold->{ reserve_id } })->count, 0,
170 "Koha::Hold->delete should have deleted the hold" );
172 $number_of_logs = $schema->resultset('ActionLog')->search(
173 { module => 'HOLDS', action => 'DELETE', object => $hold->{ reserve_id } } )->count;
174 is( $number_of_logs, 1, 'With HoldsLogs, Koha::Hold->delete should have been logged' );
176 $schema->storage->txn_rollback();
179 subtest 'suspend() tests' => sub {
181 plan tests => 16;
183 $schema->storage->txn_begin;
185 # Disable logging
186 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
188 my $hold = $builder->build_object(
189 { class => 'Koha::Holds',
190 value => { found => undef, suspend => 0, suspend_until => undef, waitingdate => undef }
194 ok( !$hold->is_suspended, 'Hold is not suspended' );
195 my $suspended = $hold->suspend_hold;
196 is( ref($suspended) , 'Koha::Hold', 'suspend returns the Koha::Hold object' );
197 is( $suspended->id, $hold->id, 'suspend returns the same object' );
198 ok( $suspended->is_suspended, 'The hold is suspended' );
199 is( $suspended->suspend_until, undef, 'It is an indefinite suspension' );
201 # resume the hold
202 $suspended->resume;
203 $hold->discard_changes;
205 # create a DT
206 my $date = dt_from_string()->add( days => 1 );
207 $suspended = $hold->suspend_hold( $date );
208 is( ref($suspended) , 'Koha::Hold', 'suspend returns the Koha::Hold object' );
209 is( $suspended->id, $hold->id, 'suspend returns the same object' );
210 ok( $suspended->is_suspended, 'The hold is suspended' );
211 is( $suspended->suspend_until, $date->truncate( to => 'day' ), 'It is an indefinite suspension' );
213 # resume the hold
214 $suspended->resume;
215 $hold->discard_changes;
217 # set hold found=W
218 $hold->set_waiting;
219 throws_ok
220 { $hold->suspend_hold; }
221 'Koha::Exceptions::Hold::CannotSuspendFound',
222 'Exception is thrown when a found hold is tried to suspend';
224 is( $@->status, 'W', 'Exception gets the \'status\' parameter set correctly' );
226 # set hold found=T
227 $hold->set_waiting(1);
228 throws_ok
229 { $hold->suspend_hold; }
230 'Koha::Exceptions::Hold::CannotSuspendFound',
231 'Exception is thrown when a found hold is tried to suspend';
233 is( $@->status, 'T', 'Exception gets the \'status\' parameter set correctly' );
235 my $holds_module = Test::MockModule->new('Koha::Hold');
236 $holds_module->mock( 'is_found', 1 );
238 # bad data case
239 $hold->found('X');
240 throws_ok
241 { $hold->suspend_hold }
242 'Koha::Exceptions::Hold::CannotSuspendFound',
243 'Exception is thrown when a found hold is tried to suspend';
245 is( $@->error, 'Unhandled data exception on found hold (id='
246 . $hold->id
247 . ', found='
248 . $hold->found
249 . ')' , 'Exception gets the \'status\' parameter set correctly' );
251 $holds_module->unmock( 'is_found' );
253 # Enable logging
254 t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
256 my $logs_count = $schema->resultset('ActionLog')->search(
257 { module => 'HOLDS', action => 'SUSPEND', object => $hold->id } )->count;
259 $hold = $builder->build_object(
260 { class => 'Koha::Holds',
261 value => { suspend => 0, suspend_until => undef, waitingdate => undef, found => undef }
265 $hold->suspend_hold;
266 my $new_logs_count = $schema->resultset('ActionLog')->search(
267 { module => 'HOLDS', action => 'SUSPEND', object => $hold->id } )->count;
269 is( $new_logs_count, $logs_count + 1, 'If logging is enabled, suspending a hold gets logged' );
271 $schema->storage->txn_rollback;