3 # This file is part of Koha.
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.
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.
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>.
20 use Koha::Acquisition::Orders;
24 # Dummy app for testing the plugin
25 use Mojolicious::Lite;
27 app->log->level('error');
29 plugin 'Koha::REST::Plugin::Objects';
30 plugin 'Koha::REST::Plugin::Query';
31 plugin 'Koha::REST::Plugin::Pagination';
33 get '/cities' => sub {
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 );
40 get '/orders' => sub {
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 );
48 get '/patrons/:patron_id/holds' => sub {
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)} );
59 use Test::More tests => 4;
62 use t::lib::TestBuilder;
65 my $t = Test::Mojo->new;
67 my $schema = Koha::Database->new()->schema();
68 my $builder = t::lib::TestBuilder->new;
70 subtest 'objects.search helper' => sub {
74 $schema->storage->txn_begin;
76 # Remove existing cities to have more control on the search results
79 # Create three sample cities that match the query. This makes sure we
80 # always have a "next" link regardless of Mojolicious::Plugin::OpenAPI version.
81 $builder->build_object({
82 class => 'Koha::Cities',
87 $builder->build_object({
88 class => 'Koha::Cities',
90 city_name => 'Manuela'
93 $builder->build_object({
94 class => 'Koha::Cities',
96 city_name => 'Manuelab'
100 $t->get_ok('/cities?name=manuel&_per_page=1&_page=1')
102 ->header_like( 'Link' => qr/<http:\/\/.*[\?&]_page=2.*>; rel="next",/ )
105 ->json_is('/0/name' => 'Manuel');
107 $builder->build_object({
108 class => 'Koha::Cities',
110 city_name => 'Emanuel'
115 $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=starts_with')
121 ->json_is('/0/name' => 'Manuel')
122 ->json_is('/1/name' => 'Manuela')
123 ->json_is('/2/name' => 'Manuelab');
126 $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=ends_with')
131 ->json_is('/0/name' => 'Manuel')
132 ->json_is('/1/name' => 'Emanuel');
135 $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=exact')
139 ->json_is('/0/name' => 'Manuel');
142 $t->get_ok('/cities?name=manuel&_per_page=4&_page=1&_match=contains')
149 ->json_is('/0/name' => 'Manuel')
150 ->json_is('/1/name' => 'Manuela')
151 ->json_is('/2/name' => 'Manuelab')
152 ->json_is('/3/name' => 'Emanuel');
154 $schema->storage->txn_rollback;
157 subtest 'objects.search helper, sorting on mapped column' => sub {
161 $schema->storage->txn_begin;
163 # Have complete control over the existing cities to ease testing
164 Koha::Cities->delete;
166 $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'A', city_country => 'Argentina' } });
167 $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'B', city_country => 'Argentina' } });
169 $t->get_ok('/cities?_order_by=%2Bname&_order_by=+country')
174 ->json_is('/0/name' => 'A')
175 ->json_is('/1/name' => 'B');
177 $t->get_ok('/cities?_order_by=-name')
182 ->json_is('/0/name' => 'B')
183 ->json_is('/1/name' => 'A');
185 $schema->storage->txn_rollback;
188 subtest 'objects.search helper, embed' => sub {
192 $schema->storage->txn_begin;
194 my $order = $builder->build_object({ class => 'Koha::Acquisition::Orders' });
196 $t->get_ok('/orders?order_id=' . $order->ordernumber)
197 ->json_is('/0',$order->to_api({ embed => ( { fund => {} } ) }));
199 $schema->storage->txn_rollback;
202 subtest 'objects.search helper, with path parameters and _match' => sub {
205 $schema->storage->txn_begin;
207 Koha::Holds->search()->delete;
209 my $patron = Koha::Patrons->find(10);
210 $patron->delete if $patron;
211 $patron = $builder->build_object( { class => "Koha::Patrons" } );
212 $patron->borrowernumber(10)->store;
213 $builder->build_object(
215 class => "Koha::Holds",
216 value => { borrowernumber => $patron->borrowernumber }
220 $t->get_ok('/patrons/1/holds?_match=exact')
221 ->json_is('/count' => 0, 'there should be no holds for borrower 1 with _match=exact');
223 $t->get_ok('/patrons/1/holds?_match=contains')
224 ->json_is('/count' => 0, 'there should be no holds for borrower 1 with _match=contains');
226 $t->get_ok('/patrons/10/holds?_match=exact')
227 ->json_is('/count' => 1, 'there should be 1 hold for borrower 10 with _match=exact');
229 $t->get_ok('/patrons/10/holds?_match=contains')
230 ->json_is('/count' => 1, 'there should be 1 hold for borrower 10 with _match=contains');
232 $schema->storage->txn_rollback;