Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright 2023 Rijksmuseum, Koha development team |
4 |
# |
5 |
# This file is part of Koha |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
use Test::MockModule; |
22 |
use Test::More tests => 1; |
23 |
|
24 |
use t::lib::TestBuilder; |
25 |
|
26 |
use Koha::Database; |
27 |
use Koha::Cache::Memory::Lite; |
28 |
use Koha::Plugins; |
29 |
use Koha::Plugins::Methods; |
30 |
|
31 |
my $builder = t::lib::TestBuilder->new; |
32 |
my $schema = Koha::Database->new->schema; |
33 |
|
34 |
$schema->storage->txn_begin; |
35 |
|
36 |
# We need to mock can_load from Module::Load::Conditional in Koha/Plugins |
37 |
my $mock = Test::MockModule->new('Koha::Plugins')->mock( can_load => 1 ); |
38 |
|
39 |
subtest 'delete' => sub { |
40 |
plan tests => 7; |
41 |
|
42 |
my $count = Koha::Plugins::Methods->count; |
43 |
my $count_data = $schema->resultset('PluginData')->count; |
44 |
Koha::Cache::Memory::Lite->get_instance->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY ); |
45 |
my @enabled_plugins = Koha::Plugins->get_enabled_plugins; |
46 |
|
47 |
# Mock creation of new plugin |
48 |
my $method = $builder->build_object( { class => 'Koha::Plugins::Methods' } ); |
49 |
my $values = { plugin_class => $method->plugin_class, plugin_key => '__ENABLED__', plugin_value => 1 }; |
50 |
my $data = $builder->build( { source => 'PluginData', value => $values } ); |
51 |
# Add yet another record in data |
52 |
$values->{plugin_key} = 'WHATEVER'; |
53 |
$values->{plugin_value} = 'great data'; |
54 |
$builder->build( { source => 'PluginData', value => $values } ); |
55 |
|
56 |
# Note: no_auto => 1 in next call prevent loading of a not-existing module |
57 |
my $plugin_mock = Test::MockModule->new( $method->plugin_class, no_auto => 1 ) |
58 |
->mock( new => bless( { mocked => 1 }, $method->plugin_class ) ); |
59 |
|
60 |
# Check for new method and if get_enabled_plugins sees it too |
61 |
is( Koha::Plugins::Methods->count, $count + 1, 'Expect a new method' ); |
62 |
Koha::Cache::Memory::Lite->get_instance->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY ); |
63 |
is( scalar Koha::Plugins->get_enabled_plugins, 1 + @enabled_plugins, 'Recheck get_enabled_plugins' ); |
64 |
|
65 |
# Delete the method from plugin_methods, check table and cache |
66 |
Koha::Plugins::Methods->search( { plugin_class => $method->plugin_class } )->delete; |
67 |
is( |
68 |
Koha::Cache::Memory::Lite->get_instance->get_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY ), |
69 |
undef, 'Cache has been cleared by delete method' |
70 |
); |
71 |
is( Koha::Plugins::Methods->count, $count, 'Original number of methods' ); |
72 |
is( $schema->resultset('PluginData')->count, $count_data + 2, 'No data records removed' ); |
73 |
is( |
74 |
$schema->resultset('PluginData')->search( |
75 |
{ |
76 |
plugin_class => $method->plugin_class, |
77 |
plugin_key => '__ENABLED__', |
78 |
} |
79 |
)->next->plugin_value, |
80 |
0, |
81 |
'Check if plugin is disabled' |
82 |
); |
83 |
is( scalar Koha::Plugins->get_enabled_plugins, scalar @enabled_plugins, 'Original number of enabled plugins' ); |
84 |
}; |
85 |
|
86 |
$schema->storage->txn_rollback; |