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( $javascript, qr/test_focus_test_field_001/, 'Generated javascript contains expected field-specific function' ); |
114 |
|
115 |
# Test 4: FrameworkPlugin can launch plugin-based valuebuilder (would need CGI mock for full test) |
116 |
ok( $framework_plugin->{launcher}, 'FrameworkPlugin has launcher method loaded from plugin' ); |
117 |
|
118 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
119 |
$schema->storage->txn_rollback; |
120 |
}; |
121 |
|
122 |
subtest 'Plugin Base valuebuilder methods tests' => sub { |
123 |
|
124 |
plan tests => 6; |
125 |
|
126 |
$schema->storage->txn_begin; |
127 |
|
128 |
# Temporarily remove any installed plugins data |
129 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
130 |
|
131 |
my $plugins = Koha::Plugins->new; |
132 |
$plugins->InstallPlugins; |
133 |
|
134 |
# Test 1: get_valuebuilders() with plugin that doesn't have get_valuebuilder method |
135 |
my $regular_plugin = Koha::Plugin::Test->new->enable; |
136 |
my $valuebuilders = $regular_plugin->get_valuebuilders(); |
137 |
is( ref($valuebuilders), 'ARRAY', 'get_valuebuilders returns array reference' ); |
138 |
is( scalar @$valuebuilders, 0, 'get_valuebuilders returns empty array when plugin lacks get_valuebuilder method' ); |
139 |
|
140 |
# Test 2: get_valuebuilders() with plugin that has get_valuebuilder method |
141 |
my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable; |
142 |
$valuebuilders = $valuebuilder_plugin->get_valuebuilders(); |
143 |
is( scalar @$valuebuilders, 1, 'get_valuebuilders returns one item when plugin has get_valuebuilder method' ); |
144 |
is( $valuebuilders->[0], 'test_plugin_valuebuilder.pl', 'get_valuebuilders returns correct valuebuilder name' ); |
145 |
|
146 |
# Test 3: get_valuebuilder_url() method |
147 |
my $url = $valuebuilder_plugin->get_valuebuilder_url(); |
148 |
my $expected_class = ref($valuebuilder_plugin); |
149 |
like( $url, qr|/cgi-bin/koha/plugins/run\.pl\?class=$expected_class&method=launcher|, |
150 |
'get_valuebuilder_url returns correct URL format' ); |
151 |
|
152 |
# Test 4: get_valuebuilder() method directly |
153 |
my $valuebuilder_name = $valuebuilder_plugin->get_valuebuilder(); |
154 |
is( $valuebuilder_name, 'test_plugin_valuebuilder.pl', 'get_valuebuilder returns expected name' ); |
155 |
|
156 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
157 |
$schema->storage->txn_rollback; |
158 |
}; |
159 |
|
160 |
subtest 'Real FrameworkPlugin integration with valuebuilder plugins' => sub { |
161 |
|
162 |
plan tests => 12; |
163 |
|
164 |
$schema->storage->txn_begin; |
165 |
|
166 |
# Temporarily remove any installed plugins data |
167 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
168 |
|
169 |
my $plugins = Koha::Plugins->new; |
170 |
$plugins->InstallPlugins; |
171 |
|
172 |
# Enable our test valuebuilder plugin |
173 |
my $valuebuilder_plugin = Koha::Plugin::TestValuebuilder->new->enable; |
174 |
ok( $valuebuilder_plugin->is_enabled, 'TestValuebuilder plugin is enabled' ); |
175 |
|
176 |
# Test 1: Verify the plugin provides a valuebuilder |
177 |
my $valuebuilder_name = $valuebuilder_plugin->get_valuebuilder(); |
178 |
is( $valuebuilder_name, 'test_plugin_valuebuilder.pl', 'TestValuebuilder plugin returns correct valuebuilder name' ); |
179 |
|
180 |
# Test 2: Verify get_valuebuilders_installed finds our plugin |
181 |
my @valuebuilders = $plugins->get_valuebuilders_installed(); |
182 |
is( scalar @valuebuilders, 1, 'get_valuebuilders_installed finds one valuebuilder' ); |
183 |
is( $valuebuilders[0]->{name}, $valuebuilder_name, 'Found valuebuilder has correct name' ); |
184 |
isa_ok( $valuebuilders[0]->{plugin}, 'Koha::Plugin::TestValuebuilder', 'Found valuebuilder has correct plugin type' ); |
185 |
|
186 |
# Test 3: FrameworkPlugin can load our plugin-based valuebuilder |
187 |
my $framework_plugin = Koha::FrameworkPlugin->new({ name => $valuebuilder_name }); |
188 |
ok( $framework_plugin, 'FrameworkPlugin created successfully with plugin valuebuilder name' ); |
189 |
is( $framework_plugin->name, $valuebuilder_name, 'FrameworkPlugin has correct name' ); |
190 |
ok( !$framework_plugin->errstr, 'FrameworkPlugin has no errors' ); |
191 |
|
192 |
# Test 4: FrameworkPlugin can build JavaScript from our plugin |
193 |
my $build_result = $framework_plugin->build({ id => 'test_field_123' }); |
194 |
ok( $build_result, 'FrameworkPlugin build succeeds with real plugin valuebuilder' ); |
195 |
|
196 |
my $javascript = $framework_plugin->javascript; |
197 |
ok( $javascript, 'FrameworkPlugin generates JavaScript from plugin' ); |
198 |
like( $javascript, qr/test_focus_test_field_123/, 'Generated JavaScript contains expected function for field ID' ); |
199 |
like( $javascript, qr/test_click_test_field_123/, 'Generated JavaScript contains expected click function for field ID' ); |
200 |
|
201 |
Koha::Plugins->RemovePlugins( { destructive => 1 } ); |
202 |
$schema->storage->txn_rollback; |
203 |
}; |