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

(-)a/Koha/Plugins.pm (+35 lines)
Lines 83-88 sub call { Link Here
83
    return @responses;
83
    return @responses;
84
}
84
}
85
85
86
=head2 call_recursive
87
88
Calls a plugin method for all enabled plugins,
89
passing the return value from the previous plugin
90
to the next one.
91
92
    @response = Koha::Plugins->call_recursive($method, @args)
93
94
=cut
95
96
sub call_recursive {
97
    my ( $class, $method, @args ) = @_;
98
99
    my @responses;
100
    if ( C4::Context->config('enable_plugins') ) {
101
102
        my @plugins = $class->new( { enable_plugins => 1 } )->GetPlugins( { method => $method } );
103
        @plugins = grep { $_->can($method) } @plugins;
104
105
        foreach my $plugin (@plugins) {
106
            my @response = eval { $plugin->$method(@args) };
107
108
            if ($@) {
109
                warn sprintf( "Plugin error (%s): %s", $plugin->get_metadata->{name}, $@ );
110
                next;
111
            } else {
112
                @args = @response;
113
            }
114
        }
115
116
    }
117
118
    return @args;
119
}
120
86
=head2 GetPlugins
121
=head2 GetPlugins
87
122
88
This will return a list of all available plugins, optionally limited by
123
This will return a list of all available plugins, optionally limited by
(-)a/t/db_dependent/Koha/Plugins/Plugins.t (-1 / +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('test_call_recursive', 1);
96
    is( scalar @responses, 1, "Got back one element" );
97
    is( $responses[0], 11, "Got expected response" );
98
99
    (@responses) = Koha::Plugins->call_recursive('test_call_recursive', 'abcd');
100
    is( scalar @responses, 1, "Got back one element" );
101
    is( $responses[0], 'abcdabcd', "Got expected response" );
102
103
    t::lib::Mocks::mock_config('enable_plugins', 0);
104
    (@responses) = Koha::Plugins->call_recursive('test_call_recursive', 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;
(-)a/t/lib/Koha/Plugin/Test.pm (-1 / +6 lines)
Lines 308-313 sub intranet_catalog_biblio_tab { Link Here
308
    return @tabs;
308
    return @tabs;
309
}
309
}
310
310
311
sub test_call_recursive {
312
    my ( $self, $value ) = @_;
313
314
    return $value x 2;
315
}
316
311
sub _private_sub {
317
sub _private_sub {
312
    return "";
318
    return "";
313
}
319
}
314
- 

Return to bug 28026