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;
26 use JSON qw(encode_json);
28 use t::lib::TestBuilder;
31 use Koha::AuthorisedValueCategories;
32 use Koha::Illrequests;
33 use Koha::DateUtils qw( format_sqldatetime );
35 my $schema = Koha::Database->new->schema;
36 my $builder = t::lib::TestBuilder->new;
38 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
40 my $t = Test::Mojo->new('Koha::REST::V1');
42 subtest 'list() tests' => sub {
46 # Mock ILLBackend (as object)
47 my $backend = Test::MockObject->new;
48 $backend->set_isa('Koha::Illbackends::Mock');
49 $backend->set_always('name', 'Mock');
50 $backend->set_always('capabilities', sub { return 'bar'; } );
54 my ( $self, $rq ) = @_;
56 ID => $rq->illrequest_id,
57 Title => $rq->patron->borrowernumber
62 'status_graph', sub {},
65 # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
66 my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
67 $illreqmodule->mock( 'load_backend',
68 sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
71 $schema->storage->txn_begin;
73 Koha::Illrequests->search->delete;
75 # create an authorized user
76 my $librarian = $builder->build_object(
78 class => 'Koha::Patrons',
79 value => { flags => 2 ** 22 } # 22 => ill
82 my $password = 'thePassword123';
83 $librarian->set_password( { password => $password, skip_validation => 1 } );
84 my $userid = $librarian->userid;
86 # create an unauthorized user
87 my $patron = $builder->build_object(
89 class => 'Koha::Patrons',
90 value => { flags => 0 }
94 $patron->set_password( { password => $password, skip_validation => 1 } );
95 my $unauth_userid = $patron->userid;
97 # Make sure the ILL_STATUS_ALIAS authorised value category is defined
98 unless ( Koha::AuthorisedValueCategories->search( { category_name => 'ILL_STATUS_ALIAS' } )->count > 0 ) {
99 $builder->build_object(
100 { class => 'Koha::AuthorisedValueCategories', value => { category_name => 'ILL_STATUS_ALIAS' } } );
103 my $tag = "Print copy";
104 my $av_code = "print_copy";
105 my $av = $builder->build_object(
106 { class => 'Koha::AuthorisedValues',
108 category => 'ILL_STATUS_ALIAS',
109 authorised_value => $av_code,
115 # No requests, expect empty
116 $t->get_ok("//$userid:$password@/api/v1/ill/requests")
121 # Prepare some expected response structure
122 my $request_status = {
127 my $response_status = {
128 backend => $backend->name,
129 code =>$request_status->{code},
130 str =>$request_status->{str},
134 my $response_status_av = {
135 category => "ILL_STATUS_ALIAS",
141 # Create some ILL requests
142 my $req_1 = $builder->build_object(
144 class => 'Koha::Illrequests',
146 borrowernumber => $patron->borrowernumber,
148 status => $request_status->{code},
149 backend => $backend->name,
154 my $req_2 = $builder->build_object(
156 class => 'Koha::Illrequests',
159 status => $request_status->{code},
160 backend => $backend->name,
161 status_alias => $av->authorised_value,
167 my $ret = $builder->build_object({ class => 'Koha::Illrequests', value => { status => 'RET' } });
169 # Three requests exist, expect all three to be returned
170 $t->get_ok("//$userid:$password@/api/v1/ill/requests")
172 ->json_is( [ $req_1->to_api({user=> $librarian}), $req_2->to_api({user=> $librarian}), $ret->to_api({user=> $librarian}) ]);
174 my $status_query = encode_json({ status => 'REQ' });
175 my $status_alias_query = encode_json({ status_av => $av_code });
177 # x-koha-embed: +strings
178 # Two requests exist with status 'REQ', expect them to be returned
179 # One of which also has a status_alias, expect that to be in that request's body
180 $t->get_ok( "//$userid:$password@/api/v1/ill/requests?q=$status_query" => { "x-koha-embed" => "+strings" } )
181 ->status_is(200)->json_is(
183 { _strings => { status => $response_status }, %{ $req_1->to_api( { user => $librarian } ) } },
185 _strings => { status => $response_status, status_av => $response_status_av },
186 %{ $req_2->to_api( { user => $librarian } ) }
191 # One request with status_alias 'print_copy' exists, expect that to be returned
192 $t->get_ok( "//$userid:$password@/api/v1/ill/requests?q=$status_alias_query" => { "x-koha-embed" => "+strings" } )
193 ->status_is(200)->json_is(
196 _strings => { status => $response_status, status_av => $response_status_av },
197 %{ $req_2->to_api( { user => $librarian } ) }
202 # x-koha-embed: patron
203 my $patron_query = encode_json({ borrowernumber => $patron->borrowernumber });
205 # One request related to $patron, make sure it comes back
206 $t->get_ok( "//$userid:$password@/api/v1/ill/requests" => { "x-koha-embed" => "patron" } )->status_is(200)
207 ->json_has( '/0/patron', $patron->to_api( { user => $librarian } ) );
209 # x-koha-embed: comments
211 my $comment_text = "This is the comment";
212 my $comment = $builder->build_object({ class => 'Koha::ILL::Comments', value => { illrequest_id => $req_1->illrequest_id, comment => $comment_text , borrowernumber => $patron->borrowernumber } } );
214 # Make sure comments come back
215 $t->get_ok("//$userid:$password@/api/v1/ill/requests" => {"x-koha-embed" => "comments"} )
217 ->json_has('/0/comments', $comment_text);
219 # x-koha-embed: id_prefix
220 # Mock Illrequest::Config to return a static prefix
221 my $id_prefix = 'ILL';
222 my $config = Test::MockObject->new;
223 $config->set_isa('Koha::Illrequest::Config::Mock');
224 $config->set_always('getPrefixes', $id_prefix);
226 # Make sure id_prefix comes back
227 $t->get_ok("//$userid:$password@/api/v1/ill/requests" => {"x-koha-embed" => "id_prefix"} )
229 ->json_has('/0/id_prefix', $id_prefix);
231 # ILLHiddenRequestStatuses syspref
232 # Hide 'REQ', expect to return just 1 'RET'
233 t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'REQ' );
234 $t->get_ok("//$userid:$password@/api/v1/ill/requests")->status_is(200)
235 ->json_is( [ $ret->to_api( { user => $librarian } ) ] );
237 # Hide 'RET', expect to return 2 'REQ'
238 t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'RET' );
239 $t->get_ok("//$userid:$password@/api/v1/ill/requests?_order_by=staff_notes")->status_is(200)
240 ->json_is( [ $req_1->to_api( { user => $librarian } ), $req_2->to_api( { user => $librarian } ) ] );
243 # Warn on unsupported query parameter
244 $t->get_ok( "//$userid:$password@/api/v1/ill/requests?request_blah=blah" )
247 [{ path => '/query/request_blah', message => 'Malformed query string'}]
250 # Unauthorized attempt to list
252 "//$unauth_userid:$password@/api/v1/ill/requests")
255 # DELETE method not supported
257 "//$unauth_userid:$password@/api/v1/ill/requests/1")
260 #TODO; test complex query on extended_attributes
262 $schema->storage->txn_rollback;
265 subtest 'add() tests' => sub {
269 $schema->storage->txn_begin;
271 # create an authorized user
272 my $patron = $builder->build_object({
273 class => 'Koha::Patrons',
274 value => { flags => 2 ** 22 } # 22 => ill
276 my $password = 'thePassword123';
277 $patron->set_password({ password => $password, skip_validation => 1 });
278 my $userid = $patron->userid;
280 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
282 # Create an ILL request
283 my $illrequest = $builder->build_object(
285 class => 'Koha::Illrequests',
288 branchcode => $library->branchcode,
289 borrowernumber => $patron->borrowernumber,
295 # Mock ILLBackend (as object)
296 my $backend = Test::MockObject->new;
297 $backend->set_isa('Koha::Illbackends::Mock');
298 $backend->set_always('name', 'Mock');
303 my ( $self, $rq ) = @_;
305 ID => $rq->illrequest_id,
306 Title => $rq->patron->borrowernumber
311 'status_graph', sub {},
314 # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
315 my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
318 sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
325 $self->{_my_backend} = $backend if ($backend);
334 my ( $self, $name ) = @_;
339 my ($body, $request ) = @_;
341 my $api_req = $builder->build_object(
343 class => 'Koha::Illrequests',
345 borrowernumber => $patron->borrowernumber,
348 backend => $backend->name,
357 return $capabilities->{$name};
361 $schema->storage->txn_begin;
363 Koha::Illrequests->search->delete;
366 ill_backend_id => 'Mock',
367 patron_id => $patron->borrowernumber,
368 library_id => $library->branchcode
371 ## Authorized user test
372 $t->post_ok( "//$userid:$password@/api/v1/ill/requests" => json => $body)
375 $schema->storage->txn_rollback;