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::Illrequests;
32 use Koha::DateUtils qw( format_sqldatetime );
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
37 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
39 my $t = Test::Mojo->new('Koha::REST::V1');
41 subtest 'list_legacy() tests' => sub {
45 # Mock ILLBackend (as object)
46 my $backend = Test::MockObject->new;
47 $backend->set_isa('Koha::Illbackends::Mock');
48 $backend->set_always('name', 'Mock');
49 $backend->set_always('capabilities', sub { return 'bar'; } );
53 my ( $self, $rq ) = @_;
55 ID => $rq->illrequest_id,
56 Title => $rq->patron->borrowernumber
61 'status_graph', sub {},
64 # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
65 my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
66 $illreqmodule->mock( 'load_backend',
67 sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
70 $schema->storage->txn_begin;
72 Koha::Illrequests->search->delete;
74 # create an authorized user
75 my $patron = $builder->build_object({
76 class => 'Koha::Patrons',
77 value => { flags => 2 ** 22 } # 22 => ill
79 my $password = 'thePassword123';
80 $patron->set_password({ password => $password, skip_validation => 1 });
81 my $userid = $patron->userid;
83 ## Authorized user tests
84 # No requests, so empty array should be returned
85 $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
89 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
90 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
91 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
93 # Create an ILL request
94 my $illrequest = $builder->build_object(
96 class => 'Koha::Illrequests',
100 branchcode => $library->branchcode,
101 borrowernumber => $patron_1->borrowernumber,
107 # The api response is always augmented with the id_prefix
108 my $response = $illrequest->unblessed;
109 $response->{id_prefix} = $illrequest->id_prefix;
111 my $req_formatted = add_formatted($response);
113 # One illrequest created, should get returned
114 $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
116 ->json_is( [ $req_formatted ] );
118 # One illrequest created, returned with augmented data
119 $t->get_ok( "//$userid:$password@/api/v1/illrequests?embed=patron,library,capabilities,metadata,requested_partners" )
121 ->json_has( '/0/patron', 'patron embedded' )
122 ->json_is( '/0/patron/patron_id', $patron_1->borrowernumber, 'The right patron is embeded')
123 ->json_has( '/0/requested_partners', 'requested_partners embedded' )
124 ->json_has( '/0/capabilities', 'capabilities embedded' )
125 ->json_has( '/0/library', 'library embedded' )
126 ->json_has( '/0/metadata', 'metadata embedded' )
127 ->json_hasnt( '/1', 'Only one request was created' );
129 # Create another ILL request
130 my $illrequest2 = $builder->build_object(
132 class => 'Koha::Illrequests',
136 branchcode => $library->branchcode,
137 borrowernumber => $patron_2->borrowernumber,
143 # The api response is always augmented with the id_prefix
144 my $response2 = $illrequest2->unblessed;
145 $response2->{id_prefix} = $illrequest2->id_prefix;
147 my $req2_formatted = add_formatted($response2);
149 # Two illrequest created, should get returned
150 $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
152 ->json_is( [ $req_formatted, $req2_formatted ] );
154 # Warn on unsupported query parameter
155 $t->get_ok( "//$userid:$password@/api/v1/illrequests?request_blah=blah" )
158 [{ path => '/query/request_blah', message => 'Malformed query string'}]
161 # Test the borrowernumber parameter
162 $t->get_ok( "//$userid:$password@/api/v1/illrequests?borrowernumber=" . $patron_2->borrowernumber )
164 ->json_is( [ $response2 ] );
166 # Test the ILLHiddenRequestStatuses syspref
167 t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS1' );
168 $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
170 ->json_is( [ $req2_formatted ] );
172 t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS2' );
173 $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
175 ->json_is( [ $req_formatted ] );
177 $schema->storage->txn_rollback;
180 subtest 'list() tests' => sub {
184 $schema->storage->txn_begin;
186 Koha::Illrequests->search->delete;
188 my $librarian = $builder->build_object(
190 class => 'Koha::Patrons',
191 value => { flags => 2 ** 22 } # 22 => ill
194 my $password = 'thePassword123';
195 $librarian->set_password( { password => $password, skip_validation => 1 } );
196 my $userid = $librarian->userid;
198 my $patron = $builder->build_object(
200 class => 'Koha::Patrons',
201 value => { flags => 0 }
205 $patron->set_password( { password => $password, skip_validation => 1 } );
206 my $unauth_userid = $patron->userid;
208 $t->get_ok("//$userid:$password@/api/v1/ill_requests")
212 my $req_1 = $builder->build_object({ class => 'Koha::Illrequests', value => { biblio_id => undef, status => 'REQ' } });
213 my $req_2 = $builder->build_object({ class => 'Koha::Illrequests', value => { biblio_id => undef, status => 'REQ' } } );
214 my $ret = $builder->build_object({ class => 'Koha::Illrequests', value => { biblio_id => undef, status => 'RET' } } );
216 $t->get_ok("//$userid:$password@/api/v1/ill_requests")
218 ->json_is( [ $req_1->to_api, $req_2->to_api, $ret->to_api ]);
220 my $query = encode_json({ status => 'REQ' });
223 $t->get_ok("//$userid:$password@/api/v1/ill_requests?q=$query" )
225 ->json_is( [ $req_1->to_api, $req_2->to_api ]);
227 $schema->storage->txn_rollback;
232 my @format_dates = ( 'placed', 'updated', 'completed' );
233 # We need to embellish the request with properties that the API
234 # controller calculates on the fly
235 # Create new "formatted" columns for each date column
236 # that needs formatting
237 foreach my $field(@format_dates) {
238 if (defined $req->{$field}) {
239 $req->{$field . "_formatted"} = format_sqldatetime(