Bug 19337: Make basic_workflow.t configurable through ENV
[koha.git] / t / db_dependent / Koha_SearchEngine_Elasticsearch_Search.t
blob5a6b86d322213013caace9d7525241ab13f2ab2f
1 # Copyright 2015 Catalyst IT
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>.
18 use Modern::Perl;
20 use Module::Load::Conditional qw[can_load check_install requires];
21 use Test::More;
22 use t::lib::Mocks;
24 if ( ! can_load(
25     modules => { 'Koha::SearchEngine::Elasticsearch::Indexer' => undef, } )
26 ) {
27     my $missing_module;
28     if ( $Module::Load::Conditional::ERROR =~ /Can\'t locate (.*?) / ) {
29         $missing_module = $1;
30     }
31     my $es_dep_msg = "Required module $missing_module is not installed";
32     plan( skip_all => $es_dep_msg );
34 else {
35     plan tests => 12;
38 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
40 my $builder = Koha::SearchEngine::Elasticsearch::QueryBuilder->new( { index => 'mydb' } );
42 use_ok('Koha::SearchEngine::Elasticsearch::Search');
44 ok(
45     my $searcher = Koha::SearchEngine::Elasticsearch::Search->new(
46         { 'nodes' => ['localhost:9200'], 'index' => 'mydb' }
47     ),
48     'Creating a Koha::SearchEngine::Elasticsearch::Search object'
51 is( $searcher->index, 'mydb', 'Testing basic accessor' );
53 ok( my $query = $builder->build_query('easy'), 'Build a search query');
55 SKIP: {
57     eval { $builder->get_elasticsearch_params; };
59     skip 'ElasticSeatch configuration not available', 6
60         if $@;
62     ok( my $results = $searcher->search( $query) , 'Do a search ' );
64     ok( my $marc = $searcher->json2marc( $results->first ), 'Convert JSON to MARC');
66     is (my $count = $searcher->count( $query ), 0 , 'Get a count of the results, without returning results ');
68     ok ($results = $searcher->search_compat( $query ), 'Test search_compat' );
70     ok (($results,$count) = $searcher->search_auth_compat ( $query ), 'Test search_auth_compat' );
72     is ( $count = $searcher->count_auth_use($searcher,1), 0, 'Testing count_auth_use');
76 subtest 'json2marc' => sub {
77     plan tests => 4;
78     my $leader = '00626nam a2200193   4500';
79     my $_001 = 42;
80     my $_010a = '123456789';
81     my $_010d = 145;
82     my $_200a = 'a title';
83     my $json = [ # It's not a JSON, see the POD of json2marc
84         [ 'LDR', undef, undef, '_', $leader ],
85         [ '001', undef, undef, '_', $_001 ],
86         [ '010', ' ', ' ', 'a', $_010a, 'd', $_010d ],
87         [ '200', '1', ' ', 'a', $_200a, ], # Yes UNIMARC but we don't mind here
88     ];
90     my $marc = $searcher->json2marc( $json );
91     is( $marc->leader, $leader, );
92     is( $marc->field('001')->data, $_001, );
93     is( $marc->subfield('010', 'a'), $_010a, );
94     is( $marc->subfield('200', 'a'), $_200a, );
98 subtest 'build_query tests' => sub {
99     plan tests => 6;
101     t::lib::Mocks::mock_preference('DisplayLibraryFacets','both');
102     my $query = $builder->build_query();
103     ok( defined $query->{aggregations}{homebranch},
104         'homebranch added to facets if DisplayLibraryFacets=both' );
105     ok( defined $query->{aggregations}{holdingbranch},
106         'holdingbranch added to facets if DisplayLibraryFacets=both' );
107     t::lib::Mocks::mock_preference('DisplayLibraryFacets','holding');
108     $query = $builder->build_query();
109     ok( !defined $query->{aggregations}{homebranch},
110         'homebranch not added to facets if DisplayLibraryFacets=holding' );
111     ok( defined $query->{aggregations}{holdingbranch},
112         'holdingbranch added to facets if DisplayLibraryFacets=holding' );
113     t::lib::Mocks::mock_preference('DisplayLibraryFacets','home');
114     $query = $builder->build_query();
115     ok( defined $query->{aggregations}{homebranch},
116         'homebranch added to facets if DisplayLibraryFacets=home' );
117     ok( !defined $query->{aggregations}{holdingbranch},
118         'holdingbranch not added to facets if DisplayLibraryFacets=home' );