From ae611a4eca706e54a3d1d8763722ec8ada4fc3e1 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 14 Dec 2023 11:58:40 +0000 Subject: [PATCH] Bug 35568: Add a transform_prepared_letter plugin hook to allow modification of notices created via GetPreparedLetter A plugin hook for GetPreparedLetter would be very useful. It could be used to add universal headers and/or footers to messages, attach news, and modify notices in other ways in whole or in part. It could even be used for call webhooks when certain types of notices are sent. For example, the MessageBee ( https://uniquelibrary.com/messagebee/ ) plugin for Koha generates YAML based notice content, which is then further processed by the before_send_messages hook. This process is unreliable due to the possible changes in data between the creation of the yaml and the reading of that yaml and transforming it into JSON with much more data. For example, the yaml may contain an issue_id, but that checkout may have been returned in the meantime. A hook like this would allow the MessageBee plugin to skip the yaml step and create the json data directly which is then sent to MessageBee for transmission to the patron. Test Plan: 1) Apply this patch 2) prove t/db_dependent/Koha/Plugins/Transform_prepared_letter_hook.t 3) Download and install the Kitchen Sink plugin v2.4.0 https://github.com/bywatersolutions/dev-koha-plugin-kitchen-sink/releases/tag/v2.4.0 4) Generate any notice or slip, such as a checkin or checkout notice 5) Note that all notices now end with "Thanks for using your local library!" Signed-off-by: David Nind Signed-off-by: Martin Renvoize --- C4/Letters.pm | 2 + .../Plugins/Transform_prepared_letter_hook.t | 110 ++++++++++++++++++ t/lib/plugins/Koha/Plugin/Test.pm | 9 ++ 3 files changed, 121 insertions(+) create mode 100755 t/db_dependent/Koha/Plugins/Transform_prepared_letter_hook.t diff --git a/C4/Letters.pm b/C4/Letters.pm index a0404ac7075..82e6a13466f 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -712,6 +712,8 @@ sub GetPreparedLetter { $letter->{content} =~ s/<<\S*>>//go; #remove any stragglers + Koha::Plugins->call( 'transform_prepared_letter', { letter => $letter, params => \%params } ); + return $letter; } diff --git a/t/db_dependent/Koha/Plugins/Transform_prepared_letter_hook.t b/t/db_dependent/Koha/Plugins/Transform_prepared_letter_hook.t new file mode 100755 index 00000000000..060fd5cfc73 --- /dev/null +++ b/t/db_dependent/Koha/Plugins/Transform_prepared_letter_hook.t @@ -0,0 +1,110 @@ +#!/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 t::lib::Mocks; +use t::lib::TestBuilder; + +use Koha::DateUtils qw( dt_from_string output_pref ); +use C4::Letters qw( GetPreparedLetter ); + +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'); +} + +t::lib::Mocks::mock_config( 'enable_plugins', 1 ); + +my $schema = Koha::Database->schema; + +my $builder = t::lib::TestBuilder->new(); + +my $dbh = C4::Context->dbh; + +subtest 'Test transform_prepared_letter' => sub { + plan tests => 4; + + $schema->storage->txn_begin(); + + $dbh->do(q|DELETE FROM letter|); + + my $now_value = dt_from_string(); + my $mocked_datetime = Test::MockModule->new('DateTime'); + $mocked_datetime->mock( 'now', sub { return $now_value->clone; } ); + + my $library = $builder->build( { source => 'Branch' } ); + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + my $plugins = Koha::Plugins->new; + + warning_is { $plugins->InstallPlugins; } undef; + + my $prepared_letter; + + my $sth = $dbh->prepare( + q{ + INSERT INTO letter (module, code, name, title, content) + VALUES ('test',?,'Test',?,?)} + ); + + $sth->execute( + "TEST_PATRON", + "[% borrower.firstname %]", + "[% borrower.id %]" + ); + + warning_like { + $prepared_letter = GetPreparedLetter( + ( + module => 'test', + letter_code => 'TEST_PATRON', + tables => { + borrowers => $patron->borrowernumber, + }, + ) + ) + + } + qr/transform_prepared_letter called with letter content/, + 'GetPreparedLetter calls the transform_prepared_letter hook'; + + is( + $prepared_letter->{content}, + $patron->borrowernumber . "\nThank you for using your local library!", + 'Patron object used correctly with scalar for content' + ); + is( + $prepared_letter->{title}, + $patron->firstname . "!", + 'Patron object used correctly with scalar for title' + ); + + $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 32f7dedb489..6e1daf7fb11 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -391,6 +391,15 @@ sub ill_table_actions { ); } +sub transform_prepared_letter { + my ( $self, $params ) = @_; + + $params->{letter}->{title} .= '!'; + $params->{letter}->{content} .= "\nThank you for using your local library!"; + + Koha::Exception->throw("transform_prepared_letter called with letter content $params->{letter}->{content}"); +} + sub _private_sub { return ""; } -- 2.44.0