Bug 19370: (QA follow-up) Use OpenAPI's handling of pipe separated values
[koha.git] / t / Koha / REST / Plugin / Query.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::Query';
26
27 get '/empty' => sub {
28     my $c = shift;
29     $c->render( json => undef, status => 200 );
30 };
31
32 get '/query' => sub {
33     my $c     = shift;
34     my $input = {
35         _page     => 2,
36         _per_page => 3,
37         firstname => 'Manuel',
38         surname   => 'Cohen Arazi'
39     };
40     my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
41     $c->render(
42         json => {
43             filtered_params => $filtered_params,
44             reserved_params => $reserved_params
45         },
46         status => 200
47     );
48 };
49
50 get '/query_full' => sub {
51     my $c     = shift;
52     my $input = {
53         _match    => 'exact',
54         _order_by => 'blah',
55         _page     => 2,
56         _per_page => 3,
57         firstname => 'Manuel',
58         surname   => 'Cohen Arazi'
59     };
60     my ( $filtered_params, $reserved_params ) = $c->extract_reserved_params($input);
61     $c->render(
62         json => {
63             filtered_params => $filtered_params,
64             reserved_params => $reserved_params
65         },
66         status => 200
67     );
68 };
69
70 get '/dbic_merge_sorting' => sub {
71     my $c = shift;
72     my $attributes = { a => 'a', b => 'b' };
73     $attributes = $c->dbic_merge_sorting(
74         {
75             attributes => $attributes,
76             params     => { _match => 'exact', _order_by => [ 'uno', '-dos', '+tres', ' cuatro' ] }
77         }
78     );
79     $c->render( json => $attributes, status => 200 );
80 };
81
82 # The tests
83
84 use Test::More tests => 2;
85 use Test::Mojo;
86
87 subtest 'extract_reserved_params() tests' => sub {
88
89     plan tests => 8;
90
91     my $t = Test::Mojo->new;
92
93     $t->get_ok('/query')->status_is(200)
94       ->json_is( '/filtered_params' =>
95           { firstname => 'Manuel', surname => 'Cohen Arazi' } )
96       ->json_is( '/reserved_params' => { _page => 2, _per_page => 3 } );
97
98     $t->get_ok('/query_full')->status_is(200)
99       ->json_is(
100         '/filtered_params' => {
101             firstname => 'Manuel',
102             surname   => 'Cohen Arazi'
103         } )
104       ->json_is(
105         '/reserved_params' => {
106             _page     => 2,
107             _per_page => 3,
108             _match    => 'exact',
109             _order_by => 'blah'
110         } );
111
112 };
113
114 subtest 'dbic_merge_sorting() tests' => sub {
115
116     plan tests => 5;
117
118     my $t = Test::Mojo->new;
119
120     $t->get_ok('/dbic_merge_sorting')->status_is(200)
121       ->json_is( '/a' => 'a', 'Existing values are kept (a)' )
122       ->json_is( '/b' => 'b', 'Existing values are kept (b)' )->json_is(
123         '/order_by' => [
124             'uno',
125             { -desc => 'dos' },
126             { -asc  => 'tres' },
127             { -asc  => 'cuatro' }
128         ]
129       );
130 };