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 correctly';
65 my $nullified_created_by = $builder->build_object(
67 class => 'Koha::Checkouts::ReturnClaims',
74 is( $nullified_created_by->created_by, undef, 'Is undef' );
75 ok( $nullified_created_by->in_storage, 'In storage' );
77 ref($nullified_created_by->notes('Some other note')->store),
78 'Koha::Checkouts::ReturnClaim',
79 'No exception, store success'
82 is( Koha::Checkouts::ReturnClaims->search({ issue_id => $checkout->id })->count, 0, 'No claims stored' );
84 my $claim = Koha::Checkouts::ReturnClaim->new(
86 issue_id => $checkout->id,
87 itemnumber => $checkout->itemnumber,
88 borrowernumber => $checkout->borrowernumber,
89 notes => 'Some notes',
90 created_by => $librarian->borrowernumber
94 is( ref($claim), 'Koha::Checkouts::ReturnClaim', 'Object type is correct' );
95 is( Koha::Checkouts::ReturnClaims->search( { issue_id => $checkout->id } )->count, 1, 'Claim stored on the DB');
97 { # hide useless warnings
99 open STDERR, '>', '/dev/null';
101 Koha::Checkouts::ReturnClaim->new(
103 issue_id => $checkout->id,
104 itemnumber => $checkout->itemnumber,
105 borrowernumber => $checkout->borrowernumber,
106 notes => 'Some notes',
107 created_by => $librarian->borrowernumber
111 'Koha::Exceptions::Object::DuplicateID',
112 'An exception is thrown on duplicate issue_id';
117 qr/(return_claims\.)?issue_id/,
118 'Exception field is correct'
122 { # hide useless warnings
124 open STDERR, '>', '/dev/null';
126 my $another_checkout = $builder->build_object({ class => 'Koha::Checkouts' });
127 my $checkout_id = $another_checkout->id;
128 $another_checkout->delete;
133 $THE_claim = Koha::Checkouts::ReturnClaim->new(
135 issue_id => $checkout_id,
136 itemnumber => $checkout->itemnumber,
137 borrowernumber => $checkout->borrowernumber,
138 notes => 'Some notes',
139 created_by => $librarian->borrowernumber
143 'Koha::Exceptions::Object::FKConstraint',
144 'An exception is thrown on invalid issue_id';
147 is( $@->broken_fk, 'issue_id', 'Exception field is correct' );
150 $schema->storage->txn_rollback;
153 subtest "resolve() tests" => sub {
157 $schema->storage->txn_begin;
160 my $librarian = $builder->build_object({ class => 'Koha::Patrons' });
161 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
162 my $item = $builder->build_sample_item({ itemlost => $itemlost });
164 my $checkout = $builder->build_object(
166 class => 'Koha::Checkouts',
168 borrowernumber => $patron->borrowernumber,
169 itemnumber => $item->itemnumber,
170 branchcode => $patron->branchcode
175 my $claim = Koha::Checkouts::ReturnClaim->new(
177 issue_id => $checkout->id,
178 itemnumber => $checkout->itemnumber,
179 borrowernumber => $checkout->borrowernumber,
180 notes => 'Some notes',
181 created_by => $librarian->borrowernumber
186 { $claim->resolve({ resolution => 1 }); }
187 'Koha::Exceptions::MissingParameter',
188 "Not passing 'resolved_by' makes it throw an exception";
191 { $claim->resolve({ resolved_by => 1 }); }
192 'Koha::Exceptions::MissingParameter',
193 "Not passing 'resolution' makes it throw an exception";
195 my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' });
196 my $deleted_patron_id = $deleted_patron->id;
197 $deleted_patron->delete;
199 { # hide useless warnings
201 open STDERR, '>', '/dev/null';
204 { $claim->resolve({ resolution => "X", resolved_by => $deleted_patron_id }) }
205 'Koha::Exceptions::Object::FKConstraint',
206 "Exception thrown on invalid resolver";
211 my $today = dt_from_string;
212 my $tomorrow = dt_from_string->add( days => 1 );
217 resolved_by => $librarian->id,
218 resolved_on => $tomorrow,
222 is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
223 is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
225 # Make sure $item is refreshed
226 $item->discard_changes;
227 is( $item->itemlost, $itemlost, 'Item lost status remains unchanged' );
229 # New checkout and claim
231 $checkout = $builder->build_object(
233 class => 'Koha::Checkouts',
235 borrowernumber => $patron->borrowernumber,
236 itemnumber => $item->itemnumber,
237 branchcode => $patron->branchcode
242 $claim = Koha::Checkouts::ReturnClaim->new(
244 issue_id => $checkout->id,
245 itemnumber => $checkout->itemnumber,
246 borrowernumber => $checkout->borrowernumber,
247 notes => 'Some notes',
248 created_by => $librarian->borrowernumber
252 my $new_lost_status = 2;
257 resolved_by => $librarian->id,
258 resolved_on => $tomorrow,
259 new_lost_status => $new_lost_status,
263 is( output_pref( { str => $claim->resolved_on } ), output_pref( { dt => $tomorrow } ), 'resolved_on set to the passed param' );
264 is( $claim->updated_by, $librarian->id, 'updated_by set to the passed resolved_by' );
266 # Make sure $item is refreshed
267 $item->discard_changes;
268 is( $item->itemlost, $new_lost_status, 'Item lost status is updated' );
270 $schema->storage->txn_rollback;