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