Bug 21073: (follow-up) QA fixes after rebase
[koha.git] / t / db_dependent / Plugins.t
blobc72f27d683af42f4b4faca7f35073bc489b6e3a2
1 #!/usr/bin/perl
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
8 # version.
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>.
17 use Modern::Perl;
19 use Archive::Extract;
20 use CGI;
21 use File::Basename;
22 use File::Temp qw( tempdir tempfile );
23 use FindBin qw($Bin);
24 use Module::Load::Conditional qw(can_load);
25 use Test::MockModule;
26 use Test::More tests => 49;
28 use C4::Context;
29 use Koha::Database;
30 use Koha::Plugins::Methods;
32 use t::lib::Mocks;
34 BEGIN {
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 {
49 plan tests => 2;
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', all => 1 });
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' }, all => 1 });
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 {
72 plan tests => 1;
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__' } )
81 ->delete;
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 subtest 'Version upgrade tests' => sub {
93 plan tests => 1;
95 $schema->storage->txn_begin;
97 my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
99 # make sure there's no version on the DB
100 $schema->resultset('PluginData')
101 ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
102 ->delete;
104 $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
105 my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
107 is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
109 $schema->storage->txn_rollback;
112 $schema->storage->txn_begin;
113 Koha::Plugins::Methods->delete;
115 Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
117 ok( Koha::Plugins::Methods->search( { plugin_class => 'Koha::Plugin::Test' } )->count, 'Test plugin methods added to database' );
119 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
120 $mock_plugin->mock( 'test_template', sub {
121 my ( $self, $file ) = @_;
122 my $template = $self->get_template({ file => $file });
123 $template->param( filename => $file );
124 return $template->output;
127 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
129 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
131 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
132 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
134 ok( $plugin->can('report'), 'Test plugin can report' );
135 ok( $plugin->can('tool'), 'Test plugin can tool' );
136 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
137 ok( $plugin->can('intranet_catalog_biblio_enhancements'), 'Test plugin can intranet_catalog_biblio_enhancements');
138 ok( $plugin->can('intranet_catalog_biblio_enhancements_toolbar_button'), 'Test plugin can intranet_catalog_biblio_enhancements_toolbar_button' );
139 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
140 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
141 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
142 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' );
143 ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
144 ok( $plugin->can('intranet_head'), 'Test plugin can intranet_head' );
145 ok( $plugin->can('intranet_js'), 'Test plugin can intranet_js' );
146 ok( $plugin->can('configure'), 'Test plugin can configure' );
147 ok( $plugin->can('install'), 'Test plugin can install' );
148 ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
149 ok( $plugin->can('uninstall'), 'Test plugin can install' );
151 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
153 my $metadata = $plugin->get_metadata();
154 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
156 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
157 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
159 # test absolute path change in get_template with Koha::Plugin::Test
160 # using the mock set before
161 # we also add tmpdir as an approved template dir
162 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
163 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context->temporary_directory );
164 print $fh 'I am [% filename %]';
165 close $fh;
166 my $classname = ref($plugin);
167 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
169 my $result = $plugin->enable;
170 is( ref($result), 'Koha::Plugin::Test' );
172 # testing GetPlugins
173 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
174 method => 'report'
177 my @names = map { $_->get_metadata()->{'name'} } @plugins;
178 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
179 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
180 metadata => { my_example_tag => 'find_me' },
183 @names = map { $_->get_metadata()->{'name'} } @plugins;
184 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
186 $result = $plugin->disable;
187 is( ref($result), 'Koha::Plugin::Test' );
189 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins();
190 @names = map { $_->get_metadata()->{'name'} } @plugins;
191 is( scalar grep( /^Test Plugin$/, @names), 0, "GetPlugins does not found disabled Test Plugin" );
193 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ all => 1 });
194 @names = map { $_->get_metadata()->{'name'} } @plugins;
195 is( scalar grep( /^Test Plugin$/, @names), 1, "With all param, GetPlugins found disabled Test Plugin" );
197 for my $pass ( 1 .. 2 ) {
198 my $plugins_dir;
199 my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
200 my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
201 if ( $pass == 1 ) {
202 my $plugins_dir1 = tempdir( CLEANUP => 1 );
203 t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
204 $plugins_dir = $plugins_dir1;
205 push @INC, $plugins_dir1;
206 } else {
207 my $plugins_dir1 = tempdir( CLEANUP => 1 );
208 my $plugins_dir2 = tempdir( CLEANUP => 1 );
209 t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
210 $plugins_dir = $plugins_dir2;
211 pop @INC;
212 push @INC, $plugins_dir2;
213 push @INC, $plugins_dir1;
215 my $full_pm_path = $plugins_dir . '/' . $pm_path;
217 my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
218 unless ( $ae->extract( to => $plugins_dir ) ) {
219 warn "ERROR: " . $ae->error;
221 use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
222 $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
223 my $table = $plugin->get_qualified_table_name( 'mytable' );
225 ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
226 $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"
227 Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
228 Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
229 my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
230 my $info = $sth->fetchall_arrayref;
231 is( @$info, 0, "Table $table does no longer exist" );
232 ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
235 subtest 'output and output_html tests' => sub {
237 plan tests => 6;
239 # Trick stdout to be able to test
240 local *STDOUT;
241 my $stdout;
242 open STDOUT, '>', \$stdout;
244 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
245 $plugin->test_output;
247 like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
248 like($stdout, qr{Content-Type: application/json; charset=UTF-8}, 'Correct content-type');
249 like($stdout, qr{¡Hola output!}, 'Correct data');
251 # reset the stdout buffer
252 $stdout = '';
253 close STDOUT;
254 open STDOUT, '>', \$stdout;
256 $plugin->test_output_html;
258 like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
259 like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
260 like($stdout, qr{¡Hola output_html!}, 'Correct data');
263 subtest 'Test _version_compare' => sub {
265 plan tests => 6;
267 is( Koha::Plugins::Base::_version_compare( '1.1.1', '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
268 is( Koha::Plugins::Base::_version_compare( '2.2.2', '1.1.1' ), 1, "1.1.1 is greater then 2.2.2" );
269 is( Koha::Plugins::Base::_version_compare( '1.1.1', '1.1.1' ), 0, "1.1.1 is equal to 1.1.1" );
270 is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ), 0, "1.01.001 is equal to 1.1.1" );
271 is( Koha::Plugins::Base::_version_compare( '1', '1.0.0' ), 0, "1 is equal to 1.0.0" );
272 is( Koha::Plugins::Base::_version_compare( '1.0', '1.0.0' ), 0, "1.0 is equal to 1.0.0" );
275 subtest 'new() tests' => sub {
277 plan tests => 2;
279 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
280 t::lib::Mocks::mock_config( 'enable_plugins', 0 );
282 my $result = Koha::Plugins->new();
283 is( $result, undef, 'calling new() on disabled plugins returns undef' );
285 $result = Koha::Plugins->new({ enable_plugins => 1 });
286 is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );
289 $schema->storage->txn_rollback;