Lines 17-37
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 32; |
20 |
use Test::More tests => 4; |
21 |
use Test::MockModule; |
21 |
|
22 |
use Test::Mojo; |
22 |
use Test::Mojo; |
23 |
use Test::Warn; |
23 |
use Test::Warn; |
24 |
use t::lib::Mocks; |
24 |
use t::lib::Mocks; |
25 |
use t::lib::TestBuilder; |
25 |
use t::lib::TestBuilder; |
26 |
|
26 |
|
27 |
use DateTime; |
27 |
use C4::Circulation qw(AddIssue); |
28 |
|
|
|
29 |
use C4::Context; |
30 |
use C4::Circulation; |
31 |
|
28 |
|
32 |
use Koha::Checkouts::ReturnClaims; |
29 |
use Koha::Checkouts::ReturnClaims; |
33 |
use Koha::Database; |
30 |
use Koha::Database; |
34 |
use Koha::DateUtils; |
31 |
use Koha::DateUtils qw(dt_from_string); |
35 |
|
32 |
|
36 |
my $schema = Koha::Database->schema; |
33 |
my $schema = Koha::Database->schema; |
37 |
my $builder = t::lib::TestBuilder->new; |
34 |
my $builder = t::lib::TestBuilder->new; |
Lines 39-114
my $builder = t::lib::TestBuilder->new;
Link Here
|
39 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
36 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
40 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
37 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
41 |
|
38 |
|
42 |
$schema->storage->txn_begin; |
39 |
subtest 'claim_returned() tests' => sub { |
43 |
|
40 |
|
44 |
my $dbh = C4::Context->dbh; |
41 |
plan tests => 9; |
45 |
|
42 |
|
46 |
my $librarian = $builder->build_object( |
43 |
$schema->storage->txn_begin; |
47 |
{ |
44 |
|
48 |
class => 'Koha::Patrons', |
45 |
my $librarian = $builder->build_object( |
49 |
value => { flags => 1 } |
46 |
{ |
50 |
} |
47 |
class => 'Koha::Patrons', |
51 |
); |
48 |
value => { flags => 1 } |
52 |
my $password = 'thePassword123'; |
49 |
} |
53 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
50 |
); |
54 |
my $userid = $librarian->userid; |
51 |
my $password = 'thePassword123'; |
55 |
|
52 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
56 |
my $patron = $builder->build_object( |
53 |
my $userid = $librarian->userid; |
57 |
{ |
54 |
|
58 |
class => 'Koha::Patrons', |
55 |
my $patron = $builder->build_object( |
59 |
value => { flags => 0 } |
56 |
{ |
60 |
} |
57 |
class => 'Koha::Patrons', |
61 |
); |
58 |
value => { flags => 0 } |
62 |
my $unauth_password = 'thePassword000'; |
59 |
} |
63 |
$patron->set_password( |
60 |
); |
64 |
{ password => $unauth_password, skip_validattion => 1 } ); |
|
|
65 |
my $unauth_userid = $patron->userid; |
66 |
my $patron_id = $patron->borrowernumber; |
67 |
|
68 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
69 |
my $module = new Test::MockModule('C4::Context'); |
70 |
$module->mock( 'userenv', sub { { branch => $branchcode } } ); |
71 |
|
72 |
my $item1 = $builder->build_sample_item; |
73 |
my $itemnumber1 = $item1->itemnumber; |
74 |
|
75 |
my $date_due = DateTime->now->add( weeks => 2 ); |
76 |
my $issue1 = |
77 |
C4::Circulation::AddIssue( $patron->unblessed, $item1->barcode, $date_due ); |
78 |
|
79 |
t::lib::Mocks::mock_preference( 'ClaimReturnedChargeFee', 'ask' ); |
80 |
t::lib::Mocks::mock_preference( 'ClaimReturnedLostValue', '99' ); |
81 |
|
82 |
# Test creating a return claim |
83 |
## Invalid id |
84 |
$t->post_ok( |
85 |
"//$userid:$password@/api/v1/return_claims" => json => { |
86 |
item_id => 1, |
87 |
charge_lost_fee => Mojo::JSON->false, |
88 |
created_by => $librarian->id, |
89 |
notes => "This is a test note." |
90 |
} |
91 |
)->status_is(404) |
92 |
->json_is( '/error' => 'Checkout not found' ); |
93 |
|
94 |
## Valid id |
95 |
$t->post_ok( |
96 |
"//$userid:$password@/api/v1/return_claims" => json => { |
97 |
item_id => $itemnumber1, |
98 |
charge_lost_fee => Mojo::JSON->false, |
99 |
created_by => $librarian->id, |
100 |
notes => "This is a test note." |
101 |
} |
102 |
)->status_is(201) |
103 |
->header_like( Location => qr|^\/api\/v1\/return_claims/\d*|, 'SWAGGER3.4.1'); |
104 |
|
61 |
|
105 |
my $claim_id = $t->tx->res->json->{claim_id}; |
62 |
t::lib::Mocks::mock_userenv({ branchcode => $librarian->branchcode }); |
106 |
|
63 |
|
107 |
## Duplicate id |
64 |
my $item = $builder->build_sample_item; |
108 |
warning_like { |
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 { |
109 |
$t->post_ok( |
87 |
$t->post_ok( |
110 |
"//$userid:$password@/api/v1/return_claims" => json => { |
88 |
"//$userid:$password@/api/v1/return_claims" => json => { |
111 |
item_id => $itemnumber1, |
89 |
item_id => $item->itemnumber, |
112 |
charge_lost_fee => Mojo::JSON->false, |
90 |
charge_lost_fee => Mojo::JSON->false, |
113 |
created_by => $librarian->id, |
91 |
created_by => $librarian->id, |
114 |
notes => "This is a test note." |
92 |
notes => "This is a test note." |
Lines 117-173
warning_like {
Link Here
|
117 |
} |
95 |
} |
118 |
qr/^DBD::mysql::st execute failed: Duplicate entry/; |
96 |
qr/^DBD::mysql::st execute failed: Duplicate entry/; |
119 |
|
97 |
|
120 |
# Test editing a claim note |
98 |
$issue->delete; |
121 |
## Valid claim id |
99 |
|
122 |
$t->put_ok( |
100 |
$t->post_ok( |
123 |
"//$userid:$password@/api/v1/return_claims/$claim_id/notes" => json => { |
101 |
"//$userid:$password@/api/v1/return_claims" => json => { |
124 |
notes => "This is a different test note.", |
102 |
item_id => $item->itemnumber, |
125 |
updated_by => $librarian->id, |
103 |
charge_lost_fee => Mojo::JSON->false, |
126 |
} |
104 |
created_by => $librarian->id, |
127 |
)->status_is(200); |
105 |
notes => "This is a test note." |
128 |
my $claim = Koha::Checkouts::ReturnClaims->find($claim_id); |
106 |
} |
129 |
is( $claim->notes, "This is a different test note." ); |
107 |
)->status_is(404) |
130 |
is( $claim->updated_by, $librarian->id ); |
108 |
->json_is( '/error' => 'Checkout not found' ); |
131 |
ok( $claim->updated_on ); |
109 |
|
132 |
|
110 |
$schema->storage->txn_rollback; |
133 |
## Bad claim id |
111 |
}; |
134 |
$t->put_ok( |
112 |
|
135 |
"//$userid:$password@/api/v1/return_claims/99999999999/notes" => json => { |
113 |
subtest 'update_notes() tests' => sub { |
136 |
notes => "This is a different test note.", |
114 |
|
137 |
updated_by => $librarian->id, |
115 |
plan tests => 8; |
138 |
} |
116 |
|
139 |
)->status_is(404) |
117 |
$schema->storage->txn_begin; |
140 |
->json_is( '/error' => 'Claim not found' ); |
118 |
|
141 |
|
119 |
my $librarian = $builder->build_object( |
142 |
# Resolve a claim |
120 |
{ |
143 |
## Valid claim id |
121 |
class => 'Koha::Patrons', |
144 |
$t->put_ok( |
122 |
value => { flags => 1 } |
145 |
"//$userid:$password@/api/v1/return_claims/$claim_id/resolve" => json => { |
123 |
} |
146 |
resolved_by => $librarian->id, |
124 |
); |
147 |
resolution => "FOUNDINLIB", |
125 |
my $password = 'thePassword123'; |
148 |
} |
126 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
149 |
)->status_is(200); |
127 |
my $userid = $librarian->userid; |
150 |
$claim = Koha::Checkouts::ReturnClaims->find($claim_id); |
128 |
|
151 |
is( $claim->resolution, "FOUNDINLIB" ); |
129 |
my $item = $builder->build_sample_item; |
152 |
is( $claim->updated_by, $librarian->id ); |
130 |
|
153 |
ok( $claim->resolved_on ); |
131 |
t::lib::Mocks::mock_userenv( { branchcode => $item->homebranch } ) |
154 |
|
132 |
; # needed by AddIssue |
155 |
## Invalid claim id |
133 |
|
156 |
$t->put_ok( |
134 |
my $issue = AddIssue( $librarian->unblessed, $item->barcode, |
157 |
"//$userid:$password@/api/v1/return_claims/999999999999/resolve" => json => { |
135 |
dt_from_string->add( weeks => 2 ) ); |
158 |
resolved_by => $librarian->id, |
136 |
|
159 |
resolution => "FOUNDINLIB", |
137 |
my $claim = $issue->claim_returned( |
160 |
} |
138 |
{ |
161 |
)->status_is(404) |
139 |
created_by => $librarian->borrowernumber, |
162 |
->json_is( '/error' => 'Claim not found' ); |
140 |
notes => 'Dummy notes' |
163 |
|
141 |
} |
164 |
# Test deleting a return claim |
142 |
); |
165 |
$t->delete_ok("//$userid:$password@/api/v1/return_claims/$claim_id") |
143 |
|
166 |
->status_is( 204, 'SWAGGER3.2.4' ) |
144 |
my $claim_id = $claim->id; |
167 |
->content_is( '', 'SWAGGER3.3.4' ); |
145 |
|
168 |
$claim = Koha::Checkouts::ReturnClaims->find($claim_id); |
146 |
# Test editing a claim note |
169 |
isnt( $claim, "Return claim was deleted" ); |
147 |
## Valid claim id |
170 |
|
148 |
$t->put_ok( |
171 |
$t->delete_ok("//$userid:$password@/api/v1/return_claims/$claim_id") |
149 |
"//$userid:$password@/api/v1/return_claims/$claim_id/notes" => json => { |
172 |
->status_is(404) |
150 |
notes => "This is a different test note.", |
173 |
->json_is( '/error' => 'Claim not found' ); |
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 => 8; |
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 |
my $issue = AddIssue( $librarian->unblessed, $item->barcode, dt_from_string->add( weeks => 2 ) ); |
197 |
|
198 |
my $claim = $issue->claim_returned( |
199 |
{ |
200 |
created_by => $librarian->borrowernumber, |
201 |
notes => 'Dummy notes' |
202 |
} |
203 |
); |
204 |
|
205 |
my $claim_id = $claim->id; |
206 |
|
207 |
# Resolve a claim |
208 |
$t->put_ok( |
209 |
"//$userid:$password@/api/v1/return_claims/$claim_id/resolve" => json => { |
210 |
resolved_by => $librarian->id, |
211 |
resolution => "FOUNDINLIB", |
212 |
} |
213 |
)->status_is(200); |
214 |
|
215 |
$claim->discard_changes; |
216 |
is( $claim->resolution, "FOUNDINLIB" ); |
217 |
is( $claim->resolved_by, $librarian->id ); |
218 |
ok( $claim->resolved_on ); |
219 |
|
220 |
# Make sure the claim doesn't exist on the DB anymore |
221 |
$claim->delete; |
222 |
|
223 |
## Invalid claim id |
224 |
$t->put_ok( |
225 |
"//$userid:$password@/api/v1/return_claims/$claim_id/resolve" => json => |
226 |
{ |
227 |
resolved_by => $librarian->id, |
228 |
resolution => "FOUNDINLIB", |
229 |
} |
230 |
)->status_is(404) |
231 |
->json_is( '/error' => 'Claim not found' ); |
232 |
|
233 |
$schema->storage->txn_rollback; |
234 |
}; |
235 |
|
236 |
subtest 'delete() tests' => sub { |
237 |
|
238 |
plan tests => 7; |
239 |
|
240 |
$schema->storage->txn_begin; |
241 |
|
242 |
my $librarian = $builder->build_object( |
243 |
{ |
244 |
class => 'Koha::Patrons', |
245 |
value => { flags => 1 } |
246 |
} |
247 |
); |
248 |
my $password = 'thePassword123'; |
249 |
$librarian->set_password( { password => $password, skip_validation => 1 } ); |
250 |
my $userid = $librarian->userid; |
251 |
|
252 |
my $item = $builder->build_sample_item; |
253 |
|
254 |
t::lib::Mocks::mock_userenv({ branchcode => $item->homebranch }); |
255 |
|
256 |
my $issue = C4::Circulation::AddIssue( $librarian->unblessed, |
257 |
$item->barcode, dt_from_string->add( weeks => 2 ) ); |
258 |
|
259 |
my $claim = $issue->claim_returned( |
260 |
{ |
261 |
created_by => $librarian->borrowernumber, |
262 |
notes => 'Dummy notes' |
263 |
} |
264 |
); |
265 |
|
266 |
# Test deleting a return claim |
267 |
$t->delete_ok("//$userid:$password@/api/v1/return_claims/" . $claim->id) |
268 |
->status_is( 204, 'SWAGGER3.2.4' ) |
269 |
->content_is( '', 'SWAGGER3.3.4' ); |
270 |
|
271 |
my $THE_claim = Koha::Checkouts::ReturnClaims->find($claim->id); |
272 |
isnt( $THE_claim, "Return claim was deleted" ); |
273 |
|
274 |
$t->delete_ok("//$userid:$password@/api/v1/return_claims/" . $claim->id) |
275 |
->status_is(404) |
276 |
->json_is( '/error' => 'Claim not found' ); |
277 |
|
278 |
$schema->storage->txn_rollback; |
279 |
}; |
174 |
- |
|
|