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 '/patrons' => sub {
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 );
37 get '/patrons_to_model' => sub {
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 );
48 if ( exists $params->{nombre} ) {
49 $params->{firstname} = delete $params->{nombre};
56 use Test::More tests => 1;
59 use t::lib::TestBuilder;
62 my $schema = Koha::Database->new()->schema();
65 my $builder = t::lib::TestBuilder->new;
67 subtest 'objects.search helper' => sub {
71 my $t = Test::Mojo->new;
73 $schema->storage->txn_begin;
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',
84 $builder->build_object({
85 class => 'Koha::Patrons',
87 firstname => 'Manuela'
91 $t->get_ok('/patrons?firstname=manuel&_per_page=1&_page=1')
93 ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
96 ->json_is('/0/firstname' => 'Manuel');
98 $builder->build_object({
99 class => 'Koha::Patrons',
101 firstname => 'Emanuel'
106 $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=starts_with')
111 ->json_is('/0/firstname' => 'Manuel')
112 ->json_is('/1/firstname' => 'Manuela');
115 $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=ends_with')
120 ->json_is('/0/firstname' => 'Manuel')
121 ->json_is('/1/firstname' => 'Emanuel');
124 $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=exact')
128 ->json_is('/0/firstname' => 'Manuel');
131 $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=contains')
137 ->json_is('/0/firstname' => 'Manuel')
138 ->json_is('/1/firstname' => 'Manuela')
139 ->json_is('/2/firstname' => 'Emanuel');
143 $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
148 ->json_is('/0/firstname' => 'Manuel')
149 ->json_is('/1/firstname' => 'Manuela');
152 $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
157 ->json_is('/0/firstname' => 'Manuel')
158 ->json_is('/1/firstname' => 'Emanuel');
161 $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=exact')
165 ->json_is('/0/firstname' => 'Manuel');
168 $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=contains')
174 ->json_is('/0/firstname' => 'Manuel')
175 ->json_is('/1/firstname' => 'Manuela')
176 ->json_is('/2/firstname' => 'Emanuel');
178 $schema->storage->txn_rollback;