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

(-)a/C4/Circulation.pm (-1 / +1 lines)
Lines 178-184 sub barcodedecode { Link Here
178
    my ($barcode, $filter) = @_;
178
    my ($barcode, $filter) = @_;
179
    my $branch = C4::Context::mybranch();
179
    my $branch = C4::Context::mybranch();
180
    $filter = C4::Context->preference('itemBarcodeInputFilter') unless $filter;
180
    $filter = C4::Context->preference('itemBarcodeInputFilter') unless $filter;
181
    ($barcode) = Koha::Plugins->call('item_barcode_transform',  $barcode ) || $barcode;
181
    ($barcode) = Koha::Plugins->call_recursive('item_barcode_transform',  $barcode );
182
    $filter or return $barcode;     # ensure filter is defined, else return untouched barcode
182
    $filter or return $barcode;     # ensure filter is defined, else return untouched barcode
183
	if ($filter eq 'whitespace') {
183
	if ($filter eq 'whitespace') {
184
		$barcode =~ s/\s//g;
184
		$barcode =~ s/\s//g;
(-)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 / +32 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 => 54;
28
use Test::More tests => 55;
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;
Lines 96-102 subtest 'GetPlugins() tests' => sub { Link Here
96
126
97
    @plugins = $plugins->GetPlugins({ metadata => { my_example_tag  => 'find_me' }, all => 1 });
127
    @plugins = $plugins->GetPlugins({ metadata => { my_example_tag  => 'find_me' }, all => 1 });
98
    @names = map { $_->get_metadata()->{'name'} } @plugins;
128
    @names = map { $_->get_metadata()->{'name'} } @plugins;
99
    is( scalar @names, 2, "Only two plugins found via a metadata tag" );
129
    is( scalar @names, 4, "Only four plugins found via a metadata tag" );
100
130
101
    $schema->storage->txn_rollback;
131
    $schema->storage->txn_rollback;
102
};
132
};
(-)a/t/lib/Koha/Plugin/TestBarcodes.pm (+43 lines)
Line 0 Link Here
1
package Koha::Plugin::TestBarcodes;
2
3
use Scalar::Util qw( looks_like_number );
4
5
## It's good practice to use Modern::Perl
6
use Modern::Perl;
7
8
## Required for all plugins
9
use base qw(Koha::Plugins::Base);
10
11
our $VERSION = 1.01;
12
our $metadata = {
13
    name            => 'TestBarcodes Plugin',
14
    author          => 'Kyle M Hall',
15
    description     => 'TestBarcodes plugin',
16
    date_authored   => '2013-01-14',
17
    date_updated    => '2013-01-14',
18
    minimum_version => '3.11',
19
    maximum_version => undef,
20
    version         => $VERSION,
21
    my_example_tag  => 'find_me',
22
};
23
24
sub new {
25
    my ( $class, $args ) = @_;
26
    $args->{'metadata'} = $metadata;
27
    my $self = $class->SUPER::new($args);
28
    return $self;
29
}
30
31
sub item_barcode_transform {
32
    my ( $self, $barcode ) = @_;
33
    return $barcode unless looks_like_number( $barcode );
34
    return $barcode + 1;
35
}
36
37
sub patron_barcode_transform {
38
    my ( $self, $barcode ) = @_;
39
    return $barcode unless looks_like_number( $barcode );
40
    return $barcode + 1;
41
}
42
43
1;
(-)a/t/lib/Koha/Plugin/TestBarcodes2.pm (-1 / +43 lines)
Line 0 Link Here
0
- 
1
package Koha::Plugin::TestBarcodes2;
2
3
use Scalar::Util qw( looks_like_number );
4
5
## It's good practice to use Modern::Perl
6
use Modern::Perl;
7
8
## Required for all plugins
9
use base qw(Koha::Plugins::Base);
10
11
our $VERSION = 1.01;
12
our $metadata = {
13
    name            => 'TestBarcodes2 Plugin',
14
    author          => 'Kyle M Hall',
15
    description     => 'TestBarcodes2 plugin',
16
    date_authored   => '2013-01-14',
17
    date_updated    => '2013-01-14',
18
    minimum_version => '3.11',
19
    maximum_version => undef,
20
    version         => $VERSION,
21
    my_example_tag  => 'find_me',
22
};
23
24
sub new {
25
    my ( $class, $args ) = @_;
26
    $args->{'metadata'} = $metadata;
27
    my $self = $class->SUPER::new($args);
28
    return $self;
29
}
30
31
sub item_barcode_transform {
32
    my ( $self, $barcode ) = @_;
33
    return $barcode unless looks_like_number( $barcode );
34
    return $barcode + 2;
35
}
36
37
sub patron_barcode_transform {
38
    my ( $self, $barcode ) = @_;
39
    return $barcode unless looks_like_number( $barcode );
40
    return $barcode + 2;
41
}
42
43
1;

Return to bug 26351