diff --git a/Koha/Plugins/Handler.pm b/Koha/Plugins/Handler.pm index 7d0e602a05..26dc1ed73a 100644 --- a/Koha/Plugins/Handler.pm +++ b/Koha/Plugins/Handler.pm @@ -19,8 +19,9 @@ package Koha::Plugins::Handler; use Modern::Perl; -use Array::Utils qw( array_minus ); -use File::Path qw( remove_tree ); +use Array::Utils qw( array_minus ); +use File::Path qw( remove_tree ); +use List::MoreUtils qw( any ); use Module::Load qw( load ); @@ -69,6 +70,10 @@ sub run { if ($has_method) { load $plugin_class; my $plugin = $plugin_class->new( { cgi => $cgi, enable_plugins => $args->{'enable_plugins'} } ); + + my @management_methods = qw( enable disable install uninstall upgrade configure ); + return unless ( $plugin->is_enabled || any { $plugin_method eq $_ } @management_methods ); + return $plugin->$plugin_method($params); } else { warn "Plugin does not have method $plugin_method"; diff --git a/t/db_dependent/Koha/Plugins/Plugins.t b/t/db_dependent/Koha/Plugins/Plugins.t index 9988bfff14..66b057dd93 100755 --- a/t/db_dependent/Koha/Plugins/Plugins.t +++ b/t/db_dependent/Koha/Plugins/Plugins.t @@ -26,7 +26,7 @@ use List::MoreUtils qw(none); use Module::Load::Conditional qw(can_load); use Test::MockModule; use Test::NoWarnings; -use Test::More tests => 20; +use Test::More tests => 21; use Test::Warn; use Test::Exception; @@ -414,6 +414,45 @@ subtest 'Koha::Plugin::Test' => sub { $schema->storage->txn_rollback; }; +subtest 'Handler::run() skips disabled plugins' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + # Temporarily remove any installed plugins data + Koha::Plugins->RemovePlugins( { destructive => 1 } ); + + 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 plugin runs successfully + my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } ); + $plugin->enable; + is( + Koha::Plugins::Handler->run( { class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 } ), + "Koha::Plugin::Test::report", 'Handler::run() executes enabled plugin' + ); + + # Disabled plugin is skipped + $plugin->disable; + is( + Koha::Plugins::Handler->run( { class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 } ), + undef, 'Handler::run() returns undef for disabled plugin' + ); + + # Management methods still work on disabled plugins + $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } ); + ok( !$plugin->is_enabled, 'Plugin is still disabled after Handler::run skipped it' ); + Koha::Plugins::Handler->run( { class => "Koha::Plugin::Test", method => 'enable', enable_plugins => 1 } ); + $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } ); + ok( $plugin->is_enabled, 'Handler::run() allows enable method on disabled plugin' ); + + $schema->storage->txn_rollback; +}; + subtest 'RemovePlugins' => sub { plan tests => 3;