Bug 21073: (QA follow-up) Only public subs should be considered
[koha.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 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 => 50;
27
28 use C4::Context;
29 use Koha::Database;
30 use Koha::Plugins::Methods;
31
32 use t::lib::Mocks;
33
34 BEGIN {
35     # Mock pluginsdir before loading Plugins module
36     my $path = dirname(__FILE__) . '/../lib';
37     t::lib::Mocks::mock_config( 'pluginsdir', $path );
38
39     use_ok('Koha::Plugins');
40     use_ok('Koha::Plugins::Handler');
41     use_ok('Koha::Plugins::Base');
42     use_ok('Koha::Plugin::Test');
43 }
44
45 my $schema = Koha::Database->new->schema;
46
47 subtest 'GetPlugins() tests' => sub {
48
49     plan tests => 2;
50
51     $schema->storage->txn_begin;
52     # Temporarily remove any installed plugins data
53     Koha::Plugins::Methods->delete;
54
55     my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
56     $plugins->InstallPlugins;
57
58     my @plugins = $plugins->GetPlugins({ method => 'report', all => 1 });
59
60     my @names = map { $_->get_metadata()->{'name'} } @plugins;
61     is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
62
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" );
66
67     $schema->storage->txn_rollback;
68 };
69
70 subtest 'Version upgrade tests' => sub {
71
72     plan tests => 1;
73
74     $schema->storage->txn_begin;
75
76     my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
77
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;
82
83     $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
84     my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
85
86     is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
87
88     $schema->storage->txn_rollback;
89 };
90
91 subtest 'Version upgrade tests' => sub {
92
93     plan tests => 1;
94
95     $schema->storage->txn_begin;
96
97     my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
98
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;
103
104     $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
105     my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
106
107     is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
108
109     $schema->storage->txn_rollback;
110 };
111
112 $schema->storage->txn_begin;
113 Koha::Plugins::Methods->delete;
114
115 Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
116
117 ok( Koha::Plugins::Methods->search( { plugin_class => 'Koha::Plugin::Test' } )->count, 'Test plugin methods added to database' );
118 is( Koha::Plugins::Methods->search({ plugin_class => 'Koha::Plugin::Test', plugin_method => '_private_sub' })->count, 0, 'Private methods are skipped' );
119
120 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
121 $mock_plugin->mock( 'test_template', sub {
122     my ( $self, $file ) = @_;
123     my $template = $self->get_template({ file => $file });
124     $template->param( filename => $file );
125     return $template->output;
126 });
127
128 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
129
130 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
131
132 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
133 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
134
135 ok( $plugin->can('report'), 'Test plugin can report' );
136 ok( $plugin->can('tool'), 'Test plugin can tool' );
137 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
138 ok( $plugin->can('intranet_catalog_biblio_enhancements'), 'Test plugin can intranet_catalog_biblio_enhancements');
139 ok( $plugin->can('intranet_catalog_biblio_enhancements_toolbar_button'), 'Test plugin can intranet_catalog_biblio_enhancements_toolbar_button' );
140 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
141 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
142 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
143 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' );
144 ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
145 ok( $plugin->can('intranet_head'), 'Test plugin can intranet_head' );
146 ok( $plugin->can('intranet_js'), 'Test plugin can intranet_js' );
147 ok( $plugin->can('configure'), 'Test plugin can configure' );
148 ok( $plugin->can('install'), 'Test plugin can install' );
149 ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
150 ok( $plugin->can('uninstall'), 'Test plugin can install' );
151
152 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
153
154 my $metadata = $plugin->get_metadata();
155 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
156
157 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
158 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
159
160 # test absolute path change in get_template with Koha::Plugin::Test
161 # using the mock set before
162 # we also add tmpdir as an approved template dir
163 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
164 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context->temporary_directory );
165 print $fh 'I am [% filename %]';
166 close $fh;
167 my $classname = ref($plugin);
168 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
169
170 my $result = $plugin->enable;
171 is( ref($result), 'Koha::Plugin::Test' );
172
173 # testing GetPlugins
174 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
175     method => 'report'
176 });
177
178 my @names = map { $_->get_metadata()->{'name'} } @plugins;
179 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
180 @plugins =  Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
181     metadata => { my_example_tag  => 'find_me' },
182 });
183
184 @names = map { $_->get_metadata()->{'name'} } @plugins;
185 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
186
187 $result = $plugin->disable;
188 is( ref($result), 'Koha::Plugin::Test' );
189
190 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins();
191 @names = map { $_->get_metadata()->{'name'} } @plugins;
192 is( scalar grep( /^Test Plugin$/, @names), 0, "GetPlugins does not found disabled Test Plugin" );
193
194 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ all => 1 });
195 @names = map { $_->get_metadata()->{'name'} } @plugins;
196 is( scalar grep( /^Test Plugin$/, @names), 1, "With all param, GetPlugins found disabled Test Plugin" );
197
198 for my $pass ( 1 .. 2 ) {
199     my $plugins_dir;
200     my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
201     my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
202     if ( $pass == 1 ) {
203         my $plugins_dir1 = tempdir( CLEANUP => 1 );
204         t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
205         $plugins_dir = $plugins_dir1;
206         push @INC, $plugins_dir1;
207     } else {
208         my $plugins_dir1 = tempdir( CLEANUP => 1 );
209         my $plugins_dir2 = tempdir( CLEANUP => 1 );
210         t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
211         $plugins_dir = $plugins_dir2;
212         pop @INC;
213         push @INC, $plugins_dir2;
214         push @INC, $plugins_dir1;
215     }
216     my $full_pm_path = $plugins_dir . '/' . $pm_path;
217
218     my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
219     unless ( $ae->extract( to => $plugins_dir ) ) {
220         warn "ERROR: " . $ae->error;
221     }
222     use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
223     $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
224     my $table = $plugin->get_qualified_table_name( 'mytable' );
225
226     ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
227     $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"
228     Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
229     Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
230     my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
231     my $info = $sth->fetchall_arrayref;
232     is( @$info, 0, "Table $table does no longer exist" );
233     ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
234 }
235
236 subtest 'output and output_html tests' => sub {
237
238     plan tests => 6;
239
240     # Trick stdout to be able to test
241     local *STDOUT;
242     my $stdout;
243     open STDOUT, '>', \$stdout;
244
245     my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
246     $plugin->test_output;
247
248     like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
249     like($stdout, qr{Content-Type: application/json; charset=UTF-8}, 'Correct content-type');
250     like($stdout, qr{¡Hola output!}, 'Correct data');
251
252     # reset the stdout buffer
253     $stdout = '';
254     close STDOUT;
255     open STDOUT, '>', \$stdout;
256
257     $plugin->test_output_html;
258
259     like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
260     like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
261     like($stdout, qr{¡Hola output_html!}, 'Correct data');
262 };
263
264 subtest 'Test _version_compare' => sub {
265
266     plan tests => 6;
267
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" );
274 };
275
276 subtest 'new() tests' => sub {
277
278     plan tests => 2;
279
280     t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
281     t::lib::Mocks::mock_config( 'enable_plugins', 0 );
282
283     my $result = Koha::Plugins->new();
284     is( $result, undef, 'calling new() on disabled plugins returns undef' );
285
286     $result = Koha::Plugins->new({ enable_plugins => 1 });
287     is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );
288 };
289
290 $schema->storage->txn_rollback;