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>
20 use Test::More tests => 2;
26 my $schema = Koha::Database->schema;
28 use_ok('Koha::SearchEngine::Elasticsearch');
30 subtest 'get_fixer_rules() tests' => sub {
34 $schema->storage->txn_begin;
36 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
40 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
41 $se->mock( '_foreach_mapping', sub {
42 my ($self, $sub ) = @_;
44 foreach my $map ( @mappings ) {
57 my $see = Koha::SearchEngine::Elasticsearch->new({ index => 'biblios' });
66 marc_type => 'marc21',
75 marc_type => 'marc21',
80 $see->get_elasticsearch_mappings(); #sort_fields will call this and use the actual db values unless we call it first
81 my $result = $see->get_fixer_rules();
82 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', )});
83 is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet.$append', )});
84 is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
85 is( $result->[3], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', )});
86 is( $result->[4], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', )});
87 is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet.$append', )});
88 is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
89 is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', )});
90 is( $result->[8], q{move_field(_id,es_id)});
92 $mappings[0]->{type} = 'boolean';
93 $mappings[1]->{type} = 'boolean';
94 $result = $see->get_fixer_rules();
95 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', )});
96 is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet.$append', )});
97 is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
98 is( $result->[3], q{unless exists('} . $mappings[0]->{name} . q{') add_field('} . $mappings[0]->{name} . q{', 0) end});
99 is( $result->[4], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', )});
100 is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', )});
101 is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet.$append', )});
102 is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
103 is( $result->[8], q{unless exists('} . $mappings[1]->{name} . q{') add_field('} . $mappings[1]->{name} . q{', 0) end});
104 is( $result->[9], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', )});
105 is( $result->[10], q{move_field(_id,es_id)});
107 $mappings[0]->{type} = 'sum';
108 $mappings[1]->{type} = 'sum';
109 $result = $see->get_fixer_rules();
110 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', )});
111 is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet.$append', )});
112 is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
113 is( $result->[3], q{sum('} . $mappings[0]->{name} . q{')});
114 is( $result->[4], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', )});
115 is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', )});
116 is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet.$append', )});
117 is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
118 is( $result->[8], q{sum('} . $mappings[1]->{name} . q{')});
119 is( $result->[9], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', )});
120 is( $result->[10], q{move_field(_id,es_id)});
122 $mappings[0]->{type} = 'string';
123 $mappings[0]->{facet} = 0;
124 $mappings[1]->{type} = 'string';
125 $mappings[1]->{facet} = 0;
127 $result = $see->get_fixer_rules();
128 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', )});
129 is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
130 is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', )});
131 is( $result->[3], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', )});
132 is( $result->[4], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
133 is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', )});
134 is( $result->[6], q{move_field(_id,es_id)});
136 $mappings[0]->{suggestible} = 0;
137 $mappings[1]->{suggestible} = 0;
139 $result = $see->get_fixer_rules();
140 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', )});
141 is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort.$append', )});
142 is( $result->[2], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', )});
143 is( $result->[3], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', )});
144 is( $result->[4], q{move_field(_id,es_id)});
146 $mappings[0]->{sort} = 0;
147 $mappings[1]->{sort} = undef;
149 $see->get_elasticsearch_mappings(); #sort_fields will call this and use the actual db values unless we call it first
150 $result = $see->get_fixer_rules();
151 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', )});
152 is( $result->[1], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', )});
153 is( $result->[2], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort.$append', )});
154 is( $result->[3], q{move_field(_id,es_id)});
156 t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
158 $result = $see->get_fixer_rules();
159 is( $result->[0], q{move_field(_id,es_id)});
160 is( $result->[1], undef, q{No mapping when marc_type doesn't match marchflavour} );
162 $schema->storage->txn_rollback;