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

(-)a/Koha/Plugins.pm (+42 lines)
Lines 32-37 use C4::Output; Link Here
32
32
33
use Koha::Cache::Memory::Lite;
33
use Koha::Cache::Memory::Lite;
34
use Koha::Exceptions::Plugin;
34
use Koha::Exceptions::Plugin;
35
use Koha::Plugins::Datas;
35
use Koha::Plugins::Methods;
36
use Koha::Plugins::Methods;
36
37
37
use constant ENABLED_PLUGINS_CACHE_KEY => 'enabled_plugins';
38
use constant ENABLED_PLUGINS_CACHE_KEY => 'enabled_plugins';
Lines 306-311 sub InstallPlugins { Link Here
306
    return @plugins;
307
    return @plugins;
307
}
308
}
308
309
310
=head2 RemovePlugins
311
312
    Koha::Plugins->RemovePlugins( {
313
        [ plugin_class => MODULE_NAME, destructive => 1, disable => 1 ],
314
    } );
315
316
    This is primarily for unit testing. Take care when you pass the
317
    destructive flag (know what you are doing)!
318
319
    The method removes records from plugin_methods for one or all plugins.
320
321
    If you pass the destructive flag, it will remove records too from
322
    plugin_data for one or all plugins. Destructive overrules disable.
323
324
    If you pass disable, it will disable one or all plugins (in plugin_data).
325
326
    If you do not pass destructive or disable, this method does not touch
327
    records in plugin_data. The cache key for enabled plugins will be cleared
328
    only if you pass disabled or destructive.
329
330
=cut
331
332
sub RemovePlugins {
333
    my ( $class, $params ) = @_;
334
335
    my $cond = {
336
        $params->{plugin_class}
337
        ? ( plugin_class => $params->{plugin_class} )
338
        : ()
339
    };
340
    Koha::Plugins::Methods->search($cond)->delete;
341
    if ( $params->{destructive} ) {
342
        Koha::Plugins::Datas->search($cond)->delete;
343
        Koha::Cache::Memory::Lite->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY );
344
    } elsif ( $params->{disable} ) {
345
        $cond->{plugin_key} = '__ENABLED__';
346
        Koha::Plugins::Datas->search($cond)->update( { plugin_value => 0 } );
347
        Koha::Cache::Memory::Lite->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY );
348
    }
