Line 0
Link Here
|
0 |
- |
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 utf8; |
21 |
use Encode; |
22 |
|
23 |
use Test::More tests => 2; |
24 |
use Test::MockModule; |
25 |
use Test::Mojo; |
26 |
use Test::Warn; |
27 |
|
28 |
use t::lib::Mocks; |
29 |
use t::lib::TestBuilder; |
30 |
|
31 |
use Mojo::JSON qw(encode_json); |
32 |
|
33 |
use C4::Auth; |
34 |
use C4::Biblio qw( DelBiblio ); |
35 |
use C4::Circulation qw( AddIssue AddReturn ); |
36 |
|
37 |
use Koha::Biblios; |
38 |
use Koha::Database; |
39 |
use Koha::Checkouts; |
40 |
use Koha::Old::Checkouts; |
41 |
|
42 |
use Mojo::JSON qw(encode_json); |
43 |
|
44 |
my $schema = Koha::Database->new->schema; |
45 |
my $builder = t::lib::TestBuilder->new; |
46 |
|
47 |
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); |
48 |
|
49 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
50 |
|
51 |
subtest 'get() tests' => sub { |
52 |
|
53 |
plan tests => 22; |
54 |
|
55 |
$schema->storage->txn_begin; |
56 |
|
57 |
my $patron = $builder->build_object( |
58 |
{ |
59 |
class => 'Koha::Patrons', |
60 |
value => { flags => 0 } |
61 |
} |
62 |
); |
63 |
my $password = 'thePassword123'; |
64 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
65 |
$patron->discard_changes; |
66 |
my $userid = $patron->userid; |
67 |
|
68 |
my $biblio = $builder->build_sample_biblio({ |
69 |
title => 'The unbearable lightness of being', |
70 |
author => 'Milan Kundera' |
71 |
}); |
72 |
|
73 |
my $formatted = $biblio->metadata->record->as_formatted; |
74 |
DelBiblio( $biblio->id ); |
75 |
|
76 |
$t->get_ok("//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber) |
77 |
->status_is(403); |
78 |
|
79 |
$patron->flags(4)->store; |
80 |
|
81 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber |
82 |
=> { Accept => 'application/weird+format' } ) |
83 |
->status_is(400); |
84 |
|
85 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber |
86 |
=> { Accept => 'application/json' } ) |
87 |
->status_is(200) |
88 |
->json_is( '/title', 'The unbearable lightness of being' ) |
89 |
->json_is( '/author', 'Milan Kundera' ); |
90 |
|
91 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber |
92 |
=> { Accept => 'application/marcxml+xml' } ) |
93 |
->status_is(200); |
94 |
|
95 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber |
96 |
=> { Accept => 'application/marc-in-json' } ) |
97 |
->status_is(200); |
98 |
|
99 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber |
100 |
=> { Accept => 'application/marc' } ) |
101 |
->status_is(200); |
102 |
|
103 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber |
104 |
=> { Accept => 'text/plain' } ) |
105 |
->status_is(200) |
106 |
->content_is($formatted); |
107 |
|
108 |
my $biblio_exist = $builder->build_sample_biblio(); |
109 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio_exist->biblionumber |
110 |
=> { Accept => 'application/marc' } ) |
111 |
->status_is(404) |
112 |
->json_is( '/error', 'Object not found.' ); |
113 |
|
114 |
subtest 'marc-in-json encoding tests' => sub { |
115 |
|
116 |
plan tests => 3; |
117 |
|
118 |
my $title_with_diacritics = "L'insoutenable légèreté de l'être"; |
119 |
|
120 |
my $biblio = $builder->build_sample_biblio( |
121 |
{ |
122 |
title => $title_with_diacritics, |
123 |
author => "Milan Kundera" |
124 |
} |
125 |
); |
126 |
|
127 |
DelBiblio( $biblio->id ); |
128 |
|
129 |
my $result = $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber |
130 |
=> { Accept => 'application/marc-in-json' } ) |
131 |
->status_is(200)->tx->res->body; |
132 |
|
133 |
my $encoded_title = Encode::encode( "UTF-8", $title_with_diacritics ); |
134 |
|
135 |
like( $result, qr/\Q$encoded_title/, "The title is not double encoded" ); |
136 |
}; |
137 |
|
138 |
subtest 'marcxml encoding tests' => sub { |
139 |
plan tests => 3; |
140 |
|
141 |
my $marcflavour = C4::Context->preference('marcflavour'); |
142 |
t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC'); |
143 |
|
144 |
|
145 |
my $title_with_diacritics = "L'insoutenable légèreté de l'être"; |
146 |
|
147 |
my $biblio = $builder->build_sample_biblio( |
148 |
{ |
149 |
title => $title_with_diacritics, |
150 |
author => "Milan Kundera" |
151 |
} |
152 |
); |
153 |
|
154 |
my $record = $biblio->metadata->record; |
155 |
$record->leader(' nam 3 4500'); |
156 |
$biblio->metadata->metadata($record->as_xml_record('UNIMARC')); |
157 |
$biblio->metadata->store; |
158 |
|
159 |
DelBiblio( $biblio->id ); |
160 |
|
161 |
my $result = $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios/" . $biblio->biblionumber |
162 |
=> { Accept => 'application/marcxml+xml' } ) |
163 |
->status_is(200)->tx->res->body; |
164 |
|
165 |
my $encoded_title = Encode::encode( "UTF-8", $title_with_diacritics ); |
166 |
|
167 |
like( $result, qr/\Q$encoded_title/, "The title is not double encoded" ); |
168 |
t::lib::Mocks::mock_preference('marcflavour', $marcflavour); |
169 |
}; |
170 |
|
171 |
$schema->storage->txn_rollback; |
172 |
}; |
173 |
|
174 |
subtest 'list() tests' => sub { |
175 |
|
176 |
plan tests => 17; |
177 |
|
178 |
$schema->storage->txn_begin; |
179 |
|
180 |
my $patron = $builder->build_object( |
181 |
{ |
182 |
class => 'Koha::Patrons', |
183 |
value => { flags => 0 } |
184 |
} |
185 |
); |
186 |
my $password = 'thePassword123'; |
187 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
188 |
$patron->discard_changes; |
189 |
my $userid = $patron->userid; |
190 |
|
191 |
t::lib::Mocks::mock_preference('marcflavour', 'UNIMARC'); |
192 |
|
193 |
my $title_with_diacritics = "L'insoutenable légèreté de l'être"; |
194 |
my $biblio = $builder->build_sample_biblio( |
195 |
{ |
196 |
title => $title_with_diacritics, |
197 |
author => "Milan Kundera", |
198 |
} |
199 |
); |
200 |
|
201 |
my $record = $biblio->metadata->record; |
202 |
$record->leader(' nam 3 4500'); |
203 |
$biblio->metadata->metadata($record->as_xml_record('UNIMARC'))->store; |
204 |
|
205 |
my $biblio_id_1 = $biblio->id; |
206 |
|
207 |
t::lib::Mocks::mock_preference('marcflavour', 'MARC21'); |
208 |
my $biblio_id_2 = $builder->build_sample_biblio->id; |
209 |
|
210 |
DelBiblio( $biblio_id_1 ); |
211 |
DelBiblio( $biblio_id_2 ); |
212 |
|
213 |
my $query = encode_json( [ { biblio_id => $biblio_id_1 }, { biblio_id => $biblio_id_2 } ] ); |
214 |
|
215 |
$t->get_ok("//$userid:$password@/api/v1/deleted/biblios?q=$query")->status_is(403); |
216 |
|
217 |
$patron->flags(4)->store; |
218 |
|
219 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'application/weird+format' } ) |
220 |
->status_is(400); |
221 |
|
222 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'application/json' } )->status_is(200); |
223 |
|
224 |
my $result = $t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'application/marcxml+xml' } ) |
225 |
->status_is(200)->tx->res->body; |
226 |
|
227 |
my $encoded_title = Encode::encode( "UTF-8", $title_with_diacritics ); |
228 |
like( $result, qr/\Q$encoded_title/, "The title is not double encoded" ); |
229 |
|
230 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'application/marc-in-json' } ) |
231 |
->status_is(200); |
232 |
|
233 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'application/marc' } )->status_is(200); |
234 |
|
235 |
$t->get_ok( "//$userid:$password@/api/v1/deleted/biblios?q=$query" => { Accept => 'text/plain' } )->status_is(200); |
236 |
|
237 |
# DELETE any biblio with ISBN = TOMAS |
238 |
Koha::Biblios->search({ 'biblioitem.isbn' => 'TOMAS' }, { join => [ 'biblioitem' ] }) |
239 |
->delete; |
240 |
|
241 |
|
242 |
my $isbn_query = encode_json({ isbn => 'TOMAS' }); |
243 |
my $tomas_biblio = $builder->build_sample_biblio({ |
244 |
isbn => 'TOMAS' |
245 |
}); |
246 |
DelBiblio( $tomas_biblio->id ); |
247 |
$t->get_ok( "//$userid:$password@/api/v1/biblios?q=$isbn_query" => |
248 |
{ Accept => 'text/plain' } ) |
249 |
->status_is(200); |
250 |
|
251 |
$schema->storage->txn_rollback; |
252 |
}; |