|
Link Here
|
| 20 |
# Dummy app for testing the plugin |
20 |
# Dummy app for testing the plugin |
| 21 |
use Mojolicious::Lite; |
21 |
use Mojolicious::Lite; |
| 22 |
|
22 |
|
|
|
23 |
use Koha::Patrons; |
| 24 |
|
| 25 |
use t::lib::TestBuilder; |
| 26 |
|
| 23 |
app->log->level('error'); |
27 |
app->log->level('error'); |
| 24 |
|
28 |
|
| 25 |
plugin 'Koha::REST::Plugin::Pagination'; |
29 |
plugin 'Koha::REST::Plugin::Pagination'; |
| 26 |
|
30 |
|
|
|
31 |
my $schema = Koha::Database->new->schema; |
| 32 |
my $builder = t::lib::TestBuilder->new; |
| 33 |
|
| 34 |
$schema->storage->txn_begin; |
| 35 |
|
| 36 |
# Add 10 known 'Jonathan' patrons |
| 37 |
my @patron_ids; |
| 38 |
for my $i ( 1 .. 10 ) { |
| 39 |
push @patron_ids, $builder->build_object({ class => 'Koha::Patrons', value => { firstname => 'Jonathan' } })->id; |
| 40 |
} |
| 41 |
# Add two non-Jonathans |
| 42 |
push @patron_ids, $builder->build_object({ class => 'Koha::Patrons' })->id; |
| 43 |
push @patron_ids, $builder->build_object({ class => 'Koha::Patrons' })->id; |
| 44 |
|
| 27 |
# For add_pagination_headers() |
45 |
# For add_pagination_headers() |
| 28 |
|
46 |
|
| 29 |
get '/empty' => sub { |
47 |
get '/empty' => sub { |
|
Link Here
|
| 33 |
|
51 |
|
| 34 |
get '/pagination_headers' => sub { |
52 |
get '/pagination_headers' => sub { |
| 35 |
my $c = shift; |
53 |
my $c = shift; |
| 36 |
$c->add_pagination_headers({ total => 10, base_total => 12, params => { _page => 2, _per_page => 3, firstname => 'Jonathan' } }); |
|
|
| 37 |
$c->render( json => { ok => 1 }, status => 200 ); |
| 38 |
}; |
| 39 |
|
54 |
|
| 40 |
get '/pagination_headers_first_page' => sub { |
55 |
my $args = $c->req->params->to_hash; |
| 41 |
my $c = shift; |
56 |
|
| 42 |
$c->add_pagination_headers({ total => 10, base_total => 12, params => { _page => 1, _per_page => 3, firstname => 'Jonathan' } }); |
57 |
my $result_set = Koha::Patrons->search( |
| 43 |
$c->render( json => { ok => 1 }, status => 200 ); |
58 |
{ borrowernumber => \@patron_ids } |
| 44 |
}; |
59 |
); |
|
|
60 |
|
| 61 |
my $rows = ($args->{_per_page}) ? |
| 62 |
( $args->{_per_page} == -1 ) ? undef : $args->{_per_page} |
| 63 |
: C4::Context->preference('RESTdefaultPageSize'); |
| 64 |
|
| 65 |
my $objects_rs = $result_set->search( { firstname => 'Jonathan' } ,{ page => $args->{_page} // 1, rows => $rows }); |
| 66 |
|
| 67 |
$c->stash('koha.pagination.page' => $args->{_page}); |
| 68 |
$c->stash('koha.pagination.per_page' => $args->{_per_page}); |
| 69 |
$c->stash('koha.pagination.base_total' => $result_set->count); |
| 70 |
$c->stash('koha.pagination.query_params' => $args); |
| 71 |
$c->stash('koha.pagination.total' => $objects_rs->is_paged ? $objects_rs->pager->total_entries : $objects_rs->count); |
| 72 |
|
| 73 |
$c->add_pagination_headers; |
| 45 |
|
74 |
|
| 46 |
get '/pagination_headers_last_page' => sub { |
|
|
| 47 |
my $c = shift; |
| 48 |
$c->add_pagination_headers({ total => 10, base_total => 12, params => { _page => 4, _per_page => 3, firstname => 'Jonathan' } }); |
| 49 |
$c->render( json => { ok => 1 }, status => 200 ); |
75 |
$c->render( json => { ok => 1 }, status => 200 ); |
| 50 |
}; |
76 |
}; |
| 51 |
|
77 |
|
|
Link Here
|
| 58 |
$c->render( json => $filter, status => 200 ); |
84 |
$c->render( json => $filter, status => 200 ); |
| 59 |
}; |
85 |
}; |
| 60 |
|
86 |
|
| 61 |
get '/pagination_headers_without_page_size' => sub { |
|
|
| 62 |
my $c = shift; |
| 63 |
$c->add_pagination_headers({ total => 10, base_total => 12, params => { _page => 2, firstname => 'Jonathan' } }); |
| 64 |
$c->render( json => { ok => 1 }, status => 200 ); |
| 65 |
}; |
| 66 |
|
| 67 |
get '/pagination_headers_without_page' => sub { |
| 68 |
my $c = shift; |
| 69 |
$c->add_pagination_headers({ total => 10, base_total => 12, params => { _per_page => 3, firstname => 'Jonathan' } }); |
| 70 |
$c->render( json => { ok => 1 }, status => 200 ); |
| 71 |
}; |
| 72 |
|
| 73 |
get '/pagination_headers_with_minus_one' => sub { |
| 74 |
my $c = shift; |
| 75 |
$c->add_pagination_headers( |
| 76 |
{ |
| 77 |
total => 10, |
| 78 |
base_total => 12, |
| 79 |
params => { _per_page => -1, firstname => 'Jonathan' } |
| 80 |
} |
| 81 |
); |
| 82 |
$c->render( json => { ok => 1 }, status => 200 ); |
| 83 |
}; |
| 84 |
|
| 85 |
get '/pagination_headers_with_minus_one_and_invalid_page' => sub { |
| 86 |
my $c = shift; |
| 87 |
$c->add_pagination_headers( |
| 88 |
{ |
| 89 |
total => 10, |
| 90 |
base_total => 12, |
| 91 |
params => { page => 100, _per_page => -1, firstname => 'Jonathan' } |
| 92 |
} |
| 93 |
); |
| 94 |
$c->render( json => { ok => 1 }, status => 200 ); |
| 95 |
}; |
| 96 |
|
| 97 |
# The tests |
87 |
# The tests |
| 98 |
|
88 |
|
| 99 |
use Test::More tests => 2; |
89 |
use Test::More tests => 2; |
|
Link Here
|
| 109 |
|
99 |
|
| 110 |
$t->get_ok('/empty') |
100 |
$t->get_ok('/empty') |
| 111 |
->status_is( 200 ) |
101 |
->status_is( 200 ) |
| 112 |
->header_is( 'X-Total-Count' => undef, 'X-Total-Count is undefined' ) |
102 |
->header_is( 'X-Total-Count' => undef, 'X-Total-Count is undefined' ) |
| 113 |
->header_is( 'X-Base-Total-Count' => undef, 'X-Base-Total-Count is undefined' ) |
103 |
->header_is( 'X-Base-Total-Count' => undef, 'X-Base-Total-Count is undefined' ) |
| 114 |
->header_is( 'Link' => undef, 'Link is undefined' ); |
104 |
->header_is( 'Link' => undef, 'Link is undefined' ); |
| 115 |
|
105 |
|
| 116 |
$t->get_ok('/pagination_headers') |
106 |
$t->get_ok('/pagination_headers?firstname=Jonathan&_page=2&_per_page=3') |
| 117 |
->status_is( 200 ) |
107 |
->status_is( 200 ) |
| 118 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) |
108 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count correctly set' ) |
| 119 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) |
109 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count correctly set' ) |
| 120 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="prev",/ ) |
110 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="prev",/ ) |
| 121 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=1.*>; rel="prev",/ ) |
111 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=1.*>; rel="prev",/ ) |
| 122 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ ) |
112 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ ) |
|
Link Here
|
| 130 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=4.*>; rel="last"/ ) |
120 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=4.*>; rel="last"/ ) |
| 131 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
121 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
| 132 |
|
122 |
|
| 133 |
$t->get_ok('/pagination_headers_first_page') |
123 |
$t->get_ok('/pagination_headers?firstname=Jonathan&_page=1&_per_page=3') |
| 134 |
->status_is( 200 ) |
124 |
->status_is( 200 ) |
| 135 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) |
125 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count correctly set' ) |
| 136 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) |
126 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count correctly set' ) |
| 137 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*>; rel="prev",/ ) |
127 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*>; rel="prev",/ ) |
| 138 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="next",/ ) |
128 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="next",/ ) |
| 139 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=2.*>; rel="next",/ ) |
129 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=2.*>; rel="next",/ ) |
|
Link Here
|
| 145 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=4.*>; rel="last"/ ) |
135 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=4.*>; rel="last"/ ) |
| 146 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
136 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
| 147 |
|
137 |
|
| 148 |
$t->get_ok('/pagination_headers_last_page') |
138 |
$t->get_ok('/pagination_headers?firstname=Jonathan&_page=4&_per_page=3') |
| 149 |
->status_is( 200 ) |
139 |
->status_is( 200 ) |
| 150 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) |
140 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count correctly set' ) |
| 151 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) |
141 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count correctly set' ) |
| 152 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="prev",/ ) |
142 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="prev",/ ) |
| 153 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=3.*>; rel="prev",/ ) |
143 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=3.*>; rel="prev",/ ) |
| 154 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ ) |
144 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ ) |
|
Link Here
|
| 161 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
151 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
| 162 |
|
152 |
|
| 163 |
t::lib::Mocks::mock_preference('RESTdefaultPageSize', 3); |
153 |
t::lib::Mocks::mock_preference('RESTdefaultPageSize', 3); |
| 164 |
$t->get_ok('/pagination_headers_without_page_size') |
154 |
$t->get_ok('/pagination_headers?firstname=Jonathan&_page=2') |
| 165 |
->status_is( 200 ) |
155 |
->status_is( 200 ) |
| 166 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) |
156 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count correctly set' ) |
| 167 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) |
157 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count correctly set' ) |
| 168 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="prev",/ ) |
158 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="prev",/ ) |
| 169 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/ ) |
159 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/ ) |
| 170 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ ) |
160 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ ) |
|
Link Here
|
| 178 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=4.*>; rel="last"/ ) |
168 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=4.*>; rel="last"/ ) |
| 179 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
169 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
| 180 |
|
170 |
|
| 181 |
$t->get_ok('/pagination_headers_without_page') |
171 |
$t->get_ok('/pagination_headers?firstname=Jonathan&_per_page=3') |
| 182 |
->status_is( 200 ) |
172 |
->status_is( 200 ) |
| 183 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, even without page param' ) |
173 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, even without page param' ) |
| 184 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) |
174 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count correctly set' ) |
| 185 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="prev",/, 'First page, no previous' ) |
175 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="prev",/, 'First page, no previous' ) |
| 186 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/, 'First page, no previous' ) |
176 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/, 'First page, no previous' ) |
| 187 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/, 'First page, no previous' ) |
177 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/, 'First page, no previous' ) |
| 188 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="next",/ ) |
178 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="next",/ ) |
| 189 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=3.*>; rel="next",/ ) |
179 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=2.*>; rel="next",/ ) |
| 190 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="next",/ ) |
180 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="next",/ ) |
| 191 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="first",/ ) |
181 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*per_page=3.*>; rel="first",/ ) |
| 192 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="first",/ ) |
182 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="first",/ ) |
|
Link Here
|
| 195 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=4.*>; rel="last"/ ) |
185 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=4.*>; rel="last"/ ) |
| 196 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
186 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
| 197 |
|
187 |
|
| 198 |
$t->get_ok('/pagination_headers_with_minus_one') |
188 |
$t->get_ok('/pagination_headers?firstname=Jonathan&_per_page=-1') |
| 199 |
->status_is( 200 ) |
189 |
->status_is( 200 ) |
| 200 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, with per_page=-1' ) |
190 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, with per_page=-1' ) |
| 201 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) |
191 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count correctly set' ) |
| 202 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="prev",/, 'First page, no previous' ) |
192 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="prev",/, 'First page, no previous' ) |
| 203 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/, 'First page, no previous' ) |
193 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/, 'First page, no previous' ) |
| 204 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/, 'First page, no previous' ) |
194 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/, 'First page, no previous' ) |
|
Link Here
|
| 210 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="last"/ ) |
200 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="last"/ ) |
| 211 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
201 |
->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ ); |
| 212 |
|
202 |
|
| 213 |
$t->get_ok('/pagination_headers_with_minus_one_and_invalid_page') |
203 |
$t->get_ok('/pagination_headers?firstname=Jonathan&_per_page=-1&_page=100') |
| 214 |
->status_is( 200 ) |
204 |
->status_is( 200 ) |
| 215 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, with per_page=-1' ) |
205 |
->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, with per_page=-1' ) |
| 216 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) |
206 |
->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count correctly set' ) |
| 217 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="prev",/, 'First page, no previous' ) |
207 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*per_page=-1.*>; rel="prev",/, 'First page, no previous' ) |
| 218 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/, 'First page, no previous' ) |
208 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*page=1.*>; rel="prev",/, 'First page, no previous' ) |
| 219 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/, 'First page, no previous' ) |
209 |
->header_unlike( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/, 'First page, no previous' ) |
| 220 |
- |
|
|