From a7c1c1210e062a98e25a51eb0d7f52974e35ef71 Mon Sep 17 00:00:00 2001 From: Stefan Berndtsson Date: Mon, 17 Oct 2022 16:48:30 +0200 Subject: [PATCH] Bug 31896: New after_recall_action hook Hooks added: after_recall_action with new action add How to test: Run tests in t/db_dependent/Koha/Plugins/Recall_hooks.t Sponsored by: Gothenburg University Library --- Koha/Recalls.pm | 9 ++ t/db_dependent/Koha/Plugins/Recall_hooks.t | 98 ++++++++++++++++++++++ t/lib/plugins/Koha/Plugin/Test.pm | 10 +++ 3 files changed, 117 insertions(+) create mode 100644 t/db_dependent/Koha/Plugins/Recall_hooks.t diff --git a/Koha/Recalls.pm b/Koha/Recalls.pm index ef73bf9917..342f808d93 100644 --- a/Koha/Recalls.pm +++ b/Koha/Recalls.pm @@ -22,6 +22,7 @@ use Modern::Perl; use Koha::Database; use Koha::Recall; use Koha::DateUtils qw( dt_from_string ); +use Koha::Plugins; use C4::Stats qw( UpdateStats ); @@ -173,6 +174,14 @@ sub add_recall { ccode => $item->ccode, }); + Koha::Plugins->call( + 'after_recall_action', + { + action => 'add', + payload => { recall => $recall } + } + ); + # add action log C4::Log::logaction( 'RECALLS', 'CREATE', $recall->id, "Recall requested by borrower #" . $recall->patron_id, $interface ) if ( C4::Context->preference('RecallsLog') ); diff --git a/t/db_dependent/Koha/Plugins/Recall_hooks.t b/t/db_dependent/Koha/Plugins/Recall_hooks.t new file mode 100644 index 0000000000..c46d7dcab1 --- /dev/null +++ b/t/db_dependent/Koha/Plugins/Recall_hooks.t @@ -0,0 +1,98 @@ +#!/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 => 4; +use Test::MockModule; +use Test::Warn; + +use File::Basename; + +use Koha::Account qw( add_credit ); + +use t::lib::Mocks; +use t::lib::TestBuilder; + +BEGIN { + # Mock pluginsdir before loading Plugins module + 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::Plugin::Test'); +} + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + +subtest 'after_recall_action hook' => sub { + + plan tests => 1; + + $schema->storage->txn_begin; + + my $plugins = Koha::Plugins->new; + $plugins->InstallPlugins; + + my $plugin = Koha::Plugin::Test->new->enable; + # Avoid testing useless warnings + my $test_plugin = Test::MockModule->new('Koha::Plugin::Test'); + $test_plugin->mock( 'after_item_action', undef ); + $test_plugin->mock( 'after_circ_action', undef ); + $test_plugin->mock( 'after_biblio_action', undef ); + $test_plugin->mock( 'patron_barcode_transform', undef ); + $test_plugin->mock( 'item_barcode_transform', undef ); + + my $item = $builder->build_sample_item(); + my $biblio = $item->biblio; + my $branch = $item->holdingbranch; + my $category = $builder->build({ source => 'Category' })->{ categorycode }; + my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category, branchcode => $branch } }); + my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category, branchcode => $branch } }); + t::lib::Mocks::mock_userenv({ patron => $patron1 }); + + Koha::CirculationRules->set_rules({ + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + 'recall_due_date_interval' => undef, + 'recalls_allowed' => 10, + } + }); + + C4::Circulation::AddIssue( $patron2->unblessed, $item->barcode ); + + warning_like { + Koha::Recalls->add_recall({ + patron => $patron1, + biblio => $biblio, + branchcode => $branch, + item => undef, + expirationdate => undef, + interface => 'COMMANDLINE', + }); + } + qr/after_recall_action called with action: add, ref: Koha::Recall/, + '->add_recall calls the after_recall_action hook with action add'; + + $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 eee99859d1..c82b0dc610 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -348,6 +348,16 @@ sub background_tasks { }; } +sub after_recall_action { + my ( $self, $params ) = @_; + + my $action = $params->{action}; + my $recall = $params->{payload}->{recall}; + + Koha::Exception->throw( + "after_recall_action called with action: $action, ref: " . ref($recall) ); +} + sub _private_sub { return ""; } -- 2.37.1