a25eb21830dee9608fbeec07d7665c9fbb785d41
[koha-ffzg.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
20 use Koha::Acquisition::Orders;
21 use Koha::Cities;
22 use Koha::Holds;
23
24 # Dummy app for testing the plugin
25 use Mojolicious::Lite;
26
27 app->log->level('error');
28
29 plugin 'Koha::REST::Plugin::Objects';
30 plugin 'Koha::REST::Plugin::Query';
31 plugin 'Koha::REST::Plugin::Pagination';
32
33 get '/cities' => sub {
34     my $c = shift;
35     $c->validation->output($c->req->params->to_hash);
36     my $cities = $c->objects->search(Koha::Cities->new);
37     $c->render( status => 200, json => $cities );
38 };
39
40 get '/orders' => sub {
41     my $c = shift;
42     $c->stash('koha.embed', ( { fund => {} } ) );
43     $c->validation->output($c->req->params->to_hash);
44     my $orders = $c->objects->search(Koha::Acquisition::Orders->new);
45     $c->render( status => 200, json => $orders );
46 };
47
48 get '/patrons/:patron_id/holds' => sub {
49     my $c = shift;
50     my $params = $c->req->params->to_hash;
51     $params->{patron_id} = $c->stash("patron_id");
52     $c->validation->output($params);
53     my $holds_set = Koha::Holds->new;
54     my $holds     = $c->objects->search( $holds_set );
55     $c->render( status => 200, json => {count => scalar(@$holds)} );
56 };
57
58 # The tests
59 use Test::More tests => 4;
60 use Test::Mojo;
61
62 use t::lib::TestBuilder;
63 use Koha::Database;
64
65 my $t = Test::Mojo->new;
66
67 my $schema  = Koha::Database->new()->schema();
68 my $builder = t::lib::TestBuilder->new;
69
70 subtest 'objects.search helper' => sub {
71
72     plan tests => 34;
73
74     $schema->storage->txn_begin;
75
76     # Remove existing cities to have more control on the search restuls
77     Koha::Cities->delete;
78
79     # Create two sample patrons that match the query
80     $builder->build_object({
81         class => 'Koha::Cities',
82         value => {
83             city_name => 'Manuel'
84         }
85     });
86     $builder->build_object({
87         class => 'Koha::Cities',
88         value => {
89             city_name => 'Manuela'
90         }
91     });
92
93     $t->get_ok('/cities?name=manuel&_per_page=1&_page=1')
94         ->status_is(200)
95         ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
96         ->json_has('/0')
97         ->json_hasnt('/1')
98         ->json_is('/0/name' => 'Manuel');
99
100     $builder->build_object({
101         class => 'Koha::Cities',
102         value => {
103             city_name => 'Emanuel'
104         }
105     });
106
107     # _match=starts_with
108     $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=starts_with')
109         ->status_is(200)
110         ->json_has('/0')
111         ->json_has('/1')
112         ->json_hasnt('/2')
113         ->json_is('/0/name' => 'Manuel')
114         ->json_is('/1/name' => 'Manuela');
115
116     # _match=ends_with
117     $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=ends_with')
118         ->status_is(200)
119         ->json_has('/0')
120         ->json_has('/1')
121         ->json_hasnt('/2')
122         ->json_is('/0/name' => 'Manuel')
123         ->json_is('/1/name' => 'Emanuel');
124
125     # _match=exact
126     $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=exact')
127         ->status_is(200)
128         ->json_has('/0')
129         ->json_hasnt('/1')
130         ->json_is('/0/name' => 'Manuel');
131
132     # _match=contains
133     $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=contains')
134         ->status_is(200)
135         ->json_has('/0')
136         ->json_has('/1')
137         ->json_has('/2')
138         ->json_hasnt('/3')
139         ->json_is('/0/name' => 'Manuel')
140         ->json_is('/1/name' => 'Manuela')
141         ->json_is('/2/name' => 'Emanuel');
142
143     $schema->storage->txn_rollback;
144 };
145
146 subtest 'objects.search helper, sorting on mapped column' => sub {
147
148     plan tests => 14;
149
150     $schema->storage->txn_begin;
151
152     # Have complete control over the existing cities to ease testing
153     Koha::Cities->delete;
154
155     $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'A', city_country => 'Argentina' } });
156     $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'B', city_country => 'Argentina' } });
157
158     $t->get_ok('/cities?_order_by=%2Bname&_order_by=+country')
159       ->status_is(200)
160       ->json_has('/0')
161       ->json_has('/1')
162       ->json_hasnt('/2')
163       ->json_is('/0/name' => 'A')
164       ->json_is('/1/name' => 'B');
165
166     $t->get_ok('/cities?_order_by=-name')
167       ->status_is(200)
168       ->json_has('/0')
169       ->json_has('/1')
170       ->json_hasnt('/2')
171       ->json_is('/0/name' => 'B')
172       ->json_is('/1/name' => 'A');
173
174     $schema->storage->txn_rollback;
175 };
176
177 subtest 'objects.search helper, embed' => sub {
178
179     plan tests => 2;
180
181     $schema->storage->txn_begin;
182
183     my $order = $builder->build_object({ class => 'Koha::Acquisition::Orders' });
184
185     $t->get_ok('/orders?order_id=' . $order->ordernumber)
186       ->json_is('/0',$order->to_api({ embed => ( { fund => {} } ) }));
187
188     $schema->storage->txn_rollback;
189 };
190
191 subtest 'objects.search helper, with path parameters and _match' => sub {
192     plan tests => 4;
193
194     $schema->storage->txn_begin;
195
196     Koha::Holds->search()->delete;
197
198     $builder->build_object({class=>"Koha::Holds", value => {borrowernumber => 10 }});
199
200     $t->get_ok('/patrons/1/holds?_match=exact')
201       ->json_is('/count' => 0, 'there should be no holds for borrower 1 with _match=exact');
202
203     $t->get_ok('/patrons/1/holds?_match=contains')
204       ->json_is('/count' => 0, 'there should be no holds for borrower 1 with _match=contains');
205
206     $schema->storage->txn_rollback;
207 };