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