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 <david@davidnind.com>
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
This commit is contained in:
Olivia Reynolds 2026-07-28 14:50:32 +00:00 committed by Pedro Amorim
parent dca7313511
commit fd24968eb2
No known key found for this signature in database
GPG Key ID: 06B75BA7F3894D55
2 changed files with 8 additions and 3 deletions

View File

@ -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}

View File

@ -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();