Bug 17553: Move existing tests
[koha-ffzg.git] / t / db_dependent / Plugins.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 32;
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__) . '/..' );
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 my $mock_plugin = Test::MockModule->new( 'Koha::Plugin::Test' );
28 $mock_plugin->mock( 'test_template', sub {
29     my ( $self, $file ) = @_;
30     my $template = $self->get_template({ file => $file });
31     $template->param( filename => $file );
32     return $template->output;
33 });
34
35 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
36
37 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1, cgi => CGI->new });
38
39 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
40 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
41
42 ok( $plugin->can('report'), 'Test plugin can report' );
43 ok( $plugin->can('tool'), 'Test plugin can tool' );
44 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
45 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
46 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
47 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
48 ok( $plugin->can('configure'), 'Test plugin can configure' );
49 ok( $plugin->can('install'), 'Test plugin can install' );
50 ok( $plugin->can('uninstall'), 'Test plugin can install' );
51
52 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
53
54 my $metadata = $plugin->get_metadata();
55 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
56
57 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
58 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
59
60 # test absolute path change in get_template with Koha::Plugin::Test
61 # using the mock set before
62 # we also add tmpdir as an approved template dir
63 t::lib::Mocks::mock_config( 'pluginsdir', [ File::Spec->tmpdir ] );
64 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1 );
65 print $fh 'I am [% filename %]';
66 close $fh;
67 my $classname = ref($plugin);
68 like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' );
69
70 # testing GetPlugins
71 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
72     method => 'report'
73 });
74 my @names = map { $_->get_metadata()->{'name'} } @plugins;
75 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
76 @plugins =  Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
77     metadata => { my_example_tag  => 'find_me' },
78 });
79 @names = map { $_->get_metadata()->{'name'} } @plugins;
80 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
81 # Test two metadata conditions; one does not exist for Test.pm
82 # Since it is a required key, we should not find the same results
83 my @plugins2 = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
84     metadata => { my_example_tag  => 'find_me', not_there => '1' },
85 });
86 isnt( scalar @plugins2, scalar @plugins, 'GetPlugins with two metadata conditions' );
87
88 for my $pass ( 1 .. 2 ) {
89     my $plugins_dir;
90     my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
91     my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
92     if ( $pass == 1 ) {
93         my $plugins_dir1 = tempdir( CLEANUP => 1 );
94         t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
95         $plugins_dir = $plugins_dir1;
96         push @INC, $plugins_dir1;
97     } else {
98         my $plugins_dir1 = tempdir( CLEANUP => 1 );
99         my $plugins_dir2 = tempdir( CLEANUP => 1 );
100         t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
101         $plugins_dir = $plugins_dir2;
102         pop @INC;
103         push @INC, $plugins_dir2;
104         push @INC, $plugins_dir1;
105     }
106     my $full_pm_path = $plugins_dir . '/' . $pm_path;
107
108     my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
109     unless ( $ae->extract( to => $plugins_dir ) ) {
110         warn "ERROR: " . $ae->error;
111     }
112     use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
113     $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
114     my $table = $plugin->get_qualified_table_name( 'mytable' );
115
116     ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
117     $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"
118     Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
119     my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
120     my $info = $sth->fetchall_arrayref;
121     is( @$info, 0, "Table $table does no longer exist" );
122     ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
123 }