|
Line 0
Link Here
|
|
|
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 <http://www.gnu.org/licenses>. |
| 16 |
|
| 17 |
use Modern::Perl; |
| 18 |
|
| 19 |
use Test::NoWarnings; |
| 20 |
use Test::More tests => 9; |
| 21 |
|
| 22 |
use File::Basename; |
| 23 |
|
| 24 |
use C4::Context; |
| 25 |
|
| 26 |
use Koha::FrameworkPlugin; |
| 27 |
|
| 28 |
use t::lib::Mocks; |
| 29 |
use t::lib::TestBuilder; |
| 30 |
|
| 31 |
BEGIN { |
| 32 |
# Mock pluginsdir before loading Plugins module |
| 33 |
my $path = dirname(__FILE__) . '/../../../lib/plugins'; |
| 34 |
t::lib::Mocks::mock_config( 'pluginsdir', $path ); |
| 35 |
|
| 36 |
use_ok('Koha::Plugins'); |
| 37 |
use_ok('Koha::Plugins::Handler'); |
| 38 |
use_ok('Koha::Plugin::Test'); |
| 39 |
use_ok('Koha::Plugin::TestValuebuilder'); |
| 40 |
} |
| 41 |
|
| 42 |
my $schema = Koha::Database->new->schema; |
| 43 |
my $builder = t::lib::TestBuilder->new; |
| 44 |
|
| 45 |
t::lib::Mocks::mock_config( 'enable_plugins', 1 ); |
| 46 |
|
| 47 |
subtest 'get_valuebuilders_installed() tests' => sub { |
| 48 |
|
| 49 |
plan tests => 6; |
| 50 |
|
| 51 |
$schema->storage->txn_begin; |
| 52 |
|
| 53 |
# Temporarily remove any installed plugins data |
| 54 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
| 55 |
|
| 56 |
my $plugins = Koha::Plugins->new; |
| 57 |
$plugins->InstallPlugins; |
| 58 |
|
| 59 |
# Test 1: No valuebuilders when no plugins have get_valuebuilder method |
| 60 |
# Only enable the regular Test plugin (which doesn't have get_valuebuilder) |
| 61 |
my $regular_plugin = Koha::Plugin::Test->new->enable; |
| 62 |
my @valuebuilders = $plugins->get_valuebuilders_installed(); |
| 63 |
is( scalar @valuebuilders, 0, 'No valuebuilders when enabled plugin lacks get_valuebuilder method' ); |
| 64 |
|
| 65 |
# Test 2: Enable the TestValuebuilder plugin which provides a valuebuilder |
| 66 |
my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable; |
| 67 |
@valuebuilders = $plugins->get_valuebuilders_installed(); |
| 68 |
is( scalar @valuebuilders, 1, 'One valuebuilder found when TestValuebuilder plugin is enabled' ); |
| 69 |
|
| 70 |
# Test 3: Check the structure of returned valuebuilder |
| 71 |
my $valuebuilder = $valuebuilders[0]; |
| 72 |
is( ref($valuebuilder), 'HASH', 'Valuebuilder is returned as hashref' ); |
| 73 |
is( $valuebuilder->{name}, 'test_plugin_valuebuilder.pl', 'Valuebuilder name matches TestValuebuilder plugin' ); |
| 74 |
isa_ok( $valuebuilder->{plugin}, 'Koha::Plugin::TestValuebuilder', 'Valuebuilder plugin is correct object type' ); |
| 75 |
|
| 76 |
# Test 4: Disable the valuebuilder plugin and verify it's not found |
| 77 |
$valuebuilder_plugin->disable; |
| 78 |
@valuebuilders = $plugins->get_valuebuilders_installed(); |
| 79 |
is( scalar @valuebuilders, 0, 'No valuebuilders when TestValuebuilder plugin is disabled' ); |
| 80 |
|
| 81 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
| 82 |
$schema->storage->txn_rollback; |
| 83 |
}; |
| 84 |
|
| 85 |
subtest 'FrameworkPlugin valuebuilder integration tests' => sub { |
| 86 |
|
| 87 |
plan tests => 6; |
| 88 |
|
| 89 |
$schema->storage->txn_begin; |
| 90 |
|
| 91 |
# Temporarily remove any installed plugins data |
| 92 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
| 93 |
|
| 94 |
my $plugins = Koha::Plugins->new; |
| 95 |
$plugins->InstallPlugins; |
| 96 |
|
| 97 |
# Enable the TestValuebuilder plugin |
| 98 |
my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable; |
| 99 |
my $test_valuebuilder_name = $valuebuilder_plugin->get_valuebuilder(); |
| 100 |
|
| 101 |
# Test 1: FrameworkPlugin can find plugin-based valuebuilder |
| 102 |
my $framework_plugin = Koha::FrameworkPlugin->new( { name => $test_valuebuilder_name } ); |
| 103 |
ok( $framework_plugin, 'FrameworkPlugin object created successfully' ); |
| 104 |
is( $framework_plugin->name, $test_valuebuilder_name, 'FrameworkPlugin has correct name' ); |
| 105 |
|
| 106 |
# Test 2: FrameworkPlugin can load plugin-based valuebuilder |
| 107 |
my $build_result = $framework_plugin->build( { id => 'test_field_001' } ); |
| 108 |
ok( $build_result, 'FrameworkPlugin build method succeeds with plugin valuebuilder' ); |
| 109 |
|
| 110 |
# Test 3: FrameworkPlugin generates javascript from plugin |
| 111 |
my $javascript = $framework_plugin->javascript; |
| 112 |
ok( $javascript, 'FrameworkPlugin generates javascript' ); |
| 113 |
like( |
| 114 |
$javascript, qr/test_focus_test_field_001/, |
| 115 |
'Generated javascript contains expected field-specific function' |
| 116 |
); |
| 117 |
|
| 118 |
# Test 4: FrameworkPlugin can launch plugin-based valuebuilder (would need CGI mock for full test) |
| 119 |
ok( $framework_plugin->{launcher}, 'FrameworkPlugin has launcher method loaded from plugin' ); |
| 120 |
|
| 121 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
| 122 |
$schema->storage->txn_rollback; |
| 123 |
}; |
| 124 |
|
| 125 |
subtest 'Plugin Base valuebuilder methods tests' => sub { |
| 126 |
|
| 127 |
plan tests => 6; |
| 128 |
|
| 129 |
$schema->storage->txn_begin; |
| 130 |
|
| 131 |
# Temporarily remove any installed plugins data |
| 132 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
| 133 |
|
| 134 |
my $plugins = Koha::Plugins->new; |
| 135 |
$plugins->InstallPlugins; |
| 136 |
|
| 137 |
# Test 1: get_valuebuilders() with plugin that doesn't have get_valuebuilder method |
| 138 |
my $regular_plugin = Koha::Plugin::Test->new->enable; |
| 139 |
my $valuebuilders = $regular_plugin->get_valuebuilders(); |
| 140 |
is( ref($valuebuilders), 'ARRAY', 'get_valuebuilders returns array reference' ); |
| 141 |
is( scalar @$valuebuilders, 0, 'get_valuebuilders returns empty array when plugin lacks get_valuebuilder method' ); |
| 142 |
|
| 143 |
# Test 2: get_valuebuilders() with plugin that has get_valuebuilder method |
| 144 |
my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable; |
| 145 |
$valuebuilders = $valuebuilder_plugin->get_valuebuilders(); |
| 146 |
is( scalar @$valuebuilders, 1, 'get_valuebuilders returns one item when plugin has get_valuebuilder method' ); |
| 147 |
is( $valuebuilders->[0], 'test_plugin_valuebuilder.pl', 'get_valuebuilders returns correct valuebuilder name' ); |
| 148 |
|
| 149 |
# Test 3: get_valuebuilder_url() method |
| 150 |
my $url = $valuebuilder_plugin->get_valuebuilder_url(); |
| 151 |
my $expected_class = ref($valuebuilder_plugin); |
| 152 |
like( |
| 153 |
$url, qr|/cgi-bin/koha/plugins/run\.pl\?class=$expected_class&method=launcher|, |
| 154 |
'get_valuebuilder_url returns correct URL format' |
| 155 |
); |
| 156 |
|
| 157 |
# Test 4: get_valuebuilder() method directly |
| 158 |
my $valuebuilder_name = $valuebuilder_plugin->get_valuebuilder(); |
| 159 |
is( $valuebuilder_name, 'test_plugin_valuebuilder.pl', 'get_valuebuilder returns expected name' ); |
| 160 |
|
| 161 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
| 162 |
$schema->storage->txn_rollback; |
| 163 |
}; |
| 164 |
|
| 165 |
subtest 'Real FrameworkPlugin integration with valuebuilder plugins' => sub { |
| 166 |
|
| 167 |
plan tests => 12; |
| 168 |
|
| 169 |
$schema->storage->txn_begin; |
| 170 |
|
| 171 |
# Temporarily remove any installed plugins data |
| 172 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
| 173 |
|
| 174 |
my $plugins = Koha::Plugins->new; |
| 175 |
$plugins->InstallPlugins; |
| 176 |
|
| 177 |
# Enable our test valuebuilder plugin |
| 178 |
my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable; |
| 179 |
ok( $valuebuilder_plugin->is_enabled, 'TestValuebuilder plugin is enabled' ); |
| 180 |
|
| 181 |
# Test 1: Verify the plugin provides a valuebuilder |
| 182 |
my $valuebuilder_name = $valuebuilder_plugin->get_valuebuilder(); |
| 183 |
is( |
| 184 |
$valuebuilder_name, 'test_plugin_valuebuilder.pl', |
| 185 |
'TestValuebuilder plugin returns correct valuebuilder name' |
| 186 |
); |
| 187 |
|
| 188 |
# Test 2: Verify get_valuebuilders_installed finds our plugin |
| 189 |
my @valuebuilders = $plugins->get_valuebuilders_installed(); |
| 190 |
is( scalar @valuebuilders, 1, 'get_valuebuilders_installed finds one valuebuilder' ); |
| 191 |
is( $valuebuilders[0]->{name}, $valuebuilder_name, 'Found valuebuilder has correct name' ); |
| 192 |
isa_ok( |
| 193 |
$valuebuilders[0]->{plugin}, 'Koha::Plugin::TestValuebuilder', |
| 194 |
'Found valuebuilder has correct plugin type' |
| 195 |
); |
| 196 |
|
| 197 |
# Test 3: FrameworkPlugin can load our plugin-based valuebuilder |
| 198 |
my $framework_plugin = Koha::FrameworkPlugin->new( { name => $valuebuilder_name } ); |
| 199 |
ok( $framework_plugin, 'FrameworkPlugin created successfully with plugin valuebuilder name' ); |
| 200 |
is( $framework_plugin->name, $valuebuilder_name, 'FrameworkPlugin has correct name' ); |
| 201 |
ok( !$framework_plugin->errstr, 'FrameworkPlugin has no errors' ); |
| 202 |
|
| 203 |
# Test 4: FrameworkPlugin can build JavaScript from our plugin |
| 204 |
my $build_result = $framework_plugin->build( { id => 'test_field_123' } ); |
| 205 |
ok( $build_result, 'FrameworkPlugin build succeeds with real plugin valuebuilder' ); |
| 206 |
|
| 207 |
my $javascript = $framework_plugin->javascript; |
| 208 |
ok( $javascript, 'FrameworkPlugin generates JavaScript from plugin' ); |
| 209 |
like( $javascript, qr/test_focus_test_field_123/, 'Generated JavaScript contains expected function for field ID' ); |
| 210 |
like( |
| 211 |
$javascript, qr/test_click_test_field_123/, |
| 212 |
'Generated JavaScript contains expected click function for field ID' |
| 213 |
); |
| 214 |
|
| 215 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
| 216 |
$schema->storage->txn_rollback; |
| 217 |
}; |