Bug 20996: Remove warning 'Un-mocked method'
[koha.git] / t / db_dependent / api / v1 / illrequests.t
1 #!/usr/bin/env perl
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Test::More tests => 1;
21 use Test::MockModule;
22 use Test::MockObject;
23 use Test::Mojo;
24 use Test::Warn;
25
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
28
29 use C4::Auth;
30 use Koha::Illrequests;
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' );
36
37 my $remote_address = '127.0.0.1';
38 my $t              = Test::Mojo->new('Koha::REST::V1');
39
40 subtest 'list() tests' => sub {
41
42     plan tests => 18;
43
44     # Mock ILLBackend (as object)
45     my $backend = Test::MockObject->new;
46     $backend->set_isa('Koha::Illbackends::Mock');
47     $backend->set_always('name', 'Mock');
48     $backend->set_always('capabilities', sub { return 'bar'; } );
49     $backend->mock(
50         'metadata',
51         sub {
52             my ( $self, $rq ) = @_;
53             return {
54                 ID => $rq->illrequest_id,
55                 Title => $rq->patron->borrowernumber
56             }
57         }
58     );
59     $backend->mock(
60         'status_graph', sub {},
61     );
62
63     # Mock Koha::Illrequest::load_backend (to load Mocked Backend)
64     my $illreqmodule = Test::MockModule->new('Koha::Illrequest');
65     $illreqmodule->mock( 'load_backend',
66         sub { my $self = shift; $self->{_my_backend} = $backend; return $self }
67     );
68
69     $schema->storage->txn_begin;
70
71     Koha::Illrequests->search->delete;
72     # ill => 22 (userflags.sql)
73     my ( $borrowernumber, $session_id ) = create_user_and_session({ authorized => 22 });
74
75     ## Authorized user tests
76     # No requests, so empty array should be returned
77     my $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
78     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
79     $tx->req->env( { REMOTE_ADDR => $remote_address } );
80     $t->request_ok($tx)->status_is(200)->json_is( [] );
81
82     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
83     my $patron  = $builder->build_object( { class => 'Koha::Patrons' } );
84
85     # Create an ILL request
86     my $illrequest = $builder->build_object(
87         {
88             class => 'Koha::Illrequests',
89             value => {
90                 backend        => 'Mock',
91                 branchcode     => $library->branchcode,
92                 borrowernumber => $patron->borrowernumber
93             }
94         }
95     );
96
97     # One illrequest created, should get returned
98     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
99     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
100     $tx->req->env( { REMOTE_ADDR => $remote_address } );
101     $t->request_ok($tx)->status_is(200)->json_is( [ $illrequest->unblessed ] );
102
103     # One illrequest created, returned with augmented data
104     $tx = $t->ua->build_tx( GET =>
105           '/api/v1/illrequests?embed=patron,library,capabilities,metadata' );
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)
109         ->json_has( '/0/patron', 'patron embedded' )
110         ->json_has( '/0/capabilities', 'capabilities embedded' )
111         ->json_has( '/0/library', 'library embedded'  )
112         ->json_has( '/0/metadata', 'metadata embedded'  );
113
114     # Create another ILL request
115     my $illrequest2 = $builder->build_object(
116         {
117             class => 'Koha::Illrequests',
118             value => {
119                 backend        => 'Mock',
120                 branchcode     => $library->branchcode,
121                 borrowernumber => $patron->borrowernumber
122             }
123         }
124     );
125
126     # Two illrequest created, should get returned
127     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests' );
128     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
129     $tx->req->env( { REMOTE_ADDR => $remote_address } );
130     $t->request_ok($tx)->status_is(200)
131       ->json_is( [ $illrequest->unblessed, $illrequest2->unblessed ] );
132
133     # Warn on unsupported query parameter
134     $tx = $t->ua->build_tx( GET => '/api/v1/illrequests?request_blah=blah' );
135     $tx->req->cookies( { name => 'CGISESSID', value => $session_id } );
136     $tx->req->env( { REMOTE_ADDR => $remote_address } );
137     $t->request_ok($tx)->status_is(400)->json_is(
138         [{ path => '/query/request_blah', message => 'Malformed query string'}]
139     );
140
141     $schema->storage->txn_rollback;
142 };
143
144 sub create_user_and_session {
145
146     my $args = shift;
147     my $dbh  = C4::Context->dbh;
148
149     my $flags = ( $args->{authorized} ) ? 2**$args->{authorized} : 0;
150
151     my $user = $builder->build(
152         {
153             source => 'Borrower',
154             value  => {
155                 flags => $flags
156             }
157         }
158     );
159
160     # Create a session for the authorized user
161     my $session = C4::Auth::get_session('');
162     $session->param( 'number',   $user->{borrowernumber} );
163     $session->param( 'id',       $user->{userid} );
164     $session->param( 'ip',       '127.0.0.1' );
165     $session->param( 'lasttime', time() );
166     $session->flush;
167
168     return ( $user->{borrowernumber}, $session->id );
169 }
170
171 1;