@@ -, +, @@ --- Koha/Plugins.pm | 24 +++++++++++++++++++ Koha/Template/Plugin/KohaPlugins.pm | 17 ++++++++++++++ t/db_dependent/Koha/Plugins/Plugins.t | 34 ++++++++++++++++++++++++++- 3 files changed, 74 insertions(+), 1 deletion(-) --- a/Koha/Plugins.pm +++ a/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 --- a/Koha/Template/Plugin/KohaPlugins.pm +++ a/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; --- a/t/db_dependent/Koha/Plugins/Plugins.t +++ a/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,38 @@ 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; --