Bug 7804 - Add Koha Plugin System - Unit Tests
authorKyle M Hall <kyle@bywatersolutions.com>
Mon, 14 Jan 2013 13:23:46 +0000 (08:23 -0500)
committerJared Camins-Esakov <jcamins@cpbibliography.com>
Wed, 20 Mar 2013 18:49:53 +0000 (14:49 -0400)
Tests Ok
~/kohaclone$ perl t/db_dependent/Plugins.t
1..15
ok 1 - use Koha::Plugins;
ok 2 - use Koha::Plugins::Handler;
ok 3 - use Koha::Plugins::Base;
ok 4 - use Koha::Plugin::Test;
ok 5 - Test can_load
ok 6 - Test plugin class isa Koha::Plugin::Test
ok 7 - Test plugin parent class isa Koha::Plugins::Base
ok 8 - Test plugin can report
ok 9 - Test plugin can tool
ok 10 - Test plugin can configure
ok 11 - Test plugin can install
ok 12 - Test plugin can install
ok 13 - Test $plugin->get_metadata()
ok 14 - Test $plugin->get_qualified_table_name()
ok 15 - Test $plugin->get_plugin_http_path()

(and all others as well)

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
Koha/Plugins.pm
Koha/Plugins/Base.pm
Koha/Plugins/Handler.pm
t/Koha/Plugin/Test.pm [new file with mode: 0644]
t/db_dependent/Plugins.t [new file with mode: 0755]

index 797f600..bc4a3bb 100644 (file)
@@ -26,8 +26,6 @@ use C4::Context;
 use C4::Output;
 
 BEGIN {
 use C4::Output;
 
 BEGIN {
-    die('Plugins not enabled in config') unless ( C4::Context->config("enable_plugins") );
-
     push @INC, C4::Context->config("pluginsdir");
 }
 
     push @INC, C4::Context->config("pluginsdir");
 }
 
@@ -40,6 +38,8 @@ Koha::Plugins - Module for loading and managing plugins.
 sub new {
     my ( $class, $args ) = @_;
 
 sub new {
     my ( $class, $args ) = @_;
 
+    die('Plugins not enabled in config') unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
+
     $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
 
     return bless( $args, $class );
     $args->{'pluginsdir'} = C4::Context->config("pluginsdir");
 
     return bless( $args, $class );
index f1cff3b..0e1aedc 100644 (file)
@@ -27,8 +27,6 @@ use C4::Context;
 use C4::Auth;
 
 BEGIN {
 use C4::Auth;
 
 BEGIN {
-    die('Plugins not enabled in config') unless ( C4::Context->config("enable_plugins") );
-
     push @INC, C4::Context->config("pluginsdir");
 }
 
     push @INC, C4::Context->config("pluginsdir");
 }
 
@@ -41,6 +39,8 @@ C4::Plugins::Base - Base Module for plugins
 sub new {
     my ( $class, $args ) = @_;
 
 sub new {
     my ( $class, $args ) = @_;
 
+    die('Plugins not enabled in config') unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
+
     $args->{'class'} = $class;
     $args->{'template'} = Template->new( { ABSOLUTE => 1 } );
 
     $args->{'class'} = $class;
     $args->{'template'} = Template->new( { ABSOLUTE => 1 } );
 
index 7ecdb79..d2f324a 100644 (file)
@@ -26,8 +26,6 @@ use Module::Load::Conditional qw(can_load);
 use C4::Context;
 
 BEGIN {
 use C4::Context;
 
 BEGIN {
-    die('Plugins not enabled in config') unless ( C4::Context->config("enable_plugins") );
-
     push @INC, C4::Context->config("pluginsdir");
 }
 
     push @INC, C4::Context->config("pluginsdir");
 }
 
@@ -52,6 +50,9 @@ Runs a plugin
 
 sub run {
     my ( $class, $args ) = @_;
 
 sub run {
     my ( $class, $args ) = @_;
+
+    die('Plugins not enabled in config') unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
+
     my $plugin_class  = $args->{'class'};
     my $plugin_method = $args->{'method'};
     my $cgi           = $args->{'cgi'};
     my $plugin_class  = $args->{'class'};
     my $plugin_method = $args->{'method'};
     my $cgi           = $args->{'cgi'};
diff --git a/t/Koha/Plugin/Test.pm b/t/Koha/Plugin/Test.pm
new file mode 100644 (file)
index 0000000..d34a6eb
--- /dev/null
@@ -0,0 +1,53 @@
+package Koha::Plugin::Test;
+
+## It's good practive to use Modern::Perl
+use Modern::Perl;
+
+## Required for all plugins
+use base qw(Koha::Plugins::Base);
+
+our $VERSION = 1.01;
+our $metadata = {
+    name            => 'Test Plugin',
+    author          => 'Kyle M Hall',
+    description     => 'Test plugin',
+    date_authored   => '2013-01-14',
+    date_updated    => '2013-01-14',
+    minimum_version => '3.11',
+    maximum_version => undef,
+    version         => $VERSION,
+};
+
+## This is the minimum code required for a plugin's 'new' method
+## More can be added, but none should be removed
+sub new {
+    my ( $class, $args ) = @_;
+    $args->{'metadata'} = $metadata;
+    my $self = $class->SUPER::new($args);
+    return $self;
+}
+
+sub report {
+    my ( $self, $args ) = @_;
+    return 1;
+}
+
+sub tool {
+    my ( $self, $args ) = @_;
+    return 1;
+}
+
+sub configure {
+    my ( $self, $args ) = @_;
+    return 1;
+}
+
+sub install {
+    my ( $self, $args ) = @_;
+    return 1;
+}
+
+sub uninstall {
+    my ( $self, $args ) = @_;
+    return 1;
+}
diff --git a/t/db_dependent/Plugins.t b/t/db_dependent/Plugins.t
new file mode 100755 (executable)
index 0000000..52495ea
--- /dev/null
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 15;
+use File::Basename;
+
+use Module::Load::Conditional qw(can_load);
+
+use C4::Context;
+
+BEGIN {
+    push( @INC, dirname(__FILE__) . '/..' );
+
+    use_ok('Koha::Plugins');
+    use_ok('Koha::Plugins::Handler');
+    use_ok('Koha::Plugins::Base');
+    use_ok('Koha::Plugin::Test');
+}
+
+ok( can_load( modules => { "Koha::Plugin::Test" => undef } ), 'Test can_load' );
+
+my $plugin = Koha::Plugin::Test->new({ enable_plugins => 1});
+
+isa_ok( $plugin, "Koha::Plugin::Test", 'Test plugin class' );
+isa_ok( $plugin, "Koha::Plugins::Base", 'Test plugin parent class' );
+
+ok( $plugin->can('report'), 'Test plugin can report' );
+ok( $plugin->can('tool'), 'Test plugin can tool' );
+ok( $plugin->can('configure'), 'Test plugin can configure' );
+ok( $plugin->can('install'), 'Test plugin can install' );
+ok( $plugin->can('uninstall'), 'Test plugin can install' );
+
+my $metadata = $plugin->get_metadata();
+ok( $metadata->{'name'} eq 'Test Plugin', 'Test $plugin->get_metadata()' );
+
+ok( $plugin->get_qualified_table_name('mytable') eq 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
+ok( $plugin->get_plugin_http_path() eq '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
\ No newline at end of file