From ffc284950c5f4695342e5b0e3927ea5eeb65cd85 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Tue, 20 Apr 2021 10:17:16 +0200 Subject: [PATCH] Bug 28173: Add plugin hooks object_store_pre and object_store_post These two hooks are called in Koha::Object::store, which means any object stored in the database will trigger these hooks. object_store_pre is called just before `update_or_insert`, allowing to modify the object before sending the INSERT/UPDATE query object_store_post is called just after `update_or_insert`, so it can be used for any actions that require to have the final object, like indexing, logging, ... Test plan: 1. prove t/db_dependent/Koha/Plugins/Object_hooks.t --- Koha/Object.pm | 7 ++- t/db_dependent/Koha/Plugins/Object_hooks.t | 58 ++++++++++++++++++++++ t/lib/Koha/Plugin/Test.pm | 16 ++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 t/db_dependent/Koha/Plugins/Object_hooks.t diff --git a/Koha/Object.pm b/Koha/Object.pm index 57b1429b89..62fb20c1bc 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -166,7 +166,12 @@ sub store { } try { - return $self->_result()->update_or_insert() ? $self : undef; + require Koha::Plugins; + Koha::Plugins->call('object_store_pre', $self); + my $result = $self->_result()->update_or_insert(); + Koha::Plugins->call('object_store_post', $self); + + return $result ? $self : undef; } catch { # Catch problems and raise relevant exceptions diff --git a/t/db_dependent/Koha/Plugins/Object_hooks.t b/t/db_dependent/Koha/Plugins/Object_hooks.t new file mode 100644 index 0000000000..9dcfc860b3 --- /dev/null +++ b/t/db_dependent/Koha/Plugins/Object_hooks.t @@ -0,0 +1,58 @@ +#!/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 . + +use Modern::Perl; + +use Test::More tests => 4; +use Test::MockModule; + +use File::Basename; + +use t::lib::Mocks; + +BEGIN { + # Mock pluginsdir before loading Plugins module + my $path = dirname(__FILE__) . '/../../../lib'; + 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->new->schema; + +require t::lib::TestBuilder; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + +subtest 'object_store_pre and object_store_post hook tests' => sub { + plan tests => 2; + + $schema->storage->txn_begin; + + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; + Koha::Plugin::Test->new->enable; + + my $item = $builder->build_sample_item(); + + is($item->itemnotes, 'notes added by object_store_pre'); + is($item->itemnotes_nonpublic, 'notes added by object_store_post'); + + $schema->storage->txn_rollback; +}; diff --git a/t/lib/Koha/Plugin/Test.pm b/t/lib/Koha/Plugin/Test.pm index 1c296b0595..a101bcf90b 100644 --- a/t/lib/Koha/Plugin/Test.pm +++ b/t/lib/Koha/Plugin/Test.pm @@ -308,6 +308,22 @@ sub intranet_catalog_biblio_tab { return @tabs; } +sub object_store_pre { + my ($self, $object) = @_; + + if ($object->_type eq 'Item') { + $object->itemnotes('notes added by object_store_pre'); + } +} + +sub object_store_post { + my ($self, $object) = @_; + + if ($object->_type eq 'Item') { + $object->_result->update({ itemnotes_nonpublic => 'notes added by object_store_post' }); + } +} + sub _private_sub { return ""; } -- 2.20.1