Bug 21670: Add debug to Plugins.t
[srvgit] / t / db_dependent / Koha / Plugins / Plugins.t
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 Archive::Extract;
20 use CGI;
21 use Cwd qw(abs_path);
22 use File::Basename;
23 use File::Spec;
24 use File::Temp qw( tempdir tempfile );
25 use FindBin qw($Bin);
26 use Module::Load::Conditional qw(can_load);
27 use Test::MockModule;
28 use Test::More tests => 55;
29
30 use C4::Context;
31 use Koha::Database;
32 use Koha::Plugins::Methods;
33
34 use t::lib::Mocks;
35
36 BEGIN {
37     # Mock pluginsdir before loading Plugins module
38     my $path = dirname(__FILE__) . '/../../../lib';
39     t::lib::Mocks::mock_config( 'pluginsdir', $path );
40
41     use_ok('Koha::Plugins');
42     use_ok('Koha::Plugins::Handler');
43     use_ok('Koha::Plugins::Base');
44     use_ok('Koha::Plugin::Test');
45 }
46
47 my $schema = Koha::Database->new->schema;
48
49 subtest 'call() tests' => sub {
50     plan tests => 3;
51
52     $schema->storage->txn_begin;
53     # Temporarily remove any installed plugins data
54     Koha::Plugins::Methods->delete;
55
56     t::lib::Mocks::mock_config('enable_plugins', 1);
57     my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
58     my @plugins = $plugins->InstallPlugins;
59     foreach my $plugin (@plugins) {
60         $plugin->enable();
61     }
62
63     my @responses = Koha::Plugins->call('check_password', { password => 'foo' });
64
65     my $expected = [ { error => 1, msg => 'PIN should be four digits' } ];
66     is_deeply(\@responses, $expected, 'call() should return all responses from plugins');
67
68     # Make sure parameters are correctly passed to the plugin method
69     @responses = Koha::Plugins->call('check_password', { password => '1234' });
70
71     $expected = [ { error => 0 } ];
72     is_deeply(\@responses, $expected, 'call() should return all responses from plugins');
73
74     t::lib::Mocks::mock_config('enable_plugins', 0);
75     @responses = Koha::Plugins->call('check_password', { password => '1234' });
76     is_deeply(\@responses, [], 'call() should return an empty array if plugins are disabled');
77
78     $schema->storage->txn_rollback;
79 };
80
81 subtest 'GetPlugins() tests' => sub {
82
83     plan tests => 2;
84
85     $schema->storage->txn_begin;
86     # Temporarily remove any installed plugins data
87     Koha::Plugins::Methods->delete;
88
89     my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
90     $plugins->InstallPlugins;
91
92     my @plugins = $plugins->GetPlugins({ method => 'report', all => 1 });
93
94     my @names = map { $_->get_metadata()->{'name'} } @plugins;
95     is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
96
97     @plugins = $plugins->GetPlugins({ metadata => { my_example_tag  => 'find_me' }, all => 1 });
98     @names = map { $_->get_metadata()->{'name'} } @plugins;
99     is( scalar @names, 2, "Only two plugins found via a metadata tag" );
100
101     $schema->storage->txn_rollback;
102 };
103
104 subtest 'Version upgrade tests' => sub {
105
106     plan tests => 1;
107
108     $schema->storage->txn_begin;
109
110     my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
111
112     # make sure there's no version on the DB
113     $schema->resultset('PluginData')
114         ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
115         ->delete;
116
117     $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
118     my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
119
120     is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
121
122     $schema->storage->txn_rollback;
123 };
124
125 subtest 'is_enabled() tests' => sub {
126
127     plan tests => 3;
128     $schema->storage->txn_begin;
129
130     # Make sure there's no previous installs or leftovers on DB
131     Koha::Plugins::Methods->delete;
132     $schema->resultset('PluginData')->delete;
133
134     my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
135     ok( $plugin->is_enabled, 'Plugins enabled by default' );
136
137     # disable
138     $plugin->disable;
139     ok( !$plugin->is_enabled, 'Calling ->disable disables the plugin' );
140
141     # enable
142     $plugin->enable;
143     ok( $plugin->is_enabled, 'Calling ->enable enabled the plugin' );
144
145     $schema->storage->txn_rollback;
146 };
147
148 $schema->storage->txn_begin;
149 Koha::Plugins::Methods->delete;
150
151 Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
152
153 ok( Koha::Plugins::Methods->search( { plugin_class => 'Koha::Plugin::Test' } )->count, 'Test plugin methods added to database' );
154 is( Koha::Plugins::Methods->search({ plugin_class => 'Koha::Plugin::Test', plugin_method => '_private_sub' })->count, 0, 'Private methods are skipped' );
155
156 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
157 $mock_plugin->mock( 'test_template', sub {
158     my ( $self, $file ) = @_;
159     my $template = $self->get_template({ file => $file });
160     $template->param( filename => $file );
161     return $template->output;
162 });
163
164 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
165
166 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
167
168 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
169 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
170
171 ok( $plugin->can('report'), 'Test plugin can report' );
172 ok( $plugin->can('tool'), 'Test plugin can tool' );
173 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
174 ok( $plugin->can('intranet_catalog_biblio_enhancements'), 'Test plugin can intranet_catalog_biblio_enhancements');
175 ok( $plugin->can('intranet_catalog_biblio_enhancements_toolbar_button'), 'Test plugin can intranet_catalog_biblio_enhancements_toolbar_button' );
176 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
177 ok( $plugin->can('after_hold_create'), 'Test plugin can after_hold_create' );
178 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
179 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
180 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' );
181 ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
182 ok( $plugin->can('intranet_head'), 'Test plugin can intranet_head' );
183 ok( $plugin->can('intranet_js'), 'Test plugin can intranet_js' );
184 ok( $plugin->can('configure'), 'Test plugin can configure' );
185 ok( $plugin->can('install'), 'Test plugin can install' );
186 ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
187 ok( $plugin->can('uninstall'), 'Test plugin can install' );
188
189 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
190
191 my $metadata = $plugin->get_metadata();
192 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
193
194 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
195 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
196
197 # test absolute path change in get_template with Koha::Plugin::Test
198 # using the mock set before
199 # we also add tmpdir as an approved template dir
200 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
201 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context->temporary_directory );
202 print $fh 'I am [% filename %]';
203 close $fh;
204 my $classname = ref($plugin);
205 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
206
207 my $result = $plugin->enable;
208 is( ref($result), 'Koha::Plugin::Test' );
209
210 # testing GetPlugins
211 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
212     method => 'report'
213 });
214
215 my @names = map { $_->get_metadata()->{'name'} } @plugins;
216 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
217 @plugins =  Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
218     metadata => { my_example_tag  => 'find_me' },
219 });
220
221 @names = map { $_->get_metadata()->{'name'} } @plugins;
222 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
223
224 $result = $plugin->disable;
225 is( ref($result), 'Koha::Plugin::Test' );
226
227 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins();
228 @names = map { $_->get_metadata()->{'name'} } @plugins;
229 is( scalar grep( /^Test Plugin$/, @names), 0, "GetPlugins does not found disabled Test Plugin" );
230
231 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ all => 1 });
232 @names = map { $_->get_metadata()->{'name'} } @plugins;
233 is( scalar grep( /^Test Plugin$/, @names), 1, "With all param, GetPlugins found disabled Test Plugin" );
234
235 for my $pass ( 1 .. 2 ) {
236     my $plugins_dir;
237     my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
238     my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
239     if ( $pass == 1 ) {
240         my $plugins_dir1 = tempdir( CLEANUP => 1 );
241         t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
242         $plugins_dir = $plugins_dir1;
243         push @INC, $plugins_dir1;
244     } else {
245         my $plugins_dir1 = tempdir( CLEANUP => 1 );
246         my $plugins_dir2 = tempdir( CLEANUP => 1 );
247         t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
248         $plugins_dir = $plugins_dir2;
249         pop @INC;
250         push @INC, $plugins_dir2;
251         push @INC, $plugins_dir1;
252     }
253     my $full_pm_path = $plugins_dir . '/' . $pm_path;
254
255     my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
256     unless ( $ae->extract( to => $plugins_dir ) ) {
257         warn "ERROR: " . $ae->error;
258     }
259     use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
260     $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
261     my $table = $plugin->get_qualified_table_name( 'mytable' );
262
263     ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
264     $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"
265     Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
266     ok( -f $full_pm_path, "Koha::Plugins::Handler::delete works correctly (pass $pass)" );
267     Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
268     my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
269     my $info = $sth->fetchall_arrayref;
270     is( @$info, 0, "Table $table does no longer exist" );
271     ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly (pass $pass)" );
272 }
273
274 subtest 'output and output_html tests' => sub {
275
276     plan tests => 6;
277
278     # Trick stdout to be able to test
279     local *STDOUT;
280     my $stdout;
281     open STDOUT, '>', \$stdout;
282
283     my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
284     $plugin->test_output;
285
286     like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
287     like($stdout, qr{Content-Type: application/json; charset=UTF-8}, 'Correct content-type');
288     like($stdout, qr{¡Hola output!}, 'Correct data');
289
290     # reset the stdout buffer
291     $stdout = '';
292     close STDOUT;
293     open STDOUT, '>', \$stdout;
294
295     $plugin->test_output_html;
296
297     like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
298     like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
299     like($stdout, qr{¡Hola output_html!}, 'Correct data');
300 };
301
302 subtest 'Test _version_compare' => sub {
303
304     plan tests => 12;
305
306     t::lib::Mocks::mock_config( 'enable_plugins', 1 );
307
308     is( Koha::Plugins::Base::_version_compare( '1.1.1',    '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
309     is( Koha::Plugins::Base::_version_compare( '2.2.2',    '1.1.1' ),  1, "1.1.1 is greater then 2.2.2" );
310     is( Koha::Plugins::Base::_version_compare( '1.1.1',    '1.1.1' ),  0, "1.1.1 is equal to 1.1.1" );
311     is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ),  0, "1.01.001 is equal to 1.1.1" );
312     is( Koha::Plugins::Base::_version_compare( '1',        '1.0.0' ),  0, "1 is equal to 1.0.0" );
313     is( Koha::Plugins::Base::_version_compare( '1.0',      '1.0.0' ),  0, "1.0 is equal to 1.0.0" );
314
315     # OO tests
316     my $plugin = Koha::Plugin::Test->new;
317     is( $plugin->_version_compare( '1.1.1',    '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
318     is( $plugin->_version_compare( '2.2.2',    '1.1.1' ),  1, "1.1.1 is greater then 2.2.2" );
319     is( $plugin->_version_compare( '1.1.1',    '1.1.1' ),  0, "1.1.1 is equal to 1.1.1" );
320     is( $plugin->_version_compare( '1.01.001', '1.1.1' ),  0, "1.01.001 is equal to 1.1.1" );
321     is( $plugin->_version_compare( '1',        '1.0.0' ),  0, "1 is equal to 1.0.0" );
322     is( $plugin->_version_compare( '1.0',      '1.0.0' ),  0, "1.0 is equal to 1.0.0" );
323 };
324
325 subtest 'bundle_path() tests' => sub {
326
327     plan tests => 1;
328
329     t::lib::Mocks::mock_config( 'enable_plugins', 1 );
330
331     my @current_dir = File::Spec->splitdir(abs_path(__FILE__));
332     # remote Plugins.t
333     pop @current_dir;
334     # remove /Plugins
335     pop @current_dir;
336     # remove /Koha
337     pop @current_dir;
338     # remove db_dependent
339     pop @current_dir;
340
341     my $plugin = Koha::Plugin::Test->new;
342
343     is( $plugin->bundle_path, File::Spec->catdir(@current_dir) . '/lib/Koha/Plugin/Test' );
344
345 };
346
347 subtest 'new() tests' => sub {
348
349     plan tests => 2;
350
351     t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
352     t::lib::Mocks::mock_config( 'enable_plugins', 0 );
353
354     my $result = Koha::Plugins->new();
355     is( $result, undef, 'calling new() on disabled plugins returns undef' );
356
357     $result = Koha::Plugins->new({ enable_plugins => 1 });
358     is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );
359 };
360
361 Koha::Plugins::Methods->delete;