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>.
20 use Test::More tests => 1;
23 use t::lib::TestBuilder;
29 my $schema = Koha::Database->new->schema;
30 my $builder = t::lib::TestBuilder->new;
32 my $t = Test::Mojo->new('Koha::REST::V1');
33 t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
35 subtest 'q handling tests' => sub {
39 $schema->storage->txn_begin;
41 my $librarian = $builder->build_object(
43 class => 'Koha::Patrons',
44 value => { flags => 2**2 } # catalogue flag = 2
47 my $password = 'thePassword123';
48 $librarian->set_password( { password => $password, skip_validation => 1 } );
49 my $userid = $librarian->userid;
52 Koha::Cities->new->delete;
54 # No cities, so empty array should be returned
55 $t->get_ok("//$userid:$password@/api/v1/cities")->status_is(200)
58 my $names = [ 'AA', 'BA', 'BA', 'CA', 'DA', 'EB', 'FB', 'GB', 'HB', 'IB', ];
61 foreach my $i ( 0 .. 9 ) {
62 $builder->build_object(
63 { class => 'Koha::Cities', value => { city_name => $names->[$i] } }
67 t::lib::Mocks::mock_preference( 'RESTdefaultPageSize', 20 );
69 my $q_ends_with_a = 'q={"name":{"-like":"%A"}}';
73 "//$userid:$password@/api/v1/cities?$q_ends_with_a")
74 ->status_is(200)->tx->res->json;
76 is( scalar @{$cities}, 5, '5 cities retrieved' );
78 my $q_starts_with_a = 'q={"name":{"-like":"A%"}}';
82 "//$userid:$password@/api/v1/cities?$q_ends_with_a&$q_starts_with_a")
83 ->status_is(200)->tx->res->json;
85 is( scalar @{$cities}, 1, '1 city retrieved' );
87 my $q_empty_list = "q=[]";
91 "//$userid:$password@/api/v1/cities?$q_ends_with_a&$q_starts_with_a&$q_empty_list")
92 ->status_is(200)->tx->res->json;
94 is( scalar @{$cities}, 1, 'empty list as trailing query, 1 city retrieved' );
98 "//$userid:$password@/api/v1/cities?$q_empty_list&$q_ends_with_a&$q_starts_with_a")
99 ->status_is(200)->tx->res->json;
101 is( scalar @{$cities}, 1, 'empty list as first query, 1 city retrieved' );
103 $t->get_ok("//$userid:$password@/api/v1/cities" => { 'x-koha-request-id' => 100 } )
104 ->header_is( 'x-koha-request-id' => 100 );
106 $schema->storage->txn_rollback;