Bug 29234: Further clean Z3950 Tests
[koha-ffzg.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
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 1;
21
22 use Test::MockModule;
23 use Test::MockObject;
24 use Test::Mojo;
25
26 use t::lib::TestBuilder;
27 use t::lib::Mocks;
28
29 use Koha::Illrequests;
30 use Koha::DateUtils qw( format_sqldatetime );
31
32 my $schema  = Koha::Database->new->schema;
33 my $builder = t::lib::TestBuilder->new;
34
35 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37 my $t = Test::Mojo->new('Koha::REST::V1');
38
39 subtest 'list() tests' => sub {
40
41     plan tests => 30;
42
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'; } );
48     $backend->mock(
49         'metadata',
50         sub {
51             my ( $self, $rq ) = @_;
52             return {
53                 ID => $rq->illrequest_id,
54                 Title => $rq->patron->borrowernumber
55             }
56         }
57     );
58     $backend->mock(
59         'status_graph', sub {},
60     );
61
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 }
66     );
67
68     $schema->storage->txn_begin;
69
70     Koha::Illrequests->search->delete;
71
72     # create an authorized user
73     my $patron = $builder->build_object({
74         class => 'Koha::Patrons',
75         value => { flags => 2 ** 22 } # 22 => ill
76     });
77     my $password = 'thePassword123';
78     $patron->set_password({ password => $password, skip_validation => 1 });
79     my $userid = $patron->userid;
80
81     ## Authorized user tests
82     # No requests, so empty array should be returned
83     $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
84       ->status_is(200)
85       ->json_is( [] );
86
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' } );
90
91     # Create an ILL request
92     my $illrequest = $builder->build_object(
93         {
94             class => 'Koha::Illrequests',
95             value => {
96                 backend        => 'Mock',
97                 branchcode     => $library->branchcode,
98                 borrowernumber => $patron_1->borrowernumber,
99                 status         => 'STATUS1',
100             }
101         }
102     );
103
104     # The api response is always augmented with the id_prefix
105     my $response = $illrequest->unblessed;
106     $response->{id_prefix} = $illrequest->id_prefix;
107
108     my $req_formatted = add_formatted($response);
109
110     # One illrequest created, should get returned
111     $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
112       ->status_is(200)
113       ->json_is( [ $req_formatted ] );
114
115     # One illrequest created, returned with augmented data
116     $t->get_ok( "//$userid:$password@/api/v1/illrequests?embed=patron,library,capabilities,metadata,requested_partners" )
117       ->status_is(200)
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' );
125
126     # Create another ILL request
127     my $illrequest2 = $builder->build_object(
128         {
129             class => 'Koha::Illrequests',
130             value => {
131                 backend        => 'Mock',
132                 branchcode     => $library->branchcode,
133                 borrowernumber => $patron_2->borrowernumber,
134                 status         => 'STATUS2',
135             }
136         }
137     );
138
139     # The api response is always augmented with the id_prefix
140     my $response2 = $illrequest2->unblessed;
141     $response2->{id_prefix} = $illrequest2->id_prefix;
142
143     my $req2_formatted = add_formatted($response2);
144
145     # Two illrequest created, should get returned
146     $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
147       ->status_is(200)
148       ->json_is( [ $req_formatted, $req2_formatted ] );
149
150     # Warn on unsupported query parameter
151     $t->get_ok( "//$userid:$password@/api/v1/illrequests?request_blah=blah" )
152       ->status_is(400)
153       ->json_is(
154         [{ path => '/query/request_blah', message => 'Malformed query string'}]
155     );
156
157     # Test the borrowernumber parameter
158     $t->get_ok( "//$userid:$password@/api/v1/illrequests?borrowernumber=" . $patron_2->borrowernumber )
159       ->status_is(200)
160       ->json_is( [ $response2 ] );
161
162     # Test the ILLHiddenRequestStatuses syspref
163     t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS1' );
164     $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
165       ->status_is(200)
166       ->json_is( [ $req2_formatted ] );
167
168     t::lib::Mocks::mock_preference( 'ILLHiddenRequestStatuses', 'STATUS2' );
169     $t->get_ok( "//$userid:$password@/api/v1/illrequests" )
170       ->status_is(200)
171       ->json_is( [ $req_formatted ] );
172
173     $schema->storage->txn_rollback;
174 };
175
176 sub add_formatted {
177     my $req = shift;
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(
186                 $req->{$field},
187                 undef,
188                 undef,
189                 1
190             );
191         }
192     }
193     return $req;
194 }