View | Details | Raw Unified | Return to bug 28026
Collapse All | Expand All

(-)a/Koha/Plugins.pm (+35 lines)
Lines 82-87 sub call { Link Here
82
    return @responses;
82
    return @responses;
83
}
83
}
84
84
85
=head2 call_recursive
86
87
Calls a plugin method for all enabled plugins,
88
passing the return value from the previous plugin
89
to the next one.
90
91
    @response = Koha::Plugins->call_recursive($method, @args)
92
93
=cut
94
95
sub call_recursive {
96
    my ( $class, $method, @args ) = @_;
97
98
    my @responses;
99
    if ( C4::Context->config('enable_plugins') ) {
100
101
        my @plugins = $class->new( { enable_plugins => 1 } )->GetPlugins( { method => $method } );
102
        @plugins = grep { $_->can($method) } @plugins;
103
104
        foreach my $plugin (@plugins) {
105
            my @response = eval { $plugin->$method(@args) };
106
107
            if ($@) {
108
                warn sprintf( "Plugin error (%s): %s", $plugin->get_metadata->{name}, $@ );
109
                next;
110
            } else {
111
                @args = @response;
112
            }
113
        }
114
115
    }
116
117
    return @args;
118
}
119
85
=head2 GetPlugins
120
=head2 GetPlugins
86
121
87
This will return a list of all available plugins, optionally limited by
122
This will return a list of all available plugins, optionally limited by
(-)a/t/db_dependent/Koha/Plugins/Plugins.t (-2 / +31 lines)
Lines 25-31 use File::Temp qw( tempdir tempfile ); Link Here
25
use FindBin qw($Bin);
25
use FindBin qw($Bin);
26
use Module::Load::Conditional qw(can_load);
26
use Module::Load::Conditional qw(can_load);
27
use Test::MockModule;
27
use Test::MockModule;
28
use Test::More tests => 53;
28
use Test::More tests => 54;
29
29
30
use C4::Context;
30
use C4::Context;
31
use Koha::Database;
31
use Koha::Database;
Lines 78-83 subtest 'call() tests' => sub { Link Here
78
    $schema->storage->txn_rollback;
78
    $schema->storage->txn_rollback;
79
};
79
};
80
80
81
subtest 'call_recursive() tests' => sub {
82
    plan tests => 6;
83
84
    $schema->storage->txn_begin;
85
    # Temporarily remove any installed plugins data
86
    Koha::Plugins::Methods->delete;
87
88
    t::lib::Mocks::mock_config('enable_plugins', 1);
89
    my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
90
    my @plugins = $plugins->InstallPlugins;
91
    foreach my $plugin (@plugins) {
92
        $plugin->enable();
93
    }
94
95
    my (@responses) = Koha::Plugins->call_recursive('item_barcode_transform', 1);
96
    is( scalar @responses, 1, "Got back one element" );
97
    is( $responses[0], 4, "Got expected response" );
98
99
    (@responses) = Koha::Plugins->call_recursive('item_barcode_transform', 'abcd');
100
    is( scalar @responses, 1, "Got back one element" );
101
    is( $responses[0], 'abcd', "Got expected response" );
102
103
    t::lib::Mocks::mock_config('enable_plugins', 0);
104
    (@responses) = Koha::Plugins->call_recursive('item_barcode_transform', 1);
105
    is( scalar @responses, 1, "Got back one element" );
106
    is( $responses[0], 1, "call_recursive should return the original arguments if plugins are disabled" );
107
108
    $schema->storage->txn_rollback;
109
};
110
81
subtest 'GetPlugins() tests' => sub {
111
subtest 'GetPlugins() tests' => sub {
82
112
83
    plan tests => 2;
113
    plan tests => 2;
84
- 

Return to bug 28026