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

(-)a/Koha/Plugins/Base.pm (+51 lines)
Lines 21-26 use Modern::Perl; Link Here
21
21
22
use Module::Pluggable require => 1;
22
use Module::Pluggable require => 1;
23
use Cwd qw(abs_path);
23
use Cwd qw(abs_path);
24
use List::Util qw(max);
24
25
25
use base qw{Module::Bundled::Files};
26
use base qw{Module::Bundled::Files};
26
27
Lines 43-55 sub new { Link Here
43
44
44
    my $self = bless( $args, $class );
45
    my $self = bless( $args, $class );
45
46
47
    my $plugin_version = $self->get_metadata->{version};
48
    my $database_version = $self->retrieve_data('__INSTALLED_VERSION__');
49
46
    ## Run the installation method if it exists and hasn't been run before
50
    ## Run the installation method if it exists and hasn't been run before
47
    if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
51
    if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
48
        if ( $self->install() ) {
52
        if ( $self->install() ) {
49
            $self->store_data( { '__INSTALLED__' => 1 } );
53
            $self->store_data( { '__INSTALLED__' => 1 } );
54
            if ( my $version = $plugin_version ) {
55
                $self->store_data({ '__INSTALLED_VERSION__' => $version });
56
            }
50
        } else {
57
        } else {
51
            warn "Plugin $class failed during installation!";
58
            warn "Plugin $class failed during installation!";
52
        }
59
        }
60
    } elsif ( $self->can('upgrade') && $plugin_version && $database_version ) {
61
        if ( _version_compare( $plugin_version, $database_version ) == 1 ) {
62
            if ( $self->upgrade() ) {
63
                $self->store_data({ '__INSTALLED_VERSION__' => $plugin_version });
64
                warn "Plugin $class failed during upgrade!";
65
            }
66
        }
53
    }
67
    }
54
68
55
    return $self;
69
    return $self;
Lines 218-223 sub output { Link Here
218
    output_with_http_headers( $self->{cgi}, undef, $data, $content_type, $status, $extra_options );
232
    output_with_http_headers( $self->{cgi}, undef, $data, $content_type, $status, $extra_options );
219
}
233
}
220
234
235
=head2 _version_compare
236
237
Utility method to compare two version numbers.
238
Returns 1 if the first argument is the higher version
239
Returns -1 if the first argument is the lower version
240
Returns 0 if both versions are equal
241
242
if ( _version_compare( '2.6.26', '2.6.0' ) == 1 ) {
243
    print "2.6.26 is greater than 2.6.0\n";
244
}
245
246
=cut
247
248
sub _version_compare {
249
    my $ver1 = shift || 0;
250
    my $ver2 = shift || 0;
251
252
    my @v1 = split /[.+:~-]/, $ver1;
253
    my @v2 = split /[.+:~-]/, $ver2;
254
255
    for ( my $i = 0 ; $i < max( scalar(@v1), scalar(@v2) ) ; $i++ ) {
256
257
        # Add missing version parts if one string is shorter than the other
258
        # i.e. 0 should be lt 0.2.1 and not equal, so we append .0
259
        # 0.0.0 <=> 0.2.1 = -1
260
        push( @v1, 0 ) unless defined( $v1[$i] );
261
        push( @v2, 0 ) unless defined( $v2[$i] );
262
        if ( int( $v1[$i] ) > int( $v2[$i] ) ) {
263
            return 1;
264
        }
265
        elsif ( int( $v1[$i] ) < int( $v2[$i] ) ) {
266
            return -1;
267
        }
268
    }
269
    return 0;
270
}
271
221
1;
272
1;
222
__END__
273
__END__
223
274
(-)a/t/db_dependent/Plugins.t (-2 / +17 lines)
Lines 2-18 Link Here
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
4
5
use Test::More tests => 35;
5
use Archive::Extract;
6
use CGI;
6
use CGI;
7
use File::Basename;
7
use File::Basename;
8
use File::Spec;
8
use File::Spec;
9
use File::Temp qw( tempdir tempfile );
9
use File::Temp qw( tempdir tempfile );
10
use FindBin qw($Bin);
10
use FindBin qw($Bin);
11
use Archive::Extract;
12
use Module::Load::Conditional qw(can_load);
11
use Module::Load::Conditional qw(can_load);
13
use Test::MockModule;
12
use Test::MockModule;
13
use Test::More tests => 37;
14
14
15
use C4::Context;
15
use C4::Context;
16
use Koha::Database;
16
use t::lib::Mocks;
17
use t::lib::Mocks;
17
18
18
BEGIN {
19
BEGIN {
Lines 49-54 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' ); Link Here
49
ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
50
ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
50
ok( $plugin->can('configure'), 'Test plugin can configure' );
51
ok( $plugin->can('configure'), 'Test plugin can configure' );
51
ok( $plugin->can('install'), 'Test plugin can install' );
52
ok( $plugin->can('install'), 'Test plugin can install' );
53
ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
52
ok( $plugin->can('uninstall'), 'Test plugin can install' );
54
ok( $plugin->can('uninstall'), 'Test plugin can install' );
53
55
54
is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
56
is( Koha::Plugins::Handler->run({ class => "Koha::Plugin::Test", method => 'report', enable_plugins => 1 }), "Koha::Plugin::Test::report", 'Test run plugin report method' );
Lines 151-153 subtest 'output and output_html tests' => sub { Link Here
151
    like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
153
    like($stdout, qr{Content-Type: text/html; charset=UTF-8}, 'Correct content-type');
152
    like($stdout, qr{¡Hola output_html!}, 'Correct data');
154
    like($stdout, qr{¡Hola output_html!}, 'Correct data');
153
};
155
};
156
157
subtest 'Test _version_compare' => sub {
158
159
    plan tests => 6;
160
161
    is( Koha::Plugins::Base::_version_compare( '1.1.1',    '2.2.2' ),   -1, "1.1.1 is less then 2.2.2" );
162
    is( Koha::Plugins::Base::_version_compare( '2.2.2',    '1.1.1' ),    1, "1.1.1 is greater then 2.2.2" );
163
    is( Koha::Plugins::Base::_version_compare( '1.1.1',    '1.1.1' ),    0, "1.1.1 is equal to 1.1.1" );
164
    is( Koha::Plugins::Base::_version_compare( '1.01.001', '1.1.1' ),    0, "1.01.001 is equal to 1.1.1" );
165
    is( Koha::Plugins::Base::_version_compare( '1',        '1.0.0' ),    0, "1 is equal to 1.0.0" );
166
    is( Koha::Plugins::Base::_version_compare( '1.0',      '1.0.0' ),    0, "1.0 is equal to 1.0.0" );
167
};
168
(-)a/t/lib/Koha/Plugin/Test.pm (-1 / +5 lines)
Lines 78-83 sub install { Link Here
78
    return "Koha::Plugin::Test::install";
78
    return "Koha::Plugin::Test::install";
79
}
79
}
80
80
81
sub upgrade {
82
    my ( $self, $args ) = @_;
83
    return "Koha::Plugin::Test::install";
84
}
85
81
sub uninstall {
86
sub uninstall {
82
    my ( $self, $args ) = @_;
87
    my ( $self, $args ) = @_;
83
    return "Koha::Plugin::Test::uninstall";
88
    return "Koha::Plugin::Test::uninstall";
84
- 

Return to bug 20669