Bug 21073: (QA follow-up) Fix plugin-related tests
[koha-equinox.git] / t / db_dependent / Koha / Template / Plugin / KohaPlugins.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 14;
6 use CGI;
7 use File::Basename;
8 use File::Spec;
9 use File::Temp qw( tempdir tempfile );
10 use FindBin qw($Bin);
11 use Archive::Extract;
12 use Module::Load::Conditional qw(can_load);
13 use Test::MockModule;
14
15 use C4::Context;
16 use t::lib::Mocks;
17
18 BEGIN {
19     push( @INC, dirname(__FILE__) . '/../../../../lib' );
20
21     use_ok('Koha::Plugins');
22     use_ok('Koha::Plugins::Handler');
23     use_ok('Koha::Plugins::Base');
24     use_ok('Koha::Plugin::Test');
25 }
26
27 t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 );
28 t::lib::Mocks::mock_config( 'enable_plugins', 1 );
29
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 # Enable all plugins
34 my $plugins = Koha::Plugins->new;
35 $plugins->InstallPlugins;
36 my @plugins = $plugins->GetPlugins({ all => 1, class => 'Koha::Plugin::Test' });
37 map { $_->enable; } @plugins;
38
39 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
40 $mock_plugin->mock( 'test_template', sub {
41     my ( $self, $file ) = @_;
42     my $template = $self->get_template({ file => $file });
43     $template->param( filename => $file );
44     return $template->output;
45 });
46
47 use_ok( 'Koha::Template::Plugin::KohaPlugins', 'Can use Koha::Template::Plugin::KohaPlugins' );
48
49 ok( my $plugin = Koha::Template::Plugin::KohaPlugins->new(), 'Able to instantiate template plugin' );
50
51 ok( index( $plugin->get_plugins_opac_js, 'Koha::Plugin::Test::opac_js' ) != -1, 'Test plugin opac_js return value is part of code returned by get_plugins_opac_js' );
52 ok( index( $plugin->get_plugins_opac_head, 'Koha::Plugin::Test::opac_head' ) != -1, 'Test plugin opac_head return value is part of code returned by get_plugins_opac_head' );
53 ok( index( $plugin->get_plugins_intranet_js, 'Koha::Plugin::Test::intranet_js' ) != -1, 'Test plugin intranet_js return value is part of code returned by get_plugins_intranet_js' );
54 ok( index( $plugin->get_plugins_intranet_head, 'Koha::Plugin::Test::intranet_head' ) != -1, 'Test plugin intranet_head return value is part of code returned by get_plugins_intranet_head' );
55
56 t::lib::Mocks::mock_preference('UseKohaPlugins',0);
57 t::lib::Mocks::mock_config('enable_plugins',0);
58 is( $plugin->get_plugins_opac_js, q{}, 'Test plugin opac_js return value is empty' );
59 is( $plugin->get_plugins_opac_head, q{}, 'Test plugin opac_head return value is empty' );
60 is( $plugin->get_plugins_intranet_js, q{}, 'Test plugin intranet_js return value is empty' );
61 is( $plugin->get_plugins_intranet_head, q{}, 'Test plugin intranet_head return value is empty' );
62
63 $schema->storage->txn_rollback;