Bug 16586: [QA Follow-up] Add test for two metadata conditions
[koha-equinox.git] / t / db_dependent / Plugins.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 24;
7 use File::Basename;
8 use FindBin qw($Bin);
9 use Archive::Extract;
10 use Module::Load::Conditional qw(can_load);
11
12 use C4::Context;
13
14 BEGIN {
15     push( @INC, dirname(__FILE__) . '/..' );
16
17     use_ok('Koha::Plugins');
18     use_ok('Koha::Plugins::Handler');
19     use_ok('Koha::Plugins::Base');
20     use_ok('Koha::Plugin::Test');
21 }
22
23 ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
24
25 my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1});
26
27 isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
28 isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
29
30 ok( $plugin->can('report'), 'Test plugin can report' );
31 ok( $plugin->can('tool'), 'Test plugin can tool' );
32 ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
33 ok( $plugin->can('configure'), 'Test plugin can configure' );
34 ok( $plugin->can('install'), 'Test plugin can install' );
35 ok( $plugin->can('uninstall'), 'Test plugin can install' );
36
37 is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
38
39 my $metadata = $plugin->get_metadata();
40 is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
41
42 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
43 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
44
45 # testing GetPlugins
46 my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
47     method => 'report'
48 });
49 my @names = map { $_->get_metadata()->{'name'} } @plugins;
50 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
51 @plugins =  Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
52     metadata => { my_example_tag  => 'find_me' },
53 });
54 @names = map { $_->get_metadata()->{'name'} } @plugins;
55 is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
56 # Test two metadata conditions; one does not exist for Test.pm
57 # Since it is a required key, we should not find the same results
58 my @plugins2 = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
59     metadata => { my_example_tag  => 'find_me', not_there => '1' },
60 });
61 isnt( scalar @plugins2, scalar @plugins, 'GetPlugins with two metadata conditions' );
62
63 SKIP: {
64     my $plugins_dir = C4::Context->config("pluginsdir");
65     skip "plugindir not set", 4 unless defined $plugins_dir;
66     skip "plugindir not writable", 4 unless -w $plugins_dir;
67     # no need to skip further tests if KitchenSink would already exist
68
69     my $ae = Archive::Extract->new( archive => "$Bin/KitchenSinkPlugin.kpz", type => 'zip' );
70     unless ( $ae->extract( to => $plugins_dir ) ) {
71         warn "ERROR: " . $ae->error;
72     }
73     use_ok('Koha::Plugin::Com::ByWaterSolutions::KitchenSink');
74     $plugin = Koha::Plugin::Com::ByWaterSolutions::KitchenSink->new({ enable_plugins => 1});
75     my $table = $plugin->get_qualified_table_name( 'mytable' );
76
77     ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
78     Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
79     my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
80     my $info = $sth->fetchall_arrayref;
81     is( @$info, 0, "Table $table does no longer exist" );
82     ok( !( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm" ), "Koha::Plugins::Handler::delete works correctly." );
83 }