Bug 10380: Change prototype for output_pref() routine
[koha_fer] / t / DateUtils.t
1 use strict;
2 use warnings;
3 use 5.010;
4 use DateTime;
5 use DateTime::TimeZone;
6
7 use C4::Context;
8 use Test::More tests => 31;
9
10 BEGIN { use_ok('Koha::DateUtils'); }
11
12 my $tz = C4::Context->tz;
13
14 isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
15
16 my $testdate_iso = '2011-06-16';                   # Bloomsday 2011
17 my $dt = dt_from_string( $testdate_iso, 'iso' );
18
19 isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
20
21 cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
22
23 $dt->set_hour(12);
24 $dt->set_minute(0);
25
26 my $date_string;
27
28 my $dateformat = C4::Context->preference('dateformat');
29 cmp_ok  output_pref({ dt => $dt, dateformat => $dateformat }),
30         'eq',
31         output_pref($dt),
32         'output_pref gives an hashref or a dt';
33
34 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '24hr' });
35 cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
36
37 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '12hr' });
38 cmp_ok $date_string, 'eq', '2011-06-16 12:00 PM', 'iso output 12hr';
39
40 # "notime" doesn't actually mean anything in this context, but we
41 # can't pass undef or output_pref will try to access the database
42 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => 'notime', dateonly => 1 });
43 cmp_ok $date_string, 'eq', '2011-06-16', 'iso output (date only)';
44
45 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '24hr' });
46 cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
47
48 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr' });
49 cmp_ok $date_string, 'eq', '06/16/2011 12:00 PM', 'us output 12hr';
50
51 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => 'notime', dateonly => 1 });
52 cmp_ok $date_string, 'eq', '06/16/2011', 'us output (date only)';
53
54 # metric should return the French Revolutionary Calendar Really
55 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
56 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
57
58 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => 'notime', dateonly => 1 });
59 cmp_ok $date_string, 'eq', '16/06/2011', 'metric output (date only)';
60
61 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
62 cmp_ok $date_string, 'eq', '16/06/2011 12:00',
63   'output_pref_due preserves non midnight HH:SS';
64
65 $dt->set_hour(23);
66 $dt->set_minute(59);
67 $date_string = output_pref_due({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
68 cmp_ok $date_string, 'eq', '16/06/2011',
69   'output_pref_due truncates HH:SS at midnight';
70
71 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
72 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
73 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
74 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
75     'Returned Dublin object matches input' );
76
77 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
78 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
79 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
80
81 $new_dt = dt_from_string( $dt, 'iso' );
82 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
83
84 # C4::Dates allowed 00th of the month
85
86 my $ymd = '2012-01-01';
87 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
88 isa_ok( $dt0, 'DateTime',
89     'dt_from_string returns a DateTime object passed a zero metric day' );
90 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
91
92 $dt0 = dt_from_string( '01/00/2012', 'us' );
93 isa_ok( $dt0, 'DateTime',
94     'dt_from_string returns a DateTime object passed a zero us day' );
95 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
96
97 $dt0 = dt_from_string( '2012-01-00', 'iso' );
98 isa_ok( $dt0, 'DateTime',
99     'dt_from_string returns a DateTime object passed a zero iso day' );
100 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
101
102 # Return undef if passed mysql 0 dates
103 $dt0 = dt_from_string( '0000-00-00', 'iso' );
104 is( $dt0, undef, "undefined returned for 0 iso date" );
105
106 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
107 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
108
109 $formatted = format_sqldatetime( undef, 'metric' );
110 cmp_ok( $formatted, 'eq', q{},
111     'format_sqldatetime formats undef as empty string' );
112
113 $formatted = format_sqlduedatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
114 cmp_ok(
115     $formatted, 'eq',
116     '16/06/2011 12:00',
117     'format_sqlduedatetime conversion for hourly loans'
118 );
119
120 $formatted = format_sqlduedatetime( '2011-06-16 23:59:07', 'metric', '24hr' );
121 cmp_ok( $formatted, 'eq', '16/06/2011',
122     'format_sqlduedatetime conversion for daily loans' );