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;
25 use Koha::SearchEngine::Elasticsearch::Indexer;
28 my $schema = Koha::Database->schema;
30 use_ok('Koha::SearchEngine::Elasticsearch');
32 subtest 'get_fixer_rules() tests' => sub {
36 $schema->storage->txn_begin;
38 t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
42 my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' );
43 $se->mock( '_foreach_mapping', sub {
44 my ($self, $sub ) = @_;
46 foreach my $map ( @mappings ) {
59 my $see = Koha::SearchEngine::Elasticsearch->new({ index => 'biblios' });
68 marc_type => 'marc21',
77 marc_type => 'marc21',
82 my $marc_record = MARC::Record->new();
83 $marc_record->append_fields(
84 MARC::Field->new( '001', '1234567' ),
85 MARC::Field->new( '020', '', '', 'a' => '1234567890123' ),
86 MARC::Field->new( '100', '', '', 'a' => 'Author' ),
87 MARC::Field->new( '110', '', '', 'a' => 'Corp Author' ),
88 MARC::Field->new( '245', '', '', 'a' => 'Title' ),
90 my @records = ( $marc_record );
92 my $importer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => 'biblios' });
93 my $conv = $importer->_convert_marc_to_json( \@records )->next();
94 is( $conv->{author}[0][0], "Author", "First mapped author should be 100a");
95 is( $conv->{author}[1][0], "Corp Author", "Second mapped author should be 110a");
97 my $result = $see->get_fixer_rules();
98 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
99 is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet', -split => 1)});
100 is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
101 is( $result->[3], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort', -split => 1)});
102 is( $result->[4], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
103 is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet', -split => 1)});
104 is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
105 is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort', -split => 1)});
106 is( $result->[8], q{move_field(_id,es_id)});
108 $mappings[0]->{type} = 'boolean';
109 $mappings[1]->{type} = 'boolean';
110 $result = $see->get_fixer_rules();
111 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
112 is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet', -split => 1)});
113 is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
114 is( $result->[3], q{unless exists('} . $mappings[0]->{name} . q{') add_field('} . $mappings[0]->{name} . q{', 0) end});
115 is( $result->[4], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort', -split => 1)});
116 is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
117 is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet', -split => 1)});
118 is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
119 is( $result->[8], q{unless exists('} . $mappings[1]->{name} . q{') add_field('} . $mappings[1]->{name} . q{', 0) end});
120 is( $result->[9], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort', -split => 1)});
121 is( $result->[10], q{move_field(_id,es_id)});
123 $mappings[0]->{type} = 'sum';
124 $mappings[1]->{type} = 'sum';
125 $result = $see->get_fixer_rules();
126 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', )});
127 is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__facet', )});
128 is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
129 is( $result->[3], q{sum('} . $mappings[0]->{name} . q{')});
130 is( $result->[4], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort', )});
131 is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', )});
132 is( $result->[6], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__facet', )});
133 is( $result->[7], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
134 is( $result->[8], q{sum('} . $mappings[1]->{name} . q{')});
135 is( $result->[9], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort', )});
136 is( $result->[10], q{move_field(_id,es_id)});
138 $mappings[0]->{type} = 'string';
139 $mappings[0]->{facet} = 0;
140 $mappings[1]->{type} = 'string';
141 $mappings[1]->{facet} = 0;
143 $result = $see->get_fixer_rules();
144 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
145 is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__suggestion.input.$append')});
146 is( $result->[2], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort', -split => 1)});
147 is( $result->[3], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
148 is( $result->[4], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__suggestion.input.$append')});
149 is( $result->[5], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort', -split => 1)});
150 is( $result->[6], q{move_field(_id,es_id)});
152 $mappings[0]->{suggestible} = 0;
153 $mappings[1]->{suggestible} = 0;
155 $result = $see->get_fixer_rules();
156 is( $result->[0], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{.$append', -split => 1)});
157 is( $result->[1], q{marc_map('} . $mappings[0]->{marc_field} . q{','} . $mappings[0]->{name} . q{__sort', -split => 1)});
158 is( $result->[2], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{.$append', -split => 1)});
159 is( $result->[3], q{marc_map('} . $mappings[1]->{marc_field} . q{','} . $mappings[1]->{name} . q{__sort', -split => 1)});
160 is( $result->[4], q{move_field(_id,es_id)});
162 t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' );
164 $result = $see->get_fixer_rules();
165 is( $result->[0], q{move_field(_id,es_id)});
166 is( $result->[1], undef, q{No mapping when marc_type doesn't match marchflavour} );
168 $schema->storage->txn_rollback;