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 restuls
79 # Create two sample patrons that match the query
80 $builder->build_object({
81 class => 'Koha::Cities',
86 $builder->build_object({
87 class => 'Koha::Cities',
89 city_name => 'Manuela'
93 $t->get_ok('/cities?name=manuel&_per_page=1&_page=1')
95 ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
98 ->json_is('/0/name' => 'Manuel');
100 $builder->build_object({
101 class => 'Koha::Cities',
103 city_name => 'Emanuel'
108 $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=starts_with')
113 ->json_is('/0/name' => 'Manuel')
114 ->json_is('/1/name' => 'Manuela');
117 $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=ends_with')
122 ->json_is('/0/name' => 'Manuel')
123 ->json_is('/1/name' => 'Emanuel');
126 $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=exact')
130 ->json_is('/0/name' => 'Manuel');
133 $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=contains')
139 ->json_is('/0/name' => 'Manuel')
140 ->json_is('/1/name' => 'Manuela')
141 ->json_is('/2/name' => 'Emanuel');
143 $schema->storage->txn_rollback;
146 subtest 'objects.search helper, sorting on mapped column' => sub {
150 $schema->storage->txn_begin;
152 # Have complete control over the existing cities to ease testing
153 Koha::Cities->delete;
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' } });
158 $t->get_ok('/cities?_order_by=%2Bname&_order_by=+country')
163 ->json_is('/0/name' => 'A')
164 ->json_is('/1/name' => 'B');
166 $t->get_ok('/cities?_order_by=-name')
171 ->json_is('/0/name' => 'B')
172 ->json_is('/1/name' => 'A');
174 $schema->storage->txn_rollback;
177 subtest 'objects.search helper, embed' => sub {
181 $schema->storage->txn_begin;
183 my $order = $builder->build_object({ class => 'Koha::Acquisition::Orders' });
185 $t->get_ok('/orders?order_id=' . $order->ordernumber)
186 ->json_is('/0',$order->to_api({ embed => ( { fund => {} } ) }));
188 $schema->storage->txn_rollback;
191 subtest 'objects.search helper, with path parameters and _match' => sub {
194 $schema->storage->txn_begin;
196 Koha::Holds->search()->delete;
198 $builder->build_object({class=>"Koha::Holds", value => {borrowernumber => 10 }});
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');
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');
206 $schema->storage->txn_rollback;