82dc5537f6bb395c328a6fa2a33275c1e7cf6a23
[srvgit] / t / db_dependent / Koha / SearchEngine / Elasticsearch / ExportConfig.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 => 18;
21
22 use Koha::Database;
23 use Koha::SearchFields;
24 use Koha::SearchMarcMaps;
25
26 use_ok('Koha::SearchEngine::Elasticsearch');
27
28 my $schema = Koha::Database->new->schema;
29
30 $schema->storage->txn_begin;
31
32 Koha::SearchFields->search->delete;
33 Koha::SearchMarcMaps->search->delete;
34 $schema->resultset('SearchMarcToField')->search->delete;
35
36
37
38 my $search_field = Koha::SearchFields->find_or_create(
39     {
40         name    => 'title',
41         label   => 'Title',
42         type    => 'string',
43         weight  => 17,
44         staff_client => 0,
45         opac         => 1,
46     },
47     { key => 'name' } );
48
49 my $marc_field = Koha::SearchMarcMaps->find_or_create(
50     {
51         index_name => 'biblios',
52         marc_type => 'marc21',
53         marc_field => '247'
54     } );
55
56 $search_field->add_to_search_marc_maps($marc_field,
57     {
58         facet => 0,
59         suggestible => 0,
60         sort => undef
61     } );
62
63 $marc_field = Koha::SearchMarcMaps->find_or_create(
64     {
65         index_name => 'biblios',
66         marc_type => 'marc21',
67         marc_field => '212'
68     } );
69
70 $search_field->add_to_search_marc_maps($marc_field,
71     {
72         facet => 0,
73         suggestible => 0,
74         sort => undef
75     } );
76
77 $marc_field = Koha::SearchMarcMaps->find_or_create(
78     {
79         index_name => 'biblios',
80         marc_type => 'unimarc',
81         marc_field => '200a'
82     } );
83
84 $search_field->add_to_search_marc_maps($marc_field,
85     {
86         facet => 0,
87         suggestible => 1,
88         sort => undef
89     } );
90
91 my $mappings = Koha::SearchEngine::Elasticsearch::raw_elasticsearch_mappings();
92
93 is( $mappings->{biblios}{title}{type}, 'string', 'Title is of type string');
94 is( $mappings->{biblios}{title}{label}, 'Title', 'title has label Title');
95 is( $mappings->{biblios}{title}{facet_order}, undef, 'Facet order is undef');
96 is( $mappings->{biblios}{title}{opac}, 1, 'title is opac searchable');
97 is( $mappings->{biblios}{title}{staff_client}, 0, 'title is not staff searchable');
98
99 is(scalar(@{ $mappings->{biblios}{title}{mappings} }), 3, 'Title has 3 mappings');
100
101 my $f212_map = $mappings->{biblios}{title}{mappings}[0];
102 is( $f212_map->{marc_field}, 212, 'First mapping is on field 212');
103 is( $f212_map->{marc_type}, 'marc21', 'First mapping is for marc21');
104 is( $f212_map->{facet}, '', 'First mapping facet is empty');
105 is( $f212_map->{suggestible}, '', 'First mapping is not suggestible');
106 is( $f212_map->{sort}, undef, 'First mapping is not sortable');
107
108 my $f247_map = $mappings->{biblios}{title}{mappings}[1];
109 is( $f247_map->{marc_field}, 247, 'Second mapping is on field 247');
110 is( $f247_map->{marc_type}, 'marc21', 'Second mapping is for marc21');
111 is( $f247_map->{facet}, '', 'Second mapping facet is empty');
112 is( $f247_map->{suggestible}, '', 'Second mapping is not suggestible');
113 is( $f247_map->{sort}, undef, 'Second mapping is not sortable');
114
115 $mappings = Koha::SearchEngine::Elasticsearch::raw_elasticsearch_mappings('unimarc');
116
117 is(scalar(@{ $mappings->{biblios}{title}{mappings} }), 1, 'Title has 1 mappings');
118
119 $schema->storage->txn_rollback;