2123b5e0aec513be45ec0e63fa14ccfb10974ecb
[srvgit] / t / db_dependent / api / v1 / return_claims.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 4;
21
22 use Test::Mojo;
23 use Test::Warn;
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use C4::Circulation qw(AddIssue);
28
29 use Koha::Checkouts::ReturnClaims;
30 use Koha::Database;
31 use Koha::DateUtils qw(dt_from_string);
32
33 my $schema  = Koha::Database->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37 my $t = Test::Mojo->new('Koha::REST::V1');
38
39 subtest 'claim_returned() tests' => sub {
40
41     plan tests => 9;
42
43     $schema->storage->txn_begin;
44
45     my $librarian = $builder->build_object(
46         {
47             class => 'Koha::Patrons',
48             value => { flags => 1 }
49         }
50     );
51     my $password = 'thePassword123';
52     $librarian->set_password( { password => $password, skip_validation => 1 } );
53     my $userid = $librarian->userid;
54
55     my $patron = $builder->build_object(
56         {
57             class => 'Koha::Patrons',
58             value => { flags => 0 }
59         }
60     );
61
62     t::lib::Mocks::mock_userenv({ branchcode => $librarian->branchcode });
63
64     my $item  = $builder->build_sample_item;
65     my $issue = AddIssue( $patron->unblessed, $item->barcode, dt_from_string->add( weeks => 2 ) );
66
67     t::lib::Mocks::mock_preference( 'ClaimReturnedChargeFee', 'ask' );
68     t::lib::Mocks::mock_preference( 'ClaimReturnedLostValue', '99' );
69
70     ## Valid id
71     $t->post_ok(
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."
77         }
78     )->status_is(201)->header_like(
79         Location => qr|^\/api\/v1\/return_claims/\d*|,
80         'SWAGGER3.4.1'
81     );
82
83     my $claim_id = $t->tx->res->json->{claim_id};
84
85     ## Duplicate id
86     warning_like {
87         $t->post_ok(
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."
93             }
94         )->status_is(409)
95     }
96     qr/DBD::mysql::st execute failed: Duplicate entry/;
97
98     $issue->delete;
99
100     $t->post_ok(
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."
106         }
107     )->status_is(404)
108      ->json_is( '/error' => 'Checkout not found' );
109
110     $schema->storage->txn_rollback;
111 };
112
113 subtest 'update_notes() tests' => sub {
114
115     plan tests => 8;
116
117     $schema->storage->txn_begin;
118
119     my $librarian = $builder->build_object(
120         {
121             class => 'Koha::Patrons',
122             value => { flags => 1 }
123         }
124     );
125     my $password = 'thePassword123';
126     $librarian->set_password( { password => $password, skip_validation => 1 } );
127     my $userid = $librarian->userid;
128
129     my $item = $builder->build_sample_item;
130
131     t::lib::Mocks::mock_userenv( { branchcode => $item->homebranch } )
132       ;    # needed by AddIssue
133
134     my $issue = AddIssue( $librarian->unblessed, $item->barcode,
135         dt_from_string->add( weeks => 2 ) );
136
137     my $claim = $issue->claim_returned(
138         {
139             created_by => $librarian->borrowernumber,
140             notes      => 'Dummy notes'
141         }
142     );
143
144     my $claim_id = $claim->id;
145
146     # Test editing a claim note
147     ## Valid claim id
148     $t->put_ok(
149         "//$userid:$password@/api/v1/return_claims/$claim_id/notes" => json => {
150             notes      => "This is a different test note.",
151             updated_by => $librarian->id,
152         }
153     )->status_is(200);
154
155     $claim->discard_changes;
156
157     is( $claim->notes,      "This is a different test note." );
158     is( $claim->updated_by, $librarian->id );
159     ok( $claim->updated_on );
160
161     # Make sure the claim doesn't exist on the DB anymore
162     $claim->delete;
163
164     ## Bad claim id
165     $t->put_ok(
166         "//$userid:$password@/api/v1/return_claims/$claim_id/notes" => json => {
167             notes      => "This is a different test note.",
168             updated_by => $librarian->id,
169         }
170     )->status_is(404)
171      ->json_is( '/error' => 'Claim not found' );
172
173     $schema->storage->txn_rollback;
174 };
175
176 subtest 'resolve_claim() tests' => sub {
177
178     plan tests => 13;
179
180     $schema->storage->txn_begin;
181
182     my $librarian = $builder->build_object(
183         {
184             class => 'Koha::Patrons',
185             value => { flags => 1 }
186         }
187     );
188     my $password = 'thePassword123';
189     $librarian->set_password( { password => $password, skip_validation => 1 } );
190     my $userid = $librarian->userid;
191
192     my $item = $builder->build_sample_item;
193
194     t::lib::Mocks::mock_userenv( { branchcode => $item->homebranch } ); # needed by AddIssue
195
196     # Picking 1 that should exist
197     my $ClaimReturnedLostValue = 1;
198     t::lib::Mocks::mock_preference('ClaimReturnedLostValue', $ClaimReturnedLostValue);
199
200     my $issue = AddIssue( $librarian->unblessed, $item->barcode, dt_from_string->add( weeks => 2 ) );
201
202     my $claim = $issue->claim_returned(
203         {
204             created_by => $librarian->borrowernumber,
205             notes      => 'Dummy notes'
206         }
207     );
208
209     my $claim_id = $claim->id;
210
211     $claim->set(
212         {
213             created_by => undef,
214             updated_by => undef,
215         }
216     )->store; # resolve the claim must work even if the created_by patron has been removed
217
218     # Resolve a claim
219     $t->put_ok(
220         "//$userid:$password@/api/v1/return_claims/$claim_id/resolve" => json => {
221             resolved_by => $librarian->id,
222             resolution  => "FOUNDINLIB",
223         }
224     )->status_is(200);
225
226     $claim->discard_changes;
227     is( $claim->resolution, "FOUNDINLIB" );
228     is( $claim->resolved_by, $librarian->id );
229     is( $claim->updated_by, $librarian->id );
230     ok( $claim->resolved_on );
231
232     is( $claim->checkout->item->itemlost, $ClaimReturnedLostValue );
233
234     $claim->update({resolution => undef, resolved_by => undef, resolved_on => undef });
235     $t->put_ok(
236         "//$userid:$password@/api/v1/return_claims/$claim_id/resolve" => json => {
237             resolved_by => $librarian->id,
238             resolution  => "FOUNDINLIB",
239             new_lost_status => 0,
240         }
241     )->status_is(200);
242     is( $claim->get_from_storage->checkout->item->itemlost, 0 );
243
244
245     # Make sure the claim doesn't exist on the DB anymore
246     $claim->delete;
247
248     ## Invalid claim id
249     $t->put_ok(
250         "//$userid:$password@/api/v1/return_claims/$claim_id/resolve" => json =>
251         {
252             resolved_by => $librarian->id,
253             resolution  => "FOUNDINLIB",
254         }
255     )->status_is(404)
256      ->json_is( '/error' => 'Claim not found' );
257
258     $schema->storage->txn_rollback;
259 };
260
261 subtest 'delete() tests' => sub {
262
263     plan tests => 7;
264
265     $schema->storage->txn_begin;
266
267     my $librarian = $builder->build_object(
268         {
269             class => 'Koha::Patrons',
270             value => { flags => 1 }
271         }
272     );
273     my $password = 'thePassword123';
274     $librarian->set_password( { password => $password, skip_validation => 1 } );
275     my $userid = $librarian->userid;
276
277     my $item = $builder->build_sample_item;
278
279     t::lib::Mocks::mock_userenv({ branchcode => $item->homebranch });
280
281     my $issue = C4::Circulation::AddIssue( $librarian->unblessed,
282         $item->barcode, dt_from_string->add( weeks => 2 ) );
283
284     my $claim = $issue->claim_returned(
285         {
286             created_by => $librarian->borrowernumber,
287             notes      => 'Dummy notes'
288         }
289     );
290
291     # Test deleting a return claim
292     $t->delete_ok("//$userid:$password@/api/v1/return_claims/" . $claim->id)
293       ->status_is( 204, 'SWAGGER3.2.4' )
294       ->content_is( '', 'SWAGGER3.3.4' );
295
296     my $THE_claim = Koha::Checkouts::ReturnClaims->find($claim->id);
297     isnt( $THE_claim, "Return claim was deleted" );
298
299     $t->delete_ok("//$userid:$password@/api/v1/return_claims/" . $claim->id)
300       ->status_is(404)
301       ->json_is( '/error' => 'Claim not found' );
302
303     $schema->storage->txn_rollback;
304 };