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 => 2;
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 Koha::Checkouts::ReturnClaim->new(
118 issue_id => $checkout->id,
119 itemnumber => $checkout->itemnumber,
120 borrowernumber => $checkout->borrowernumber,
121 notes => 'Some notes',
122 created_by => $librarian->borrowernumber
126 'Koha::Exceptions::Object::DuplicateID',
127 'An exception is thrown on duplicate issue_id';
132 qr/(return_claims\.)?issue_id/,
133 'Exception field is correct'
137 { # hide useless warnings
139 open STDERR, '>', '/dev/null';
141 my $another_checkout = $builder->build_object({ class => 'Koha::Checkouts' });
142 my $checkout_id = $another_checkout->id;
143 $another_checkout->delete;
148 $THE_claim = Koha::Checkouts::ReturnClaim->new(
150 issue_id => $checkout_id,
151 itemnumber => $checkout->itemnumber,
152 borrowernumber => $checkout->borrowernumber,
153 notes => 'Some notes',
154 created_by => $librarian->borrowernumber
158 'Koha::Exceptions::Object::FKConstraint',
159 'An exception is thrown on invalid issue_id';
162 is( $@->broken_fk, 'issue_id', 'Exception field is correct' );
165 $schema->storage->txn_rollback;
168 subtest "resolve() tests" => sub {
172 $schema->storage->txn_begin;
175 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
176 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
177 my $item = $builder->build_sample_item({ itemlost => $itemlost });
179 my $checkout = $builder->build_object(
181 class => 'Koha::Checkouts',
183 borrowernumber => $patron->borrowernumber,
184 itemnumber => $item->itemnumber,
185 branchcode => $patron->branchcode
190 my $claim = Koha::Checkouts::ReturnClaim->new(
192 issue_id => $checkout->id,
193 itemnumber => $checkout->itemnumber,
194 borrowernumber => $checkout->borrowernumber,
195 notes => 'Some notes',
196 created_by => $librarian->borrowernumber
201 { $claim->resolve({ resolution => 1 }); }
202 'Koha::Exceptions::MissingParameter',
203 "Not passing 'resolved_by' makes it throw an exception";
206 { $claim->resolve({ resolved_by => 1 }); }
207 'Koha::Exceptions::MissingParameter',
208 "Not passing 'resolution' makes it throw an exception";
210 my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
211 my $deleted_patron_id = $deleted_patron->id;
212 $deleted_patron->delete;
214 { # hide useless warnings
216 open STDERR, '>', '/dev/null';
219 { $claim->resolve({ resolution => "X", resolved_by => $deleted_patron_id }) }
220 'Koha::Exceptions::Object::FKConstraint',
221 "Exception thrown on invalid resolver";
226 my $today = dt_from_string;
227 my $tomorrow = dt_from_string->add( days => 1 );
232 resolved_by => $librarian->id,
233 resolved_on => $tomorrow,
237 is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
238 is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
240 # Make sure $item is refreshed
241 $item->discard_changes;
242 is( $item->itemlost, $itemlost, 'Item lost status remains unchanged' );
244 # New checkout and claim
246 $checkout = $builder->build_object(
248 class => 'Koha::Checkouts',
250 borrowernumber => $patron->borrowernumber,
251 itemnumber => $item->itemnumber,
252 branchcode => $patron->branchcode
257 $claim = Koha::Checkouts::ReturnClaim->new(
259 issue_id => $checkout->id,
260 itemnumber => $checkout->itemnumber,
261 borrowernumber => $checkout->borrowernumber,
262 notes => 'Some notes',
263 created_by => $librarian->borrowernumber
267 my $new_lost_status = 2;
272 resolved_by => $librarian->id,
273 resolved_on => $tomorrow,
274 new_lost_status => $new_lost_status,
278 is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
279 is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
281 # Make sure $item is refreshed
282 $item->discard_changes;
283 is( $item->itemlost, $new_lost_status, 'Item lost status is updated' );
285 $schema->storage->txn_rollback;