Bug 8612: [QA Follow-up] Remove two newlines from template output
[koha.git] / t / db_dependent / Plugins.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 28;
6 use File::Basename;
7 use File::Temp qw( tempdir );
8 use FindBin qw($Bin);
9 use Archive::Extract;
10 use Module::Load::Conditional qw(can_load);
11
12 use C4::Context;
13 use t::lib::Mocks;
14
15 BEGIN {
16     push( @INC, dirname(__FILE__) . '/..' );
17
18     use_ok('Koha::Plugins');
19     use_ok('Koha::Plugins::Handler');
20     use_ok('Koha::Plugins::Base');
21     use_ok('Koha::Plugin::Test');
22 }
23
24 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
25
26 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1});
27
28 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
29 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
30
31 ok( $plugin->can('report'), 'Test plugin can report' );
32 ok( $plugin->can('tool'), 'Test plugin can tool' );
33 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
34 ok( $plugin->can('configure'), 'Test plugin can configure' );
35 ok( $plugin->can('install'), 'Test plugin can install' );
36 ok( $plugin->can('uninstall'), 'Test plugin can install' );
37
38 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
39
40 my $metadata = $plugin->get_metadata();
41 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
42
43 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
44 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
45
46 # testing GetPlugins
47 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
48     method => 'report'
49 });
50 my @names = map { $_->get_metadata()->{'name'} } @plugins;
51 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
52 @plugins =  Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
53     metadata => { my_example_tag  => 'find_me' },
54 });
55 @names = map { $_->get_metadata()->{'name'} } @plugins;
56 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
57 # Test two metadata conditions; one does not exist for Test.pm
58 # Since it is a required key, we should not find the same results
59 my @plugins2 = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
60     metadata => { my_example_tag  => 'find_me', not_there => '1' },
61 });
62 isnt( scalar @plugins2, scalar @plugins, 'GetPlugins with two metadata conditions' );
63
64 for my $pass ( 1 .. 2 ) {
65     my $plugins_dir;
66     my $module_name = 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink';
67     my $pm_path = 'Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm';
68     if ( $pass == 1 ) {
69         my $plugins_dir1 = tempdir( CLEANUP => 1 );
70         t::lib::Mocks::mock_config('pluginsdir', $plugins_dir1);
71         $plugins_dir = $plugins_dir1;
72         push @INC, $plugins_dir1;
73     } else {
74         my $plugins_dir1 = tempdir( CLEANUP => 1 );
75         my $plugins_dir2 = tempdir( CLEANUP => 1 );
76         t::lib::Mocks::mock_config('pluginsdir', [ $plugins_dir2, $plugins_dir1 ]);
77         $plugins_dir = $plugins_dir2;
78         pop @INC;
79         push @INC, $plugins_dir2;
80         push @INC, $plugins_dir1;
81     }
82     my $full_pm_path = $plugins_dir . '/' . $pm_path;
83
84     my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
85     unless ( $ae->extract( to => $plugins_dir ) ) {
86         warn "ERROR: " . $ae->error;
87     }
88     use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
89     $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
90     my $table = $plugin->get_qualified_table_name( 'mytable' );
91
92     ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
93     $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"
94     Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
95     my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
96     my $info = $sth->fetchall_arrayref;
97     is( @$info, 0, "Table $table does no longer exist" );
98     ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
99 }