From edc8f4933585cae8986e4a1eae58cabf5ba07918 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 27 Sep 2021 08:21:34 -0300 Subject: [PATCH] Bug 29121: Regression tests This patch adds regression tests for broken ->install and ->upgrade methods on plugins. It adds two dummie plugins named BrokenInstall and BrokenUpgrade, for convenience. Tests are added to catch the warnings that will be logged when trying to load this plugins on different scenarios. Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart --- t/db_dependent/Koha/Plugins/Plugins.t | 27 +++++++++++++++++------- t/lib/Koha/Plugin/BrokenInstall.pm | 30 +++++++++++++++++++++++++++ t/lib/Koha/Plugin/BrokenUpgrade.pm | 30 +++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 t/lib/Koha/Plugin/BrokenInstall.pm create mode 100644 t/lib/Koha/Plugin/BrokenUpgrade.pm diff --git a/t/db_dependent/Koha/Plugins/Plugins.t b/t/db_dependent/Koha/Plugins/Plugins.t index 7ae6664664..8003165895 100755 --- a/t/db_dependent/Koha/Plugins/Plugins.t +++ b/t/db_dependent/Koha/Plugins/Plugins.t @@ -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 => 55; +use Test::More tests => 58; +use Test::Warn; use C4::Context; use Koha::Database; @@ -47,7 +48,8 @@ BEGIN { my $schema = Koha::Database->new->schema; subtest 'call() tests' => sub { - plan tests => 3; + + plan tests => 4; $schema->storage->txn_begin; # Temporarily remove any installed plugins data @@ -55,7 +57,13 @@ subtest 'call() tests' => sub { t::lib::Mocks::mock_config('enable_plugins', 1); my $plugins = Koha::Plugins->new({ enable_plugins => 1 }); - my @plugins = $plugins->InstallPlugins; + + my @plugins; + + warnings_are + { @plugins = $plugins->InstallPlugins; } + [ "Calling 'install' died for plugin Koha::Plugin::BrokenInstall", "Calling 'upgrade' died for plugin Koha::Plugin::BrokenUpgrade" ]; + foreach my $plugin (@plugins) { $plugin->enable(); } @@ -80,14 +88,16 @@ subtest 'call() tests' => sub { 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; + + warnings_are { $plugins->InstallPlugins; } + [ "Calling 'install' died for plugin Koha::Plugin::BrokenInstall", "Calling 'upgrade' died for plugin Koha::Plugin::BrokenUpgrade" ]; my @plugins = $plugins->GetPlugins({ method => 'report', all => 1 }); @@ -148,7 +158,8 @@ subtest 'is_enabled() tests' => sub { $schema->storage->txn_begin; Koha::Plugins::Methods->delete; -Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins(); +warnings_are { Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins(); } +[ "Calling 'install' died for plugin Koha::Plugin::BrokenInstall", "Calling 'upgrade' died for plugin Koha::Plugin::BrokenUpgrade" ]; 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' ); @@ -262,7 +273,9 @@ 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(); + warnings_are + { Koha::Plugins->new( { enable_plugins => 1 } )->InstallPlugins(); } + [ "Calling 'install' died for plugin Koha::Plugin::BrokenInstall", "Calling 'upgrade' died for plugin Koha::Plugin::BrokenUpgrade" ]; 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' ); diff --git a/t/lib/Koha/Plugin/BrokenInstall.pm b/t/lib/Koha/Plugin/BrokenInstall.pm new file mode 100644 index 0000000000..f55df4ab20 --- /dev/null +++ b/t/lib/Koha/Plugin/BrokenInstall.pm @@ -0,0 +1,30 @@ +package Koha::Plugin::BrokenInstall; + +use Modern::Perl; + +use base qw(Koha::Plugins::Base); + +our $VERSION = 1.01; +our $metadata = { + name => 'Broken install plugin', + author => 'Kyle M Hall', + description => 'Broken install plugin', + date_authored => '2013-01-14', + date_updated => '2013-01-14', + minimum_version => '3.11', + maximum_version => undef, + version => $VERSION, +}; + +sub new { + my ( $class, $args ) = @_; + $args->{'metadata'} = $metadata; + my $self = $class->SUPER::new($args); + return $self; +} + +sub install { + die "Why Liz? WHY?"; +} + +1; diff --git a/t/lib/Koha/Plugin/BrokenUpgrade.pm b/t/lib/Koha/Plugin/BrokenUpgrade.pm new file mode 100644 index 0000000000..7ecde3ead8 --- /dev/null +++ b/t/lib/Koha/Plugin/BrokenUpgrade.pm @@ -0,0 +1,30 @@ +package Koha::Plugin::BrokenUpgrade; + +use Modern::Perl; + +use base qw(Koha::Plugins::Base); + +our $VERSION = 1.01; +our $metadata = { + name => 'Broken upgrade plugin', + author => 'Kyle M Hall', + description => 'Broken upgrade plugin', + date_authored => '2013-01-14', + date_updated => '2013-01-14', + minimum_version => '3.11', + maximum_version => undef, + version => $VERSION, +}; + +sub new { + my ( $class, $args ) = @_; + $args->{'metadata'} = $metadata; + my $self = $class->SUPER::new($args); + return $self; +} + +sub upgrade { + die "Why Liz? WHY?"; +} + +1; -- 2.39.5