From 22139db49dcd1293d170f2a40cf5adfec7f8f223 Mon Sep 17 00:00:00 2001 From: Colin Campbell Date: Wed, 15 Jun 2011 11:18:38 +0100 Subject: [PATCH] Bug 5549 : Add Tests For Koha::DateUtils Add testscript for DateUtils Add a parameter to override system pref to output_pref not for use in calling software (its superfluous) but to enable testing of call with different settings --- Koha/DateUtils.pm | 20 +++++++++++++++++--- t/dateutils.t | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 t/dateutils.t diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm index fb9e2133a8..1e7d19819d 100644 --- a/Koha/DateUtils.pm +++ b/Koha/DateUtils.pm @@ -80,15 +80,29 @@ s/(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})/$1-$2-$3T$4:$5:$6/; $date_string =~ s/00T/01T/; } } - return DateTime::Format::DateParse->parse_datetime( $date_string, $tz->name() ); + return DateTime::Format::DateParse->parse_datetime( $date_string, + $tz->name() ); } return DateTime->now( time_zone => $tz ); } +=head2 output_pref + +$date_string = output_pref($dt, [$format] ); + +Returns a string containing the time & date formatted as per the C4::Context setting + +A second parameter allows overriding of the syspref value. This is for testing only +In usage use the DateTime objects own methods for non standard formatting + +=cut + sub output_pref { - my $dt = shift; - my $pref = C4::Context->preference('dateformat'); + my $dt = shift; + my $force_pref = shift; # if testing we want to override Context + my $pref = + defined $force_pref ? $force_pref : C4::Context->preference('dateformat'); given ($pref) { when (/^iso/) { return $dt->strftime('%Y-%m-%d %H:%M'); diff --git a/t/dateutils.t b/t/dateutils.t new file mode 100644 index 0000000000..3d64d4f742 --- /dev/null +++ b/t/dateutils.t @@ -0,0 +1,32 @@ +use strict; +use warnings; +use 5.010; + +use C4::Context; +use Test::More tests => 7; # last test to print + +BEGIN { use_ok('Koha::DateUtils'); } + +my $tz = C4::Context->tz; + +isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' ); + +my $testdate_iso = '2011-06-16'; # Bloomsday 2011 +my $dt = dt_from_string( $testdate_iso, 'iso' ); + +isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' ); + +cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' ); + +$dt->set_hour(12); +$dt->set_minute(0); + +my $date_string = output_pref( $dt, 'iso' ); +cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output'; + +$date_string = output_pref( $dt, 'us' ); +cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output'; + +# metric should return the French Revolutionary Calendar Really +$date_string = output_pref( $dt, 'metric' ); +cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output'; -- 2.39.5