Bug 23204: Move code in a unit tested sub

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Alex Arnaud 2019-08-06 07:45:39 +00:00 committed by Martin Renvoize
parent e1f8522391
commit 85f3a3a302
Signed by: martin.renvoize
GPG key ID: 422B469130441A0F
3 changed files with 164 additions and 30 deletions

View file

@ -244,6 +244,51 @@ sub get_elasticsearch_mappings {
return $all_mappings{$self->index};
}
=head2 raw_elasticsearch_mappings
Return elasticsearch mapping as it is in database.
marc_type: marc21|unimarc|normarc
$raw_mappings = raw_elasticsearch_mappings( $marc_type )
=cut
sub raw_elasticsearch_mappings {
my ( $marc_type ) = @_;
my $schema = Koha::Database->new()->schema();
my $search_fields = Koha::SearchFields->search();
my $mappings = {};
while ( my $search_field = $search_fields->next ) {
my $marc_to_fields = $schema->resultset('SearchMarcToField')->search( { search_field_id => $search_field->id } );
while ( my $marc_to_field = $marc_to_fields->next ) {
my $marc_map = Koha::SearchMarcMaps->find( $marc_to_field->search_marc_map_id );
next if $marc_type && $marc_map->marc_type ne $marc_type;
$mappings->{ $marc_map->index_name }{ $search_field->name }{label} = $search_field->label;
$mappings->{ $marc_map->index_name }{ $search_field->name }{type} = $search_field->type;
$mappings->{ $marc_map->index_name }{ $search_field->name }{facet_order} = $search_field->facet_order;
push (@{ $mappings->{ $marc_map->index_name }{ $search_field->name }{mappings} },
{
facet => $marc_to_field->facet || '',
marc_type => $marc_map->marc_type,
marc_field => $marc_map->marc_field,
sort => $marc_to_field->sort,
suggestible => $marc_to_field->suggestible || ''
});
}
}
return $mappings;
}
=head2 _get_elasticsearch_field_config
Get the Elasticsearch field config for the given purpose and data type.

View file

@ -56,6 +56,7 @@ use Modern::Perl;
use Koha::Database;
use Koha::SearchFields;
use Koha::SearchMarcMaps;
use Koha::SearchEngine::Elasticsearch;
use YAML;
use Getopt::Long;
@ -76,34 +77,6 @@ if ( $type && $type !~ /^(marc21|unimarc|normarc)$/ ) {
pod2usage(1);
}
my $schema = Koha::Database->new()->schema();
my $mappings = Koha::SearchEngine::Elasticsearch::raw_elasticsearch_mappings( $type );
my $search_fields = Koha::SearchFields->search();
my $yaml = {};
while ( my $search_field = $search_fields->next ) {
my $marc_to_fields = $schema->resultset('SearchMarcToField')->search( { search_field_id => $search_field->id } );
while ( my $marc_to_field = $marc_to_fields->next ) {
my $marc_map = Koha::SearchMarcMaps->find( $marc_to_field->search_marc_map_id );
next if $type && $marc_map->marc_type ne $type;
$yaml->{ $marc_map->index_name }{ $search_field->name }{label} = $search_field->label;
$yaml->{ $marc_map->index_name }{ $search_field->name }{type} = $search_field->type;
$yaml->{ $marc_map->index_name }{ $search_field->name }{facet_order} = $search_field->facet_order;
push (@{ $yaml->{ $marc_map->index_name }{ $search_field->name }{mappings} },
{
facet => $marc_to_field->facet || '',
marc_type => $marc_map->marc_type,
marc_field => $marc_map->marc_field,
sort => $marc_to_field->sort,
suggestible => $marc_to_field->suggestible || ''
});
}
}
print Dump($yaml);
print Dump($mappings);

View file

@ -0,0 +1,116 @@
#!/usr/bin/perl
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use Test::More tests => 16;
use Koha::Database;
use Koha::SearchFields;
use Koha::SearchMarcMaps;
use_ok('Koha::SearchEngine::Elasticsearch');
my $schema = Koha::Database->new->schema;
$schema->storage->txn_begin;
Koha::SearchFields->search->delete;
Koha::SearchMarcMaps->search->delete;
$schema->resultset('SearchMarcToField')->search->delete;
my $search_field = Koha::SearchFields->find_or_create(
{
name => 'title',
label => 'Title',
type => 'string',
weight => 17
},
{ key => 'name' } );
my $marc_field = Koha::SearchMarcMaps->find_or_create(
{
index_name => 'biblios',
marc_type => 'marc21',
marc_field => '247'
} );
$search_field->add_to_search_marc_maps($marc_field,
{
facet => 0,
suggestible => 0,
sort => undef
} );
$marc_field = Koha::SearchMarcMaps->find_or_create(
{
index_name => 'biblios',
marc_type => 'marc21',
marc_field => '212'
} );
$search_field->add_to_search_marc_maps($marc_field,
{
facet => 0,
suggestible => 0,
sort => undef
} );
$marc_field = Koha::SearchMarcMaps->find_or_create(
{
index_name => 'biblios',
marc_type => 'unimarc',
marc_field => '200a'
} );
$search_field->add_to_search_marc_maps($marc_field,
{
facet => 0,
suggestible => 1,
sort => undef
} );
my $mappings = Koha::SearchEngine::Elasticsearch::raw_elasticsearch_mappings();
is( $mappings->{biblios}{title}{type}, 'string', 'Title is of type string');
is( $mappings->{biblios}{title}{label}, 'Title', 'title has label Title');
is( $mappings->{biblios}{title}{facet_order}, undef, 'Facet order is undef');
is(scalar(@{ $mappings->{biblios}{title}{mappings} }), 3, 'Title has 3 mappings');
my $f247_map = $mappings->{biblios}{title}{mappings}[0];
is( $f247_map->{marc_field}, 247, 'First mapping is on field 247');
is( $f247_map->{marc_type}, 'marc21', 'First mapping is for marc21');
is( $f247_map->{facet}, '', 'First mapping facet is empty');
is( $f247_map->{suggestible}, '', 'First mapping is not suggestible');
is( $f247_map->{sort}, undef, 'First mapping is not sortable');
my $f212_map = $mappings->{biblios}{title}{mappings}[1];
is( $f212_map->{marc_field}, 212, 'Second mapping is on field 247');
is( $f212_map->{marc_type}, 'marc21', 'Second mapping is for marc21');
is( $f212_map->{facet}, '', 'Second mapping facet is empty');
is( $f212_map->{suggestible}, '', 'Second mapping is not suggestible');
is( $f212_map->{sort}, undef, 'Second mapping is not sortable');
$mappings = Koha::SearchEngine::Elasticsearch::raw_elasticsearch_mappings('unimarc');
is(scalar(@{ $mappings->{biblios}{title}{mappings} }), 1, 'Title has 1 mappings');
$schema->storage->txn_rollback;