From 8c75721f4a215c20cc675272f91a54a20901df9b Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 9 May 2025 17:11:33 -0300 Subject: [PATCH] Bug 39870: Unit tests --- .../Koha/Plugins/Notices_content_hook.t | 104 ++++++++++++++++++ t/lib/plugins/Koha/Plugin/Test.pm | 10 ++ 2 files changed, 114 insertions(+) create mode 100755 t/db_dependent/Koha/Plugins/Notices_content_hook.t diff --git a/t/db_dependent/Koha/Plugins/Notices_content_hook.t b/t/db_dependent/Koha/Plugins/Notices_content_hook.t new file mode 100755 index 00000000000..879b1f203b2 --- /dev/null +++ b/t/db_dependent/Koha/Plugins/Notices_content_hook.t @@ -0,0 +1,104 @@ +#!/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::NoWarnings; +use Test::More tests => 5; +use Test::Warn; + +use File::Basename; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +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(); + +subtest 'notices_content() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin(); + + my $dbh = C4::Context->dbh; + + # just to be sure + $dbh->do(q|DELETE FROM letter WHERE module='test'|); + + 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_HOLD", + "[% hold.id %]", + "[% plugin_content.test.custom_attribute.message %]. " + . "We identified this is the '[% plugin_content.test.custom_attribute.module %]' module" + ); + + my $hold = $builder->build_object( { class => 'Koha::Holds' } ); + warning_like { + $prepared_letter = GetPreparedLetter( + ( + module => 'test', + letter_code => 'TEST_HOLD', + tables => { + reserves => $hold->unblessed, + } + ) + ) + } + qr/transform_prepared_letter called with letter content/, + 'GetPreparedLetter calls the transform_prepared_letter hook'; + + is( + $prepared_letter->{content}, + "notices_content() called. We identified this is the 'test' module" + . "\nThank you for using your local library!", + 'Patron object used correctly with scalar for content' + ); + is( + $prepared_letter->{title}, + $hold->id . "!", + 'Hold 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 77600be3d5e..46f0732dc6a 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -156,6 +156,16 @@ sub before_send_messages { Koha::Exception->throw("before_send_messages called"); } +sub notices_content { + my ( $self, $params ) = @_; + return { + custom_attribute => { + module => $params->{module}, + message => 'notices_content() called', + }, + }; +} + sub before_biblio_action { my ( $self, $params ) = @_; -- 2.49.0