3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Test::More tests => 1;
26 use t::lib::TestBuilder;
30 use Koha::Illrequests;
31 use Koha::DateUtils qw( format_sqldatetime );
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
38 my $remote_address = '127.0.0.1';
39 my $t = Test::Mojo->new('Koha::REST::V1');
41 subtest 'list() 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;
73 # ill => 22 (userflags.sql)
74 my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 22 });
76 ## Authorized user tests
77 # No requests, so empty array should be returned
78 my $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
79 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
80 $tx->req->env( { REMOTE_ADDR => $remote_address } );
81 $t->request_ok($tx)->status_is(200)->json_is( [] );
83 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
84 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
86 # Create an ILL request
87 my $illrequest = $builder->build_object(
89 class => 'Koha::Illrequests',
92 branchcode => $library->branchcode,
93 borrowernumber => $patron->borrowernumber
98 # The api response is always augmented with the id_prefix
99 my $response = $illrequest->unblessed;
100 $response->{id_prefix} = $illrequest->id_prefix;
102 my $req_formatted = add_formatted($response);
104 # One illrequest created, should get returned
105 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
106 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
107 $tx->req->env( { REMOTE_ADDR => $remote_address } );
108 $t->request_ok($tx)->status_is(200)->json_is( [ $req_formatted ] );
110 # One illrequest created, returned with augmented data
111 $tx = $t->ua->build_tx( GET =>
112 '/api/v1/illrequests?embed=patron,library,capabilities,metadata' );
113 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
114 $tx->req->env( { REMOTE_ADDR => $remote_address } );
115 $t->request_ok($tx)->status_is(200)
116 ->json_has( '/0/patron', 'patron embedded' )
117 ->json_is( '/0/patron/patron_id', $patron->borrowernumber, 'The right patron is embeded')
118 ->json_has( '/0/capabilities', 'capabilities embedded' )
119 ->json_has( '/0/library', 'library embedded' )
120 ->json_has( '/0/metadata', 'metadata embedded' )
121 ->json_hasnt( '/1', 'Only one request was created' );
123 # Create another ILL request
124 my $illrequest2 = $builder->build_object(
126 class => 'Koha::Illrequests',
129 branchcode => $library->branchcode,
130 borrowernumber => $patron->borrowernumber
135 # The api response is always augmented with the id_prefix
136 my $response2 = $illrequest2->unblessed;
137 $response2->{id_prefix} = $illrequest2->id_prefix;
139 my $req2_formatted = add_formatted($response2);
141 # Two illrequest created, should get returned
142 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
143 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
144 $tx->req->env( { REMOTE_ADDR => $remote_address } );
145 $t->request_ok($tx)->status_is(200)
146 ->json_is( [ $req_formatted, $req2_formatted ] );
148 # Warn on unsupported query parameter
149 $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
150 $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
151 $tx->req->env( { REMOTE_ADDR => $remote_address } );
152 $t->request_ok($tx)->status_is(400)->json_is(
153 [{ path => '/query/request_blah', message => 'Malformed query string'}]
156 $schema->storage->txn_rollback;
161 my @format_dates = ( 'placed', 'updated', 'completed' );
162 # We need to embellish the request with properties that the API
163 # controller calculates on the fly
164 # Create new "formatted" columns for each date column
165 # that needs formatting
166 foreach my $field(@format_dates) {
167 if (defined $req->{$field}) {
168 $req->{$field . "_formatted"} = format_sqldatetime(
179 sub create_user_and_session {
182 my $dbh = C4::Context->dbh;
184 my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
186 my $user = $builder->build(
188 source => 'Borrower',
195 # Create a session for the authorized user
196 my $session = C4::Auth::get_session('');
197 $session->param( 'number', $user->{borrowernumber} );
198 $session->param( 'id', $user->{userid} );
199 $session->param( 'ip', '127.0.0.1' );
200 $session->param( 'lasttime', time() );
203 return ( $user->{borrowernumber}, $session->id );