349
}
350
309
1;
351
1;
310
__END__
352
__END__
311
353
(-)a/t/db_dependent/Koha/Plugins/Plugins.t (-4 / +90 lines)
Lines 24-34 use File::Temp qw( tempdir tempfile ); Link Here
24
use FindBin qw($Bin);
24
use FindBin qw($Bin);
25
use Module::Load::Conditional qw(can_load);
25
use Module::Load::Conditional qw(can_load);
26
use Test::MockModule;
26
use Test::MockModule;
27
use Test::More tests => 16;
27
use Test::More tests => 17;
28
use Test::Warn;
28
use Test::Warn;
29
29
30
use C4::Context;
30
use C4::Context;
31
use Koha::Cache::Memory::Lite;
31
use Koha::Database;
32
use Koha::Database;
33
use Koha::Plugins::Datas;
32
use Koha::Plugins::Methods;
34
use Koha::Plugins::Methods;
33
35
34
use t::lib::Mocks;
36
use t::lib::Mocks;
Lines 323-329 subtest 'Koha::Plugin::Test' => sub { Link Here
323
    $schema->storage->txn_rollback;
325
    $schema->storage->txn_rollback;
324
};
326
};
325
327
326
$schema->storage->txn_begin;    # matching rollback at the very end
328
subtest 'RemovePlugins' => sub {
329
    plan tests => 3;
330
331
    $schema->storage->txn_begin;
332
    t::lib::Mocks::mock_config( 'enable_plugins', 1 );
333
334
    our $class_basename = 'Koha::Plugin::TestMR::' . time;
335
336
    sub reload_plugin {
337
        my ( $i, $mocks ) = @_;
338
        Koha::Plugins::Data->new(
339
            { plugin_class => "$class_basename$i", plugin_key => '__ENABLED__', plugin_value => 1 } )->store;
340
        Koha::Plugins::Method->new( { plugin_class => "$class_basename$i", plugin_method => "testmr$i" } )->store;
341
342
        # no_auto => 1 here prevents loading of a not-existing module
343
        $mocks->[$i] = Test::MockModule->new( "$class_basename$i", no_auto => 1 )->mock( new => 1 )
344
            unless $mocks->[$i];
345
    }
346
347
    # We will (re)create new plugins (without modules)
348
    # This requires mocking can_load from Module::Load::Conditional
349
    my $mlc_mock     = Test::MockModule->new('Koha::Plugins')->mock( can_load => 1 );
350
    my $plugin_mocks = [];
351
    my @enabled_plugins;
352
353
    subtest 'Destructive flag' => sub {
354
        reload_plugin( $_, $plugin_mocks ) for 1 .. 3;
355
        Koha::Plugins->RemovePlugins( { destructive => 1 } );
356
        is( Koha::Plugins::Datas->count,   0, 'No data in plugin_data' );
357
        is( Koha::Plugins::Methods->count, 0, 'No data in plugin_methods' );
358
        @enabled_plugins = Koha::Plugins->get_enabled_plugins;    # testing if cache cleared
359
        is( scalar @enabled_plugins, 0, 'No enabled plugins' );
360
361
        reload_plugin( $_, $plugin_mocks ) for 1 .. 3;
362
        Koha::Plugins->RemovePlugins( { plugin_class => "${class_basename}2", destructive => 1 } );
363
        is( Koha::Plugins::Datas->count,   2, '2 in plugin_data' );
364
        is( Koha::Plugins::Methods->count, 2, '2 in plugin_methods' );
365
        @enabled_plugins = Koha::Plugins->get_enabled_plugins;
366
        is( scalar @enabled_plugins, 2, '2 enabled plugins' );
367
        Koha::Plugins->RemovePlugins( { destructive => 1 } );
368
    };
369
370
    subtest 'Disable flag' => sub {
371
        reload_plugin( $_, $plugin_mocks ) for 1 .. 4;
372
        Koha::Plugins->RemovePlugins( { disable => 1 } );
373
        is( Koha::Plugins::Datas->count,   4, '4 in plugin_data' );
374
        is( Koha::Plugins::Methods->count, 0, 'No data in plugin_methods' );
375
        @enabled_plugins = Koha::Plugins->get_enabled_plugins;
376
        is( scalar @enabled_plugins, 0, '0 enabled plugins' );
377
378
        reload_plugin( $_, $plugin_mocks ) for 5 .. 6;
379
        Koha::Plugins->RemovePlugins( { plugin_class => "${class_basename}5", disable => 1 } );
380
        is( Koha::Plugins::Datas->count,   6, '6 in plugin_data' );
381
        is( Koha::Plugins::Methods->count, 1, '1 in plugin_methods' );
382
        @enabled_plugins = Koha::Plugins->get_enabled_plugins;
383
        is( scalar @enabled_plugins, 1, '1 enabled plugins' );
384
        Koha::Plugins->RemovePlugins( { destructive => 1 } );
385
    };
386
387
    subtest 'No flags' => sub {
388
        reload_plugin( $_, $plugin_mocks ) for 1 .. 2;
389
        Koha::Plugins->RemovePlugins;
390
        is( Koha::Plugins::Datas->count,   2, '2 in plugin_data' );
391
        is( Koha::Plugins::Methods->count, 0, 'No data in plugin_methods' );
392
        @enabled_plugins = Koha::Plugins->get_enabled_plugins;
393
        is( scalar @enabled_plugins, 2, '2 enabled plugins' );
394
395
        reload_plugin( $_, $plugin_mocks ) for 3 .. 4;
396
        Koha::Plugins->RemovePlugins( { plugin_class => "${class_basename}4" } );
397
        is( Koha::Plugins::Datas->count,   4, '4 in plugin_data' );
398
        is( Koha::Plugins::Methods->count, 1, '1 in plugin_methods' );
399
        @enabled_plugins = Koha::Plugins->get_enabled_plugins;
400
        is( scalar @enabled_plugins, 2, '2 enabled plugins (from cache)' );
401
402
        # clear cache and try again, expect 4 since RemovePlugins did not touch plugin_data here
403
        Koha::Cache::Memory::Lite->clear_from_cache( Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY );
404
        @enabled_plugins = Koha::Plugins->get_enabled_plugins;
405
        is( scalar @enabled_plugins, 4, '4 enabled plugins' );
406
        Koha::Plugins->RemovePlugins( { destructive => 1 } );
407
    };
408
409
    $schema->storage->txn_rollback;
410
};
411
412
413
$schema->storage->txn_begin;    # Matching rollback at very end
327
414
328
subtest 'output and output_html tests' => sub {
415
subtest 'output and output_html tests' => sub {
329
416
Lines 412-416 subtest 'new() tests' => sub { Link Here
412
    is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );
499
    is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );
413
};
500
};
414
501
415
Koha::Plugins::Methods->delete;
416
$schema->storage->txn_rollback;
502
$schema->storage->txn_rollback;
417
- 
503
#!/usr/bin/perl

Return to bug 35536