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 => 4;
25 use t::lib::TestBuilder;
27 use C4::Circulation qw(AddIssue);
29 use Koha::Checkouts::ReturnClaims;
31 use Koha::DateUtils qw(dt_from_string);
33 my $schema = Koha::Database->schema;
34 my $builder = t::lib::TestBuilder->new;
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37 my $t = Test::Mojo->new('Koha::REST::V1');
39 subtest 'claim_returned() tests' => sub {
43 $schema->storage->txn_begin;
45 my $librarian = $builder->build_object(
47 class => 'Koha::Patrons',
48 value => { flags => 1 }
51 my $password = 'thePassword123';
52 $librarian->set_password( { password => $password, skip_validation => 1 } );
53 my $userid = $librarian->userid;
55 my $patron = $builder->build_object(
57 class => 'Koha::Patrons',
58 value => { flags => 0 }
62 t::lib::Mocks::mock_userenv({ branchcode => $librarian->branchcode });
64 my $item = $builder->build_sample_item;
65 my $issue = AddIssue( $patron->unblessed, $item->barcode, dt_from_string->add( weeks => 2 ) );
67 t::lib::Mocks::mock_preference( 'ClaimReturnedChargeFee', 'ask' );
68 t::lib::Mocks::mock_preference( 'ClaimReturnedLostValue', '99' );
72 "//$userid:$password@/api/v1/return_claims" => json => {
73 item_id => $item->itemnumber,
74 charge_lost_fee => Mojo::JSON->false,
75 created_by => $librarian->id,
76 notes => "This is a test note."
78 )->status_is(201)->header_like(
79 Location => qr|^\/api\/v1\/return_claims/\d*|,
83 my $claim_id = $t->tx->res->json->{claim_id};
88 "//$userid:$password@/api/v1/return_claims" => json => {
89 item_id => $item->itemnumber,
90 charge_lost_fee => Mojo::JSON->false,
91 created_by => $librarian->id,
92 notes => "This is a test note."
96 qr/DBD::mysql::st execute failed: Duplicate entry/;
101 "//$userid:$password@/api/v1/return_claims" => json => {
102 item_id => $item->itemnumber,
103 charge_lost_fee => Mojo::JSON->false,
104 created_by => $librarian->id,
105 notes => "This is a test note."
108 ->json_is( '/error' => 'Checkout not found' );
110 $schema->storage->txn_rollback;
113 subtest 'update_notes() tests' => sub {
117 $schema->storage->txn_begin;
119 my $librarian = $builder->build_object(
121 class => 'Koha::Patrons',
122 value => { flags => 1 }
125 my $password = 'thePassword123';
126 $librarian->set_password( { password => $password, skip_validation => 1 } );
127 my $userid = $librarian->userid;
129 my $item = $builder->build_sample_item;
131 t::lib::Mocks::mock_userenv( { branchcode => $item->homebranch } )
132 ; # needed by AddIssue
134 my $issue = AddIssue( $librarian->unblessed, $item->barcode,
135 dt_from_string->add( weeks => 2 ) );
137 my $claim = $issue->claim_returned(
139 created_by => $librarian->borrowernumber,
140 notes => 'Dummy notes'
144 my $claim_id = $claim->id;
146 # Test editing a claim note
149 "//$userid:$password@/api/v1/return_claims/$claim_id/notes" => json => {
150 notes => "This is a different test note.",
151 updated_by => $librarian->id,
155 $claim->discard_changes;
157 is( $claim->notes, "This is a different test note." );
158 is( $claim->updated_by, $librarian->id );
159 ok( $claim->updated_on );
161 # Make sure the claim doesn't exist on the DB anymore
166 "//$userid:$password@/api/v1/return_claims/$claim_id/notes" => json => {
167 notes => "This is a different test note.",
168 updated_by => $librarian->id,
171 ->json_is( '/error' => 'Claim not found' );
173 $schema->storage->txn_rollback;
176 subtest 'resolve_claim() tests' => sub {
180 $schema->storage->txn_begin;
182 my $librarian = $builder->build_object(
184 class => 'Koha::Patrons',
185 value => { flags => 1 }
188 my $password = 'thePassword123';
189 $librarian->set_password( { password => $password, skip_validation => 1 } );
190 my $userid = $librarian->userid;
192 my $item = $builder->build_sample_item;
194 t::lib::Mocks::mock_userenv( { branchcode => $item->homebranch } ); # needed by AddIssue
196 my $issue = AddIssue( $librarian->unblessed, $item->barcode, dt_from_string->add( weeks => 2 ) );
198 my $claim = $issue->claim_returned(
200 created_by => $librarian->borrowernumber,
201 notes => 'Dummy notes'
205 my $claim_id = $claim->id;
212 )->store; # resolve the claim must work even if the created_by patron has been removed
216 "//$userid:$password@/api/v1/return_claims/$claim_id/resolve" => json => {
217 resolved_by => $librarian->id,
218 resolution => "FOUNDINLIB",
222 $claim->discard_changes;
223 is( $claim->resolution, "FOUNDINLIB" );
224 is( $claim->resolved_by, $librarian->id );
225 is( $claim->updated_by, $librarian->id );
226 ok( $claim->resolved_on );
228 # Make sure the claim doesn't exist on the DB anymore
233 "//$userid:$password@/api/v1/return_claims/$claim_id/resolve" => json =>
235 resolved_by => $librarian->id,
236 resolution => "FOUNDINLIB",
239 ->json_is( '/error' => 'Claim not found' );
241 $schema->storage->txn_rollback;
244 subtest 'delete() tests' => sub {
248 $schema->storage->txn_begin;
250 my $librarian = $builder->build_object(
252 class => 'Koha::Patrons',
253 value => { flags => 1 }
256 my $password = 'thePassword123';
257 $librarian->set_password( { password => $password, skip_validation => 1 } );
258 my $userid = $librarian->userid;
260 my $item = $builder->build_sample_item;
262 t::lib::Mocks::mock_userenv({ branchcode => $item->homebranch });
264 my $issue = C4::Circulation::AddIssue( $librarian->unblessed,
265 $item->barcode, dt_from_string->add( weeks => 2 ) );
267 my $claim = $issue->claim_returned(
269 created_by => $librarian->borrowernumber,
270 notes => 'Dummy notes'
274 # Test deleting a return claim
275 $t->delete_ok("//$userid:$password@/api/v1/return_claims/" . $claim->id)
276 ->status_is( 204, 'SWAGGER3.2.4' )
277 ->content_is( '', 'SWAGGER3.3.4' );
279 my $THE_claim = Koha::Checkouts::ReturnClaims->find($claim->id);
280 isnt( $THE_claim, "Return claim was deleted" );
282 $t->delete_ok("//$userid:$password@/api/v1/return_claims/" . $claim->id)
284 ->json_is( '/error' => 'Claim not found' );
286 $schema->storage->txn_rollback;