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 $schema->storage->txn_rollback;
115 subtest "resolve() tests" => sub {
119 $schema->storage->txn_begin;
122 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
123 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
124 my $item = $builder->build_sample_item({ itemlost => $itemlost });
126 my $checkout = $builder->build_object(
128 class => 'Koha::Checkouts',
130 borrowernumber => $patron->borrowernumber,
131 itemnumber => $item->itemnumber,
132 branchcode => $patron->branchcode
137 my $claim = Koha::Checkouts::ReturnClaim->new(
139 issue_id => $checkout->id,
140 itemnumber => $checkout->itemnumber,
141 borrowernumber => $checkout->borrowernumber,
142 notes => 'Some notes',
143 created_by => $librarian->borrowernumber
148 { $claim->resolve({ resolution => 1 }); }
149 'Koha::Exceptions::MissingParameter',
150 "Not passing 'resolved_by' makes it throw an exception";
153 { $claim->resolve({ resolved_by => 1 }); }
154 'Koha::Exceptions::MissingParameter',
155 "Not passing 'resolution' makes it throw an exception";
157 my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
158 my $deleted_patron_id = $deleted_patron->id;
159 $deleted_patron->delete;
161 { # hide useless warnings
163 open STDERR, '>', '/dev/null';
166 { $claim->resolve({ resolution => "X", resolved_by => $deleted_patron_id }) }
167 'Koha::Exceptions::Object::FKConstraint',
168 "Exception thrown on invalid resolver";
173 my $today = dt_from_string;
174 my $tomorrow = dt_from_string->add( days => 1 );
179 resolved_by => $librarian->id,
180 resolved_on => $tomorrow,
184 is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
185 is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
187 # Make sure $item is refreshed
188 $item->discard_changes;
189 is( $item->itemlost, $itemlost, 'Item lost status remains unchanged' );
191 # New checkout and claim
193 $checkout = $builder->build_object(
195 class => 'Koha::Checkouts',
197 borrowernumber => $patron->borrowernumber,
198 itemnumber => $item->itemnumber,
199 branchcode => $patron->branchcode
204 $claim = Koha::Checkouts::ReturnClaim->new(
206 issue_id => $checkout->id,
207 itemnumber => $checkout->itemnumber,
208 borrowernumber => $checkout->borrowernumber,
209 notes => 'Some notes',
210 created_by => $librarian->borrowernumber
214 my $new_lost_status = 2;
219 resolved_by => $librarian->id,
220 resolved_on => $tomorrow,
221 new_lost_status => $new_lost_status,
225 is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
226 is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
228 # Make sure $item is refreshed
229 $item->discard_changes;
230 is( $item->itemlost, $new_lost_status, 'Item lost status is updated' );
232 # Resolve claim for checkout that has been cleaned from the database
234 $checkout = $builder->build_object(
236 class => 'Koha::Checkouts',
238 borrowernumber => $patron->borrowernumber,
239 itemnumber => $item->itemnumber,
240 branchcode => $patron->branchcode
245 $claim = Koha::Checkouts::ReturnClaim->new(
247 issue_id => $checkout->id,
248 itemnumber => $checkout->itemnumber,
249 borrowernumber => $checkout->borrowernumber,
250 notes => 'Some notes',
251 created_by => $librarian->borrowernumber
260 resolved_by => $librarian->id,
261 resolved_on => $tomorrow,
262 new_lost_status => $new_lost_status,
266 is( $claim->issue_id, undef, "Resolved claim cleaned checkout is updated correctly" );
268 $schema->storage->txn_rollback;
271 subtest 'item() tests' => sub {
275 $schema->storage->txn_begin;
277 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
278 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
279 my $item = $builder->build_sample_item;
280 my $checkout = $builder->build_object(
282 class => 'Koha::Checkouts',
284 borrowernumber => $patron->borrowernumber,
285 itemnumber => $item->itemnumber,
286 branchcode => $patron->branchcode
291 my $claim = Koha::Checkouts::ReturnClaim->new(
293 issue_id => $checkout->id,
294 itemnumber => $checkout->itemnumber,
295 borrowernumber => $checkout->borrowernumber,
296 notes => 'Some notes',
297 created_by => $librarian->borrowernumber
301 my $return_claim_item = $claim->item;
302 is( ref( $return_claim_item ), 'Koha::Item', 'Koha::Checkouts::ReturnClaim->item should return a Koha::Item' );
303 is( $claim->itemnumber, $return_claim_item->itemnumber, 'Koha::Checkouts::ReturnClaim->item should return the correct item' );
305 my $itemnumber = $item->itemnumber;
306 $checkout->delete; # Required to allow deletion of item
309 my $claims = Koha::Checkouts::ReturnClaims->search({ itemnumber => $itemnumber });
310 is( $claims->count, 0, 'Koha::Checkouts::ReturnClaim is deleted on item deletion' );
312 $schema->storage->txn_rollback;
315 subtest 'patron() tests' => sub {
319 $schema->storage->txn_begin;
321 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
322 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
323 my $item = $builder->build_sample_item;
324 my $checkout = $builder->build_object(
326 class => 'Koha::Checkouts',
328 borrowernumber => $patron->borrowernumber,
329 itemnumber => $item->itemnumber,
330 branchcode => $patron->branchcode
335 my $claim = Koha::Checkouts::ReturnClaim->new(
337 issue_id => $checkout->id,
338 itemnumber => $checkout->itemnumber,
339 borrowernumber => $checkout->borrowernumber,
340 notes => 'Some notes',
341 created_by => $librarian->borrowernumber
345 my $return_claim_patron = $claim->patron;
346 is( ref( $return_claim_patron ), 'Koha::Patron', 'Koha::Checkouts::ReturnClaim->patron should return a Koha::Patron' );
347 is( $claim->borrowernumber, $return_claim_patron->borrowernumber, 'Koha::Checkouts::ReturnClaim->patron should return the correct borrower' );
349 my $borrowernumber = $patron->borrowernumber;
350 $checkout->delete; # Required to allow deletion of patron
353 my $claims = Koha::Checkouts::ReturnClaims->search({ borrowernumber => $borrowernumber });
354 is( $claims->count, 0, 'Koha::Checkouts::ReturnClaim is deleted on borrower deletion' );
356 $schema->storage->txn_rollback;
359 subtest 'old_checkout() tests' => sub {
363 $schema->storage->txn_begin;
365 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
366 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
367 my $item = $builder->build_sample_item;
368 my $old_checkout = $builder->build_object(
370 class => 'Koha::Old::Checkouts',
372 borrowernumber => $patron->borrowernumber,
373 itemnumber => $item->itemnumber,
374 branchcode => $patron->branchcode
379 my $claim = Koha::Checkouts::ReturnClaim->new(
381 issue_id => $old_checkout->id,
382 itemnumber => $old_checkout->itemnumber,
383 borrowernumber => $old_checkout->borrowernumber,
384 notes => 'Some notes',
385 created_by => $librarian->borrowernumber
389 my $return_claim_old_checkout = $claim->old_checkout;
390 is( ref( $return_claim_old_checkout ), 'Koha::Old::Checkout', 'Koha::Checkouts::ReturnClaim->old_checkout should return a Koha::Old::Checkout' );
391 is( $claim->issue_id, $return_claim_old_checkout->issue_id, 'Koha::Checkouts::ReturnClaim->old_checkout should return the correct borrower' );
393 my $issue_id = $old_checkout->issue_id;
394 $old_checkout->delete;
396 my $claims = Koha::Checkouts::ReturnClaims->search({ issue_id => $issue_id });
397 is( $claims->count, 1, 'Koha::Checkouts::ReturnClaim remains on old_checkout deletion' );
398 # FIXME: Should we actually set null on OldCheckout deletion?
400 $claim->issue_id(undef)->store;
401 is( $claim->old_checkout, undef, 'Koha::Checkouts::ReturnClaim->old_checkout should return undef if no old_checkout linked' );
403 $schema->storage->txn_rollback;
406 subtest 'checkout() tests' => sub {
410 $schema->storage->txn_begin;
412 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
413 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
414 my $item = $builder->build_sample_item;
415 my $checkout = $builder->build_object(
417 class => 'Koha::Checkouts',
419 borrowernumber => $patron->borrowernumber,
420 itemnumber => $item->itemnumber,
421 branchcode => $patron->branchcode
426 my $claim = Koha::Checkouts::ReturnClaim->new(
428 issue_id => $checkout->id,
429 itemnumber => $checkout->itemnumber,
430 borrowernumber => $checkout->borrowernumber,
431 notes => 'Some notes',
432 created_by => $librarian->borrowernumber
436 my $return_claim_checkout = $claim->checkout;
437 is( ref( $return_claim_checkout ), 'Koha::Checkout', 'Koha::Checkouts::ReturnClaim->checkout should return a Koha::Checkout' );
438 is( $claim->issue_id, $return_claim_checkout->issue_id, 'Koha::Checkouts::ReturnClaim->checkout should return the correct borrower' );
440 my $issue_id = $checkout->issue_id;
443 my $claims = Koha::Checkouts::ReturnClaims->search({ issue_id => $issue_id });
444 is( $claims->count, 1, 'Koha::Checkouts::ReturnClaim remains on checkout deletion' );
446 $claim->issue_id(undef)->store;
447 is( $claim->checkout, undef, 'Koha::Checkouts::ReturnClaim->checkout should return undef if no checkout linked' );
449 $schema->storage->txn_rollback;