Bug 30717: (QA follow-up) Move to module
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Fri, 13 May 2022 07:29:04 +0000 (07:29 +0000)
committerFridolin Somers <fridolin.somers@biblibre.com>
Sat, 14 May 2022 01:41:55 +0000 (15:41 -1000)
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 <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
Koha/DateUtils.pm
cataloguing/value_builder/dateaccessioned.pl
t/DateUtils.t

index acd5dd1..2ee889a 100644 (file)
@@ -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;
index 272b5ae..2f027c8 100755 (executable)
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 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  = <<END_OF_JS;
 <script>
index 27bea28..575e9c1 100755 (executable)
@@ -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