From fd24968eb2b1292622f8ab0e448517e0fa900a6d Mon Sep 17 00:00:00 2001 From: Olivia Reynolds Date: Tue, 28 Jul 2026 14:50:32 +0000 Subject: [PATCH] Bug 41648: Handle parse errors in KohaDates If dt_from_string throws an exception whilst trying to parse the input, catch it and return an empty result. Test plan: 1. Apply patch. 2. Prove t/db_dependent/Template/Plugin/KohaDates.t Sponsored-by: Open Fifth Signed-off-by: David Nind Signed-off-by: Pedro Amorim --- Koha/Template/Plugin/KohaDates.pm | 4 +++- t/db_dependent/Template/Plugin/KohaDates.t | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Koha/Template/Plugin/KohaDates.pm b/Koha/Template/Plugin/KohaDates.pm index e202d5dd63..af681cc83b 100644 --- a/Koha/Template/Plugin/KohaDates.pm +++ b/Koha/Template/Plugin/KohaDates.pm @@ -31,7 +31,9 @@ sub filter { return "" unless $text; $config->{with_hours} //= 0; - my $dt = dt_from_string( $text, 'iso' ); + my $dt = eval { dt_from_string( $text, 'iso' ) }; + return "" if $@; + $dt->add(%$_) for _parse_config_for_durations($config); # Allow date add/subtract; see _parse_config_for_durations return $config->{as_due_date} diff --git a/t/db_dependent/Template/Plugin/KohaDates.t b/t/db_dependent/Template/Plugin/KohaDates.t index e4c0dd43cf..4666cd6a70 100755 --- a/t/db_dependent/Template/Plugin/KohaDates.t +++ b/t/db_dependent/Template/Plugin/KohaDates.t @@ -7,7 +7,7 @@ use Koha::DateUtils qw( dt_from_string output_pref ); use Test::MockModule; use Test::NoWarnings; -use Test::More tests => 10; +use Test::More tests => 11; use t::lib::Mocks; BEGIN { @@ -22,10 +22,13 @@ my $context = C4::Context->new(); my $filter = Koha::Template::Plugin::KohaDates->new(); ok( $filter, "new()" ); +my $filtered_date = $filter->filter('01-01-01'); +is( $filtered_date, '', 'invalid date returns empty string' ) or diag('invalid date fails'); + t::lib::Mocks::mock_preference( "dateformat", 'iso' ); $context->clear_syspref_cache(); -my $filtered_date = $filter->filter($date); +$filtered_date = $filter->filter($date); is( $filtered_date, $date, "iso conversion" ) or diag("iso conversion fails"); #$filter = Koha::Template::Plugin::KohaDates->new();