Bugzilla – Attachment 123829 Details for
Bug 28026
Add a 'call_recursive' method to Koha::Plugins
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 28026: Add call_recursive() as a supplement for call()
Bug-28026-Add-callrecursive-as-a-supplement-for-ca.patch (text/plain), 4.41 KB, created by
Tomás Cohen Arazi (tcohen)
on 2021-08-13 16:17:06 UTC
(
hide
)
Description:
Bug 28026: Add call_recursive() as a supplement for call()
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2021-08-13 16:17:06 UTC
Size:
4.41 KB
patch
obsolete
>From 6cdb6cd12357ccc19974a0b67c9b5233bcea02e6 Mon Sep 17 00:00:00 2001 >From: Kyle M Hall <kyle@bywatersolutions.com> >Date: Tue, 23 Mar 2021 16:07:20 +0000 >Subject: [PATCH] Bug 28026: Add call_recursive() as a supplement for call() > >The method call() is not sufficient for all plugin hook types. It's >possible that more than one plugin will be installed that wants to >return the arguaments passed in an updated form. These transformation >plugins need to work recursively rather than independantly. > >This patch adss a `call_recursive()` method that takes the output of >the first plugin and uses it as the input for the next plugin and so >on. This allowes each plugin to see the current version of the arguament >list and modify it as necessary. > >Test plan >1/ Run the included tests - t/db_dependent/Koha/Plugins/Plugins.t > >Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> >Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io> >--- > Koha/Plugins.pm | 35 +++++++++++++++++++++++++++ > t/db_dependent/Koha/Plugins/Plugins.t | 32 +++++++++++++++++++++++- > t/lib/Koha/Plugin/Test.pm | 6 +++++ > 3 files changed, 72 insertions(+), 1 deletion(-) > >diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm >index b8f9f86a33..e1f4992aca 100644 >--- a/Koha/Plugins.pm >+++ b/Koha/Plugins.pm >@@ -83,6 +83,41 @@ sub call { > return @responses; > } > >+=head2 call_recursive >+ >+Calls a plugin method for all enabled plugins, >+passing the return value from the previous plugin >+to the next one. >+ >+ @response = Koha::Plugins->call_recursive($method, @args) >+ >+=cut >+ >+sub call_recursive { >+ my ( $class, $method, @args ) = @_; >+ >+ my @responses; >+ if ( C4::Context->config('enable_plugins') ) { >+ >+ my @plugins = $class->new( { enable_plugins => 1 } )->GetPlugins( { method => $method } ); >+ @plugins = grep { $_->can($method) } @plugins; >+ >+ foreach my $plugin (@plugins) { >+ my @response = eval { $plugin->$method(@args) }; >+ >+ if ($@) { >+ warn sprintf( "Plugin error (%s): %s", $plugin->get_metadata->{name}, $@ ); >+ next; >+ } else { >+ @args = @response; >+ } >+ } >+ >+ } >+ >+ return @args; >+} >+ > =head2 GetPlugins > > This will return a list of all available plugins, optionally limited by >diff --git a/t/db_dependent/Koha/Plugins/Plugins.t b/t/db_dependent/Koha/Plugins/Plugins.t >index f49bd252fc..977012e0b1 100755 >--- a/t/db_dependent/Koha/Plugins/Plugins.t >+++ b/t/db_dependent/Koha/Plugins/Plugins.t >@@ -25,7 +25,7 @@ use File::Temp qw( tempdir tempfile ); > use FindBin qw($Bin); > use Module::Load::Conditional qw(can_load); > use Test::MockModule; >-use Test::More tests => 53; >+use Test::More tests => 54; > > use C4::Context; > use Koha::Database; >@@ -78,6 +78,36 @@ subtest 'call() tests' => sub { > $schema->storage->txn_rollback; > }; > >+subtest 'call_recursive() tests' => sub { >+ plan tests => 6; >+ >+ $schema->storage->txn_begin; >+ # Temporarily remove any installed plugins data >+ Koha::Plugins::Methods->delete; >+ >+ t::lib::Mocks::mock_config('enable_plugins', 1); >+ my $plugins = Koha::Plugins->new({ enable_plugins => 1 }); >+ my @plugins = $plugins->InstallPlugins; >+ foreach my $plugin (@plugins) { >+ $plugin->enable(); >+ } >+ >+ my (@responses) = Koha::Plugins->call_recursive('test_call_recursive', 1); >+ is( scalar @responses, 1, "Got back one element" ); >+ is( $responses[0], 11, "Got expected response" ); >+ >+ (@responses) = Koha::Plugins->call_recursive('test_call_recursive', 'abcd'); >+ is( scalar @responses, 1, "Got back one element" ); >+ is( $responses[0], 'abcdabcd', "Got expected response" ); >+ >+ t::lib::Mocks::mock_config('enable_plugins', 0); >+ (@responses) = Koha::Plugins->call_recursive('test_call_recursive', 1); >+ is( scalar @responses, 1, "Got back one element" ); >+ is( $responses[0], 1, "call_recursive should return the original arguments if plugins are disabled" ); >+ >+ $schema->storage->txn_rollback; >+}; >+ > subtest 'GetPlugins() tests' => sub { > > plan tests => 2; >diff --git a/t/lib/Koha/Plugin/Test.pm b/t/lib/Koha/Plugin/Test.pm >index 36565397b7..98fcb85824 100644 >--- a/t/lib/Koha/Plugin/Test.pm >+++ b/t/lib/Koha/Plugin/Test.pm >@@ -308,6 +308,12 @@ sub intranet_catalog_biblio_tab { > return @tabs; > } > >+sub test_call_recursive { >+ my ( $self, $value ) = @_; >+ >+ return $value x 2; >+} >+ > 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 28026
:
118679
|
118680
|
122443
|
122527
|
123788
|
123789
|
123829
|
124311