Bug 25273: Make match-heading rely on authority type configuration
[koha.git] / t / db_dependent / Koha / SearchEngine / Elasticsearch.t
1 #!/usr/bin/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 => 6;
21 use Test::Exception;
22
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
25
26 use Test::MockModule;
27
28 use MARC::Record;
29 use Try::Tiny;
30 use List::Util qw( any );
31
32 use Koha::SearchEngine::Elasticsearch;
33 use Koha::SearchEngine::Elasticsearch::Search;
34
35 my $schema = Koha::Database->new->schema;
36 $schema->storage->txn_begin;
37
38 subtest '_read_configuration() tests' => sub {
39
40     plan tests => 10;
41
42     my $configuration;
43     t::lib::Mocks::mock_config( 'elasticsearch', undef );
44
45     # 'elasticsearch' missing in configuration
46     throws_ok {
47         $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
48     }
49     'Koha::Exceptions::Config::MissingEntry',
50       'Configuration problem, exception thrown';
51     is(
52         $@->message,
53         "Missing <elasticsearch> entry in koha-conf.xml",
54         'Exception message is correct'
55     );
56
57     # 'elasticsearch' present but no 'server' entry
58     t::lib::Mocks::mock_config( 'elasticsearch', {} );
59     throws_ok {
60         $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
61     }
62     'Koha::Exceptions::Config::MissingEntry',
63       'Configuration problem, exception thrown';
64     is(
65         $@->message,
66         "Missing <elasticsearch>/<server> entry in koha-conf.xml",
67         'Exception message is correct'
68     );
69
70     # 'elasticsearch' and 'server' entries present, but no 'index_name'
71     t::lib::Mocks::mock_config( 'elasticsearch', { server => 'a_server' } );
72     throws_ok {
73         $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
74     }
75     'Koha::Exceptions::Config::MissingEntry',
76       'Configuration problem, exception thrown';
77     is(
78         $@->message,
79         "Missing <elasticsearch>/<index_name> entry in koha-conf.xml",
80         'Exception message is correct'
81     );
82
83     # Correct configuration, only one server
84     t::lib::Mocks::mock_config( 'elasticsearch',  { server => 'a_server', index_name => 'index' } );
85
86     $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
87     is( $configuration->{index_name}, 'index', 'Index configuration parsed correctly' );
88     is_deeply( $configuration->{nodes}, ['a_server'], 'Server configuration parsed correctly' );
89
90     # Correct configuration, two servers
91     my @servers = ('a_server', 'another_server');
92     t::lib::Mocks::mock_config( 'elasticsearch', { server => \@servers, index_name => 'index' } );
93
94     $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
95     is( $configuration->{index_name}, 'index', 'Index configuration parsed correctly' );
96     is_deeply( $configuration->{nodes}, \@servers , 'Server configuration parsed correctly' );
97 };
98
99 subtest 'get_elasticsearch_settings() tests' => sub {
100
101     plan tests => 1;
102
103     my $settings;
104
105     # test reading index settings
106     my $es = Koha::SearchEngine::Elasticsearch->new( {index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX} );
107     $settings = $es->get_elasticsearch_settings();
108     is( $settings->{index}{analysis}{analyzer}{analyzer_phrase}{tokenizer}, 'keyword', 'Index settings parsed correctly' );
109 };
110
111 subtest 'get_elasticsearch_mappings() tests' => sub {
112
113     plan tests => 1;
114
115     my $mappings;
116
117     # test reading mappings
118     my $es = Koha::SearchEngine::Elasticsearch->new( {index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX} );
119     $mappings = $es->get_elasticsearch_mappings();
120     is( $mappings->{data}{properties}{isbn__sort}{index}, 'false', 'Field mappings parsed correctly' );
121 };
122
123 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () tests' => sub {
124
125     plan tests => 53;
126
127     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
128     t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
129
130     my @mappings = (
131         {
132             name => 'control_number',
133             type => 'string',
134             facet => 0,
135             suggestible => 0,
136             searchable => 1,
137             sort => undef,
138             marc_type => 'marc21',
139             marc_field => '001',
140         },
141         {
142             name => 'isbn',
143             type => 'isbn',
144             facet => 0,
145             suggestible => 0,
146             searchable => 1,
147             sort => 0,
148             marc_type => 'marc21',
149             marc_field => '020a',
150         },
151         {
152             name => 'author',
153             type => 'string',
154             facet => 1,
155             suggestible => 1,
156             searchable => 1,
157             sort => undef,
158             marc_type => 'marc21',
159             marc_field => '100a',
160         },
161         {
162             name => 'author',
163             type => 'string',
164             facet => 1,
165             suggestible => 1,
166             searchable => 1,
167             sort => 1,
168             marc_type => 'marc21',
169             marc_field => '110a',
170         },
171         {
172             name => 'title',
173             type => 'string',
174             facet => 0,
175             suggestible => 1,
176             searchable => 1,
177             sort => 1,
178             marc_type => 'marc21',
179             marc_field => '245(ab)ab',
180         },
181         {
182             name => 'unimarc_title',
183             type => 'string',
184             facet => 0,
185             suggestible => 1,
186             searchable => 1,
187             sort => 1,
188             marc_type => 'unimarc',
189             marc_field => '245a',
190         },
191         {
192             name => 'title',
193             type => 'string',
194             facet => 0,
195             suggestible => undef,
196             searchable => 1,
197             sort => 0,
198             marc_type => 'marc21',
199             marc_field => '220',
200         },
201         {
202             name => 'uniform_title',
203             type => 'string',
204             facet => 0,
205             suggestible => 0,
206             searchable => 1,
207             sort => 1,
208             marc_type => 'marc21',
209             marc_field => '240a',
210         },
211         {
212             name => 'title_wildcard',
213             type => 'string',
214             facet => 0,
215             suggestible => 0,
216             searchable => 1,
217             sort => undef,
218             marc_type => 'marc21',
219             marc_field => '245',
220         },
221         {
222             name => 'sum_item_price',
223             type => 'sum',
224             facet => 0,
225             suggestible => 0,
226             searchable => 1,
227             sort => 0,
228             marc_type => 'marc21',
229             marc_field => '952g',
230         },
231         {
232             name => 'items_withdrawn_status',
233             type => 'boolean',
234             facet => 0,
235             suggestible => 0,
236             searchable => 1,
237             sort => 0,
238             marc_type => 'marc21',
239             marc_field => '9520',
240         },
241         {
242             name => 'local_classification',
243             type => 'string',
244             facet => 0,
245             suggestible => 0,
246             searchable => 1,
247             sort => 1,
248             marc_type => 'marc21',
249             marc_field => '952o',
250         },
251         {
252             name => 'type_of_record',
253             type => 'string',
254             facet => 0,
255             suggestible => 0,
256             searchable => 1,
257             sort => 0,
258             marc_type => 'marc21',
259             marc_field => 'leader_/6',
260         },
261         {
262             name => 'type_of_record_and_bib_level',
263             type => 'string',
264             facet => 0,
265             suggestible => 0,
266             searchable => 1,
267             sort => 0,
268             marc_type => 'marc21',
269             marc_field => 'leader_/6-7',
270         },
271         {
272             name => 'ff7-00',
273             type => 'string',
274             facet => 0,
275             suggestible => 0,
276             searchable => 1,
277             sort => 0,
278             marc_type => 'marc21',
279             marc_field => '007_/0',
280         },
281         {
282             name => 'issues',
283             type => 'sum',
284             facet => 0,
285             suggestible => 0,
286             searchable => 1,
287             sort => 1,
288             marc_type => 'marc21',
289             marc_field => '952l',
290         },
291     );
292
293     my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
294     $se->mock('_foreach_mapping', sub {
295         my ($self, $sub) = @_;
296
297         foreach my $map (@mappings) {
298             $sub->(
299                 $map->{name},
300                 $map->{type},
301                 $map->{facet},
302                 $map->{suggestible},
303                 $map->{sort},
304                 $map->{searchable},
305                 $map->{marc_type},
306                 $map->{marc_field}
307             );
308         }
309     });
310
311     my $see = Koha::SearchEngine::Elasticsearch::Search->new({ index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX });
312
313     my $callno = 'ABC123';
314     my $callno2 = 'ABC456';
315     my $long_callno = '1234567890' x 30;
316
317     my $marc_record_1 = MARC::Record->new();
318     $marc_record_1->leader('     cam  22      a 4500');
319     $marc_record_1->append_fields(
320         MARC::Field->new('001', '123'),
321         MARC::Field->new('007', 'ku'),
322         MARC::Field->new('020', '', '', a => '1-56619-909-3'),
323         MARC::Field->new('100', '', '', a => 'Author 1'),
324         MARC::Field->new('110', '', '', a => 'Corp Author'),
325         MARC::Field->new('210', '', '', a => 'Title 1'),
326         MARC::Field->new('240', '', '4', a => 'The uniform title with nonfiling indicator'),
327         MARC::Field->new('245', '', '', a => 'Title:', b => 'first record'),
328         MARC::Field->new('999', '', '', c => '1234567'),
329         # '  ' for testing trimming of white space in boolean value callback:
330         MARC::Field->new('952', '', '', 0 => '  ', g => '123.30', o => $callno, l => 3),
331         MARC::Field->new('952', '', '', 0 => 0, g => '127.20', o => $callno2, l => 2),
332         MARC::Field->new('952', '', '', 0 => 1, g => '0.00', o => $long_callno, l => 1),
333     );
334     my $marc_record_2 = MARC::Record->new();
335     $marc_record_2->leader('     cam  22      a 4500');
336     $marc_record_2->append_fields(
337         MARC::Field->new('100', '', '', a => 'Author 2'),
338         # MARC::Field->new('210', '', '', a => 'Title 2'),
339         # MARC::Field->new('245', '', '', a => 'Title: second record'),
340         MARC::Field->new('999', '', '', c => '1234568'),
341         MARC::Field->new('952', '', '', 0 => 1, g => 'string where should be numeric', o => $long_callno),
342     );
343     my $records = [ $marc_record_1, $marc_record_2 ];
344
345     $see->get_elasticsearch_mappings(); #sort_fields will call this and use the actual db values unless we call it first
346
347     my $docs = $see->marc_records_to_documents($records);
348
349     # First record:
350     is(scalar @{$docs}, 2, 'Two records converted to documents');
351
352     is_deeply($docs->[0]->{control_number}, ['123'], 'First record control number should be set correctly');
353
354     is_deeply($docs->[0]->{'ff7-00'}, ['k'], 'First record ff7-00 should be set correctly');
355
356     is(scalar @{$docs->[0]->{author}}, 2, 'First document author field should contain two values');
357     is_deeply($docs->[0]->{author}, ['Author 1', 'Corp Author'], 'First document author field should be set correctly');
358
359     is(scalar @{$docs->[0]->{author__sort}}, 1, 'First document author__sort field should have a single value');
360     is_deeply($docs->[0]->{author__sort}, ['Author 1 Corp Author'], 'First document author__sort field should be set correctly');
361
362     is(scalar @{$docs->[0]->{title__sort}}, 1, 'First document title__sort field should have a single');
363     is_deeply($docs->[0]->{title__sort}, ['Title: first record Title: first record'], 'First document title__sort field should be set correctly');
364
365     is($docs->[0]->{issues}, 6, 'Issues field should be sum of the issues for each item');
366     is($docs->[0]->{issues__sort}, 6, 'Issues sort field should also be a sum of the issues');
367
368     is(scalar @{$docs->[0]->{title_wildcard}}, 2, 'First document title_wildcard field should have two values');
369     is_deeply($docs->[0]->{title_wildcard}, ['Title:', 'first record'], 'First document title_wildcard field should be set correctly');
370
371
372     is(scalar @{$docs->[0]->{author__suggestion}}, 2, 'First document author__suggestion field should contain two values');
373     is_deeply(
374         $docs->[0]->{author__suggestion},
375         [
376             {
377                 'input' => 'Author 1'
378             },
379             {
380                 'input' => 'Corp Author'
381             }
382         ],
383         'First document author__suggestion field should be set correctly'
384     );
385
386     is(scalar @{$docs->[0]->{title__suggestion}}, 3, 'First document title__suggestion field should contain three values');
387     is_deeply(
388         $docs->[0]->{title__suggestion},
389         [
390             { 'input' => 'Title:' },
391             { 'input' => 'first record' },
392             { 'input' => 'Title: first record' }
393         ],
394         'First document title__suggestion field should be set correctly'
395     );
396
397     ok(!(defined $docs->[0]->{title__facet}), 'First document should have no title__facet field');
398
399     is(scalar @{$docs->[0]->{author__facet}}, 2, 'First document author__facet field should have two values');
400     is_deeply(
401         $docs->[0]->{author__facet},
402         ['Author 1', 'Corp Author'],
403         'First document author__facet field should be set correctly'
404     );
405
406     is(scalar @{$docs->[0]->{items_withdrawn_status}}, 2, 'First document items_withdrawn_status field should have two values');
407     is_deeply(
408         $docs->[0]->{items_withdrawn_status},
409         ['false', 'true'],
410         'First document items_withdrawn_status field should be set correctly'
411     );
412
413     is(
414         $docs->[0]->{sum_item_price},
415         '250.5',
416         'First document sum_item_price field should be set correctly'
417     );
418
419     ok(defined $docs->[0]->{marc_data}, 'First document marc_data field should be set');
420     ok(defined $docs->[0]->{marc_format}, 'First document marc_format field should be set');
421     is($docs->[0]->{marc_format}, 'base64ISO2709', 'First document marc_format should be set correctly');
422
423     my $decoded_marc_record = $see->decode_record_from_result($docs->[0]);
424
425     ok($decoded_marc_record->isa('MARC::Record'), "base64ISO2709 record successfully decoded from result");
426     is($decoded_marc_record->as_usmarc(), $marc_record_1->as_usmarc(), "Decoded base64ISO2709 record has same data as original record");
427
428     is(scalar @{$docs->[0]->{type_of_record}}, 1, 'First document type_of_record field should have one value');
429     is_deeply(
430         $docs->[0]->{type_of_record},
431         ['a'],
432         'First document type_of_record field should be set correctly'
433     );
434
435     is(scalar @{$docs->[0]->{type_of_record_and_bib_level}}, 1, 'First document type_of_record_and_bib_level field should have one value');
436     is_deeply(
437         $docs->[0]->{type_of_record_and_bib_level},
438         ['am'],
439         'First document type_of_record_and_bib_level field should be set correctly'
440     );
441
442     is(scalar @{$docs->[0]->{isbn}}, 4, 'First document isbn field should contain four values');
443     is_deeply($docs->[0]->{isbn}, ['978-1-56619-909-4', '9781566199094', '1-56619-909-3', '1566199093'], 'First document isbn field should be set correctly');
444
445     is_deeply(
446         $docs->[0]->{'local_classification'},
447         [$callno, $callno2, $long_callno],
448         'First document local_classification field should be set correctly'
449     );
450
451     # Nonfiling characters for sort fields
452     is_deeply(
453         $docs->[0]->{uniform_title},
454         ['The uniform title with nonfiling indicator'],
455         'First document uniform_title field should contain the title verbatim'
456     );
457     is_deeply(
458         $docs->[0]->{uniform_title__sort},
459         ['uniform title with nonfiling indicator'],
460         'First document uniform_title__sort field should contain the title with the first four initial characters removed'
461     );
462
463     # Second record:
464
465     is(scalar @{$docs->[1]->{author}}, 1, 'Second document author field should contain one value');
466     is_deeply($docs->[1]->{author}, ['Author 2'], 'Second document author field should be set correctly');
467
468     is(scalar @{$docs->[1]->{items_withdrawn_status}}, 1, 'Second document items_withdrawn_status field should have one value');
469     is_deeply(
470         $docs->[1]->{items_withdrawn_status},
471         ['true'],
472         'Second document items_withdrawn_status field should be set correctly'
473     );
474
475     is(
476         $docs->[1]->{sum_item_price},
477         0,
478         'Second document sum_item_price field should be set correctly'
479     );
480
481     is_deeply(
482         $docs->[1]->{local_classification__sort},
483         [substr($long_callno, 0, 255)],
484         'Second document local_classification__sort field should be set correctly'
485     );
486
487     # Mappings marc_type:
488
489     ok(!(defined $docs->[0]->{unimarc_title}), "No mapping when marc_type doesn't match marc flavour");
490
491     # Marc serialization format fallback for records exceeding ISO2709 max record size
492
493     my $large_marc_record = MARC::Record->new();
494     $large_marc_record->leader('     cam  22      a 4500');
495
496     $large_marc_record->append_fields(
497         MARC::Field->new('100', '', '', a => 'Author 1'),
498         MARC::Field->new('110', '', '', a => 'Corp Author'),
499         MARC::Field->new('210', '', '', a => 'Title 1'),
500         MARC::Field->new('245', '', '', a => 'Title:', b => 'large record'),
501         MARC::Field->new('999', '', '', c => '1234567'),
502     );
503
504     my $item_field = MARC::Field->new('952', '', '', o => '123456789123456789123456789', p => '123456789', z => 'test');
505     my $items_count = 1638;
506     while(--$items_count) {
507         $large_marc_record->append_fields($item_field);
508     }
509
510     $docs = $see->marc_records_to_documents([$large_marc_record]);
511
512     is($docs->[0]->{marc_format}, 'MARCXML', 'For record exceeding max record size marc_format should be set correctly');
513
514     $decoded_marc_record = $see->decode_record_from_result($docs->[0]);
515
516     ok($decoded_marc_record->isa('MARC::Record'), "MARCXML record successfully decoded from result");
517     is($decoded_marc_record->as_xml_record(), $large_marc_record->as_xml_record(), "Decoded MARCXML record has same data as original record");
518
519     push @mappings, {
520         name => 'title',
521         type => 'string',
522         facet => 0,
523         suggestible => 1,
524         sort => 1,
525         marc_type => 'marc21',
526         marc_field => '245((ab)ab',
527     };
528
529     my $exception = try {
530         $see->marc_records_to_documents($records);
531     }
532     catch {
533         return $_;
534     };
535
536     ok(defined $exception, "Exception has been thrown when processing mapping with unmatched opening parenthesis");
537     ok($exception->isa("Koha::Exceptions::Elasticsearch::MARCFieldExprParseError"), "Exception is of correct class");
538     ok($exception->message =~ /Unmatched opening parenthesis/, "Exception has the correct message");
539
540     pop @mappings;
541     push @mappings, {
542         name => 'title',
543         type => 'string',
544         facet => 0,
545         suggestible => 1,
546         sort => 1,
547         marc_type => 'marc21',
548         marc_field => '245(ab))ab',
549     };
550
551     $exception = try {
552         $see->marc_records_to_documents($records);
553     }
554     catch {
555         return $_;
556     };
557
558     ok(defined $exception, "Exception has been thrown when processing mapping with unmatched closing parenthesis");
559     ok($exception->isa("Koha::Exceptions::Elasticsearch::MARCFieldExprParseError"), "Exception is of correct class");
560     ok($exception->message =~ /Unmatched closing parenthesis/, "Exception has the correct message");
561 };
562
563 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents_array () tests' => sub {
564
565     plan tests => 5;
566
567     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
568     t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ARRAY');
569
570     my @mappings = (
571         {
572             name => 'control_number',
573             type => 'string',
574             facet => 0,
575             suggestible => 0,
576             sort => undef,
577             searchable => 1,
578             marc_type => 'marc21',
579             marc_field => '001',
580         }
581     );
582
583     my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
584     $se->mock('_foreach_mapping', sub {
585         my ($self, $sub) = @_;
586
587         foreach my $map (@mappings) {
588             $sub->(
589                 $map->{name},
590                 $map->{type},
591                 $map->{facet},
592                 $map->{suggestible},
593                 $map->{sort},
594                 $map->{searchable},
595                 $map->{marc_type},
596                 $map->{marc_field}
597             );
598         }
599     });
600
601     my $see = Koha::SearchEngine::Elasticsearch::Search->new({ index => $Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX });
602
603     my $marc_record_1 = MARC::Record->new();
604     $marc_record_1->leader('     cam  22      a 4500');
605     $marc_record_1->append_fields(
606         MARC::Field->new('001', '123'),
607         MARC::Field->new('020', '', '', a => '1-56619-909-3'),
608         MARC::Field->new('100', '', '', a => 'Author 1'),
609         MARC::Field->new('110', '', '', a => 'Corp Author'),
610         MARC::Field->new('210', '', '', a => 'Title 1'),
611         MARC::Field->new('245', '', '', a => 'Title:', b => 'first record'),
612         MARC::Field->new('999', '', '', c => '1234567'),
613     );
614     my $marc_record_2 = MARC::Record->new();
615     $marc_record_2->leader('     cam  22      a 4500');
616     $marc_record_2->append_fields(
617         MARC::Field->new('100', '', '', a => 'Author 2'),
618         # MARC::Field->new('210', '', '', a => 'Title 2'),
619         # MARC::Field->new('245', '', '', a => 'Title: second record'),
620         MARC::Field->new('999', '', '', c => '1234568'),
621         MARC::Field->new('952', '', '', 0 => 1, g => 'string where should be numeric'),
622     );
623     my $records = [ $marc_record_1, $marc_record_2 ];
624
625     $see->get_elasticsearch_mappings(); #sort_fields will call this and use the actual db values unless we call it first
626
627     my $docs = $see->marc_records_to_documents($records);
628
629     # First record:
630     is(scalar @{$docs}, 2, 'Two records converted to documents');
631
632     is_deeply($docs->[0]->{control_number}, ['123'], 'First record control number should be set correctly');
633
634     is($docs->[0]->{marc_format}, 'ARRAY', 'First document marc_format should be set correctly');
635
636     my $decoded_marc_record = $see->decode_record_from_result($docs->[0]);
637
638     ok($decoded_marc_record->isa('MARC::Record'), "ARRAY record successfully decoded from result");
639     is($decoded_marc_record->as_usmarc(), $marc_record_1->as_usmarc(), "Decoded ARRAY record has same data as original record");
640 };
641
642 subtest 'Koha::SearchEngine::Elasticsearch::marc_records_to_documents () authority tests' => sub {
643
644     plan tests => 2;
645
646     t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
647     t::lib::Mocks::mock_preference('ElasticsearchMARCFormat', 'ISO2709');
648
649     my $builder = t::lib::TestBuilder->new;
650     my $auth_type = $builder->build_object({ class => 'Koha::Authority::Types', value =>{
651             auth_tag_to_report => '150'
652         }
653     });
654
655     my @mappings = (
656         {
657             name => 'match',
658             type => 'string',
659             facet => 0,
660             suggestible => 0,
661             searchable => 1,
662             sort => 0,
663             marc_type => 'marc21',
664             marc_field => '150(ae)',
665         }
666     );
667
668     my $se = Test::MockModule->new('Koha::SearchEngine::Elasticsearch');
669     $se->mock('_foreach_mapping', sub {
670         my ($self, $sub) = @_;
671
672         foreach my $map (@mappings) {
673             $sub->(
674                 $map->{name},
675                 $map->{type},
676                 $map->{facet},
677                 $map->{suggestible},
678                 $map->{sort},
679                 $map->{searchable},
680                 $map->{marc_type},
681                 $map->{marc_field}
682             );
683         }
684     });
685
686     my $see = Koha::SearchEngine::Elasticsearch::Search->new({ index => $Koha::SearchEngine::Elasticsearch::AUTHORITIES_INDEX });
687     my $marc_record_1 = MARC::Record->new();
688     $marc_record_1->append_fields(
689         MARC::Field->new('001', '123'),
690         MARC::Field->new('007', 'ku'),
691         MARC::Field->new('020', '', '', a => '1-56619-909-3'),
692         MARC::Field->new('150', '', '', a => 'Subject', v => 'Genresubdiv', x => 'Generalsubdiv', z => 'Geosubdiv'),
693     );
694     my $marc_record_2 = MARC::Record->new();
695     $marc_record_2->append_fields(
696         MARC::Field->new('150', '', '', a => 'Subject', v => 'Genresubdiv', z => 'Geosubdiv', x => 'Generalsubdiv', e => 'wrongsubdiv' ),
697     );
698     my $records = [ $marc_record_1, $marc_record_2 ];
699
700     $see->get_elasticsearch_mappings(); #sort_fields will call this and use the actual db values unless we call it first
701
702     my $docs = $see->marc_records_to_documents($records);
703
704     ok(
705         any { $_ eq "Subject formsubdiv Genresubdiv generalsubdiv Generalsubdiv geographicsubdiv Geosubdiv" }
706         @{$docs->[0]->{'match-heading'}},
707         "First record match-heading should contain the correctly formatted heading"
708     );
709     ok(
710         any { $_ eq "Subject formsubdiv Genresubdiv geographicsubdiv Geosubdiv generalsubdiv Generalsubdiv" }
711         @{$docs->[1]->{'match-heading'}},
712         "Second record match-heading should contain the correctly formatted heading without wrong subfield"
713     );
714 };
715
716 $schema->storage->txn_rollback;