3 # This file is part of Koha.
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
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.
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>.
24 use File::Temp qw( tempdir tempfile );
26 use Module::Load::Conditional qw(can_load);
28 use Test::More tests => 61;
33 use Koha::Plugins::Methods;
38 # Mock pluginsdir before loading Plugins module
39 my $path = dirname(__FILE__) . '/../../../lib/plugins';
40 t::lib::Mocks::mock_config( 'pluginsdir', $path );
42 use_ok('Koha::Plugins');
43 use_ok('Koha::Plugins::Handler');
44 use_ok('Koha::Plugins::Base');
45 use_ok('Koha::Plugin::Test');
46 use_ok('Koha::Plugin::TestItemBarcodeTransform');
49 my $schema = Koha::Database->new->schema;
51 subtest 'call() tests' => sub {
55 $schema->storage->txn_begin;
56 # Temporarily remove any installed plugins data
57 Koha::Plugins::Methods->delete;
58 $schema->resultset('PluginData')->delete();
60 t::lib::Mocks::mock_config('enable_plugins', 1);
61 my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
65 warning_is { @plugins = $plugins->InstallPlugins; } undef;
67 foreach my $plugin (@plugins) {
71 my @responses = Koha::Plugins->call('check_password', { password => 'foo' });
73 my $expected = [ { error => 1, msg => 'PIN should be four digits' } ];
74 is_deeply(\@responses, $expected, 'call() should return all responses from plugins');
76 # Make sure parameters are correctly passed to the plugin method
77 @responses = Koha::Plugins->call('check_password', { password => '1234' });
79 $expected = [ { error => 0 } ];
80 is_deeply(\@responses, $expected, 'call() should return all responses from plugins');
82 t::lib::Mocks::mock_config('enable_plugins', 0);
83 @responses = Koha::Plugins->call('check_password', { password => '1234' });
84 is_deeply(\@responses, [], 'call() should return an empty array if plugins are disabled');
86 $schema->storage->txn_rollback;
89 subtest 'more call() tests' => sub {
93 $schema->storage->txn_begin;
94 # Temporarily remove any installed plugins data
95 Koha::Plugins::Methods->delete;
96 $schema->resultset('PluginData')->delete();
98 t::lib::Mocks::mock_config('enable_plugins', 1);
99 my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
102 warning_is { @plugins = $plugins->InstallPlugins; } undef;
104 foreach my $plugin (@plugins) {
108 # Barcode is multiplied by 2 by Koha::Plugin::Test, and again by 4 by Koha::Plugin::TestItemBarcodeTransform
109 # showing that call has passed the same ref to multiple plugins to operate on
112 { Koha::Plugins->call('item_barcode_transform', \$bc); }
113 [ qq{Plugin error (Test Plugin): Exception 'Koha::Exception' thrown 'item_barcode_transform called with parameter: 1'\n},
114 qq{Plugin error (Test Plugin for item_barcode_transform): Exception 'Koha::Exception' thrown 'item_barcode_transform called with parameter: 2'\n} ];
115 is( $bc, 8, "Got expected response" );
119 { Koha::Plugins->call('item_barcode_transform', \$bc); }
120 [ qq{Plugin error (Test Plugin): Exception 'Koha::Exception' thrown 'item_barcode_transform called with parameter: 8'\n},
121 qq{Plugin error (Test Plugin for item_barcode_transform): Exception 'Koha::Exception' thrown 'item_barcode_transform called with parameter: 16'\n} ];
122 is( $cn, 'abcd', "Got expected response" );
124 t::lib::Mocks::mock_config('enable_plugins', 0);
126 Koha::Plugins->call('item_barcode_transform', \$bc);
127 is( $bc, 1, "call should return the original arguments if plugins are disabled" );
129 $schema->storage->txn_rollback;
132 subtest 'GetPlugins() tests' => sub {
136 $schema->storage->txn_begin;
137 # Temporarily remove any installed plugins data
138 Koha::Plugins::Methods->delete;
140 my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
142 warning_is { $plugins->InstallPlugins; } undef;
144 my @plugins = $plugins->GetPlugins({ method => 'report', all => 1 });
146 my @names = map { $_->get_metadata()->{'name'} } @plugins;
147 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
149 @plugins = $plugins->GetPlugins({ metadata => { my_example_tag => 'find_me' }, all => 1 });
150 @names = map { $_->get_metadata()->{'name'} } @plugins;
151 is( scalar @names, 2, "Only two plugins found via a metadata tag" );
153 $schema->storage->txn_rollback;
156 subtest 'Version upgrade tests' => sub {
160 $schema->storage->txn_begin;
162 my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
164 # make sure there's no version on the DB
165 $schema->resultset('PluginData')
166 ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
169 $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
170 my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
172 is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
174 $schema->storage->txn_rollback;
177 subtest 'is_enabled() tests' => sub {
180 $schema->storage->txn_begin;
182 # Make sure there's no previous installs or leftovers on DB
183 Koha::Plugins::Methods->delete;
184 $schema->resultset('PluginData')->delete;
186 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
187 ok( $plugin->is_enabled, 'Plugins enabled by default' );
191 ok( !$plugin->is_enabled, 'Calling ->disable disables the plugin' );
195 ok( $plugin->is_enabled, 'Calling ->enable enabled the plugin' );
197 $schema->storage->txn_rollback;
200 $schema->storage->txn_begin;
201 Koha::Plugins::Methods->delete;
202 $schema->resultset('PluginData')->delete;
204 warning_is { Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins(); } undef;
206 ok( Koha::Plugins::Methods->search( { plugin_class => 'Koha::Plugin::Test' } )->count, 'Test plugin methods added to database' );
207 is( Koha::Plugins::Methods->search({ plugin_class => 'Koha::Plugin::Test', plugin_method => '_private_sub' })->count, 0, 'Private methods are skipped' );
209 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
210 $mock_plugin->mock( 'test_template', sub {
211 my ( $self, $file ) = @_;
212 my $template = $self->get_template({ file => $file });
213 $template->param( filename => $file );
214 return $template->output;
217 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
219 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
221 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
222 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
224 ok( $plugin->can('report'), 'Test plugin can report' );
225 ok( $plugin->can('tool'), 'Test plugin can tool' );
226 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
227 ok( $plugin->can('intranet_catalog_biblio_enhancements'), 'Test plugin can intranet_catalog_biblio_enhancements');
228 ok( $plugin->can('intranet_catalog_biblio_enhancements_toolbar_button'), 'Test plugin can intranet_catalog_biblio_enhancements_toolbar_button' );
229 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
230 ok( $plugin->can('after_hold_create'), 'Test plugin can after_hold_create' );
231 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
232 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
233 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' );
234 ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
235 ok( $plugin->can('intranet_head'), 'Test plugin can intranet_head' );
236 ok( $plugin->can('intranet_js'), 'Test plugin can intranet_js' );
237 ok( $plugin->can('item_barcode_transform'), 'Test plugin can barcode_transform' );
238 ok( $plugin->can('configure'), 'Test plugin can configure' );
239 ok( $plugin->can('install'), 'Test plugin can install' );
240 ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
241 ok( $plugin->can('uninstall'), 'Test plugin can install' );
243 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
245 my $metadata = $plugin->get_metadata();
246 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
248 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
249 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
251 # test absolute path change in get_template with Koha::Plugin::Test
252 # using the mock set before
253 # we also add tmpdir as an approved template dir
254 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
255 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context->temporary_directory );
256 print $fh 'I am [% filename %]';
258 my $classname = ref($plugin);
259 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
261 my $result = $plugin->enable;
262 is( ref($result), 'Koha::Plugin::Test' );
265 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
269 my @names = map { $_->get_metadata()->{'name'} } @plugins;
270 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
271 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
272 metadata => { my_example_tag => 'find_me' },
275 @names = map { $_->get_metadata()->{'name'} } @plugins;
276 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
278 $result = $plugin->disable;
279 is( ref($result), 'Koha::Plugin::Test' );
281 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins();
282 @names = map { $_->get_metadata()->{'name'} } @plugins;
283 is( scalar grep( /^Test Plugin$/, @names), 0, "GetPlugins does not found disabled Test Plugin" );
285 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ all => 1 });
286 @names = map { $_->get_metadata()->{'name'} } @plugins;
287 is( scalar grep( /^Test Plugin$/, @names), 1, "With all param, GetPlugins found disabled Test Plugin" );
289 for my $pass ( 1 .. 2 ) {
291 my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
292 my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
294 my $plugins_dir1 = tempdir( CLEANUP => 1 );
295 t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
296 $plugins_dir = $plugins_dir1;
297 push @INC, $plugins_dir1;
299 my $plugins_dir1 = tempdir( CLEANUP => 1 );
300 my $plugins_dir2 = tempdir( CLEANUP => 1 );
301 t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
302 $plugins_dir = $plugins_dir2;
304 push @INC, $plugins_dir2;
305 push @INC, $plugins_dir1;
307 my $full_pm_path = $plugins_dir . '/' . $pm_path;
309 my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
310 unless ( $ae->extract( to => $plugins_dir ) ) {
311 warn "ERROR: " . $ae->error;
313 use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
314 $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
315 my $table = $plugin->get_qualified_table_name( 'mytable' );
317 ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
318 $INC{$pm_path} = $full_pm_path; # FIXME I do not really know why, but if this is moved before the $plugin constructor, it will fail with Can't locate object method "new" via package "Koha::Plugin::Com::ByWaterSolutions::KitchenSink"
319 warning_is { Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins(); } undef;
320 ok( -f $full_pm_path, "Koha::Plugins::Handler::delete works correctly (pass $pass)" );
321 Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
322 my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
323 my $info = $sth->fetchall_arrayref;
324 is( @$info, 0, "Table $table does no longer exist" );
325 ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly (pass $pass)" );
328 subtest 'output and output_html tests' => sub {
332 # Trick stdout to be able to test
335 open STDOUT, '>', \$stdout;
337 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
338 $plugin->test_output;
340 like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
341 like($stdout, qr{Content-Type: application/json; charset=UTF-8}, 'Correct content-type');
342 like($stdout, qr{¡Hola output!}, 'Correct data');
344 # reset the stdout buffer
347 open STDOUT, '>', \$stdout;
349 $plugin->test_output_html;
351 like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
352 like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
353 like($stdout, qr{¡Hola output_html!}, 'Correct data');
356 subtest 'Test _version_compare' => sub {
360 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
362 is( Koha::Plugins::Base::_version_compare( '1.1.1', '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
363 is( Koha::Plugins::Base::_version_compare( '2.2.2', '1.1.1' ), 1, "1.1.1 is greater then 2.2.2" );
364 is( Koha::Plugins::Base::_version_compare( '1.1.1', '1.1.1' ), 0, "1.1.1 is equal to 1.1.1" );
365 is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ), 0, "1.01.001 is equal to 1.1.1" );
366 is( Koha::Plugins::Base::_version_compare( '1', '1.0.0' ), 0, "1 is equal to 1.0.0" );
367 is( Koha::Plugins::Base::_version_compare( '1.0', '1.0.0' ), 0, "1.0 is equal to 1.0.0" );
370 my $plugin = Koha::Plugin::Test->new;
371 is( $plugin->_version_compare( '1.1.1', '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
372 is( $plugin->_version_compare( '2.2.2', '1.1.1' ), 1, "1.1.1 is greater then 2.2.2" );
373 is( $plugin->_version_compare( '1.1.1', '1.1.1' ), 0, "1.1.1 is equal to 1.1.1" );
374 is( $plugin->_version_compare( '1.01.001', '1.1.1' ), 0, "1.01.001 is equal to 1.1.1" );
375 is( $plugin->_version_compare( '1', '1.0.0' ), 0, "1 is equal to 1.0.0" );
376 is( $plugin->_version_compare( '1.0', '1.0.0' ), 0, "1.0 is equal to 1.0.0" );
379 subtest 'bundle_path() tests' => sub {
383 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
385 my @current_dir = File::Spec->splitdir(abs_path(__FILE__));
392 # remove db_dependent
395 my $plugin = Koha::Plugin::Test->new;
397 is( $plugin->bundle_path, File::Spec->catdir(@current_dir) . '/lib/plugins/Koha/Plugin/Test' );
401 subtest 'new() tests' => sub {
405 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
406 t::lib::Mocks::mock_config( 'enable_plugins', 0 );
408 my $result = Koha::Plugins->new();
409 is( $result, undef, 'calling new() on disabled plugins returns undef' );
411 $result = Koha::Plugins->new({ enable_plugins => 1 });
412 is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );
415 Koha::Plugins::Methods->delete;