From 76e8990d55e62efa118c40266990fba72e6b8a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Demians?= Date: Tue, 26 Mar 2024 18:12:45 +0100 Subject: [PATCH] Bug 36433: UT --- .../Plugins/Biblio_and_Items_plugin_hooks.t | 46 ++++++++++++++++++- t/lib/plugins/Koha/Plugin/Test.pm | 9 ++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Plugins/Biblio_and_Items_plugin_hooks.t b/t/db_dependent/Koha/Plugins/Biblio_and_Items_plugin_hooks.t index 4464fb407a..10de3d8fa8 100755 --- a/t/db_dependent/Koha/Plugins/Biblio_and_Items_plugin_hooks.t +++ b/t/db_dependent/Koha/Plugins/Biblio_and_Items_plugin_hooks.t @@ -16,7 +16,7 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use Test::Warn; use File::Basename; @@ -137,3 +137,47 @@ subtest 'before_biblio_action() hooks tests' => sub { $schema->storage->txn_rollback; Koha::Plugins::Methods->delete; }; + +subtest 'elasticsearch_to_document() hooks tests' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; + + my $plugin = Koha::Plugin::Test->new->enable; + + my $test_plugin1 = Test::MockModule->new('Koha::Plugin::Test'); + $test_plugin1->mock( 'after_biblio_action', undef ); + + # Create a record + my $record = MARC::Record->new(); + $record->append_fields( MARC::Field->new('009', '123456789') ); + + my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); + my $documents; + warning_like { $documents = $indexer->marc_records_to_documents([$record]); } + qr/elasticsearch_to_document ref record: MARC::Record - ref document: HASH/, + 'ES marc_records_to_document calls the hook'; + + my $test_plugin2 = Test::MockModule->new('Koha::Plugin::Test'); + $test_plugin2->mock( 'after_biblio_action', undef ); + + # Create an ES search field 'ppn' and populate it with 009 field + $test_plugin2->mock( 'elasticsearch_to_document', sub { + my ($self, $params) = @_; + my $record = $params->{record}; + my $document = $params->{document}; + my $value = $record->field('009')->data; + $document->{ppn} = [$value]; + }); + $documents = $indexer->marc_records_to_documents([$record]); + ok(ref($documents) eq 'ARRAY', 'Indexer marc_records_to_documents returns an ARRAY'); + ok(scalar(@$documents) == 1, 'This array contains one (1) document'); + ok(ref($documents->[0]) eq 'HASH', 'The document is a HASH'); + ok(exists($documents->[0]->{ppn}), 'Generated field ppn exists'); + ok($documents->[0]->{ppn}->[0] eq '123456789', 'Field ppn contains 123456789'); +}; + diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm index 4154994984..fc200c60ed 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -414,4 +414,13 @@ sub _private_sub { return ""; } +sub elasticsearch_to_document { + my ( $self, $params ) = @_; + my $record =$params->{record}; + my $doc = $params->{document}; + + Koha::Exception->throw( + "elasticsearch_to_document ref record: " . ref($record) . " - ref document: " . ref($doc) ); +} + 1; -- 2.34.1