View | Details | Raw Unified | Return to bug 22107
Collapse All | Expand All

(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-54 / +54 lines)
Lines 16-22 Link Here
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
use Koha::Patrons;
19
use Koha::Cities;
20
20
21
# Dummy app for testing the plugin
21
# Dummy app for testing the plugin
22
use Mojolicious::Lite;
22
use Mojolicious::Lite;
Lines 27-60 plugin 'Koha::REST::Plugin::Objects'; Link Here
27
plugin 'Koha::REST::Plugin::Query';
27
plugin 'Koha::REST::Plugin::Query';
28
plugin 'Koha::REST::Plugin::Pagination';
28
plugin 'Koha::REST::Plugin::Pagination';
29
29
30
get '/patrons' => sub {
30
get '/cities' => sub {
31
    my $c = shift;
31
    my $c = shift;
32
    $c->validation->output($c->req->params->to_hash);
32
    $c->validation->output($c->req->params->to_hash);
33
    my $patrons = $c->objects->search(Koha::Patrons->new);
33
    my $cities = $c->objects->search(Koha::Cities->new);
34
    $c->render( status => 200, json => $patrons );
34
    $c->render( status => 200, json => $cities );
35
};
35
};
36
36
37
get '/patrons_to_model' => sub {
37
get '/cities_to_model' => sub {
38
    my $c = shift;
38
    my $c = shift;
39
    $c->validation->output($c->req->params->to_hash);
39
    $c->validation->output($c->req->params->to_hash);
40
    my $patrons_set = Koha::Patrons->new;
40
    my $cities_set = Koha::Cities->new;
41
    my $patrons = $c->objects->search( $patrons_set, \&to_model );
41
    my $cities = $c->objects->search( $cities_set, \&to_model );
42
    $c->render( status => 200, json => $patrons );
42
    $c->render( status => 200, json => $cities );
43
};
43
};
44
44
45
get '/patrons_to_model_to_api' => sub {
45
get '/cities_to_model_to_api' => sub {
46
    my $c = shift;
46
    my $c = shift;
47
    $c->validation->output($c->req->params->to_hash);
47
    $c->validation->output($c->req->params->to_hash);
48
    my $patrons_set = Koha::Patrons->new;
48
    my $cities_set = Koha::Cities->new;
49
    my $patrons = $c->objects->search( $patrons_set, \&to_model, \&to_api );
49
    my $cities = $c->objects->search( $cities_set, \&to_model, \&to_api );
50
    $c->render( status => 200, json => $patrons );
50
    $c->render( status => 200, json => $cities );
51
};
51
};
52
52
53
sub to_model {
53
sub to_model {
54
    my $params = shift;
54
    my $params = shift;
55
55
56
    if ( exists $params->{nombre} ) {
56
    if ( exists $params->{nombre} ) {
57
        $params->{firstname} = delete $params->{nombre};
57
        $params->{city_name} = delete $params->{nombre};
58
    }
58
    }
59
59
60
    return $params;
60
    return $params;
Lines 63-70 sub to_model { Link Here
63
sub to_api {
63
sub to_api {
64
    my $params = shift;
64
    my $params = shift;
65
65
66
    if ( exists $params->{firstname} ) {
66
    if ( exists $params->{city_name} ) {
67
        $params->{nombre} = delete $params->{firstname};
67
        $params->{nombre} = delete $params->{city_name};
68
    }
68
    }
69
69
70
    return $params;
70
    return $params;
Lines 90-201 subtest 'objects.search helper' => sub { Link Here
90
90
91
    $schema->storage->txn_begin;
91
    $schema->storage->txn_begin;
92
92
93
    # Delete existing patrons
93
    # Remove existing cities to have more control on the search restuls
94
    Koha::Patrons->search->delete;
94
    Koha::Cities->delete;
95
95
    # Create two sample patrons that match the query
96
    # Create two sample patrons that match the query
96
    $builder->build_object({
97
    $builder->build_object({
97
        class => 'Koha::Patrons',
98
        class => 'Koha::Cities',
98
        value => {
99
        value => {
99
            firstname => 'Manuel'
100
            city_name => 'Manuel'
100
        }
101
        }
101
    });
102
    });
102
    $builder->build_object({
103
    $builder->build_object({
103
        class => 'Koha::Patrons',
104
        class => 'Koha::Cities',
104
        value => {
105
        value => {
105
            firstname => 'Manuela'
106
            city_name => 'Manuela'
106
        }
107
        }
107
    });
108
    });
108
109
109
    $t->get_ok('/patrons?firstname=manuel&_per_page=1&_page=1')
110
    $t->get_ok('/cities?city_name=manuel&_per_page=1&_page=1')
110
        ->status_is(200)
111
        ->status_is(200)
111
        ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
112
        ->header_like( 'Link' => qr/<http:\/\/.*\?.*&_page=2.*>; rel="next",/ )
112
        ->json_has('/0')
113
        ->json_has('/0')
113
        ->json_hasnt('/1')
114
        ->json_hasnt('/1')
114
        ->json_is('/0/firstname' => 'Manuel');
115
        ->json_is('/0/city_name' => 'Manuel');
115
116
116
    $builder->build_object({
117
    $builder->build_object({
117
        class => 'Koha::Patrons',
118
        class => 'Koha::Cities',
118
        value => {
119
        value => {
119
            firstname => 'Emanuel'
120
            city_name => 'Emanuel'
120
        }
121
        }
121
    });
122
    });
122
123
123
    # _match=starts_with
124
    # _match=starts_with
124
    $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=starts_with')
125
    $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=starts_with')
125
        ->status_is(200)
126
        ->status_is(200)
126
        ->json_has('/0')
127
        ->json_has('/0')
127
        ->json_has('/1')
128
        ->json_has('/1')
128
        ->json_hasnt('/2')
129
        ->json_hasnt('/2')
129
        ->json_is('/0/firstname' => 'Manuel')
130
        ->json_is('/0/city_name' => 'Manuel')
130
        ->json_is('/1/firstname' => 'Manuela');
131
        ->json_is('/1/city_name' => 'Manuela');
131
132
132
    # _match=ends_with
133
    # _match=ends_with
133
    $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=ends_with')
134
    $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=ends_with')
134
        ->status_is(200)
135
        ->status_is(200)
135
        ->json_has('/0')
136
        ->json_has('/0')
136
        ->json_has('/1')
137
        ->json_has('/1')
137
        ->json_hasnt('/2')
138
        ->json_hasnt('/2')
138
        ->json_is('/0/firstname' => 'Manuel')
139
        ->json_is('/0/city_name' => 'Manuel')
139
        ->json_is('/1/firstname' => 'Emanuel');
140
        ->json_is('/1/city_name' => 'Emanuel');
140
141
141
    # _match=exact
142
    # _match=exact
142
    $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=exact')
143
    $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=exact')
143
        ->status_is(200)
144
        ->status_is(200)
144
        ->json_has('/0')
145
        ->json_has('/0')
145
        ->json_hasnt('/1')
146
        ->json_hasnt('/1')
146
        ->json_is('/0/firstname' => 'Manuel');
147
        ->json_is('/0/city_name' => 'Manuel');
147
148
148
    # _match=contains
149
    # _match=contains
149
    $t->get_ok('/patrons?firstname=manuel&_per_page=3&_page=1&_match=contains')
150
    $t->get_ok('/cities?city_name=manuel&_per_page=3&_page=1&_match=contains')
150
        ->status_is(200)
151
        ->status_is(200)
151
        ->json_has('/0')
152
        ->json_has('/0')
152
        ->json_has('/1')
153
        ->json_has('/1')
153
        ->json_has('/2')
154
        ->json_has('/2')
154
        ->json_hasnt('/3')
155
        ->json_hasnt('/3')
155
        ->json_is('/0/firstname' => 'Manuel')
156
        ->json_is('/0/city_name' => 'Manuel')
156
        ->json_is('/1/firstname' => 'Manuela')
157
        ->json_is('/1/city_name' => 'Manuela')
157
        ->json_is('/2/firstname' => 'Emanuel');
158
        ->json_is('/2/city_name' => 'Emanuel');
158
159
159
    ## _to_model tests
160
    ## _to_model tests
160
    # _match=starts_with
161
    # _match=starts_with
161
    $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
162
    $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
162
        ->status_is(200)
163
        ->status_is(200)
163
        ->json_has('/0')
164
        ->json_has('/0')
164
        ->json_has('/1')
165
        ->json_has('/1')
165
        ->json_hasnt('/2')
166
        ->json_hasnt('/2')
166
        ->json_is('/0/firstname' => 'Manuel')
167
        ->json_is('/0/city_name' => 'Manuel')
167
        ->json_is('/1/firstname' => 'Manuela');
168
        ->json_is('/1/city_name' => 'Manuela');
168
169
169
    # _match=ends_with
170
    # _match=ends_with
170
    $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
171
    $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
171
        ->status_is(200)
172
        ->status_is(200)
172
        ->json_has('/0')
173
        ->json_has('/0')
173
        ->json_has('/1')
174
        ->json_has('/1')
174
        ->json_hasnt('/2')
175
        ->json_hasnt('/2')
175
        ->json_is('/0/firstname' => 'Manuel')
176
        ->json_is('/0/city_name' => 'Manuel')
176
        ->json_is('/1/firstname' => 'Emanuel');
177
        ->json_is('/1/city_name' => 'Emanuel');
177
178
178
    # _match=exact
179
    # _match=exact
179
    $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=exact')
180
    $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=exact')
180
        ->status_is(200)
181
        ->status_is(200)
181
        ->json_has('/0')
182
        ->json_has('/0')
182
        ->json_hasnt('/1')
183
        ->json_hasnt('/1')
183
        ->json_is('/0/firstname' => 'Manuel');
184
        ->json_is('/0/city_name' => 'Manuel');
184
185
185
    # _match=contains
186
    # _match=contains
186
    $t->get_ok('/patrons_to_model?nombre=manuel&_per_page=3&_page=1&_match=contains')
187
    $t->get_ok('/cities_to_model?nombre=manuel&_per_page=3&_page=1&_match=contains')
187
        ->status_is(200)
188
        ->status_is(200)
188
        ->json_has('/0')
189
        ->json_has('/0')
189
        ->json_has('/1')
190
        ->json_has('/1')
190
        ->json_has('/2')
191
        ->json_has('/2')
191
        ->json_hasnt('/3')
192
        ->json_hasnt('/3')
192
        ->json_is('/0/firstname' => 'Manuel')
193
        ->json_is('/0/city_name' => 'Manuel')
193
        ->json_is('/1/firstname' => 'Manuela')
194
        ->json_is('/1/city_name' => 'Manuela')
194
        ->json_is('/2/firstname' => 'Emanuel');
195
        ->json_is('/2/city_name' => 'Emanuel');
195
196
196
    ## _to_model && _to_api tests
197
    ## _to_model && _to_api tests
197
    # _match=starts_with
198
    # _match=starts_with
198
    $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
199
    $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=starts_with')
199
        ->status_is(200)
200
        ->status_is(200)
200
        ->json_has('/0')
201
        ->json_has('/0')
201
        ->json_has('/1')
202
        ->json_has('/1')
Lines 204-210 subtest 'objects.search helper' => sub { Link Here
204
        ->json_is('/1/nombre' => 'Manuela');
205
        ->json_is('/1/nombre' => 'Manuela');
205
206
206
    # _match=ends_with
207
    # _match=ends_with
207
    $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
208
    $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=ends_with')
208
        ->status_is(200)
209
        ->status_is(200)
209
        ->json_has('/0')
210
        ->json_has('/0')
210
        ->json_has('/1')
211
        ->json_has('/1')
Lines 213-226 subtest 'objects.search helper' => sub { Link Here
213
        ->json_is('/1/nombre' => 'Emanuel');
214
        ->json_is('/1/nombre' => 'Emanuel');
214
215
215
    # _match=exact
216
    # _match=exact
216
    $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact')
217
    $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=exact')
217
        ->status_is(200)
218
        ->status_is(200)
218
        ->json_has('/0')
219
        ->json_has('/0')
219
        ->json_hasnt('/1')
220
        ->json_hasnt('/1')
220
        ->json_is('/0/nombre' => 'Manuel');
221
        ->json_is('/0/nombre' => 'Manuel');
221
222
222
    # _match=contains
223
    # _match=contains
223
    $t->get_ok('/patrons_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains')
224
    $t->get_ok('/cities_to_model_to_api?nombre=manuel&_per_page=3&_page=1&_match=contains')
224
        ->status_is(200)
225
        ->status_is(200)
225
        ->json_has('/0')
226
        ->json_has('/0')
226
        ->json_has('/1')
227
        ->json_has('/1')
227
- 

Return to bug 22107