From b181a0de0bbb1edda05eabd4019e5294b296d7a6 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 Content-Type: text/plain; charset=utf-8 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 Signed-off-by: David Nind Signed-off-by: Marcel de Rooy [EDIT} Adding get_from_storage ;) For current consistency. See further bug 32107. --- 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 100755 t/db_dependent/Koha/Plugins/Recall_hooks.t diff --git a/Koha/Recalls.pm b/Koha/Recalls.pm index b64d58332b..36d891d195 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 ); @@ -176,6 +177,14 @@ sub add_recall { } ); + Koha::Plugins->call( + 'after_recall_action', + { + action => 'add', + payload => { recall => $recall->get_from_storage }, # FIXME Bug 32107 + } + ); + # 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 100755 index 0000000000..31d1d1371a --- /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 b3cd288a2d..6744a7e7f3 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -359,6 +359,16 @@ sub after_account_action { "after_account_action called with action: $action, type: $type, ref: " . ref($line) ); } +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.30.2