Bug 26143: Regression tests
[koha.git] / t / Koha / REST / Plugin / Pagination.t
blobdff13fb770f8c31cd4b2f33403365febbc644ea4
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 # Dummy app for testing the plugin
21 use Mojolicious::Lite;
23 app->log->level('error');
25 plugin 'Koha::REST::Plugin::Pagination';
27 # For add_pagination_headers()
29 get '/empty' => sub {
30 my $c = shift;
31 $c->render( json => { ok => 1 }, status => 200 );
34 get '/pagination_headers' => sub {
35 my $c = shift;
36 $c->add_pagination_headers({ total => 10, params => { _page => 2, _per_page => 3, firstname => 'Jonathan' } });
37 $c->render( json => { ok => 1 }, status => 200 );
40 get '/pagination_headers_first_page' => sub {
41 my $c = shift;
42 $c->add_pagination_headers({ total => 10, params => { _page => 1, _per_page => 3, firstname => 'Jonathan' } });
43 $c->render( json => { ok => 1 }, status => 200 );
46 get '/pagination_headers_last_page' => sub {
47 my $c = shift;
48 $c->add_pagination_headers({ total => 10, params => { _page => 4, _per_page => 3, firstname => 'Jonathan' } });
49 $c->render( json => { ok => 1 }, status => 200 );
52 # For dbic_merge_pagination
54 get '/dbic_merge_pagination' => sub {
55 my $c = shift;
56 my $filter = { firstname => 'Kyle', surname => 'Hall' };
57 $filter = $c->dbic_merge_pagination({ filter => $filter, params => { _page => 1, _per_page => 3 } });
58 $c->render( json => $filter, status => 200 );
61 get '/pagination_headers_without_page_size' => sub {
62 my $c = shift;
63 $c->add_pagination_headers({ total => 10, params => { _page => 2, firstname => 'Jonathan' } });
64 $c->render( json => { ok => 1 }, status => 200 );
67 get '/pagination_headers_without_page' => sub {
68 my $c = shift;
69 $c->add_pagination_headers({ total => 10, params => { _per_page => 3, firstname => 'Jonathan' } });
70 $c->render( json => { ok => 1 }, status => 200 );
73 get '/pagination_headers_with_minus_one' => sub {
74 my $c = shift;
75 $c->add_pagination_headers(
77 total => 10,
78 params => { _per_page => -1, firstname => 'Jonathan' }
81 $c->render( json => { ok => 1 }, status => 200 );
84 get '/pagination_headers_with_minus_one_and_invalid_page' => sub {
85 my $c = shift;
86 $c->add_pagination_headers(
88 total => 10,
89 params => { page => 100, _per_page => -1, firstname => 'Jonathan' }
92 $c->render( json => { ok => 1 }, status => 200 );
95 # The tests
97 use Test::More tests => 2;
98 use Test::Mojo;
100 use t::lib::Mocks;
102 subtest 'add_pagination_headers() tests' => sub {
104 plan tests => 101;
106 my $t = Test::Mojo->new;
108 $t->get_ok('/empty')
109 ->status_is( 200 )
110 ->header_is( 'X-Total-Count' => undef, 'X-Total-Count is undefined' )
111 ->header_is( 'Link' => undef, 'Link is undefined' );
113 $t->get_ok('/pagination_headers')
114 ->status_is( 200 )
115 ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' )
116 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="prev",/ )
117 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=1.*>; rel="prev",/ )
118 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ )
119 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="next",/ )
120 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=3.*>; rel="next",/ )
121 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="next",/ )
122 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="first",/ )
123 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=1.*>; rel="first",/ )
124 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="first",/ )
125 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="last"/ )
126 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=4.*>; rel="last"/ )
127 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ );
129 $t->get_ok('/pagination_headers_first_page')
130 ->status_is( 200 )
131 ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' )
132 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*>; rel="prev",/ )
133 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="next",/ )
134 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=2.*>; rel="next",/ )
135 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="next",/ )
136 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="first",/ )
137 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=1.*>; rel="first",/ )
138 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="first",/ )
139 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="last"/ )
140 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=4.*>; rel="last"/ )
141 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ );
143 $t->get_ok('/pagination_headers_last_page')
144 ->status_is( 200 )
145 ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' )
146 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="prev",/ )
147 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=3.*>; rel="prev",/ )
148 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ )
149 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*>; rel="next",/ )
150 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="first",/ )
151 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=1.*>; rel="first",/ )
152 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="first",/ )
153 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="last"/ )
154 ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=4.*>; rel="last"/ )
155 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ );
157 t::lib::Mocks::mock_preference('RESTdefaultPageSize', 3);
158 $t->get_ok('/pagination_headers_without_page_size')
159 ->status_is( 200 )
160 ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' )
161 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="prev",/ )
162 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/ )
163 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ )
164 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="next",/ )
165 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=3.*>; rel="next",/ )
166 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="next",/ )
167 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="first",/ )
168 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="first",/ )
169 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="first",/ )
170 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="last"/ )
171 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=4.*>; rel="last"/ )
172 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ );
174 $t->get_ok('/pagination_headers_without_page')
175 ->status_is( 200 )
176 ->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, even without page param' )
177 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="prev",/, 'First page, no previous' )
178 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/, 'First page, no previous' )
179 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/, 'First page, no previous' )
180 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="next",/ )
181 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=3.*>; rel="next",/ )
182 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="next",/ )
183 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="first",/ )
184 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="first",/ )
185 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="first",/ )
186 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="last"/ )
187 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=4.*>; rel="last"/ )
188 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ );
190 $t->get_ok('/pagination_headers_with_minus_one')
191 ->status_is( 200 )
192 ->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, with per_page=-1' )
193 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="prev",/, 'First page, no previous' )
194 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/, 'First page, no previous' )
195 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/, 'First page, no previous' )
196 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="next",/, 'No next page, all resources are fetched' )
197 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="first",/ )
198 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="first",/ )
199 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="first",/ )
200 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="last"/ )
201 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="last"/ )
202 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ );
204 $t->get_ok('/pagination_headers_with_minus_one_and_invalid_page')
205 ->status_is( 200 )
206 ->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, with per_page=-1' )
207 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="prev",/, 'First page, no previous' )
208 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/, 'First page, no previous' )
209 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/, 'First page, no previous' )
210 ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="next",/, 'No next page, all resources are fetched' )
211 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="first",/ )
212 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="first",/ )
213 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="first",/ )
214 ->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="last"/ )
215 ->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="last"/ )
216 ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ );
219 subtest 'dbic_merge_pagination() tests' => sub {
221 plan tests => 3;
223 my $t = Test::Mojo->new;
225 $t->get_ok('/dbic_merge_pagination')
226 ->status_is(200)
227 ->json_is({ firstname => 'Kyle', surname => 'Hall', page => 1, rows => 3 });