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,
147 status => $request_status->{code},
148 backend => $backend->name,
153 my $req_2 = $builder->build_object(
155 class => 'Koha::Illrequests',
157 status => $request_status->{code},
158 backend => $backend->name,
159 status_alias => $av->authorised_value,
165 my $ret = $builder->build_object({ class => 'Koha::Illrequests', value => { status => 'RET' } });
167 # Three requests exist, expect all three to be returned
168 $t->get_ok("//$userid:$password@/api/v1/ill/requests")
170 ->json_is( [ $req_1->to_api, $req_2->to_api, $ret->to_api ]);
172 my $status_query = encode_json({ status => 'REQ' });
173 my $status_alias_query = encode_json({ status_av => $av_code });
175 # x-koha-embed: +strings
176 # Two requests exist with status 'REQ', expect them to be returned
177 # One of which also has a status_alias, expect that to be in that request's body
178 $t->get_ok("//$userid:$password@/api/v1/ill/requests?q=$status_query" => {"x-koha-embed" => "+strings"} )
181 { _strings => { status => $response_status }, %{$req_1->to_api} },
182 { _strings => { status => $response_status, status_av => $response_status_av }, %{$req_2->to_api} }
186 # One request with status_alias 'print_copy' exists, expect that to be returned
187 $t->get_ok("//$userid:$password@/api/v1/ill/requests?q=$status_alias_query" => {"x-koha-embed" => "+strings"} )
190 { _strings => { status => $response_status, status_av => $response_status_av }, %{$req_2->to_api} }
194 # x-koha-embed: patron
195 my $patron_query = encode_json({ borrowernumber => $patron->borrowernumber });
197 # One request related to $patron, make sure it comes back
198 $t->get_ok("//$userid:$password@/api/v1/ill/requests" => {"x-koha-embed" => "patron"} )
200 ->json_has('/0/patron', $patron->to_api);
202 # x-koha-embed: comments
204 my $comment_text = "This is the comment";
205 my $comment = $builder->build_object({ class => 'Koha::Illcomments', value => { illrequest_id => $req_1->illrequest_id, comment => $comment_text , borrowernumber => $patron->borrowernumber } } );
207 # Make sure comments come back
208 $t->get_ok("//$userid:$password@/api/v1/ill/requests" => {"x-koha-embed" => "comments"} )
210 ->json_has('/0/comments', $comment_text);
212 # x-koha-embed: id_prefix
213 # Mock Illrequest::Config to return a static prefix
214 my $id_prefix = 'ILL';
215 my $config = Test::MockObject->new;
216 $config->set_isa('Koha::Illrequest::Config::Mock');
217 $config->set_always('getPrefixes', $id_prefix);
219 # Make sure id_prefix comes back
220 $t->get_ok("//$userid:$password@/api/v1/ill/requests" => {"x-koha-embed" => "id_prefix"} )
222 ->json_has('/0/id_prefix', $id_prefix);
224 # ILLHiddenRequestStatuses syspref
225 # Hide 'REQ', expect to return just 1 'RET'
226 t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'REQ' );
227 $t->get_ok( "//$userid:$password@/api/v1/ill/requests" )
229 ->json_is( [ $ret->to_api ] );
231 # Hide 'RET', expect to return 2 'REQ'
232 t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'RET' );
233 $t->get_ok( "//$userid:$password@/api/v1/ill/requests?_order_by=staff_notes" )
235 ->json_is( [ $req_1->to_api, $req_2->to_api ]);
238 # Warn on unsupported query parameter
239 $t->get_ok( "//$userid:$password@/api/v1/ill/requests?request_blah=blah" )
242 [{ path => '/query/request_blah', message => 'Malformed query string'}]
245 # Unauthorized attempt to list
247 "//$unauth_userid:$password@/api/v1/ill/requests")
250 # DELETE method not supported
252 "//$unauth_userid:$password@/api/v1/ill/requests/1")
255 #TODO; test complex query on extended_attributes
257 $schema->storage->txn_rollback;
260 subtest 'add() tests' => sub {
264 $schema->storage->txn_begin;
266 # create an authorized user
267 my $patron = $builder->build_object({
268 class => 'Koha::Patrons',
269 value => { flags => 2 ** 22 } # 22 => ill
271 my $password = 'thePassword123';
272 $patron->set_password({ password => $password, skip_validation => 1 });
273 my $userid = $patron->userid;
275 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
277 # Create an ILL request
278 my $illrequest = $builder->build_object(
280 class => 'Koha::Illrequests',
283 branchcode => $library->branchcode,
284 borrowernumber => $patron->borrowernumber,
290 # Mock ILLBackend (as object)
291 my $backend = Test::MockObject->new;
292 $backend->set_isa('Koha::Illbackends::Mock');
293 $backend->set_always('name', 'Mock');
294 $backend->set_always('capabilities', sub {
300 my ( $self, $rq ) = @_;
302 ID => $rq->illrequest_id,
303 Title => $rq->patron->borrowernumber
308 'status_graph', sub {},
311 # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
312 my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
313 $illreqmodule->mock( 'load_backend',
314 sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
317 $schema->storage->txn_begin;
319 Koha::Illrequests->search->delete;
323 borrowernumber => $patron->borrowernumber,
324 branchcode => $library->branchcode,
326 article_author => "Jessop, E. G.",
327 article_title => "Sleep",
331 publisher => "OXFORD UNIVERSITY PRESS",
332 title => "Journal of public health medicine.",
337 ## Authorized user test
338 $t->post_ok( "//$userid:$password@/api/v1/illrequests" => json => $body)
341 $schema->storage->txn_rollback;