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>.
22 use C4::Biblio qw( AddBiblio );
30 use t::lib::TestBuilder;
32 use Test::More tests => 34;
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;
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},
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},
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");
86 is( $hold->suspend, 0, "Hold is not suspended" );
87 $hold->suspend_hold();
88 is( $hold->suspend, 1, "Hold is suspended" );
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" );
97 is( $hold->suspend, 1, "Hold is suspended" );
98 is( $hold->suspend_until, undef, "Hold is suspended without a date" );
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' );
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' );
116 is( $hold->is_found, 1, 'The hold is found');
117 is( $hold->is_in_processing, 1, 'The hold is in processing' );
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' );
125 # Test method is_cancelable_from_opac
127 is( $hold->is_cancelable_from_opac, 1, "Unfound hold is cancelable" );
129 is( $hold->is_cancelable_from_opac, 0, "Waiting hold is not cancelable" );
131 is( $hold->is_cancelable_from_opac, 0, "In transit hold is not cancelable" );
133 is( $hold->is_cancelable_from_opac, 0, "In processing hold is not cancelable" );
135 # Test method is_at_destination
137 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
139 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
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" );
145 # Test setting expiration date
146 t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdate', 1 );
147 t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdatePeriod', 2 );
148 t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdateUnitOfTime', 'years' );
150 my $hold_2 = Koha::Hold->new(
152 biblionumber => $biblionumber,
153 itemnumber => $item->id(),
154 reservedate => '2020-11-30',
155 waitingdate => '2020-11-30',
156 borrowernumber => $borrower->{borrowernumber},
157 branchcode => $branches[1]->{branchcode},
163 my $expected_date = dt_from_string( $hold_2->reservedate )->add( years => 2);
164 is($hold_2->expirationdate->ymd, $expected_date->ymd,'Expiration date is correctly set.');
166 $schema->storage->txn_rollback();
168 subtest "delete() tests" => sub {
172 $schema->storage->txn_begin();
175 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
177 my $hold = $builder->build({ source => 'Reserve' });
179 my $hold_object = Koha::Holds->find( $hold->{ reserve_id } );
180 my $deleted = $hold_object->delete;
181 is( ref($deleted), 'Koha::Hold', 'Koha::Hold->delete should return the Koha::Hold object if the hold has been correctly deleted' );
182 is( Koha::Holds->search({ reserve_id => $hold->{ reserve_id } })->count, 0,
183 "Koha::Hold->delete should have deleted the hold" );
185 my $number_of_logs = $schema->resultset('ActionLog')->search(
186 { module => 'HOLDS', action => 'DELETE', object => $hold->{ reserve_id } } )->count;
187 is( $number_of_logs, 0, 'With HoldsLogs, Koha::Hold->delete shouldn\'t have been logged' );
190 t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
192 $hold = $builder->build({ source => 'Reserve' });
194 $hold_object = Koha::Holds->find( $hold->{ reserve_id } );
195 $deleted = $hold_object->delete;
196 is( ref($deleted), 'Koha::Hold', 'Koha::Hold->delete should return a Koha::Hold object if the hold has been correctly deleted' );
197 is( Koha::Holds->search({ reserve_id => $hold->{ reserve_id } })->count, 0,
198 "Koha::Hold->delete should have deleted the hold" );
200 $number_of_logs = $schema->resultset('ActionLog')->search(
201 { module => 'HOLDS', action => 'DELETE', object => $hold->{ reserve_id } } )->count;
202 is( $number_of_logs, 1, 'With HoldsLogs, Koha::Hold->delete should have been logged' );
204 $schema->storage->txn_rollback();
207 subtest 'suspend() tests' => sub {
211 $schema->storage->txn_begin;
214 t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
216 my $hold = $builder->build_object(
217 { class => 'Koha::Holds',
218 value => { found => undef, suspend => 0, suspend_until => undef, waitingdate => undef }
222 ok( !$hold->is_suspended, 'Hold is not suspended' );
223 my $suspended = $hold->suspend_hold;
224 is( ref($suspended) , 'Koha::Hold', 'suspend returns the Koha::Hold object' );
225 is( $suspended->id, $hold->id, 'suspend returns the same object' );
226 ok( $suspended->is_suspended, 'The hold is suspended' );
227 is( $suspended->suspend_until, undef, 'It is an indefinite suspension' );
231 $hold->discard_changes;
234 my $date = dt_from_string()->add( days => 1 );
235 $suspended = $hold->suspend_hold( $date );
236 is( ref($suspended) , 'Koha::Hold', 'suspend returns the Koha::Hold object' );
237 is( $suspended->id, $hold->id, 'suspend returns the same object' );
238 ok( $suspended->is_suspended, 'The hold is suspended' );
239 is( $suspended->suspend_until, $date->truncate( to => 'day' ), 'It is an indefinite suspension' );
243 $hold->discard_changes;
248 { $hold->suspend_hold; }
249 'Koha::Exceptions::Hold::CannotSuspendFound',
250 'Exception is thrown when a found hold is tried to suspend';
252 is( $@->status, 'W', 'Exception gets the \'status\' parameter set correctly' );
257 { $hold->suspend_hold; }
258 'Koha::Exceptions::Hold::CannotSuspendFound',
259 'Exception is thrown when a found hold is tried to suspend';
261 is( $@->status, 'T', 'Exception gets the \'status\' parameter set correctly' );
264 $hold->set_processing();
266 { $hold->suspend_hold; }
267 'Koha::Exceptions::Hold::CannotSuspendFound',
268 'Exception is thrown when a found hold is tried to suspend';
270 is( $@->status, 'P', 'Exception gets the \'status\' parameter set correctly' );
272 my $holds_module = Test::MockModule->new('Koha::Hold');
273 $holds_module->mock( 'is_found', 1 );
278 { $hold->suspend_hold }
279 'Koha::Exceptions::Hold::CannotSuspendFound',
280 'Exception is thrown when a found hold is tried to suspend';
282 is( $@->error, 'Unhandled data exception on found hold (id='
286 . ')' , 'Exception gets the \'status\' parameter set correctly' );
288 $holds_module->unmock( 'is_found' );
291 t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
293 my $logs_count = $schema->resultset('ActionLog')->search(
294 { module => 'HOLDS', action => 'SUSPEND', object => $hold->id } )->count;
296 $hold = $builder->build_object(
297 { class => 'Koha::Holds',
298 value => { suspend => 0, suspend_until => undef, waitingdate => undef, found => undef }
303 my $new_logs_count = $schema->resultset('ActionLog')->search(
304 { module => 'HOLDS', action => 'SUSPEND', object => $hold->id } )->count;
306 is( $new_logs_count, $logs_count + 1, 'If logging is enabled, suspending a hold gets logged' );
308 $schema->storage->txn_rollback;