Bug 29672: Increase performance of Koha::Plugins->call
[srvgit] / t / db_dependent / Koha / Plugins / Plugins.t
index ed19ecf..7d9166d 100755 (executable)
@@ -25,7 +25,8 @@ use File::Temp qw( tempdir tempfile );
 use FindBin qw($Bin);
 use Module::Load::Conditional qw(can_load);
 use Test::MockModule;
-use Test::More tests => 52;
+use Test::More tests => 61;
+use Test::Warn;
 
 use C4::Context;
 use Koha::Database;
@@ -35,26 +36,34 @@ use t::lib::Mocks;
 
 BEGIN {
     # Mock pluginsdir before loading Plugins module
-    my $path = dirname(__FILE__) . '/../../../lib';
+    my $path = dirname(__FILE__) . '/../../../lib/plugins';
     t::lib::Mocks::mock_config( 'pluginsdir', $path );
 
     use_ok('Koha::Plugins');
     use_ok('Koha::Plugins::Handler');
     use_ok('Koha::Plugins::Base');
     use_ok('Koha::Plugin::Test');
+    use_ok('Koha::Plugin::TestItemBarcodeTransform');
 }
 
 my $schema = Koha::Database->new->schema;
 
 subtest 'call() tests' => sub {
-    plan tests => 2;
+
+    plan tests => 4;
 
     $schema->storage->txn_begin;
     # Temporarily remove any installed plugins data
     Koha::Plugins::Methods->delete;
+    $schema->resultset('PluginData')->delete();
 
+    t::lib::Mocks::mock_config('enable_plugins', 1);
     my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
-    my @plugins = $plugins->InstallPlugins;
+
+    my @plugins;
+
+    warning_is { @plugins = $plugins->InstallPlugins; } undef;
+
     foreach my $plugin (@plugins) {
         $plugin->enable();
     }
@@ -65,24 +74,71 @@ subtest 'call() tests' => sub {
     is_deeply(\@responses, $expected, 'call() should return all responses from plugins');
 
     # Make sure parameters are correctly passed to the plugin method
-    my @responses = Koha::Plugins->call('check_password', { password => '1234' });
+    @responses = Koha::Plugins->call('check_password', { password => '1234' });
 
-    my $expected = [ { error => 0 } ];
+    $expected = [ { error => 0 } ];
     is_deeply(\@responses, $expected, 'call() should return all responses from plugins');
 
+    t::lib::Mocks::mock_config('enable_plugins', 0);
+    @responses = Koha::Plugins->call('check_password', { password => '1234' });
+    is_deeply(\@responses, [], 'call() should return an empty array if plugins are disabled');
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'more call() tests' => sub {
+
+    plan tests => 6;
+
+    $schema->storage->txn_begin;
+    # Temporarily remove any installed plugins data
+    Koha::Plugins::Methods->delete;
+
+    t::lib::Mocks::mock_config('enable_plugins', 1);
+    my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
+    my @plugins;
+
+    warning_is { @plugins = $plugins->InstallPlugins; } undef;
+
+    foreach my $plugin (@plugins) {
+        $plugin->enable();
+    }
+
+    # Barcode is multiplied by 2 by Koha::Plugin::Test, and again by 4 by Koha::Plugin::TestItemBarcodeTransform
+    # showing that call has passed the same ref to multiple plugins to operate on
+    my $bc = 1;
+    warnings_are
+        { Koha::Plugins->call('item_barcode_transform', \$bc); }
+        [ qq{Plugin error (Test Plugin): Exception 'Koha::Exception' thrown 'item_barcode_transform called with parameter: 1'\n},
+          qq{Plugin error (Test Plugin for item_barcode_transform): Exception 'Koha::Exception' thrown 'item_barcode_transform called with parameter: 2'\n} ];
+    is( $bc, 8, "Got expected response" );
+
+    my $cn = 'abcd';
+    warnings_are
+        { Koha::Plugins->call('item_barcode_transform', \$bc); }
+        [ qq{Plugin error (Test Plugin): Exception 'Koha::Exception' thrown 'item_barcode_transform called with parameter: 8'\n},
+          qq{Plugin error (Test Plugin for item_barcode_transform): Exception 'Koha::Exception' thrown 'item_barcode_transform called with parameter: 16'\n} ];
+    is( $cn, 'abcd', "Got expected response" );
+
+    t::lib::Mocks::mock_config('enable_plugins', 0);
+    $bc = 1;
+    Koha::Plugins->call('item_barcode_transform', \$bc);
+    is( $bc, 1, "call should return the original arguments if plugins are disabled" );
+
     $schema->storage->txn_rollback;
 };
 
 subtest 'GetPlugins() tests' => sub {
 
-    plan tests => 2;
+    plan tests => 3;
 
     $schema->storage->txn_begin;
     # Temporarily remove any installed plugins data
     Koha::Plugins::Methods->delete;
 
     my $plugins = Koha::Plugins->new({ enable_plugins => 1 });
-    $plugins->InstallPlugins;
+
+    warning_is { $plugins->InstallPlugins; } undef;
 
     my @plugins = $plugins->GetPlugins({ method => 'report', all => 1 });
 
@@ -143,7 +199,7 @@ subtest 'is_enabled() tests' => sub {
 $schema->storage->txn_begin;
 Koha::Plugins::Methods->delete;
 
-Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
+warning_is { Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins(); } undef;
 
 ok( Koha::Plugins::Methods->search( { plugin_class => 'Koha::Plugin::Test' } )->count, 'Test plugin methods added to database' );
 is( Koha::Plugins::Methods->search({ plugin_class => 'Koha::Plugin::Test', plugin_method => '_private_sub' })->count, 0, 'Private methods are skipped' );
@@ -169,12 +225,14 @@ ok( $plugin->can('to_marc'), 'Test plugin can to_marc' );
 ok( $plugin->can('intranet_catalog_biblio_enhancements'), 'Test plugin can intranet_catalog_biblio_enhancements');
 ok( $plugin->can('intranet_catalog_biblio_enhancements_toolbar_button'), 'Test plugin can intranet_catalog_biblio_enhancements_toolbar_button' );
 ok( $plugin->can('opac_online_payment'), 'Test plugin can opac_online_payment' );
+ok( $plugin->can('after_hold_create'), 'Test plugin can after_hold_create' );
 ok( $plugin->can('opac_online_payment_begin'), 'Test plugin can opac_online_payment_begin' );
 ok( $plugin->can('opac_online_payment_end'), 'Test plugin can opac_online_payment_end' );
 ok( $plugin->can('opac_head'), 'Test plugin can opac_head' );
 ok( $plugin->can('opac_js'), 'Test plugin can opac_js' );
 ok( $plugin->can('intranet_head'), 'Test plugin can intranet_head' );
 ok( $plugin->can('intranet_js'), 'Test plugin can intranet_js' );
+ok( $plugin->can('item_barcode_transform'), 'Test plugin can barcode_transform' );
 ok( $plugin->can('configure'), 'Test plugin can configure' );
 ok( $plugin->can('install'), 'Test plugin can install' );
 ok( $plugin->can('upgrade'), 'Test plugin can upgrade' );
@@ -256,12 +314,13 @@ for my $pass ( 1 .. 2 ) {
 
     ok( -f $plugins_dir . "/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm", "KitchenSink plugin installed successfully" );
     $INC{$pm_path} = $full_pm_path; # FIXME I do not really know why, but if this is moved before the $plugin constructor, it will fail with Can't locate object method "new" via package "Koha::Plugin::Com::ByWaterSolutions::KitchenSink"
-    Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins();
+    warning_is { Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins(); } undef;
+    ok( -f $full_pm_path, "Koha::Plugins::Handler::delete works correctly (pass $pass)" );
     Koha::Plugins::Handler->delete({ class => "Koha::Plugin::Com::ByWaterSolutions::KitchenSink", enable_plugins => 1 });
     my $sth = C4::Context->dbh->table_info( undef, undef, $table, 'TABLE' );
     my $info = $sth->fetchall_arrayref;
     is( @$info, 0, "Table $table does no longer exist" );
-    ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly." );
+    ok( !( -f $full_pm_path ), "Koha::Plugins::Handler::delete works correctly (pass $pass)" );
 }
 
 subtest 'output and output_html tests' => sub {
@@ -333,7 +392,7 @@ subtest 'bundle_path() tests' => sub {
 
     my $plugin = Koha::Plugin::Test->new;
 
-    is( $plugin->bundle_path, File::Spec->catdir(@current_dir) . '/lib/Koha/Plugin/Test' );
+    is( $plugin->bundle_path, File::Spec->catdir(@current_dir) . '/lib/plugins/Koha/Plugin/Test' );
 
 };
 
@@ -351,5 +410,4 @@ subtest 'new() tests' => sub {
     is( ref($result), 'Koha::Plugins', 'calling new with enable_plugins makes it override the config' );
 };
 
-$schema->storage->txn_rollback;
 Koha::Plugins::Methods->delete;