From 44de264a251e8bc6c16148e50fd7b94040462d7f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Demians?= <f.demians@tamil.fr>
Date: Tue, 26 Mar 2024 18:12:45 +0100
Subject: [PATCH] Bug 36433: UT

Signed-off-by: Shi Yao Wang <shi-yao.wang@inlibro.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 .../Plugins/Biblio_and_Items_plugin_hooks.t   | 48 ++++++++++++++++++-
 t/lib/plugins/Koha/Plugin/Test.pm             |  8 ++++
 2 files changed, 55 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 4464fb407a5..720d058aee8 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,49 @@ 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 7472928fff5..2c5a35e27e6 100644
--- a/t/lib/plugins/Koha/Plugin/Test.pm
+++ b/t/lib/plugins/Koha/Plugin/Test.pm
@@ -458,4 +458,12 @@ 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.39.5 (Apple Git-154)