]> git.koha-community.org Git - koha.git/blob - t/db_dependent/Koha/REST/Plugin/Objects.t
Bug 24321: Make objects.search use mappings from Koha::Object(s)
[koha.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 use Koha::Cities;
20
21 # Dummy app for testing the plugin
22 use Mojolicious::Lite;
23
24 app->log->level('error');
25
26 plugin 'Koha::REST::Plugin::Objects';
27 plugin 'Koha::REST::Plugin::Query';
28 plugin 'Koha::REST::Plugin::Pagination';
29
30 get '/cities' => sub {
31     my $c = shift;
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 );
35 };
36
37 # The tests
38 use Test::More tests => 2;
39 use Test::Mojo;
40
41 use t::lib::TestBuilder;
42 use Koha::Database;
43
44 my $schema = Koha::Database->new()->schema();
45
46
47 my $builder = t::lib::TestBuilder->new;
48
49 subtest 'objects.search helper' => sub {
50
51     plan tests => 34;
52
53     my $t = Test::Mojo->new;
54
55     $schema->storage->txn_begin;
56
57     # Remove existing cities to have more control on the search restuls
58     Koha::Cities->delete;
59
60     # Create two sample patrons that match the query
61     $builder->build_object({
62         class => 'Koha::Cities',
63         value => {
64             city_name => 'Manuel'
65         }
66     });
67     $builder->build_object({
68         class => 'Koha::Cities',
69         value => {
70             city_name => 'Manuela'
71         }
72     });
73
74     $t->get_ok('/cities?name=manuel&_per_page=1&_page=1')
75         ->status_is(200)
76         ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
77         ->json_has('/0')
78         ->json_hasnt('/1')
79         ->json_is('/0/name' => 'Manuel');
80
81     $builder->build_object({
82         class => 'Koha::Cities',
83         value => {
84             city_name => 'Emanuel'
85         }
86     });
87
88     # _match=starts_with
89     $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=starts_with')
90         ->status_is(200)
91         ->json_has('/0')
92         ->json_has('/1')
93         ->json_hasnt('/2')
94         ->json_is('/0/name' => 'Manuel')
95         ->json_is('/1/name' => 'Manuela');
96
97     # _match=ends_with
98     $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=ends_with')
99         ->status_is(200)
100         ->json_has('/0')
101         ->json_has('/1')
102         ->json_hasnt('/2')
103         ->json_is('/0/name' => 'Manuel')
104         ->json_is('/1/name' => 'Emanuel');
105
106     # _match=exact
107     $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=exact')
108         ->status_is(200)
109         ->json_has('/0')
110         ->json_hasnt('/1')
111         ->json_is('/0/name' => 'Manuel');
112
113     # _match=contains
114     $t->get_ok('/cities?name=manuel&_per_page=3&_page=1&_match=contains')
115         ->status_is(200)
116         ->json_has('/0')
117         ->json_has('/1')
118         ->json_has('/2')
119         ->json_hasnt('/3')
120         ->json_is('/0/name' => 'Manuel')
121         ->json_is('/1/name' => 'Manuela')
122         ->json_is('/2/name' => 'Emanuel');
123
124     $schema->storage->txn_rollback;
125 };
126
127 subtest 'objects.search helper, sorting on mapped column' => sub {
128
129     plan tests => 14;
130
131     my $t = Test::Mojo->new;
132
133     $schema->storage->txn_begin;
134
135     # Have complete control over the existing cities to ease testing
136     Koha::Cities->delete;
137
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' } });
140
141     $t->get_ok('/cities?_order_by=%2Bname&_order_by=+country')
142       ->status_is(200)
143       ->json_has('/0')
144       ->json_has('/1')
145       ->json_hasnt('/2')
146       ->json_is('/0/name' => 'A')
147       ->json_is('/1/name' => 'B');
148
149     $t->get_ok('/cities?_order_by=-name')
150       ->status_is(200)
151       ->json_has('/0')
152       ->json_has('/1')
153       ->json_hasnt('/2')
154       ->json_is('/0/name' => 'B')
155       ->json_is('/1/name' => 'A');
156
157     $schema->storage->txn_rollback;
158 }