Bug 24013: Add tests
[koha.git] / t / db_dependent / Plugins.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Archive::Extract;
6 use CGI;
7 use Cwd qw(abs_path);
8 use File::Basename;
9 use File::Spec;
10 use File::Temp qw( tempdir tempfile );
11 use FindBin qw($Bin);
12 use Module::Load::Conditional qw(can_load);
13 use Test::MockModule;
14 use Test::More tests => 48;
15
16 use C4::Context;
17 use Koha::Database;
18
19 use t::lib::Mocks;
20
21 BEGIN {
22     push( @INC, dirname(__FILE__) . '/../lib' );
23
24     use_ok('Koha::Plugins');
25     use_ok('Koha::Plugins::Handler');
26     use_ok('Koha::Plugins::Base');
27     use_ok('Koha::Plugin::Test');
28 }
29
30 my $schema = Koha::Database->new->schema;
31
32 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
33 $mock_plugin->mock( 'test_template', sub {
34     my ( $self, $file ) = @_;
35     my $template = $self->get_template({ file => $file });
36     $template->param( filename => $file );
37     return $template->output;
38 });
39
40 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
41
42 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
43
44 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
45 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
46
47 ok( $plugin->can('report'), 'Test plugin can report' );
48 ok( $plugin->can('tool'), 'Test plugin can tool' );
49 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
50 ok( $plugin->can('intranet_catalog_biblio_enhancements'), 'Test plugin can intranet_catalog_biblio_enhancements');
51 ok( $plugin->can('intranet_catalog_biblio_enhancements_toolbar_button'), 'Test plugin can intranet_catalog_biblio_enhancements_toolbar_button' );
52 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
53 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
54 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
55 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' );
56 ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
57 ok( $plugin->can('intranet_head'), 'Test plugin can intranet_head' );
58 ok( $plugin->can('intranet_js'), 'Test plugin can intranet_js' );
59 ok( $plugin->can('configure'), 'Test plugin can configure' );
60 ok( $plugin->can('install'), 'Test plugin can install' );
61 ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
62 ok( $plugin->can('uninstall'), 'Test plugin can install' );
63
64 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
65
66 my $metadata = $plugin->get_metadata();
67 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
68
69 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
70 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
71
72 # test absolute path change in get_template with Koha::Plugin::Test
73 # using the mock set before
74 # we also add tmpdir as an approved template dir
75 t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
76 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context->temporary_directory );
77 print $fh 'I am [% filename %]';
78 close $fh;
79 my $classname = ref($plugin);
80 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
81
82 my $result = $plugin->enable;
83 is( ref($result), 'Koha::Plugin::Test' );
84
85 # testing GetPlugins
86 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
87     method => 'report'
88 });
89
90 my @names = map { $_->get_metadata()->{'name'} } @plugins;
91 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
92 @plugins =  Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
93     metadata => { my_example_tag  => 'find_me' },
94 });
95
96 @names = map { $_->get_metadata()->{'name'} } @plugins;
97 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
98 # Test two metadata conditions; one does not exist for Test.pm
99 # Since it is a required key, we should not find the same results
100 my @plugins2 = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
101     metadata => { my_example_tag  => 'find_me', not_there => '1' },
102 });
103 isnt( scalar @plugins2, scalar @plugins, 'GetPlugins with two metadata conditions' );
104
105 $result = $plugin->disable;
106 is( ref($result), 'Koha::Plugin::Test' );
107
108 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins();
109 @names = map { $_->get_metadata()->{'name'} } @plugins;
110 is( scalar grep( /^Test Plugin$/, @names), 0, "GetPlugins does not found disabled Test Plugin" );
111
112 @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ all => 1 });
113 @names = map { $_->get_metadata()->{'name'} } @plugins;
114 is( scalar grep( /^Test Plugin$/, @names), 1, "With all param, GetPlugins found disabled Test Plugin" );
115
116 for my $pass ( 1 .. 2 ) {
117     my $plugins_dir;
118     my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
119     my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
120     if ( $pass == 1 ) {
121         my $plugins_dir1 = tempdir( CLEANUP => 1 );
122         t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
123         $plugins_dir = $plugins_dir1;
124         push @INC, $plugins_dir1;
125     } else {
126         my $plugins_dir1 = tempdir( CLEANUP => 1 );
127         my $plugins_dir2 = tempdir( CLEANUP => 1 );
128         t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
129         $plugins_dir = $plugins_dir2;
130         pop @INC;
131         push @INC, $plugins_dir2;
132         push @INC, $plugins_dir1;
133     }
134     my $full_pm_path = $plugins_dir . '/' . $pm_path;
135
136     my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
137     unless ( $ae->extract( to => $plugins_dir ) ) {
138         warn "ERROR: " . $ae->error;
139     }
140     use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
141     $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
142     my $table = $plugin->get_qualified_table_name( 'mytable' );
143
144     ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
145     $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"
146     Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
147     my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
148     my $info = $sth->fetchall_arrayref;
149     is( @$info, 0, "Table $table does no longer exist" );
150     ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
151 }
152
153 subtest 'output and output_html tests' => sub {
154
155     plan tests => 6;
156
157     # Trick stdout to be able to test
158     local *STDOUT;
159     my $stdout;
160     open STDOUT, '>', \$stdout;
161
162     my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
163     $plugin->test_output;
164
165     like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
166     like($stdout, qr{Content-Type: application/json; charset=UTF-8}, 'Correct content-type');
167     like($stdout, qr{¡Hola output!}, 'Correct data');
168
169     # reset the stdout buffer
170     $stdout = '';
171     close STDOUT;
172     open STDOUT, '>', \$stdout;
173
174     $plugin->test_output_html;
175
176     like($stdout, qr/Cache-control: no-cache/, 'force_no_caching sets Cache-control as desired');
177     like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
178     like($stdout, qr{¡Hola output_html!}, 'Correct data');
179 };
180
181 subtest 'Version upgrade tests' => sub {
182
183     plan tests => 1;
184
185     $schema->storage->txn_begin;
186
187     my $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
188
189     # make sure there's no version on the DB
190     $schema->resultset('PluginData')
191         ->search( { plugin_class => $plugin->{class}, plugin_key => '__INSTALLED_VERSION__' } )
192         ->delete;
193
194     $plugin = Koha::Plugin::Test->new( { enable_plugins => 1, cgi => CGI->new } );
195     my $version = $plugin->retrieve_data('__INSTALLED_VERSION__');
196
197     is( $version, $plugin->get_metadata->{version}, 'Version has been populated correctly' );
198
199     $schema->storage->txn_rollback;
200 };
201
202
203 subtest 'Test _version_compare' => sub {
204
205     plan tests => 12;
206
207     t::lib::Mocks::mock_config( 'enable_plugins', 1 );
208
209     is( Koha::Plugins::Base::_version_compare( '1.1.1',    '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
210     is( Koha::Plugins::Base::_version_compare( '2.2.2',    '1.1.1' ),  1, "1.1.1 is greater then 2.2.2" );
211     is( Koha::Plugins::Base::_version_compare( '1.1.1',    '1.1.1' ),  0, "1.1.1 is equal to 1.1.1" );
212     is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ),  0, "1.01.001 is equal to 1.1.1" );
213     is( Koha::Plugins::Base::_version_compare( '1',        '1.0.0' ),  0, "1 is equal to 1.0.0" );
214     is( Koha::Plugins::Base::_version_compare( '1.0',      '1.0.0' ),  0, "1.0 is equal to 1.0.0" );
215
216     # OO tests
217     my $plugin = Koha::Plugin::Test->new;
218     is( $plugin->_version_compare( '1.1.1',    '2.2.2' ), -1, "1.1.1 is less then 2.2.2" );
219     is( $plugin->_version_compare( '2.2.2',    '1.1.1' ),  1, "1.1.1 is greater then 2.2.2" );
220     is( $plugin->_version_compare( '1.1.1',    '1.1.1' ),  0, "1.1.1 is equal to 1.1.1" );
221     is( $plugin->_version_compare( '1.01.001', '1.1.1' ),  0, "1.01.001 is equal to 1.1.1" );
222     is( $plugin->_version_compare( '1',        '1.0.0' ),  0, "1 is equal to 1.0.0" );
223     is( $plugin->_version_compare( '1.0',      '1.0.0' ),  0, "1.0 is equal to 1.0.0" );
224 };
225
226 subtest 'bundle_path() tests' => sub {
227
228     plan tests => 1;
229
230     t::lib::Mocks::mock_config( 'enable_plugins', 1 );
231
232     my @current_dir = File::Spec->splitdir(abs_path(__FILE__));
233     # remote Plugins.t
234     pop @current_dir;
235     # remove db_dependent
236     pop @current_dir;
237
238     my $plugin = Koha::Plugin::Test->new;
239
240     is( $plugin->bundle_path, File::Spec->catdir(@current_dir) . '/lib/Koha/Plugin/Test' );
241
242 };
243
244 subtest 'new() tests' => sub {
245
246     plan tests => 2;
247
248     t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context->temporary_directory ] );
249     t::lib::Mocks::mock_config( 'enable_plugins', 0 );
250
251     my $result = Koha::Plugins->new();
252     is( $result, undef, 'calling new() on disabled plugins returns undef' );
253
254     $result = Koha::Plugins->new({ enable_plugins => 1 });
255     is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );
256 };