From d2643f29464e88dc1837e7ad705720a976f4c718 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 29 Jul 2025 11:15:31 -0300 Subject: [PATCH] Bug 40542: Add cancellation_reason to Koha::Hold->strings_map This patch adds the mentioned AV to the returned strings structure. It also adds tests for the method. To test: 1. Apply this patch 2. Run: $ ktd --shell k$ prove t/db_dependent/Koha/Hold.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: David Nind Signed-off-by: Jonathan Druart --- Koha/Hold.pm | 30 +++++++++++++++++-- t/db_dependent/Koha/Hold.t | 61 +++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/Koha/Hold.pm b/Koha/Hold.pm index 59b498a7d22..bdebac76511 100644 --- a/Koha/Hold.pm +++ b/Koha/Hold.pm @@ -1067,10 +1067,34 @@ Returns a map of column name to string representations including the string. =cut sub strings_map { - my ($self) = @_; - return { - pickup_library_id => { str => $self->branch->branchname, type => 'library' }, + my ( $self, $params ) = @_; + + my $strings = { + pickup_library_id => { str => $self->pickup_library->branchname, type => 'library' }, }; + + if ( defined $self->cancellation_reason ) { + my $av = Koha::AuthorisedValues->search( + { + category => 'HOLD_CANCELLATION', + authorised_value => $self->cancellation_reason, + } + ); + my $cancellation_reason_str = + $av->count + ? $params->{public} + ? $av->next->opac_description + : $av->next->lib + : $self->cancellation_reason; + + $strings->{cancellation_reason} = { + category => 'HOLD_CANCELLATION', + str => $cancellation_reason_str, + type => 'av', + }; + } + + return $strings; } =head2 Internal methods diff --git a/t/db_dependent/Koha/Hold.t b/t/db_dependent/Koha/Hold.t index 55fe078227b..c34441971c3 100755 --- a/t/db_dependent/Koha/Hold.t +++ b/t/db_dependent/Koha/Hold.t @@ -20,7 +20,7 @@ use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 15; +use Test::More tests => 16; use Test::Exception; use Test::MockModule; @@ -1200,3 +1200,62 @@ subtest 'change_type() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'strings_map() tests' => sub { + + plan tests => 3; + + $schema->txn_begin; + + my $av = Koha::AuthorisedValue->new( + { + category => 'HOLD_CANCELLATION', + authorised_value => 'JUST_BECAUSE', + lib => 'Just because', + lib_opac => 'Serious reasons', + } + )->store; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + + my $hold = $builder->build_object( + { + class => 'Koha::Holds', + value => { cancellation_reason => $av->authorised_value, branchcode => $library->id } + } + ); + + my $strings_map = $hold->strings_map( { public => 0 } ); + is_deeply( + $strings_map, + { + pickup_library_id => { str => $library->branchname, type => 'library' }, + cancellation_reason => { str => $av->lib, type => 'av', category => 'HOLD_CANCELLATION' }, + }, + 'Strings map is correct' + ); + + $strings_map = $hold->strings_map( { public => 1 } ); + is_deeply( + $strings_map, + { + pickup_library_id => { str => $library->branchname, type => 'library' }, + cancellation_reason => { str => $av->lib_opac, type => 'av', category => 'HOLD_CANCELLATION' }, + }, + 'Strings map is correct (OPAC)' + ); + + $av->delete(); + + $strings_map = $hold->strings_map( { public => 1 } ); + is_deeply( + $strings_map, + { + pickup_library_id => { str => $library->branchname, type => 'library' }, + cancellation_reason => { str => $hold->cancellation_reason, type => 'av', category => 'HOLD_CANCELLATION' }, + }, + 'Strings map shows the cancellation_value when AV not present' + ); + + $schema->txn_rollback; +}; -- 2.34.1