From d344a26f0286ec74e94c40d8b3647288fadcbe1d Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 19 Apr 2024 14:26:01 -0400 Subject: [PATCH] Bug 36654: Add template toolkit function to get arbitrary Koha::Objects A lot of older notice data include identifiers but not the objects themselves. We should have a helper TT plugin to make getting those objects easy. Test Plan: 1) prove t/db_dependent/Koha/Template/Plugin/Koha.t 2) Set the CHECKOUT notice to: [% USE Koha %] [% SET b = Koha.GetObjectById('Koha::Biblios', biblio.biblionumber ); %] [% b.title %] 3) Note the notice contains the checkout title Signed-off-by: Matt Blenkinsop --- Koha/Template/Plugin/Koha.pm | 21 ++++++++++++++ t/db_dependent/Koha/Template/Plugin/Koha.t | 32 +++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Koha/Template/Plugin/Koha.pm b/Koha/Template/Plugin/Koha.pm index 0b69249779..4ff72156ee 100644 --- a/Koha/Template/Plugin/Koha.pm +++ b/Koha/Template/Plugin/Koha.pm @@ -21,6 +21,8 @@ use Modern::Perl; use base qw( Template::Plugin ); +use Try::Tiny; + use C4::Context; use Koha::Token; use Koha; @@ -120,4 +122,23 @@ sub GenerateCSRF { return $csrf_token; } +=head3 GetObjectById + +Returns an singular object of the specified object set for the given id. + +[% SET object = Koha.GetObjectById('Koha::Patrons', patron_id ) %] + +=cut + +sub GetObjectById { + my ($self, $class, $id) = @_; + + my $object; + try { + $object = $class->find($id); + }; + + return $object; +} + 1; diff --git a/t/db_dependent/Koha/Template/Plugin/Koha.t b/t/db_dependent/Koha/Template/Plugin/Koha.t index ff50b3580e..492a5c01c2 100755 --- a/t/db_dependent/Koha/Template/Plugin/Koha.t +++ b/t/db_dependent/Koha/Template/Plugin/Koha.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; use Template::Context; use Template::Stash; @@ -25,8 +25,11 @@ use Template::Stash; use C4::Auth; use Koha::Cache::Memory::Lite; use Koha::Database; +use Koha::Patrons; use Koha::Template::Plugin::Koha; +use t::lib::TestBuilder; + my $schema = Koha::Database->new->schema; subtest 'GenerateCSRF() tests' => sub { @@ -75,3 +78,30 @@ subtest 'GenerateCSRF - New CSRF token generated everytime we need one' => sub { $schema->storage->txn_rollback; }; + +subtest 'GetObjectById tests' => sub { + plan tests => 3; + + $schema->storage->txn_begin; + + my $session = C4::Auth::get_session(''); + + my $stash = Template::Stash->new( { sessionID => $session->id } ); + my $context = Template::Context->new( { STASH => $stash } ); + + my $builder = t::lib::TestBuilder->new; + my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); + + my $plugin = Koha::Template::Plugin::Koha->new($context); + + my $patron2 = $plugin->GetObjectById('Koha::Patrons', $patron->id ); + is( $patron->id, $patron2->id, "Got correct object via GetObjectById" ); + + my $invalid = $plugin->GetObjectById('Koha::Patrons', 'INVALID_ID' ); + is( $invalid, undef, "Invalid ID returns undef" ); + + $invalid = $plugin->GetObjectById('Koha::Nothing', 1 ); + is( $invalid, undef, "Invalid objects name returns undef" ); + + $schema->storage->txn_rollback; +}; -- 2.37.1 (Apple Git-137.1)