Bug 16586: Koha Plugins: Limit results of GetPlugins by metadata
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Wed, 25 May 2016 11:37:49 +0000 (13:37 +0200)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 9 Sep 2016 12:13:39 +0000 (12:13 +0000)
It would be helpful if we could find a plugin based on some metadata
tag as returned by the plugin. This extends the use of GetPlugins that
already supports searching on method.

GetPlugins is used in: admin/edi_accounts.pl, plugins/plugins-home.pl and
tools/stage-marc-import.pl. The changes in these three scripts are
minimal and just related to parameter passing.

Test t/db_dependent/Plugins.t includes another test for GetPlugins. In this
regard a metadata tag has been added to t/Koha/Plugins/Test.pm.

NOTE: This adjustment will also be used in a redesign for bug 15545.

Test plan:
Run t/db_dependent/Plugins.t.
Enable pref UseKohaPlugins and config var enable_plugins.
Go to plugins-home.pl. Verify that it still lists your plugins.
Bonus: Check edi_accounts or stage-marc-import.pl if you have a working
plugin for that.

Signed-off-by: Liz Rea <liz@catalyst.net.nz>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Koha/Plugins.pm
admin/edi_accounts.pl
plugins/plugins-home.pl
t/Koha/Plugin/Test.pm
t/db_dependent/Plugins.t
tools/stage-marc-import.pl

index a188d7c..da336ad 100644 (file)
@@ -46,18 +46,26 @@ sub new {
     return bless( $args, $class );
 }
 
-=head2 GetPlugins()
+=head2 GetPlugins
 
-This will return a list of all the available plugins of the passed type.
+This will return a list of all available plugins, optionally limited by
+method or metadata value.
 
-Usage: my @plugins = C4::Plugins::GetPlugins( $method );
+    my @plugins = C4::Plugins::GetPlugins({
+        method => 'some_method',
+        metadata => { some_key => 'some_value' },
+    });
+
+The method and metadata parameters are optional.
+Available methods currently are: 'report', 'tool', 'to_marc', 'edifact'.
+If you pass multiple keys in the metadata hash, all keys must match.
 
-At the moment, the available types are 'report', 'tool' and 'to_marc'.
 =cut
 
 sub GetPlugins {
-    my $self   = shift;
-    my $method = shift;
+    my ( $self, $params ) = @_;
+    my $method = $params->{method};
+    my $req_metadata = $params->{metadata} // {};
 
     my @plugin_classes = $self->plugins();
     my @plugins;
@@ -68,13 +76,18 @@ sub GetPlugins {
 
             my $plugin = $plugin_class->new({ enable_plugins => $self->{'enable_plugins'} });
 
-            if ($method) {
-                if ( $plugin->can($method) ) {
-                    push( @plugins, $plugin );
+            # Limit results by method or metadata
+            my $ok = 1;
+            next if $method && !$plugin->can($method);
+            my $plugin_metadata = $plugin->get_metadata;
+            foreach my $key ( keys %$req_metadata ) {
+                if( !$plugin_metadata->{$key} ||
+                  $plugin_metadata->{$key} ne $req_metadata->{$key} ) {
+                    $ok = 0;
+                    last;
                 }
-            } else {
-                push( @plugins, $plugin );
             }
+            push( @plugins, $plugin ) if $ok;
         }
     }
     return @plugins;
index 5f963bd..e5b0694 100755 (executable)
@@ -57,7 +57,9 @@ if ( $op eq 'acct_form' ) {
     $template->param( plugins_enabled => $plugins_enabled );
 
     if ( $plugins_enabled ) {
-        my @plugins = Koha::Plugins->new()->GetPlugins('edifact');
+        my @plugins = Koha::Plugins->new()->GetPlugins({
+            method => 'edifact',
+        });
         $template->param( plugins => \@plugins );
     }
 }
index 225a560..5efd9bd 100755 (executable)
@@ -50,7 +50,9 @@ if ($plugins_enabled) {
         method       => $method,
     );
 
-    my @plugins = Koha::Plugins->new()->GetPlugins($method);
+    my @plugins = Koha::Plugins->new()->GetPlugins({
+        method => $method,
+    });
 
     $template->param( plugins => \@plugins, );
 
index 6a8ca78..401ce3d 100644 (file)
@@ -1,6 +1,6 @@
 package Koha::Plugin::Test;
 
-## It's good practive to use Modern::Perl
+## It's good practice to use Modern::Perl
 use Modern::Perl;
 
 ## Required for all plugins
@@ -16,6 +16,7 @@ our $metadata = {
     minimum_version => '3.11',
     maximum_version => undef,
     version         => $VERSION,
+    my_example_tag  => 'find_me',
 };
 
 ## This is the minimum code required for a plugin's 'new' method
index 403d7c4..f918693 100755 (executable)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 22;
+use Test::More tests => 23;
 use File::Basename;
 use FindBin qw($Bin);
 use Archive::Extract;
@@ -42,9 +42,17 @@ is( $metadata->{'name'}, 'Test Plugin', 'Test $plugin->get_metadata()' );
 is( $plugin->get_qualified_table_name('mytable'), 'koha_plugin_test_mytable', 'Test $plugin->get_qualified_table_name()' );
 is( $plugin->get_plugin_http_path(), '/plugin/Koha/Plugin/Test', 'Test $plugin->get_plugin_http_path()' );
 
-my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins( 'report' );
+# testing GetPlugins
+my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
+    method => 'report'
+});
 my @names = map { $_->get_metadata()->{'name'} } @plugins;
 is( scalar grep( /^Test Plugin$/, @names), 1, "Koha::Plugins::GetPlugins functions correctly" );
+@plugins =  Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({
+    metadata => { my_example_tag  => 'find_me' },
+});
+@names = map { $_->get_metadata()->{'name'} } @plugins;
+is( scalar grep( /^Test Plugin$/, @names), 1, "GetPlugins also found Test Plugin via a metadata tag" );
 
 SKIP: {
     my $plugins_dir = C4::Context->config("pluginsdir");
index a5f60bd..99506fc 100755 (executable)
@@ -210,7 +210,9 @@ if ($completedJobID) {
     if ( C4::Context->preference('UseKohaPlugins') &&
          C4::Context->config('enable_plugins') ) {
 
-        my @plugins = Koha::Plugins->new()->GetPlugins('to_marc');
+        my @plugins = Koha::Plugins->new()->GetPlugins({
+            method => 'to_marc',
+        });
         $template->param( plugins => \@plugins );
     }
 }