From e0814f2cc051d52a128a2f23f76169cd2e2716a3 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Fri, 13 May 2022 07:29:04 +0000 Subject: [PATCH] Bug 30717: (QA follow-up) Move to module We will probably use this a bit more :) Let's put it in a module (with a trivial test). Test plan: Repeat item edit. Run t/DateUtils.t Signed-off-by: Marcel de Rooy Signed-off-by: Fridolin Somers --- Koha/DateUtils.pm | 23 ++++++++++++++++++++ cataloguing/value_builder/dateaccessioned.pl | 10 ++------- t/DateUtils.t | 14 ++++++++++-- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index acd5dd13ff..2ee889aac5 100644 --- a/Koha/DateUtils.pm +++ b/Koha/DateUtils.pm @@ -30,6 +30,7 @@ BEGIN { dt_from_string output_pref format_sqldatetime + flatpickr_date_format ); } @@ -376,4 +377,26 @@ sub format_sqldatetime { return q{}; } +=head2 flatpickr_date_format + +$date_format = flatpickr_date_format( $koha_date_format ); + +Converts Koha's date format to Flatpickr's. E.g. 'us' returns 'm/d/Y'. + +If no argument is given, the dateformat preference is assumed. + +Returns undef if format is unknown. + +=cut + +sub flatpickr_date_format { + my $arg = shift // C4::Context->preference('dateformat'); + return { + us => 'm/d/Y', + metric => 'd/m/Y', + dmydot => 'd.m.Y', + iso => 'Y-m-d', + }->{$arg}; +} + 1; diff --git a/cataloguing/value_builder/dateaccessioned.pl b/cataloguing/value_builder/dateaccessioned.pl index 272b5aecfe..2f027c8e9a 100755 --- a/cataloguing/value_builder/dateaccessioned.pl +++ b/cataloguing/value_builder/dateaccessioned.pl @@ -21,20 +21,14 @@ # along with Koha; if not, see . use Modern::Perl; -use Koha::DateUtils qw( dt_from_string output_pref ); +use Koha::DateUtils qw( dt_from_string output_pref flatpickr_date_format ); my $builder = sub { my ( $params ) = @_; my $function_name = $params->{id}; my $date = output_pref({ dt => dt_from_string, dateonly => 1 }); - - my $dateformat_pref = C4::Context->preference('dateformat'); - my $dateformat = - $dateformat_pref eq 'us' ? 'm/d/Y' - : $dateformat_pref eq 'metric' ? 'd/m/Y' - : $dateformat_pref eq 'dmydot' ? 'd.m.Y' - : 'Y-m-d'; + my $dateformat = flatpickr_date_format(); my $res = < diff --git a/t/DateUtils.t b/t/DateUtils.t index 27bea28d28..575e9c1358 100755 --- a/t/DateUtils.t +++ b/t/DateUtils.t @@ -4,14 +4,14 @@ use DateTime::TimeZone; use C4::Context; -use Test::More tests => 82; +use Test::More tests => 83; use Test::MockModule; use Test::Warn; use Time::HiRes qw/ gettimeofday /; use Try::Tiny; -use Koha::DateUtils qw( dt_from_string output_pref format_sqldatetime ); +use Koha::DateUtils qw( dt_from_string output_pref format_sqldatetime flatpickr_date_format ); use t::lib::Mocks; @@ -389,4 +389,14 @@ try { is( ref($_), 'Koha::Exceptions::WrongParameter', 'output_pref should throw an exception if str and dt parameters are passed together' ); }; +subtest 'flatpickr_date_format' => sub { + plan tests => 4; + + t::lib::Mocks::mock_preference('dateformat', 'iso'); + is( flatpickr_date_format(), 'Y-m-d', 'check fallback to pref' ); + is( flatpickr_date_format(q{}), undef, 'empty string' ); + is( flatpickr_date_format('metric'), 'd/m/Y', 'check valid arg' ); + is( flatpickr_date_format('zz'), undef, 'wrong arg' ); +}; + # End of tests -- 2.39.5