Bug 21073: Regression tests for GetPlugins
[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 => 47;
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' });
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' } });
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 $schema->storage->txn_begin;
92 Koha::Plugins::Methods->delete;
93
94 Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
95
96 ok( Koha::Plugins::Methods->search( { plugin_class => 'Koha::Plugin::Test' } )->count, 'Test plugin methods added to database' );
97
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;
104 });
105
106 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
107
108 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
109
110 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
111 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
112
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' );
129
130 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
131
132 my $metadata = $plugin->get_metadata();
133 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
134
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()' );
137
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 %]';
144 close $fh;
145 my $classname = ref($plugin);
146 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
147
148 my $result = $plugin->enable;
149 is( ref($result), 'Koha::Plugin::Test' );
150
151 # testing GetPlugins
152 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
153     method => 'report'
154 });
155
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' },
160 });
161
162 @names = map { $_->get_metadata()->{'name'} } @plugins;
163 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
164
165 $result = $plugin->disable;
166 is( ref($result), 'Koha::Plugin::Test' );
167
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" );
171
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" );
175
176 for my $pass ( 1 .. 2 ) {
177     my $plugins_dir;
178     my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
179     my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
180     if ( $pass == 1 ) {
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;
185     } else {
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;
190         pop @INC;
191         push @INC, $plugins_dir2;
192         push @INC, $plugins_dir1;
193     }
194     my $full_pm_path = $plugins_dir . '/' . $pm_path;
195
196     my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
197     unless ( $ae->extract( to => $plugins_dir ) ) {
198         warn "ERROR: " . $ae->error;
199     }
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' );
203
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." );
212 }
213
214 subtest 'output and output_html tests' => sub {
215
216     plan tests => 6;
217
218     # Trick stdout to be able to test
219     local *STDOUT;
220     my $stdout;
221     open STDOUT, '>', \$stdout;
222
223     my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
224     $plugin->test_output;
225
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');
229
230     # reset the stdout buffer
231     $stdout = '';
232     close STDOUT;
233     open STDOUT, '>', \$stdout;
234
235     $plugin->test_output_html;
236
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');
240 };
241
242 subtest 'Version upgrade tests' => sub {
243
244     plan tests => 1;
245
246     $schema->storage->txn_begin;
247
248     my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
249
250     # make sure there's no version on the DB
251     $schema->resultset('PluginData')
252         ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
253         ->delete;
254
255     $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
256     my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
257
258     is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
259
260     $schema->storage->txn_rollback;
261 };
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 };