From 4aa2e16ec18f50dfe7c00f1eafc87a7546b24f04 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 29 Apr 2019 15:21:32 -0300 Subject: [PATCH] Bug 22053: (QA follow-up) More tests This patch adds tests for: - $plugin->enable and $plugin->disable are chainable (i.e. return $self) - Disabled plugins don't inject routes To test: - Apply this patch - Run: $ kshell k$ prove t/db_dependent/Koha/REST/Plugin/PluginRoutes.t \ t/db_dependent/Plugins.t => SUCCESS: Tests pass Signed-off-by: Tomas Cohen Arazi Signed-off-by: Nick Clemens --- Koha/Plugins/Base.pm | 2 + .../Koha/REST/Plugin/PluginRoutes.t | 54 +++++++++++++++++-- t/db_dependent/Plugins.t | 8 +-- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/Koha/Plugins/Base.pm b/Koha/Plugins/Base.pm index 20d8b79829..392ab4c900 100644 --- a/Koha/Plugins/Base.pm +++ b/Koha/Plugins/Base.pm @@ -300,6 +300,8 @@ sub disable { my ($self) = @_; $self->store_data( {'__ENABLED__' => 0} ); + + return $self; } 1; diff --git a/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t b/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t index 06fecd38fa..cc50b56b90 100644 --- a/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t +++ b/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use Test::Mojo; use Test::Warn; @@ -35,18 +35,28 @@ BEGIN { t::lib::Mocks::mock_config( 'pluginsdir', $path ); } +use Koha::Database; +use Koha::Plugins; + +my $schema = Koha::Database->new->schema; + subtest 'Bad plugins tests' => sub { plan tests => 3; + $schema->storage->txn_begin; + # enable plugins t::lib::Mocks::mock_config( 'enable_plugins', 1 ); t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 ); + my @plugins = Koha::Plugins->new->GetPlugins( { all => 1 } ); + foreach my $plugin (@plugins) { + $plugin->enable; + } + # initialize Koha::REST::V1 after mocking - my $remote_address = '127.0.0.1'; my $t; - warning_is { $t = Test::Mojo->new('Koha::REST::V1'); } 'The resulting spec is invalid. Skipping Bad API Route Plugin', @@ -56,6 +66,44 @@ subtest 'Bad plugins tests' => sub { ok( !exists $routes->{'/contrib/badass/patrons/(:patron_id)/bother_wrong'}, 'Route doesn\'t exist' ); ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}, 'Route exists' ); + $schema->storage->txn_rollback; +}; + +subtest 'Disabled plugins tests' => sub { + + plan tests => 2; + + $schema->storage->txn_begin; + + # enable plugins + t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + t::lib::Mocks::mock_preference( 'UseKohaPlugins', 1 ); + + my $good_plugin; + + my @plugins = Koha::Plugins->new->GetPlugins( { all => 1 } ); + foreach my $plugin (@plugins) { + $plugin->disable; + $good_plugin = $plugin + if $plugin->{metadata}->{description} eq 'Test plugin'; + } + + # initialize Koha::REST::V1 after mocking + my $t = Test::Mojo->new('Koha::REST::V1'); + + my $routes = get_defined_routes($t); + ok( !exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}, + 'Plugin disabled, route not defined' ); + + $good_plugin->enable; + + $t = Test::Mojo->new('Koha::REST::V1'); + $routes = get_defined_routes($t); + + ok( exists $routes->{'/contrib/testplugin/patrons/(:patron_id)/bother'}, + 'Plugin enabled, route defined' ); + + $schema->storage->txn_rollback; }; sub get_defined_routes { diff --git a/t/db_dependent/Plugins.t b/t/db_dependent/Plugins.t index f34500e146..e89cebe0b3 100755 --- a/t/db_dependent/Plugins.t +++ b/t/db_dependent/Plugins.t @@ -9,7 +9,7 @@ use File::Temp qw( tempdir tempfile ); use FindBin qw($Bin); use Module::Load::Conditional qw(can_load); use Test::MockModule; -use Test::More tests => 44; +use Test::More tests => 46; use C4::Context; use Koha::Database; @@ -77,7 +77,8 @@ close $fh; my $classname = ref($plugin); like( $plugin->test_template($fn), qr/^I am $fn/, 'Template works' ); -$plugin->enable; +my $result = $plugin->enable; +is( ref($result), 'Koha::Plugin::Test' ); # testing GetPlugins my @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ @@ -99,7 +100,8 @@ my @plugins2 = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins({ }); isnt( scalar @plugins2, scalar @plugins, 'GetPlugins with two metadata conditions' ); -$plugin->disable; +$result = $plugin->disable; +is( ref($result), 'Koha::Plugin::Test' ); @plugins = Koha::Plugins->new({ enable_plugins => 1 })->GetPlugins(); @names = map { $_->get_metadata()->{'name'} } @plugins; -- 2.39.5