From 211044ad1a91d7c9bd83289fb928b34797b74756 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 31 Mar 2022 17:32:40 +0200 Subject: [PATCH] Bug 30410: Unit tests Signed-off-by: Kyle M Hall Signed-off-by: Fridolin Somers --- t/db_dependent/Koha/Plugins/BackgroundJob.t | 83 +++++++++++++++++++++ t/lib/plugins/Koha/Plugin/Test.pm | 8 ++ 2 files changed, 91 insertions(+) create mode 100755 t/db_dependent/Koha/Plugins/BackgroundJob.t diff --git a/t/db_dependent/Koha/Plugins/BackgroundJob.t b/t/db_dependent/Koha/Plugins/BackgroundJob.t new file mode 100755 index 0000000000..0f5932eca0 --- /dev/null +++ b/t/db_dependent/Koha/Plugins/BackgroundJob.t @@ -0,0 +1,83 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 1; +use Test::MockModule; + +use File::Basename; + +use Koha::BackgroundJobs; + +use t::lib::Mocks; +use t::lib::Mocks::Logger; +use t::lib::TestBuilder; + +BEGIN { + # Mock pluginsdir before loading Plugins module + my $path = dirname(__FILE__) . '/../../../lib/plugins'; + t::lib::Mocks::mock_config( 'pluginsdir', $path ); + + require Koha::Plugins; + require Koha::Plugins::Handler; + require Koha::Plugin::Test; +} + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; +my $logger = t::lib::Mocks::Logger->new; + +t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + +subtest 'background_tasks() hooks tests' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + my $bj = Koha::BackgroundJob->new; + my $tasks = $bj->type_to_class_mapping; + + ok( !exists $tasks->{foo} ); + ok( !exists $tasks->{bar} ); + + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; + + my $plugin = Koha::Plugin::Test->new->enable; + + $bj = Koha::BackgroundJob->new; + $tasks = $bj->type_to_class_mapping; + + is( $tasks->{plugin_test_foo}, 'MyPlugin::Class::Foo' ); + is( $tasks->{plugin_test_bar}, 'MyPlugin::Class::Bar' ); + + my $metadata = $plugin->get_metadata; + delete $metadata->{namespace}; + + my $test_plugin = Test::MockModule->new('Koha::Plugin::Test'); + $test_plugin->mock( 'get_metadata', sub { return $metadata; } ); + + $plugin = Koha::Plugin::Test->new; + + $bj = Koha::BackgroundJob->new; + $tasks = $bj->type_to_class_mapping; + $logger->warn_is("The plugin includes the 'background_tasks' method, but doesn't provide the required 'namespace' method (Koha::Plugin::Test)"); + + $schema->storage->txn_rollback; + Koha::Plugins::Methods->delete; +}; diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm index 61e1f942f2..eee99859d1 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -21,6 +21,7 @@ our $metadata = { minimum_version => '3.11', maximum_version => undef, version => $VERSION, + namespace => 'test', my_example_tag => 'find_me', }; @@ -340,6 +341,13 @@ sub intranet_catalog_biblio_tab { return @tabs; } +sub background_tasks { + return { + foo => 'MyPlugin::Class::Foo', + bar => 'MyPlugin::Class::Bar', + }; +} + sub _private_sub { return ""; } -- 2.39.5