Bug 5549 : Add Tests For Koha::DateUtils
authorColin Campbell <colin.campbell@ptfs-europe.com>
Wed, 15 Jun 2011 10:18:38 +0000 (11:18 +0100)
committerChris Cormack <chrisc@catalyst.net.nz>
Mon, 19 Mar 2012 23:24:37 +0000 (12:24 +1300)
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
t/dateutils.t [new file with mode: 0644]

index fb9e213..1e7d198 100644 (file)
@@ -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 (file)
index 0000000..3d64d4f
--- /dev/null
@@ -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';