@@ -, +, @@ --- Koha/Plugins/Method.pm | 21 +++++++- t/db_dependent/Koha/Plugins/Method.t | 73 ++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100755 t/db_dependent/Koha/Plugins/Method.t --- a/Koha/Plugins/Method.pm +++ a/Koha/Plugins/Method.pm @@ -17,8 +17,9 @@ package Koha::Plugins::Method; use Modern::Perl; - use Koha::Database; +use Koha::Cache::Memory::Lite; +use Koha::Plugins; use base qw(Koha::Object); @@ -30,8 +31,26 @@ Koha::Plugin::Method - Koha Plugin Method Object class =head2 Class Methods +=head3 delete + + Delete method record. + But also delete associated plugin_data records. + Clear cache key. + =cut +sub delete { + my ($self) = @_; + + # Remove corresponding records from plugin_data. Clear cache. + my $cond = { plugin_class => $self->plugin_class }; + Koha::Database->new->schema->resultset('PluginData')->search($cond)->delete; + Koha::Cache::Memory::Lite->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY ); + + # Now do the actual delete + return $self->SUPER::delete; +} + =head3 type =cut --- a/t/db_dependent/Koha/Plugins/Method.t +++ a/t/db_dependent/Koha/Plugins/Method.t @@ -0,0 +1,73 @@ +#!/usr/bin/perl + +# Copyright 2023 Rijksmuseum, Koha development team +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Test::MockModule; +use Test::More tests => 1; + +use t::lib::TestBuilder; + +use Koha::Database; +use Koha::Cache::Memory::Lite; +use Koha::Plugins; +use Koha::Plugins::Methods; + +my $builder = t::lib::TestBuilder->new; +my $schema = Koha::Database->new->schema; + +$schema->storage->txn_begin; + +# We need to mock can_load from Module::Load::Conditional in Koha/Plugins +my $mock = Test::MockModule->new('Koha::Plugins')->mock( can_load => 1 ); + +subtest 'delete' => sub { + plan tests => 7; + + my $count_m = Koha::Plugins::Methods->count; + my $count_d = $schema->resultset('PluginData')->count; + Koha::Cache::Memory::Lite->get_instance->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY ); + my @enabled_plugins = Koha::Plugins->get_enabled_plugins; + + # Mock creation of new plugin + my $method = $builder->build_object( { class => 'Koha::Plugins::Methods' } ); + my $values = { plugin_class => $method->plugin_class, plugin_key => '__ENABLED__', plugin_value => 1 }; + my $data = $builder->build( { source => 'PluginData', value => $values } ); + + # Note: no_auto => 1 in next call prevent loading of a not-existing module + my $plugin_mock = Test::MockModule->new( $method->plugin_class, no_auto => 1 ) + ->mock( new => bless( { mocked => 1 }, $method->plugin_class ) ); + + # Check for new method and if get_enabled_plugins sees it too + is( Koha::Plugins::Methods->count, $count_m + 1, 'Expect a new method' ); + is( $schema->resultset('PluginData')->count, $count_d + 1, 'Expect entry in plugin_data' ); + Koha::Cache::Memory::Lite->get_instance->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY ); + is( scalar Koha::Plugins->get_enabled_plugins, 1 + @enabled_plugins, 'Recheck get_enabled_plugins' ); + + # Delete the method from plugin_methods, check table and cache + Koha::Plugins::Methods->search( { plugin_class => $method->plugin_class } )->delete; + 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::Methods->count, $count_m, 'Original number of methods' ); + is( $schema->resultset('PluginData')->count, $count_d, 'Original count in plugin_data' ); + is( scalar Koha::Plugins->get_enabled_plugins, scalar @enabled_plugins, 'Original number of enabled plugins' ); +}; + +$schema->storage->txn_rollback; --