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