From 70daf7f2e3f3d30cbea84cc6a589bb53c787fcac Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Wed, 13 Dec 2023 15:12:30 +0000 Subject: [PATCH] Bug 35536: Add Koha::Plugins->RemovePlugins class method Content-Type: text/plain; charset=utf-8 Note: In a subsequent patch we will remove the cleanup action from Koha::Plugins::Method. Test plan: Run t/db_dependent/Koha/Plugins/Plugins.t Signed-off-by: Marcel de Rooy --- Koha/Plugins.pm | 24 ++++++++++++ t/db_dependent/Koha/Plugins/Plugins.t | 55 +++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index 520f2febf2..4ffdccdc96 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -32,6 +32,7 @@ use C4::Output; use Koha::Cache::Memory::Lite; use Koha::Exceptions::Plugin; +use Koha::Plugins::Datas; use Koha::Plugins::Methods; use constant ENABLED_PLUGINS_CACHE_KEY => 'enabled_plugins'; @@ -297,6 +298,29 @@ sub InstallPlugins { return @plugins; } +=head2 RemovePlugins + + Koha::Plugins->RemovePlugins; + Koha::Plugins->RemovePlugins({ plugin_class => NAME }); + + This is primarily for unit testing. Remove (all) records in plugin_data + and plugin_methods. Clear the cache key for enabled plugins. + +=cut + +sub RemovePlugins { + my ( $class, $params ) = @_; # optional param: plugin_class + + my $cond = { + $params->{plugin_class} + ? ( plugin_class => $params->{plugin_class} ) + : () + }; + Koha::Plugins::Datas->search($cond)->delete; + Koha::Plugins::Methods->search($cond)->delete; + Koha::Cache::Memory::Lite->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY ); +} + 1; __END__ diff --git a/t/db_dependent/Koha/Plugins/Plugins.t b/t/db_dependent/Koha/Plugins/Plugins.t index 704594eb7e..6bd8c15cff 100755 --- a/t/db_dependent/Koha/Plugins/Plugins.t +++ b/t/db_dependent/Koha/Plugins/Plugins.t @@ -24,11 +24,12 @@ use File::Temp qw( tempdir tempfile ); use FindBin qw($Bin); use Module::Load::Conditional qw(can_load); use Test::MockModule; -use Test::More tests => 16; +use Test::More tests => 17; use Test::Warn; use C4::Context; use Koha::Database; +use Koha::Plugins::Datas; use Koha::Plugins::Methods; use t::lib::Mocks; @@ -130,8 +131,6 @@ subtest 'more call() tests' => sub { $schema->storage->txn_rollback; }; -$schema->storage->txn_begin; # matching rollback at the very end - subtest 'feature_enabled tests' => sub { plan tests => 4; @@ -325,6 +324,8 @@ subtest 'Koha::Plugin::Test' => sub { $schema->storage->txn_rollback; }; +$schema->storage->txn_begin; # Matching rollback at very end + subtest 'output and output_html tests' => sub { plan tests => 6; @@ -412,5 +413,51 @@ subtest 'new() tests' => sub { is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' ); }; -Koha::Plugins::Methods->delete; +subtest 'RemovePlugins' => sub { + plan tests => 9; + + t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + + # We need to mock can_load from Module::Load::Conditional in Koha/Plugins + my $mock = Test::MockModule->new('Koha::Plugins')->mock( can_load => 1 ); + + # First call without argument + Koha::Plugins->RemovePlugins; + is( Koha::Plugins::Datas->count, 0, 'No data in plugin_data' ); + is( Koha::Plugins::Methods->count, 0, 'No data in plugin_methods' ); + my @enabled_plugins = Koha::Plugins->get_enabled_plugins; + + # Mock creation of three new plugins + my ( @data, @methods, @mocks ); + my $class_basename = 'Koha::Plugin::TestMR::' . time; + foreach my $i ( 1 .. 3 ) { + push @data, + Koha::Plugins::Data->new( + { plugin_class => "$class_basename$i", plugin_key => '__ENABLED__', plugin_value => 1 } )->store; + push @methods, + Koha::Plugins::Method->new( { plugin_class => "$class_basename$i", plugin_method => "testmr$i" } )->store; + + # no_auto => 1 here prevents loading of a not-existing module + push @mocks, + Test::MockModule->new( "$class_basename$i", no_auto => 1 ) + ->mock( new => bless( { mocked => 1 }, "$class_basename$i" ) ); + } + + # Check for new method and if get_enabled_plugins sees it too + is( Koha::Plugins::Datas->count, 3, 'Records in plugin_data' ); + is( Koha::Plugins::Methods->count, 3, 'Records in plugin_methods' ); + Koha::Cache::Memory::Lite->get_instance->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY ); + is( scalar Koha::Plugins->get_enabled_plugins, 3, 'Check get_enabled_plugins again' ); + + # Call with argument: delete specific method, check tables and cache again + Koha::Plugins->RemovePlugins( { plugin_class => $class_basename . "2" } ); + is( + Koha::Cache::Memory::Lite->get_instance->get_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY ), + undef, 'Cache has been cleared by delete method' + ); + is( Koha::Plugins::Datas->count, 2, 'One record less in plugin_data' ); + is( Koha::Plugins::Methods->count, 2, 'One record less in plugin_methods' ); + is( scalar Koha::Plugins->get_enabled_plugins, 2, 'Check get_enabled_plugins' ); +}; + $schema->storage->txn_rollback; -- 2.30.2