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>.
20 use Test::More tests => 6;
24 use Koha::DateUtils qw( dt_from_string output_pref );
25 use Koha::Checkouts::ReturnClaims;
27 use t::lib::TestBuilder;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 subtest "store() tests" => sub {
36 $schema->storage->txn_begin;
38 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
39 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
40 my $item = $builder->build_sample_item;
42 my $checkout = $builder->build_object(
44 class => 'Koha::Checkouts',
46 borrowernumber => $patron->borrowernumber,
47 itemnumber => $item->itemnumber,
48 branchcode => $patron->branchcode
54 { Koha::Checkouts::ReturnClaim->new(
56 issue_id => $checkout->id,
57 itemnumber => $checkout->itemnumber,
58 borrowernumber => $checkout->borrowernumber,
62 'Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy',
63 'Exception thrown if no created_by passed on creation';
65 my $old_checkout = $builder->build_object(
67 class => 'Koha::Old::Checkouts',
69 borrowernumber => $patron->borrowernumber,
70 itemnumber => $item->itemnumber,
71 branchcode => $patron->branchcode
76 my $nullable_created_by = Koha::Checkouts::ReturnClaim->new(
78 issue_id => $old_checkout->id,
79 itemnumber => $old_checkout->itemnumber,
80 borrowernumber => $old_checkout->borrowernumber,
81 notes => 'Some notes',
82 created_by => $librarian->borrowernumber
85 is( $nullable_created_by->created_by, $librarian->borrowernumber, 'Claim created with created_by set' );
86 ok( $nullable_created_by->in_storage, 'In storage' );
88 $nullable_created_by->created_by(undef)->store();
89 is( $nullable_created_by->created_by, undef, 'Deletion was deleted' );
90 ok( $nullable_created_by->in_storage, 'In storage' );
92 ref($nullable_created_by->notes('Some other note')->store),
93 'Koha::Checkouts::ReturnClaim',
94 'Subsequent store succeeds after created_by has been unset'
97 is( Koha::Checkouts::ReturnClaims->search({ issue_id => $checkout->id })->count, 0, 'No claims stored' );
99 my $claim = Koha::Checkouts::ReturnClaim->new(
101 issue_id => $checkout->id,
102 itemnumber => $checkout->itemnumber,
103 borrowernumber => $checkout->borrowernumber,
104 notes => 'Some notes',
105 created_by => $librarian->borrowernumber
109 is( ref($claim), 'Koha::Checkouts::ReturnClaim', 'Object type is correct' );
110 is( Koha::Checkouts::ReturnClaims->search( { issue_id => $checkout->id } )->count, 1, 'Claim stored on the DB');
112 { # hide useless warnings
114 open STDERR, '>', '/dev/null';
116 my $another_checkout = $builder->build_object({ class => 'Koha::Checkouts' });
117 my $checkout_id = $another_checkout->id;
118 $another_checkout->delete;
123 $THE_claim = Koha::Checkouts::ReturnClaim->new(
125 issue_id => $checkout_id,
126 itemnumber => $checkout->itemnumber,
127 borrowernumber => $checkout->borrowernumber,
128 notes => 'Some notes',
129 created_by => $librarian->borrowernumber
133 'Koha::Exceptions::Object::FKConstraint',
134 'An exception is thrown on invalid issue_id';
137 is( $@->broken_fk, 'issue_id', 'Exception field is correct' );
140 $schema->storage->txn_rollback;
143 subtest "resolve() tests" => sub {
147 $schema->storage->txn_begin;
150 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
151 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
152 my $item = $builder->build_sample_item({ itemlost => $itemlost });
154 my $checkout = $builder->build_object(
156 class => 'Koha::Checkouts',
158 borrowernumber => $patron->borrowernumber,
159 itemnumber => $item->itemnumber,
160 branchcode => $patron->branchcode
165 my $claim = Koha::Checkouts::ReturnClaim->new(
167 issue_id => $checkout->id,
168 itemnumber => $checkout->itemnumber,
169 borrowernumber => $checkout->borrowernumber,
170 notes => 'Some notes',
171 created_by => $librarian->borrowernumber
176 { $claim->resolve({ resolution => 1 }); }
177 'Koha::Exceptions::MissingParameter',
178 "Not passing 'resolved_by' makes it throw an exception";
181 { $claim->resolve({ resolved_by => 1 }); }
182 'Koha::Exceptions::MissingParameter',
183 "Not passing 'resolution' makes it throw an exception";
185 my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
186 my $deleted_patron_id = $deleted_patron->id;
187 $deleted_patron->delete;
189 { # hide useless warnings
191 open STDERR, '>', '/dev/null';
194 { $claim->resolve({ resolution => "X", resolved_by => $deleted_patron_id }) }
195 'Koha::Exceptions::Object::FKConstraint',
196 "Exception thrown on invalid resolver";
201 my $today = dt_from_string;
202 my $tomorrow = dt_from_string->add( days => 1 );
207 resolved_by => $librarian->id,
208 resolved_on => $tomorrow,
212 is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
213 is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
215 # Make sure $item is refreshed
216 $item->discard_changes;
217 is( $item->itemlost, $itemlost, 'Item lost status remains unchanged' );
219 # New checkout and claim
221 $checkout = $builder->build_object(
223 class => 'Koha::Checkouts',
225 borrowernumber => $patron->borrowernumber,
226 itemnumber => $item->itemnumber,
227 branchcode => $patron->branchcode
232 $claim = Koha::Checkouts::ReturnClaim->new(
234 issue_id => $checkout->id,
235 itemnumber => $checkout->itemnumber,
236 borrowernumber => $checkout->borrowernumber,
237 notes => 'Some notes',
238 created_by => $librarian->borrowernumber
242 my $new_lost_status = 2;
247 resolved_by => $librarian->id,
248 resolved_on => $tomorrow,
249 new_lost_status => $new_lost_status,
253 is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
254 is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
256 # Make sure $item is refreshed
257 $item->discard_changes;
258 is( $item->itemlost, $new_lost_status, 'Item lost status is updated' );
260 $schema->storage->txn_rollback;
263 subtest 'item() tests' => sub {
267 $schema->storage->txn_begin;
269 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
270 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
271 my $item = $builder->build_sample_item;
272 my $checkout = $builder->build_object(
274 class => 'Koha::Checkouts',
276 borrowernumber => $patron->borrowernumber,
277 itemnumber => $item->itemnumber,
278 branchcode => $patron->branchcode
283 my $claim = Koha::Checkouts::ReturnClaim->new(
285 issue_id => $checkout->id,
286 itemnumber => $checkout->itemnumber,
287 borrowernumber => $checkout->borrowernumber,
288 notes => 'Some notes',
289 created_by => $librarian->borrowernumber
293 my $return_claim_item = $claim->item;
294 is( ref( $return_claim_item ), 'Koha::Item', 'Koha::Checkouts::ReturnClaim->item should return a Koha::Item' );
295 is( $claim->itemnumber, $return_claim_item->itemnumber, 'Koha::Checkouts::ReturnClaim->item should return the correct item' );
297 my $itemnumber = $item->itemnumber;
298 $checkout->delete; # Required to allow deletion of item
301 my $claims = Koha::Checkouts::ReturnClaims->search({ itemnumber => $itemnumber });
302 is( $claims->count, 0, 'Koha::Checkouts::ReturnClaim is deleted on item deletion' );
304 $schema->storage->txn_rollback;
307 subtest 'patron() tests' => sub {
311 $schema->storage->txn_begin;
313 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
314 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
315 my $item = $builder->build_sample_item;
316 my $checkout = $builder->build_object(
318 class => 'Koha::Checkouts',
320 borrowernumber => $patron->borrowernumber,
321 itemnumber => $item->itemnumber,
322 branchcode => $patron->branchcode
327 my $claim = Koha::Checkouts::ReturnClaim->new(
329 issue_id => $checkout->id,
330 itemnumber => $checkout->itemnumber,
331 borrowernumber => $checkout->borrowernumber,
332 notes => 'Some notes',
333 created_by => $librarian->borrowernumber
337 my $return_claim_patron = $claim->patron;
338 is( ref( $return_claim_patron ), 'Koha::Patron', 'Koha::Checkouts::ReturnClaim->patron should return a Koha::Patron' );
339 is( $claim->borrowernumber, $return_claim_patron->borrowernumber, 'Koha::Checkouts::ReturnClaim->patron should return the correct borrower' );
341 my $borrowernumber = $patron->borrowernumber;
342 $checkout->delete; # Required to allow deletion of patron
345 my $claims = Koha::Checkouts::ReturnClaims->search({ borrowernumber => $borrowernumber });
346 is( $claims->count, 0, 'Koha::Checkouts::ReturnClaim is deleted on borrower deletion' );
348 $schema->storage->txn_rollback;
351 subtest 'old_checkout() tests' => sub {
355 $schema->storage->txn_begin;
357 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
358 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
359 my $item = $builder->build_sample_item;
360 my $old_checkout = $builder->build_object(
362 class => 'Koha::Old::Checkouts',
364 borrowernumber => $patron->borrowernumber,
365 itemnumber => $item->itemnumber,
366 branchcode => $patron->branchcode
371 my $claim = Koha::Checkouts::ReturnClaim->new(
373 issue_id => $old_checkout->id,
374 itemnumber => $old_checkout->itemnumber,
375 borrowernumber => $old_checkout->borrowernumber,
376 notes => 'Some notes',
377 created_by => $librarian->borrowernumber
381 my $return_claim_old_checkout = $claim->old_checkout;
382 is( ref( $return_claim_old_checkout ), 'Koha::Old::Checkout', 'Koha::Checkouts::ReturnClaim->old_checkout should return a Koha::Old::Checkout' );
383 is( $claim->issue_id, $return_claim_old_checkout->issue_id, 'Koha::Checkouts::ReturnClaim->old_checkout should return the correct borrower' );
385 my $issue_id = $old_checkout->issue_id;
386 $old_checkout->delete;
388 my $claims = Koha::Checkouts::ReturnClaims->search({ issue_id => $issue_id });
389 is( $claims->count, 1, 'Koha::Checkouts::ReturnClaim remains on old_checkout deletion' );
390 # FIXME: Should we actually set null on OldCheckout deletion?
392 $claim->issue_id(undef)->store;
393 is( $claim->old_checkout, undef, 'Koha::Checkouts::ReturnClaim->old_checkout should return undef if no old_checkout linked' );
395 $schema->storage->txn_rollback;
398 subtest 'checkout() tests' => sub {
402 $schema->storage->txn_begin;
404 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
405 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
406 my $item = $builder->build_sample_item;
407 my $checkout = $builder->build_object(
409 class => 'Koha::Checkouts',
411 borrowernumber => $patron->borrowernumber,
412 itemnumber => $item->itemnumber,
413 branchcode => $patron->branchcode
418 my $claim = Koha::Checkouts::ReturnClaim->new(
420 issue_id => $checkout->id,
421 itemnumber => $checkout->itemnumber,
422 borrowernumber => $checkout->borrowernumber,
423 notes => 'Some notes',
424 created_by => $librarian->borrowernumber
428 my $return_claim_checkout = $claim->checkout;
429 is( ref( $return_claim_checkout ), 'Koha::Checkout', 'Koha::Checkouts::ReturnClaim->checkout should return a Koha::Checkout' );
430 is( $claim->issue_id, $return_claim_checkout->issue_id, 'Koha::Checkouts::ReturnClaim->checkout should return the correct borrower' );
432 my $issue_id = $checkout->issue_id;
435 my $claims = Koha::Checkouts::ReturnClaims->search({ issue_id => $issue_id });
436 is( $claims->count, 1, 'Koha::Checkouts::ReturnClaim remains on checkout deletion' );
438 $claim->issue_id(undef)->store;
439 is( $claim->checkout, undef, 'Koha::Checkouts::ReturnClaim->checkout should return undef if no checkout linked' );
441 $schema->storage->txn_rollback;