|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 6 |
# terms of the GNU General Public License as published by the Free Software |
| 7 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 8 |
# version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License along |
| 15 |
# with Koha; if not, see <https://www.gnu.org/licenses>. |
| 16 |
|
| 17 |
use Modern::Perl; |
| 18 |
use File::Basename; |
| 19 |
use Test::MockModule; |
| 20 |
use Test::More tests => 6; |
| 21 |
use Test::NoWarnings; |
| 22 |
use Test::Warn; |
| 23 |
|
| 24 |
use C4::Context; |
| 25 |
use Koha::Cache::Memory::Lite; |
| 26 |
use Koha::Database; |
| 27 |
use Koha::Plugins::Datas; |
| 28 |
use Koha::Plugins; |
| 29 |
|
| 30 |
use t::lib::Mocks; |
| 31 |
|
| 32 |
BEGIN { |
| 33 |
# Mock pluginsdir before loading Plugins module |
| 34 |
my $path = dirname(__FILE__) . '/../../../lib/plugins'; |
| 35 |
t::lib::Mocks::mock_config( 'pluginsdir', $path ); |
| 36 |
|
| 37 |
use_ok('Koha::Plugins::Loader'); |
| 38 |
} |
| 39 |
|
| 40 |
my $schema = Koha::Database->new->schema; |
| 41 |
|
| 42 |
subtest 'get_enabled_plugins - basic functionality' => sub { |
| 43 |
plan tests => 4; |
| 44 |
|
| 45 |
$schema->storage->txn_begin; |
| 46 |
|
| 47 |
# Clear cache before testing |
| 48 |
Koha::Cache::Memory::Lite->flush(); |
| 49 |
|
| 50 |
# Remove any existing plugins |
| 51 |
Koha::Plugins::Datas->delete; |
| 52 |
|
| 53 |
# Test with no enabled plugins |
| 54 |
my @plugins = Koha::Plugins::Loader->get_enabled_plugins(); |
| 55 |
is( scalar @plugins, 0, 'Returns empty list when no plugins are enabled' ); |
| 56 |
|
| 57 |
# Test caching behavior |
| 58 |
my @plugins_cached = Koha::Plugins::Loader->get_enabled_plugins(); |
| 59 |
is( scalar @plugins_cached, 0, 'Cached empty result works correctly' ); |
| 60 |
|
| 61 |
# The core functionality of loading plugins is tested indirectly through |
| 62 |
# the Koha::Plugins tests which use the Loader |
| 63 |
ok( 1, 'Loader module loaded successfully' ); |
| 64 |
can_ok( 'Koha::Plugins::Loader', 'get_enabled_plugins' ); |
| 65 |
|
| 66 |
$schema->storage->txn_rollback; |
| 67 |
}; |
| 68 |
|
| 69 |
subtest 'get_enabled_plugins - table does not exist' => sub { |
| 70 |
plan tests => 2; |
| 71 |
|
| 72 |
$schema->storage->txn_begin; |
| 73 |
|
| 74 |
# Clear cache |
| 75 |
Koha::Cache::Memory::Lite->flush(); |
| 76 |
|
| 77 |
# Mock table_exists to return false |
| 78 |
my $mock_database = Test::MockModule->new('Koha::Database'); |
| 79 |
$mock_database->mock( 'table_exists', sub { return 0; } ); |
| 80 |
|
| 81 |
my @plugins = Koha::Plugins::Loader->get_enabled_plugins(); |
| 82 |
is( scalar @plugins, 0, 'Returns empty list when plugin_data table does not exist' ); |
| 83 |
|
| 84 |
# Verify it doesn't try to query the non-existent table |
| 85 |
# This test passes if no exception is thrown |
| 86 |
ok( 1, 'No exception thrown when table does not exist' ); |
| 87 |
|
| 88 |
$schema->storage->txn_rollback; |
| 89 |
}; |
| 90 |
|
| 91 |
subtest 'get_enabled_plugins - error handling' => sub { |
| 92 |
plan tests => 1; |
| 93 |
|
| 94 |
$schema->storage->txn_begin; |
| 95 |
|
| 96 |
# Clear cache |
| 97 |
Koha::Cache::Memory::Lite->flush(); |
| 98 |
|
| 99 |
# Remove existing plugins |
| 100 |
Koha::Plugins::Datas->delete; |
| 101 |
|
| 102 |
# Test with invalid plugin class that can't be loaded by adding directly to DB |
| 103 |
Koha::Plugins::Data->new( |
| 104 |
{ |
| 105 |
plugin_class => 'Koha::Plugin::NonExistent', |
| 106 |
plugin_key => '__ENABLED__', |
| 107 |
plugin_value => 1, |
| 108 |
} |
| 109 |
)->store; |
| 110 |
|
| 111 |
my @plugins = Koha::Plugins::Loader->get_enabled_plugins(); |
| 112 |
is( scalar @plugins, 0, 'Returns empty list when plugin class cannot be loaded' ); |
| 113 |
|
| 114 |
$schema->storage->txn_rollback; |
| 115 |
}; |
| 116 |
|
| 117 |
subtest 'Integration with Koha::Plugins' => sub { |
| 118 |
plan tests => 1; |
| 119 |
|
| 120 |
# The Loader is designed to be used by Koha::Plugins::get_enabled_plugins |
| 121 |
# Test that the integration point exists |
| 122 |
can_ok( 'Koha::Plugins', 'get_enabled_plugins' ); |
| 123 |
|
| 124 |
# Full integration testing is done in t/db_dependent/Koha/Plugins/Plugins.t |
| 125 |
# which exercises the complete plugin loading flow including the Loader |
| 126 |
}; |