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 => 1;
26 use t::lib::TestBuilder;
29 use Koha::Illrequests;
30 use Koha::DateUtils qw( format_sqldatetime );
32 my $schema = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
37 my $t = Test::Mojo->new('Koha::REST::V1');
39 subtest 'list() tests' => sub {
43 # Mock ILLBackend (as object)
44 my $backend = Test::MockObject->new;
45 $backend->set_isa('Koha::Illbackends::Mock');
46 $backend->set_always('name', 'Mock');
47 $backend->set_always('capabilities', sub { return 'bar'; } );
51 my ( $self, $rq ) = @_;
53 ID => $rq->illrequest_id,
54 Title => $rq->patron->borrowernumber
59 'status_graph', sub {},
62 # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
63 my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
64 $illreqmodule->mock( 'load_backend',
65 sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
68 $schema->storage->txn_begin;
70 Koha::Illrequests->search->delete;
72 # create an authorized user
73 my $patron = $builder->build_object({
74 class => 'Koha::Patrons',
75 value => { flags => 2 ** 22 } # 22 => ill
77 my $password = 'thePassword123';
78 $patron->set_password({ password => $password, skip_validation => 1 });
79 my $userid = $patron->userid;
81 ## Authorized user tests
82 # No requests, so empty array should be returned
83 $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
87 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
88 my $patron_1 = $builder->build_object( { class => 'Koha::Patrons' } );
89 my $patron_2 = $builder->build_object( { class => 'Koha::Patrons' } );
91 # Create an ILL request
92 my $illrequest = $builder->build_object(
94 class => 'Koha::Illrequests',
97 branchcode => $library->branchcode,
98 borrowernumber => $patron_1->borrowernumber,
104 # The api response is always augmented with the id_prefix
105 my $response = $illrequest->unblessed;
106 $response->{id_prefix} = $illrequest->id_prefix;
108 my $req_formatted = add_formatted($response);
110 # One illrequest created, should get returned
111 $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
113 ->json_is( [ $req_formatted ] );
115 # One illrequest created, returned with augmented data
116 $t->get_ok( "//$userid:$password@/api/v1/illrequests?embed=patron,library,capabilities,metadata,requested_partners" )
118 ->json_has( '/0/patron', 'patron embedded' )
119 ->json_is( '/0/patron/patron_id', $patron_1->borrowernumber, 'The right patron is embeded')
120 ->json_has( '/0/requested_partners', 'requested_partners embedded' )
121 ->json_has( '/0/capabilities', 'capabilities embedded' )
122 ->json_has( '/0/library', 'library embedded' )
123 ->json_has( '/0/metadata', 'metadata embedded' )
124 ->json_hasnt( '/1', 'Only one request was created' );
126 # Create another ILL request
127 my $illrequest2 = $builder->build_object(
129 class => 'Koha::Illrequests',
132 branchcode => $library->branchcode,
133 borrowernumber => $patron_2->borrowernumber,
139 # The api response is always augmented with the id_prefix
140 my $response2 = $illrequest2->unblessed;
141 $response2->{id_prefix} = $illrequest2->id_prefix;
143 my $req2_formatted = add_formatted($response2);
145 # Two illrequest created, should get returned
146 $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
148 ->json_is( [ $req_formatted, $req2_formatted ] );
150 # Warn on unsupported query parameter
151 $t->get_ok( "//$userid:$password@/api/v1/illrequests?request_blah=blah" )
154 [{ path => '/query/request_blah', message => 'Malformed query string'}]
157 # Test the borrowernumber parameter
158 $t->get_ok( "//$userid:$password@/api/v1/illrequests?borrowernumber=" . $patron_2->borrowernumber )
160 ->json_is( [ $response2 ] );
162 # Test the ILLHiddenRequestStatuses syspref
163 t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS1' );
164 $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
166 ->json_is( [ $req2_formatted ] );
168 t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS2' );
169 $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
171 ->json_is( [ $req_formatted ] );
173 $schema->storage->txn_rollback;
178 my @format_dates = ( 'placed', 'updated', 'completed' );
179 # We need to embellish the request with properties that the API
180 # controller calculates on the fly
181 # Create new "formatted" columns for each date column
182 # that needs formatting
183 foreach my $field(@format_dates) {
184 if (defined $req->{$field}) {
185 $req->{$field . "_formatted"} = format_sqldatetime(