From 5891417f52a440919d47013fa4b3914966175001 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 11 Dec 2023 10:43:04 +0000 Subject: [PATCH] Bug 35507: Add Plugins::Method->delete Content-Type: text/plain; charset=utf-8 Delete record in plugin_data too and clear cache. Note: Adding the singular delete, makes the plural loop and call the singular in Koha::Objects. Clearing the cache is done in the store_data call of Koha::Plugins. Test plan: Run prove $(git grep -l Koha::Plugin | grep -P "^t\/db") Signed-off-by: Marcel de Rooy --- Koha/Plugins/Method.pm | 20 ++++++- t/db_dependent/Koha/Plugins/Method.t | 86 ++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100755 t/db_dependent/Koha/Plugins/Method.t diff --git a/Koha/Plugins/Method.pm b/Koha/Plugins/Method.pm index b6e2e750b8..86d2c54464 100644 --- a/Koha/Plugins/Method.pm +++ b/Koha/Plugins/Method.pm @@ -17,8 +17,8 @@ package Koha::Plugins::Method; use Modern::Perl; - use Koha::Database; +use Koha::Plugins::Base qw(); use base qw(Koha::Object); @@ -30,8 +30,26 @@ Koha::Plugin::Method - Koha Plugin Method Object class =head2 Class Methods +=head3 delete + + Delete method record but disable plugin too and clear enabled plugins cache key. + =cut +sub delete { + my ($self) = @_; + + # Next call makes sure that plugin is disabled, and ENABLED_PLUGINS_CACHE_KEY is cleared + # The self parameter only needs the class name + Koha::Plugins::Base::store_data( + { class => $self->plugin_class }, + { __ENABLED__ => 0 } + ); + + # Now do the actual delete + return $self->SUPER::delete; +} + =head3 type =cut diff --git a/t/db_dependent/Koha/Plugins/Method.t b/t/db_dependent/Koha/Plugins/Method.t new file mode 100755 index 0000000000..feaeb2d77d --- /dev/null +++ b/t/db_dependent/Koha/Plugins/Method.t @@ -0,0 +1,86 @@ +#!/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 = Koha::Plugins::Methods->count; + my $count_data = $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 } ); + # Add yet another record in data + $values->{plugin_key} = 'WHATEVER'; + $values->{plugin_value} = 'great 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 + 1, 'Expect a new method' ); + 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, 'Original number of methods' ); + is( $schema->resultset('PluginData')->count, $count_data + 2, 'No data records removed' ); + is( + $schema->resultset('PluginData')->search( + { + plugin_class => $method->plugin_class, + plugin_key => '__ENABLED__', + } + )->next->plugin_value, + 0, + 'Check if plugin is disabled' + ); + is( scalar Koha::Plugins->get_enabled_plugins, scalar @enabled_plugins, 'Original number of enabled plugins' ); +}; + +$schema->storage->txn_rollback; -- 2.30.2