Bugzilla – Attachment 163509 Details for
Bug 28173
Add plugin hooks object_store_pre and object_store_post
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 28173: Add plugin hooks object_store_pre and object_store_post
Bug-28173-Add-plugin-hooks-objectstorepre-and-obje.patch (text/plain), 4.13 KB, created by
Julian Maurice
on 2024-03-20 08:25:15 UTC
(
hide
)
Description:
Bug 28173: Add plugin hooks object_store_pre and object_store_post
Filename:
MIME Type:
Creator:
Julian Maurice
Created:
2024-03-20 08:25:15 UTC
Size:
4.13 KB
patch
obsolete
>From 8723532dc24595bf5dab3c5c03727d16691f9fe2 Mon Sep 17 00:00:00 2001 >From: Julian Maurice <julian.maurice@biblibre.com> >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/plugins/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 560dccaff2..1e64f6accc 100644 >--- a/Koha/Object.pm >+++ b/Koha/Object.pm >@@ -167,7 +167,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..69e19c27d1 >--- /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 <http://www.gnu.org/licenses>. >+ >+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/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->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/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm >index 32f7dedb48..6c0ef5f3e3 100644 >--- a/t/lib/plugins/Koha/Plugin/Test.pm >+++ b/t/lib/plugins/Koha/Plugin/Test.pm >@@ -391,6 +391,22 @@ sub ill_table_actions { > ); > } > >+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.30.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 28173
:
119872
| 163509 |
163516