From 9d6c33fa78b2ee095d21245f46eb8708b41f5268 Mon Sep 17 00:00:00 2001
From: Stefan Berndtsson <stefan.berndtsson@gmail.com>
Date: Mon, 17 Oct 2022 16:48:58 +0200
Subject: [PATCH] Bug 31897: New before_index_action hook for Elasticsearch

Hooks added:
before_index_action with new action update

How to test:
Run tests in t/db_dependent/Koha/Plugins/Elasticsearch_Indexer_hooks.t

Sponsored by: Gothenburg University Library
---
 Koha/SearchEngine/Elasticsearch/Indexer.pm    |  9 ++
 .../Plugins/Elasticsearch_Indexer_hooks.t     | 83 +++++++++++++++++++
 t/lib/plugins/Koha/Plugin/Test.pm             | 11 +++
 3 files changed, 103 insertions(+)
 create mode 100755 t/db_dependent/Koha/Plugins/Elasticsearch_Indexer_hooks.t

diff --git a/Koha/SearchEngine/Elasticsearch/Indexer.pm b/Koha/SearchEngine/Elasticsearch/Indexer.pm
index 6101a3a372..09a5c13c88 100644
--- a/Koha/SearchEngine/Elasticsearch/Indexer.pm
+++ b/Koha/SearchEngine/Elasticsearch/Indexer.pm
@@ -116,6 +116,15 @@ sub update_index {
         $index_record_ids = $record_ids;
     }
 
+    Koha::Plugins->call('before_index_action', {
+        action => 'update',
+        payload => {
+            engine => 'Elasticsearch',
+            records => $records,
+            record_ids => $index_record_ids
+         }
+    });
+
     my $documents = $self->marc_records_to_documents($records);
     my @body;
     for (my $i = 0; $i < scalar @$index_record_ids; $i++) {
diff --git a/t/db_dependent/Koha/Plugins/Elasticsearch_Indexer_hooks.t b/t/db_dependent/Koha/Plugins/Elasticsearch_Indexer_hooks.t
new file mode 100755
index 0000000000..ba12de365a
--- /dev/null
+++ b/t/db_dependent/Koha/Plugins/Elasticsearch_Indexer_hooks.t
@@ -0,0 +1,83 @@
+#!/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 => 5;
+use Test::MockModule;
+use Test::Warn;
+use t::lib::Mocks;
+use t::lib::TestBuilder;
+
+use File::Basename;
+
+use MARC::Record;
+
+use Koha::Database;
+use Koha::Biblios;
+
+BEGIN {
+    # Mock pluginsdir before loading Plugins module
+    my $path = dirname(__FILE__) . '/../../../lib/plugins';
+    t::lib::Mocks::mock_config( 'pluginsdir', $path );
+
+    use_ok('Koha::Plugins');
+    use_ok('Koha::Plugins::Handler');
+    use_ok('Koha::Plugin::Test');
+}
+
+my $schema = Koha::Database->schema();
+
+use_ok('Koha::SearchEngine::Elasticsearch::Indexer');
+
+SKIP: {
+
+    eval { Koha::SearchEngine::Elasticsearch->get_elasticsearch_params; };
+
+    skip 'Elasticsearch configuration not available', 2
+        if $@;
+
+my $builder = t::lib::TestBuilder->new;
+my $biblio = $builder->build_sample_biblio; # create biblio before we start mocking to avoid trouble indexing on creation
+t::lib::Mocks::mock_config( 'enable_plugins', 1 );
+
+subtest 'before_index_action hook' => sub {
+    plan tests => 1;
+
+    $schema->storage->txn_begin;
+    my $plugins = Koha::Plugins->new;
+    $plugins->InstallPlugins;
+
+    my $plugin = Koha::Plugin::Test->new->enable;
+    my $test_plugin = Test::MockModule->new('Koha::Plugin::Test');
+    $test_plugin->mock( 'after_biblio_action', undef );
+
+    my $biblio = $builder->build_sample_biblio;
+    my $biblionumber = $biblio->biblionumber;
+
+    my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ 'index' => 'biblios' });
+    warning_like {
+        $indexer->update_index([$biblionumber]);
+    }
+    qr/before_index_action called with action: update, engine: Elasticsearch, ref: ARRAY/,
+    '-> update_index calls the before_index_action hook';
+
+    $schema->storage->txn_rollback;
+    Koha::Plugins::Methods->delete;
+};
+
+}
diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm
index eee99859d1..7f8855acc8 100644
--- a/t/lib/plugins/Koha/Plugin/Test.pm
+++ b/t/lib/plugins/Koha/Plugin/Test.pm
@@ -348,6 +348,17 @@ sub background_tasks {
     };
 }
 
+sub before_index_action {
+    my ( $self, $params ) = @_;
+
+    my $action = $params->{action};
+    my $engine   = $params->{payload}->{engine};
+    my $records   = $params->{payload}->{records};
+
+    Koha::Exception->throw(
+        "before_index_action called with action: $action, engine: $engine, ref: " . ref($records) );
+}
+
 sub _private_sub {
     return "";
 }
-- 
2.37.1