Bug 19196: Rename page and per_page
[koha.git] / t / Koha / REST / Plugin / Pagination.t
1 #!/usr/bin/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 # Dummy app for testing the plugin
21 use Mojolicious::Lite;
22
23 app->log->level('error');
24
25 plugin 'Koha::REST::Plugin::Pagination';
26
27
28 get '/empty' => sub {
29     my $c = shift;
30     $c->render( json => { ok => 1 }, status => 200 );
31 };
32
33 get '/pagination_headers' => sub {
34     my $c = shift;
35     $c->add_pagination_headers({ total => 10, params => { _page => 2, _per_page => 3, firstname => 'Jonathan' } });
36     $c->render( json => { ok => 1 }, status => 200 );
37 };
38
39 get '/pagination_headers_first_page' => sub {
40     my $c = shift;
41     $c->add_pagination_headers({ total => 10, params => { _page => 1, _per_page => 3, firstname => 'Jonathan' } });
42     $c->render( json => { ok => 1 }, status => 200 );
43 };
44
45 get '/pagination_headers_last_page' => sub {
46     my $c = shift;
47     $c->add_pagination_headers({ total => 10, params => { _page => 4, _per_page => 3, firstname => 'Jonathan' } });
48     $c->render( json => { ok => 1 }, status => 200 );
49 };
50
51 # The tests
52
53 use Test::More tests => 1;
54 use Test::Mojo;
55
56 subtest 'add_pagination_headers() tests' => sub {
57
58     plan tests => 45;
59
60     my $t = Test::Mojo->new;
61
62     $t->get_ok('/empty')
63       ->status_is( 200 )
64       ->header_is( 'X-Total-Count' => undef, 'X-Total-Count is undefined' )
65       ->header_is( 'Link'          => undef, 'Link is undefined' );
66
67     $t->get_ok('/pagination_headers')
68       ->status_is( 200 )
69       ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' )
70       ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="prev",/ )
71       ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=1.*>; rel="prev",/ )
72       ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ )
73       ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="next",/ )
74       ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=3.*>; rel="next",/ )
75       ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="next",/ )
76       ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="first",/ )
77       ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=1.*>; rel="first",/ )
78       ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="first",/ )
79       ->header_like( 'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="last"/ )
80       ->header_like( 'Link' => qr/<http:\/\/.*\?.*_page=4.*>; rel="last"/ )
81       ->header_like( 'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ );
82
83     $t->get_ok('/pagination_headers_first_page')
84       ->status_is( 200 )
85       ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' )
86       ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*>; rel="prev",/ )
87       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="next",/ )
88       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_page=2.*>; rel="next",/ )
89       ->header_like(   'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="next",/ )
90       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="first",/ )
91       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_page=1.*>; rel="first",/ )
92       ->header_like(   'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="first",/ )
93       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="last"/ )
94       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_page=4.*>; rel="last"/ )
95       ->header_like(   'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ );
96
97     $t->get_ok('/pagination_headers_last_page')
98       ->status_is( 200 )
99       ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' )
100       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="prev",/ )
101       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_page=3.*>; rel="prev",/ )
102       ->header_like(   'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="prev",/ )
103       ->header_unlike( 'Link' => qr/<http:\/\/.*\?.*>; rel="next",/ )
104       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="first",/ )
105       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_page=1.*>; rel="first",/ )
106       ->header_like(   'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="first",/ )
107       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_per_page=3.*>; rel="last"/ )
108       ->header_like(   'Link' => qr/<http:\/\/.*\?.*_page=4.*>; rel="last"/ )
109       ->header_like(   'Link' => qr/<http:\/\/.*\?.*firstname=Jonathan.*>; rel="last"/ );
110 };