View | Details | Raw Unified | Return to bug 30125
Collapse All | Expand All

(-)a/t/db_dependent/api/v1/pagination.t (-1 / +190 lines)
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 Test::More tests => 1;
21
use Test::Mojo;
22
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
25
26
use Koha::Cities;
27
use Koha::Database;
28
29
my $schema  = Koha::Database->new->schema;
30
my $builder = t::lib::TestBuilder->new;
31
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35
subtest 'list() pagination tests' => sub {
36
37
    plan tests => 66;
38
39
    $schema->storage->txn_begin;
40
41
    my $librarian = $builder->build_object(
42
        {
43
            class => 'Koha::Patrons',
44
            value => { flags => 2**2 }    # catalogue flag = 2
45
        }
46
    );
47
    my $password = 'thePassword123';
48
    $librarian->set_password( { password => $password, skip_validation => 1 } );
49
    my $userid = $librarian->userid;
50
51
    # delete all cities
52
    Koha::Cities->new->delete;
53
54
    # No cities, so empty array should be returned
55
    $t->get_ok("//$userid:$password@/api/v1/cities")->status_is(200)
56
      ->json_is( [] );
57
58
    my $names = [
59
        'AA',
60
        'BA',
61
        'BA',
62
        'CA',
63
        'DA',
64
        'EB',
65
        'FB',
66
        'GB',
67
        'HB',
68
        'IB',
69
    ];
70
71
    # Add 10 cities
72
    foreach my $i ( 0 .. 9 ) {
73
        $builder->build_object( { class => 'Koha::Cities', value => { city_name => $names->[$i] } } );
74
    }
75
76
    t::lib::Mocks::mock_preference( 'RESTdefaultPageSize', 20 );
77
78
    my $libraries =
79
      $t->get_ok("//$userid:$password@/api/v1/cities")->status_is(200)
80
      ->header_is( 'X-Total-Count', '10' )
81
      ->header_is( 'X-Base-Total-Count', '10' )
82
      ->header_unlike( 'Link', qr|rel="prev"| )
83
      ->header_unlike( 'Link', qr|rel="next"| )
84
      ->header_like( 'Link',
85
        qr#(_per_page=20\&_page=1|_page=1\&_per_page=20)>\; rel="first"# )
86
      ->header_like( 'Link',
87
        qr#(_per_page=20\&_page=1|_page=1\&_per_page=20)>\; rel="last"# )
88
      ->tx->res->json;
89
90
    is( scalar @{$libraries}, 10, '10 libraries retrieved' );
91
92
    $libraries =
93
      $t->get_ok("//$userid:$password@/api/v1/cities?q={\"name\":{\"-like\":\"\%A\"}}")->status_is(200)
94
      ->header_is( 'X-Total-Count', '5', 'The resultset has 5 items' )
95
      ->header_is( 'X-Base-Total-Count', '10', 'The resultset, without the filter, has 10' )
96
      ->header_unlike( 'Link', qr|rel="prev"| )
97
      ->header_unlike( 'Link', qr|rel="next"| )
98
      ->header_like( 'Link',
99
        qr#(_per_page=20.*\&_page=1.*|_page=1.*\&_per_page=20.*)>\; rel="first"# )
100
      ->header_like( 'Link',
101
        qr#(_per_page=20.*\&_page=1.*|_page=1.*\&_per_page=20.*)>\; rel="last"# )
102
      ->tx->res->json;
103
104
    is( scalar @{$libraries}, 5, '5 libraries retrieved' );
105
106
    t::lib::Mocks::mock_preference( 'RESTdefaultPageSize', 3 );
107
108
    # _per_page overrides RESTdefaultPageSize
109
    $libraries =
110
      $t->get_ok("//$userid:$password@/api/v1/cities?_per_page=20")
111
      ->status_is(200)
112
      ->header_is( 'X-Total-Count', '10' )
113
      ->header_is( 'X-Base-Total-Count', '10' )
114
      ->header_unlike( 'Link', qr|rel="prev"| )
115
      ->header_unlike( 'Link', qr|rel="next"| )
116
      ->header_like( 'Link',
117
        qr#(_per_page=20\&_page=1|_page=1\&_per_page=20)>\; rel="first"# )
118
      ->header_like( 'Link',
119
        qr#(_per_page=20\&_page=1|_page=1\&_per_page=20)>\; rel="last"# )
120
      ->tx->res->json;
121
122
    is( scalar @{$libraries}, 10, '10 libraries retrieved' );
123
124
    # page 1
125
    $libraries =
126
      $t->get_ok("//$userid:$password@/api/v1/cities")->status_is(200)
127
      ->header_is( 'X-Total-Count', '10' )
128
      ->header_is( 'X-Base-Total-Count', '10' )
129
      ->header_unlike( 'Link', qr|rel="prev"| )
130
      ->header_like( 'Link',
131
        qr#(_per_page=3\&_page=1|_page=1\&_per_page=3)>\; rel="first"# )
132
      ->header_like( 'Link',
133
        qr#(_per_page=3\&_page=2|_page=2\&_per_page=3)>\; rel="next"# )
134
      ->header_like( 'Link',
135
        qr#(_per_page=3\&_page=4|_page=4\&_per_page=3)>\; rel="last"# )
136
      ->tx->res->json;
137
138
    is( scalar @{$libraries}, 3, '3 libraries retrieved' );
139
140
    # This tests X-Base-Total-Count, .* is used for q=, as we don't want
141
    # to add all combinations to the regex
142
    $libraries =
143
      $t->get_ok("//$userid:$password@/api/v1/cities?_per_page=2&_page=2&q={\"name\":{\"-like\":\"\%A\"}}")->status_is(200)
144
      ->header_is( 'X-Total-Count', '5' )
145
      ->header_is( 'X-Base-Total-Count', '10' )
146
      ->header_like( 'Link',
147
        qr#(_per_page=2.*\&_page=1.*|_page=1.*\&_per_page=2.*)>\; rel="prev"# )
148
      ->header_like( 'Link',
149
        qr#(_per_page=2.*\&_page=1.*|_page=1.*\&_per_page=2.*)>\; rel="first"# )
150
      ->header_like( 'Link',
151
        qr#(_per_page=2.*\&_page=3.*|_page=3.*\&_per_page=2.*)>\; rel="next"# )
152
      ->header_like( 'Link',
153
        qr#(_per_page=2.*\&_page=3.*|_page=3.*\&_per_page=2.*)>\; rel="last"# )
154
      ->tx->res->json;
155
156
    is( scalar @{$libraries}, 2, '2 libraries retrieved' );
157
158
    # last page, with only one result
159
    $libraries =
160
      $t->get_ok("//$userid:$password@/api/v1/cities?_page=4")->status_is(200)
161
      ->header_is( 'X-Total-Count', '10' )
162
      ->header_is( 'X-Base-Total-Count', '10' )
163
      ->header_like( 'Link',
164
        qr#(_per_page=3\&_page=3|_page=3\&_per_page=3)>\; rel="prev"# )
165
      ->header_like( 'Link',
166
        qr#(_per_page=3\&_page=1|_page=1\&_per_page=3)>\; rel="first"# )
167
      ->header_unlike( 'Link', qr#rel="next"# )
168
      ->header_like( 'Link',
169
        qr#(_per_page=3\&_page=4|_page=4\&_per_page=3)>\; rel="last"# )
170
      ->tx->res->json;
171
172
    is( scalar @{$libraries}, 1, '1 library retrieved' );
173
174
    $libraries =
175
      $t->get_ok("//$userid:$password@/api/v1/cities?_per_page=-1")
176
      ->status_is(200)
177
      ->header_is( 'X-Total-Count', '10' )
178
      ->header_is( 'X-Base-Total-Count', '10' )
179
      ->header_unlike( 'Link', qr|rel="prev"| )
180
      ->header_unlike( 'Link', qr|rel="next"| )
181
      ->header_like( 'Link',
182
        qr#(_per_page=-1\&_page=1|_page=1\&_per_page=-1)>\; rel="first"# )
183
      ->header_like( 'Link',
184
        qr#(_per_page=-1\&_page=1|_page=1\&_per_page=-1)>\; rel="last"# )
185
      ->tx->res->json;
186
187
    is( scalar @{$libraries}, 10, '10 libraries retrieved, -1 means all' );
188
189
    $schema->storage->txn_rollback;
190
};

Return to bug 30125