@@ -, +, @@ - Run: $ sudo koha-shell kohadev k$ de kohaclone k$ prove t/db_dependent/Koha_Elasticsearch.t --- t/db_dependent/Koha_Elasticsearch.t | 104 ++++++++++++++++++++++++++++++++++-- 1 file changed, 101 insertions(+), 3 deletions(-) --- a/t/db_dependent/Koha_Elasticsearch.t +++ a/t/db_dependent/Koha_Elasticsearch.t @@ -15,9 +15,107 @@ # You should have received a copy of the GNU General Public License # along with Koha; if not, see -use strict; -use warnings; +use Modern::Perl; -use Test::More tests => 1; # last test to print +use Test::More tests => 2; +use Test::MockModule; + +use t::lib::Mocks; + +my $schema = Koha::Database->schema; use_ok('Koha::SearchEngine::Elasticsearch'); + +subtest 'get_fixer_rules() tests' => sub { + + plan tests => 26; + + $schema->storage->txn_begin; + + t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); + + my $name; + my $type; + my $facet; + my $suggestible; + my $sort; + my $marc_type; + my $marc_field; + + my $se = Test::MockModule->new( 'Koha::SearchEngine::Elasticsearch' ); + $se->mock( '_foreach_mapping', sub { + my ($self, $sub ) = @_; + + return $sub->( + $name, + $type, + $facet, + $suggestible, + $sort, + $marc_type, + $marc_field + ); + }); + + my $see = Koha::SearchEngine::Elasticsearch->new({ index => 'biblios' }); + + $name = 'author'; + $type = 'string'; + $facet = 1; + $suggestible = 1; + $sort = '~'; + $marc_type = 'marc21'; + $marc_field = '100a'; + + my $result = $see->get_fixer_rules(); + is( $result->[0], q{marc_map('} . $marc_field . q{','} . $name . q{.$append' , -split => 1)}); + is( $result->[1], q{marc_map('} . $marc_field . q{','} . $name . q{__facet' , -split => 1)}); + is( $result->[2], q{marc_map('} . $marc_field . q{','} . $name . q{__suggestion.input.$append')}); + is( $result->[3], q{marc_map('} . $marc_field . q{','} . $name . q{__sort' , -split => 1)}); + is( $result->[4], q{move_field(_id,es_id)}); + + $type = 'boolean'; + $result = $see->get_fixer_rules(); + is( $result->[0], q{marc_map('} . $marc_field . q{','} . $name . q{.$append' , -split => 1)}); + is( $result->[1], q{marc_map('} . $marc_field . q{','} . $name . q{__facet' , -split => 1)}); + is( $result->[2], q{marc_map('} . $marc_field . q{','} . $name . q{__suggestion.input.$append')}); + is( $result->[3], q{unless exists('} . $name . q{') add_field('} . $name . q{', 0) end}); + is( $result->[4], q{marc_map('} . $marc_field . q{','} . $name . q{__sort' , -split => 1)}); + is( $result->[5], q{move_field(_id,es_id)}); + + $type = 'sum'; + $result = $see->get_fixer_rules(); + is( $result->[0], q{marc_map('} . $marc_field . q{','} . $name . q{.$append' )}); + is( $result->[1], q{marc_map('} . $marc_field . q{','} . $name . q{__facet' )}); + is( $result->[2], q{marc_map('} . $marc_field . q{','} . $name . q{__suggestion.input.$append')}); + is( $result->[3], q{sum('} . $name . q{')}); + is( $result->[4], q{marc_map('} . $marc_field . q{','} . $name . q{__sort' )}); + is( $result->[5], q{move_field(_id,es_id)}); + + $type = 'string'; + $facet = 0; + + $result = $see->get_fixer_rules(); + is( $result->[0], q{marc_map('} . $marc_field . q{','} . $name . q{.$append' , -split => 1)}); + is( $result->[1], q{marc_map('} . $marc_field . q{','} . $name . q{__suggestion.input.$append')}); + is( $result->[2], q{marc_map('} . $marc_field . q{','} . $name . q{__sort' , -split => 1)}); + is( $result->[3], q{move_field(_id,es_id)}); + + $suggestible = 0; + + $result = $see->get_fixer_rules(); + is( $result->[0], q{marc_map('} . $marc_field . q{','} . $name . q{.$append' , -split => 1)}); + is( $result->[1], q{marc_map('} . $marc_field . q{','} . $name . q{__sort' , -split => 1)}); + is( $result->[2], q{move_field(_id,es_id)}); + + t::lib::Mocks::mock_preference( 'marcflavour', 'UNIMARC' ); + + $result = $see->get_fixer_rules(); + is( $result->[0], q{move_field(_id,es_id)}); + is( $result->[1], undef, q{No mapping when marc_type doesn't match marchflavour} ); + + $schema->storage->txn_rollback; + +}; + +1; --