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>.
22 use File::Temp qw( tempdir tempfile );
24 use Module::Load::Conditional qw(can_load);
26 use Test::More tests => 47;
30 use Koha::Plugins::Methods;
35 # Mock pluginsdir before loading Plugins module
36 my $path = dirname(__FILE__) . '/../lib';
37 t::lib::Mocks::mock_config( 'pluginsdir', $path );
39 use_ok('Koha::Plugins');
40 use_ok('Koha::Plugins::Handler');
41 use_ok('Koha::Plugins::Base');
42 use_ok('Koha::Plugin::Test');
45 my $schema = Koha::Database->new->schema;
47 subtest 'GetPlugins() tests' => sub {
51 $schema->storage->txn_begin;
52 # Temporarily remove any installed plugins data
53 Koha::Plugins::Methods->delete;
55 my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
56 $plugins->InstallPlugins;
58 my @plugins = $plugins->GetPlugins({ method => 'report' });
60 my @names = map { $_->get_metadata()->{'name'} } @plugins;
61 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
63 @plugins = $plugins->GetPlugins({ metadata => { my_example_tag => 'find_me' } });
64 @names = map { $_->get_metadata()->{'name'} } @plugins;
65 is( scalar @names, 2, "Only two plugins found via a metadata tag" );
67 $schema->storage->txn_rollback;
70 subtest 'Version upgrade tests' => sub {
74 $schema->storage->txn_begin;
76 my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
78 # make sure there's no version on the DB
79 $schema->resultset('PluginData')
80 ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
83 $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
84 my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
86 is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
88 $schema->storage->txn_rollback;
91 $schema->storage->txn_begin;
92 Koha::Plugins::Methods->delete;
94 Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
96 ok( Koha::Plugins::Methods->search( { plugin_class => 'Koha::Plugin::Test' } )->count, 'Test plugin methods added to database' );
98 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
99 $mock_plugin->mock( 'test_template', sub {
100 my ( $self, $file ) = @_;
101 my $template = $self->get_template({ file => $file });
102 $template->param( filename => $file );
103 return $template->output;
106 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
108 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
110 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
111 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
113 ok( $plugin->can('report'), 'Test plugin can report' );
114 ok( $plugin->can('tool'), 'Test plugin can tool' );
115 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
116 ok( $plugin->can('intranet_catalog_biblio_enhancements'), 'Test plugin can intranet_catalog_biblio_enhancements');
117 ok( $plugin->can('intranet_catalog_biblio_enhancements_toolbar_button'), 'Test plugin can intranet_catalog_biblio_enhancements_toolbar_button' );
118 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
119 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
120 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
121 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' );
122 ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
123 ok( $plugin->can('intranet_head'), 'Test plugin can intranet_head' );
124 ok( $plugin->can('intranet_js'), 'Test plugin can intranet_js' );
125 ok( $plugin->can('configure'), 'Test plugin can configure' );
126 ok( $plugin->can('install'), 'Test plugin can install' );
127 ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
128 ok( $plugin->can('uninstall'), 'Test plugin can install' );
130 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
132 my $metadata = $plugin->get_metadata();
133 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
135 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
136 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
138 # test absolute path change in get_template with Koha::Plugin::Test
139 # using the mock set before
140 # we also add tmpdir as an approved template dir
141 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
142 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context->temporary_directory );
143 print $fh 'I am [% filename %]';
145 my $classname = ref($plugin);
146 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
148 my $result = $plugin->enable;
149 is( ref($result), 'Koha::Plugin::Test' );
152 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
156 my @names = map { $_->get_metadata()->{'name'} } @plugins;
157 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
158 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
159 metadata => { my_example_tag => 'find_me' },
162 @names = map { $_->get_metadata()->{'name'} } @plugins;
163 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
165 $result = $plugin->disable;
166 is( ref($result), 'Koha::Plugin::Test' );
168 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins();
169 @names = map { $_->get_metadata()->{'name'} } @plugins;
170 is( scalar grep( /^Test Plugin$/, @names), 0, "GetPlugins does not found disabled Test Plugin" );
172 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ all => 1 });
173 @names = map { $_->get_metadata()->{'name'} } @plugins;
174 is( scalar grep( /^Test Plugin$/, @names), 1, "With all param, GetPlugins found disabled Test Plugin" );
176 for my $pass ( 1 .. 2 ) {
178 my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
179 my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
181 my $plugins_dir1 = tempdir( CLEANUP => 1 );
182 t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
183 $plugins_dir = $plugins_dir1;
184 push @INC, $plugins_dir1;
186 my $plugins_dir1 = tempdir( CLEANUP => 1 );
187 my $plugins_dir2 = tempdir( CLEANUP => 1 );
188 t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
189 $plugins_dir = $plugins_dir2;
191 push @INC, $plugins_dir2;
192 push @INC, $plugins_dir1;
194 my $full_pm_path = $plugins_dir . '/' . $pm_path;
196 my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
197 unless ( $ae->extract( to => $plugins_dir ) ) {
198 warn "ERROR: " . $ae->error;
200 use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
201 $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
202 my $table = $plugin->get_qualified_table_name( 'mytable' );
204 ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
205 $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"
206 Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
207 Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
208 my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
209 my $info = $sth->fetchall_arrayref;
210 is( @$info, 0, "Table $table does no longer exist" );
211 ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
214 subtest 'output and output_html tests' => sub {
218 # Trick stdout to be able to test
221 open STDOUT, '>', \$stdout;
223 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
224 $plugin->test_output;
226 like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
227 like($stdout, qr{Content-Type: application/json; charset=UTF-8}, 'Correct content-type');
228 like($stdout, qr{¡Hola output!}, 'Correct data');
230 # reset the stdout buffer
233 open STDOUT, '>', \$stdout;
235 $plugin->test_output_html;
237 like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
238 like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
239 like($stdout, qr{¡Hola output_html!}, 'Correct data');
242 subtest 'Version upgrade tests' => sub {
246 $schema->storage->txn_begin;
248 my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
250 # make sure there's no version on the DB
251 $schema->resultset('PluginData')
252 ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
255 $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
256 my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
258 is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
260 $schema->storage->txn_rollback;
264 subtest 'Test _version_compare' => sub {
268 is( Koha::Plugins::Base::_version_compare( '1.1.1', '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
269 is( Koha::Plugins::Base::_version_compare( '2.2.2', '1.1.1' ), 1, "1.1.1 is greater then 2.2.2" );
270 is( Koha::Plugins::Base::_version_compare( '1.1.1', '1.1.1' ), 0, "1.1.1 is equal to 1.1.1" );
271 is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ), 0, "1.01.001 is equal to 1.1.1" );
272 is( Koha::Plugins::Base::_version_compare( '1', '1.0.0' ), 0, "1 is equal to 1.0.0" );
273 is( Koha::Plugins::Base::_version_compare( '1.0', '1.0.0' ), 0, "1.0 is equal to 1.0.0" );
276 subtest 'new() tests' => sub {
280 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
281 t::lib::Mocks::mock_config( 'enable_plugins', 0 );
283 my $result = Koha::Plugins->new();
284 is( $result, undef, 'calling new() on disabled plugins returns undef' );
286 $result = Koha::Plugins->new({ enable_plugins => 1 });
287 is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );