From cb5163284d6f7a94525d94e3564fdedaa8c7e5a9 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 15 Sep 2023 16:09:56 +0100 Subject: [PATCH] Bug 31503: (follow-up) Add 'feature_enabled' to Koha::Plugins This patch adds a 'feature_enabled' accessor to the Koha::Plugins class. Passing the plugin method name for the plugin feature you're looking for will return a boolean denoting whether any enabled plugins carry said method. Included in the patch is an addition to the TT KohaPlugins plugin to enable quick access to this function. Signed-off-by: Martin Renvoize --- Koha/Plugins.pm | 24 +++++++++++++++++++ Koha/Template/Plugin/KohaPlugins.pm | 17 ++++++++++++++ t/db_dependent/Koha/Plugins/Plugins.t | 33 ++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index a3c47006b55..520f2febf28 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -139,6 +139,30 @@ sub get_enabled_plugins { return @$enabled_plugins; } + +=head2 feature_enabled + +Returns a boolean denoting whether a plugin based feature is enabled or not. + + $enabled = Koha::Plugins->feature_enabled('method_name'); + +=cut + +sub feature_enabled { + my ( $class, $method ) = @_; + + return 0 unless C4::Context->config('enable_plugins'); + + my $key = "ENABLED_PLUGIN_FEATURE_" . $method; + my $feature = Koha::Cache::Memory::Lite->get_from_cache($key); + unless ( defined($feature) ) { + my @plugins = $class->get_enabled_plugins(); + my $enabled = any { $_->can($method) } @plugins; + Koha::Cache::Memory::Lite->set_in_cache( $key, $enabled ); + } + return $feature; +} + =head2 GetPlugins This will return a list of all available plugins, optionally limited by diff --git a/Koha/Template/Plugin/KohaPlugins.pm b/Koha/Template/Plugin/KohaPlugins.pm index 7d527b34e36..35ac34bcf3f 100644 --- a/Koha/Template/Plugin/KohaPlugins.pm +++ b/Koha/Template/Plugin/KohaPlugins.pm @@ -282,4 +282,21 @@ sub get_plugins_opac_cover_images { return join( "\n", @data ); } +=head3 feature_enabled + + [% KohaPlugins.feature_enabled('method_name') %] + +This method returns true if the passed plugin hook method name is found to be installed and enabled as part of a plugin. + +=cut + +sub feature_enabled { + my ($self, $method) = @_; + + my $p = Koha::Plugins->new(); + return 0 unless $p; + + return $p->feature_enabled($method); +} + 1; diff --git a/t/db_dependent/Koha/Plugins/Plugins.t b/t/db_dependent/Koha/Plugins/Plugins.t index db68b9d1951..212416e8c33 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 => 61; +use Test::More tests => 62; use Test::Warn; use C4::Context; @@ -129,6 +129,37 @@ subtest 'more call() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'feature_enabled tests' => sub { + plan tests => 4; + + $schema->storage->txn_begin; + # Temporarily remove any installed plugins data + Koha::Plugins::Methods->delete; + $schema->resultset('PluginData')->delete(); + + t::lib::Mocks::mock_config('enable_plugins', 0); + my $enabled = Koha::Plugins->feature_enabled('check_password'); + ok( !$enabled, "check_password not available when plugins are disabled"); + + t::lib::Mocks::mock_config('enable_plugins', 1); + my $plugins = Koha::Plugins->new({ enable_plugins => 1 }); + + my @plugins; + warning_is { @plugins = $plugins->InstallPlugins; } undef; + + $enabled = Koha::Plugins->feature_enabled('check_password'); + ok( !$enabled, "check_password not available when plugins are installed but not enabled"); + + foreach my $plugin (@plugins) { + $plugin->enable(); + } + + $enabled = Koha::Plugins->feature_enabled('check_password'); + ok( $enabled, "check_password is available when at least one enabled plugin supports it"); + + $schema->storage->txn_rollback; +}; + subtest 'GetPlugins() tests' => sub { plan tests => 3; -- 2.41.0