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>.
21 # Dummy app for testing the plugin
22 use Mojolicious::Lite;
24 app->log->level('error');
26 plugin 'Koha::REST::Plugin::Objects';
27 plugin 'Koha::REST::Plugin::Query';
28 plugin 'Koha::REST::Plugin::Pagination';
30 get '/cities' => sub {
32 $c->validation->output($c->req->params->to_hash);
33 my $cities = $c->objects->search(Koha::Cities->new);
34 $c->render( status => 200, json => $cities );
38 use Test::More tests => 2;
41 use t::lib::TestBuilder;
44 my $schema = Koha::Database->new()->schema();
47 my $builder = t::lib::TestBuilder->new;
49 subtest 'objects.search helper' => sub {
53 my $t = Test::Mojo->new;
55 $schema->storage->txn_begin;
57 # Remove existing cities to have more control on the search restuls
60 # Create two sample patrons that match the query
61 $builder->build_object({
62 class => 'Koha::Cities',
67 $builder->build_object({
68 class => 'Koha::Cities',
70 city_name => 'Manuela'
74 $t->get_ok('/cities?name=manuel&_per_page=1&_page=1')
76 ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
79 ->json_is('/0/name' => 'Manuel');
81 $builder->build_object({
82 class => 'Koha::Cities',
84 city_name => 'Emanuel'
89 $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=starts_with')
94 ->json_is('/0/name' => 'Manuel')
95 ->json_is('/1/name' => 'Manuela');
98 $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=ends_with')
103 ->json_is('/0/name' => 'Manuel')
104 ->json_is('/1/name' => 'Emanuel');
107 $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=exact')
111 ->json_is('/0/name' => 'Manuel');
114 $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=contains')
120 ->json_is('/0/name' => 'Manuel')
121 ->json_is('/1/name' => 'Manuela')
122 ->json_is('/2/name' => 'Emanuel');
124 $schema->storage->txn_rollback;
127 subtest 'objects.search helper, sorting on mapped column' => sub {
131 my $t = Test::Mojo->new;
133 $schema->storage->txn_begin;
135 # Have complete control over the existing cities to ease testing
136 Koha::Cities->delete;
138 $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'A', city_country => 'Argentina' } });
139 $builder->build_object({ class => 'Koha::Cities', value => { city_name => 'B', city_country => 'Argentina' } });
141 $t->get_ok('/cities?_order_by=%2Bname&_order_by=+country')
146 ->json_is('/0/name' => 'A')
147 ->json_is('/1/name' => 'B');
149 $t->get_ok('/cities?_order_by=-name')
154 ->json_is('/0/name' => 'B')
155 ->json_is('/1/name' => 'A');
157 $schema->storage->txn_rollback;