Bug 30165: Regression tests
[koha.git] / t / db_dependent / api / v1 / query.t
1 #!/usr/bin/env 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
20 use Test::More tests => 1;
21 use Test::Mojo;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use Koha::Cities;
27 use Koha::Database;
28
29 my $schema  = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
31
32 my $t = Test::Mojo->new('Koha::REST::V1');
33 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
35 subtest 'q handling tests' => sub {
36
37     plan tests => 15;
38
39     $schema->storage->txn_begin;
40
41     my $librarian = $builder->build_object(
42         {
43             class => 'Koha::Patrons',
44             value => { flags => 2**2 }    # catalogue flag = 2
45         }
46     );
47     my $password = 'thePassword123';
48     $librarian->set_password( { password => $password, skip_validation => 1 } );
49     my $userid = $librarian->userid;
50
51     # delete all cities
52     Koha::Cities->new->delete;
53
54     # No cities, so empty array should be returned
55     $t->get_ok("//$userid:$password@/api/v1/cities")->status_is(200)
56       ->json_is( [] );
57
58     my $names = [ 'AA', 'BA', 'BA', 'CA', 'DA', 'EB', 'FB', 'GB', 'HB', 'IB', ];
59
60     # Add 10 cities
61     foreach my $i ( 0 .. 9 ) {
62         $builder->build_object(
63             { class => 'Koha::Cities', value => { city_name => $names->[$i] } }
64         );
65     }
66
67     t::lib::Mocks::mock_preference( 'RESTdefaultPageSize', 20 );
68
69     my $q_ends_with_a = 'q={"name":{"-like":"%A"}}';
70
71     my $cities =
72       $t->get_ok(
73         "//$userid:$password@/api/v1/cities?$q_ends_with_a")
74       ->status_is(200)->tx->res->json;
75
76     is( scalar @{$cities}, 5, '5 cities retrieved' );
77
78     my $q_starts_with_a = 'q={"name":{"-like":"A%"}}';
79
80     $cities =
81       $t->get_ok(
82         "//$userid:$password@/api/v1/cities?$q_ends_with_a&$q_starts_with_a")
83       ->status_is(200)->tx->res->json;
84
85     is( scalar @{$cities}, 1, '1 city retrieved' );
86
87     my $q_empty_list = "q=[]";
88
89     $cities =
90       $t->get_ok(
91         "//$userid:$password@/api/v1/cities?$q_ends_with_a&$q_starts_with_a&$q_empty_list")
92       ->status_is(200)->tx->res->json;
93
94     is( scalar @{$cities}, 1, 'empty list as trailing query, 1 city retrieved' );
95
96     $cities =
97       $t->get_ok(
98         "//$userid:$password@/api/v1/cities?$q_empty_list&$q_ends_with_a&$q_starts_with_a")
99       ->status_is(200)->tx->res->json;
100
101     is( scalar @{$cities}, 1, 'empty list as first query, 1 city retrieved' );
102
103     $schema->storage->txn_rollback;
104 };