]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Koha/REST/Plugin/Objects.t
Bug 19686: Unit tests
[koha.git] / t / db_dependent / Koha / REST / Plugin / Objects.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 use Koha::Patrons;
20
21 # Dummy app for testing the plugin
22 use Mojolicious::Lite;
23
24 app->log->level('error');
25
26 plugin 'Koha::REST::Plugin::Objects';
27 plugin 'Koha::REST::Plugin::Query';
28 plugin 'Koha::REST::Plugin::Pagination';
29
30 get '/patrons' => sub {
31     my $c = shift;
32     $c->validation->output($c->req->params->to_hash);
33     my $patrons = $c->objects->search(Koha::Patrons->new);
34     $c->render( status => 200, json => $patrons );
35 };
36
37 get '/patrons_to_model' => sub {
38     my $c = shift;
39     $c->validation->output($c->req->params->to_hash);
40     my $patrons_set = Koha::Patrons->new;
41     my $patrons = $c->objects->search( $patrons_set, \&_to_model );
42     $c->render( status => 200, json => $patrons );
43 };
44
45 sub _to_model {
46     my $params = shift;
47
48     if ( exists $params->{nombre} ) {
49         $params->{firstname} = delete $params->{nombre};
50     }
51
52     return $params;
53 }
54
55 # The tests
56 use Test::More tests => 1;
57 use Test::Mojo;
58
59 use t::lib::TestBuilder;
60 use Koha::Database;
61
62 my $schema = Koha::Database->new()->schema();
63
64
65 my $builder = t::lib::TestBuilder->new;
66
67 subtest 'objects.search helper' => sub {
68
69     plan tests => 62;
70
71     my $t = Test::Mojo->new;
72
73     $schema->storage->txn_begin;
74
75     # Delete existing patrons
76     Koha::Patrons->search->delete;
77     # Create two sample patrons that match the query
78     $builder->build_object({
79         class => 'Koha::Patrons',
80         value => {
81             firstname => 'Manuel'
82         }
83     });
84     $builder->build_object({
85         class => 'Koha::Patrons',
86         value => {
87             firstname => 'Manuela'
88         }
89     });
90
91     $t->get_ok('/patrons?firstname=manuel&_per_page=1&_page=1')
92         ->status_is(200)
93         ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
94         ->json_has('/0')
95         ->json_hasnt('/1')
96         ->json_is('/0/firstname' => 'Manuel');
97
98     $builder->build_object({
99         class => 'Koha::Patrons',
100         value => {
101             firstname => 'Emanuel'
102         }
103     });
104
105     # _match=starts_with
106     $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=starts_with')
107         ->status_is(200)
108         ->json_has('/0')
109         ->json_has('/1')
110         ->json_hasnt('/2')
111         ->json_is('/0/firstname' => 'Manuel')
112         ->json_is('/1/firstname' => 'Manuela');
113
114     # _match=ends_with
115     $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=ends_with')
116         ->status_is(200)
117         ->json_has('/0')
118         ->json_has('/1')
119         ->json_hasnt('/2')
120         ->json_is('/0/firstname' => 'Manuel')
121         ->json_is('/1/firstname' => 'Emanuel');
122
123     # _match=exact
124     $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=exact')
125         ->status_is(200)
126         ->json_has('/0')
127         ->json_hasnt('/1')
128         ->json_is('/0/firstname' => 'Manuel');
129
130     # _match=contains
131     $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=contains')
132         ->status_is(200)
133         ->json_has('/0')
134         ->json_has('/1')
135         ->json_has('/2')
136         ->json_hasnt('/3')
137         ->json_is('/0/firstname' => 'Manuel')
138         ->json_is('/1/firstname' => 'Manuela')
139         ->json_is('/2/firstname' => 'Emanuel');
140
141     ## _to_model tests
142     # _match=starts_with
143     $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
144         ->status_is(200)
145         ->json_has('/0')
146         ->json_has('/1')
147         ->json_hasnt('/2')
148         ->json_is('/0/firstname' => 'Manuel')
149         ->json_is('/1/firstname' => 'Manuela');
150
151     # _match=ends_with
152     $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
153         ->status_is(200)
154         ->json_has('/0')
155         ->json_has('/1')
156         ->json_hasnt('/2')
157         ->json_is('/0/firstname' => 'Manuel')
158         ->json_is('/1/firstname' => 'Emanuel');
159
160     # _match=exact
161     $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=exact')
162         ->status_is(200)
163         ->json_has('/0')
164         ->json_hasnt('/1')
165         ->json_is('/0/firstname' => 'Manuel');
166
167     # _match=contains
168     $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=contains')
169         ->status_is(200)
170         ->json_has('/0')
171         ->json_has('/1')
172         ->json_has('/2')
173         ->json_hasnt('/3')
174         ->json_is('/0/firstname' => 'Manuel')
175         ->json_is('/1/firstname' => 'Manuela')
176         ->json_is('/2/firstname' => 'Emanuel');
177
178     $schema->storage->txn_rollback;
179 };