Bug 13814 : add 6 unit tests in t/Boolean.t
[koha.git] / t / DateUtils.t
1 use Modern::Perl;
2 use DateTime;
3 use DateTime::TimeZone;
4
5 use C4::Context;
6 use Test::More tests => 44;
7 use Test::MockModule;
8 use Time::HiRes qw/ gettimeofday /;
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 $module_context = new Test::MockModule('C4::Context');
29 $module_context->mock(
30     'preference',
31     sub {
32         return 'us';
33     }
34 );
35
36 my $dateformat = C4::Context->preference('dateformat');
37 cmp_ok  output_pref({ dt => $dt, dateformat => $dateformat }),
38         'eq',
39         output_pref($dt),
40         'output_pref gives an hashref or a dt';
41
42 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '24hr' });
43 cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
44
45 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '12hr' });
46 cmp_ok $date_string, 'eq', '2011-06-16 12:00 PM', 'iso output 12hr';
47
48 # "notime" doesn't actually mean anything in this context, but we
49 # can't pass undef or output_pref will try to access the database
50 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => 'notime', dateonly => 1 });
51 cmp_ok $date_string, 'eq', '2011-06-16', 'iso output (date only)';
52
53 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '24hr' });
54 cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
55
56 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr' });
57 cmp_ok $date_string, 'eq', '06/16/2011 12:00 PM', 'us output 12hr';
58
59 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => 'notime', dateonly => 1 });
60 cmp_ok $date_string, 'eq', '06/16/2011', 'us output (date only)';
61
62 # metric should return the French Revolutionary Calendar Really
63 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
64 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
65
66 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => 'notime', dateonly => 1 });
67 cmp_ok $date_string, 'eq', '16/06/2011', 'metric output (date only)';
68
69 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
70 cmp_ok $date_string, 'eq', '16/06/2011 12:00',
71   'output_pref preserves non midnight HH:SS';
72
73 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
74 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
75 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
76 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
77     'Returned Dublin object matches input' );
78
79 for ( qw/ 2014-01-01 2100-01-01 9999-01-01 / ) {
80     my $duration = gettimeofday();
81     $new_dt = dt_from_string($_, 'iso', $dear_dirty_dublin);
82     $duration = gettimeofday() - $duration;
83     cmp_ok $duration, '<', 1, "Create DateTime with dt_from_string() for $_ with TZ in less than 1s";
84     $duration = gettimeofday();
85     output_pref( { dt => $new_dt } );
86     $duration = gettimeofday() - $duration;
87     cmp_ok $duration, '<', 1, "Create DateTime with output_pref() for $_ with TZ in less than 1s";
88 }
89
90 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
91 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
92 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
93
94 $new_dt = dt_from_string( $dt, 'iso' );
95 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
96
97 # C4::Dates allowed 00th of the month
98
99 my $ymd = '2012-01-01';
100 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
101 isa_ok( $dt0, 'DateTime',
102     'dt_from_string returns a DateTime object passed a zero metric day' );
103 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
104
105 $dt0 = dt_from_string( '01/00/2012', 'us' );
106 isa_ok( $dt0, 'DateTime',
107     'dt_from_string returns a DateTime object passed a zero us day' );
108 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
109
110 $dt0 = dt_from_string( '2012-01-00', 'iso' );
111 isa_ok( $dt0, 'DateTime',
112     'dt_from_string returns a DateTime object passed a zero iso day' );
113 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
114
115 # Return undef if passed mysql 0 dates
116 $dt0 = dt_from_string( '0000-00-00', 'iso' );
117 is( $dt0, undef, "undefined returned for 0 iso date" );
118
119 # Return undef if passed mysql 9999-* date
120 my $dt9999 = dt_from_string( '9999-12-31' );
121 is( $dt9999->ymd(), '9999-12-31', "dt_from_string should return a DateTime object for 9999-12-31" );
122
123 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
124 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
125
126 $formatted = format_sqldatetime( undef, 'metric' );
127 cmp_ok( $formatted, 'eq', q{},
128     'format_sqldatetime formats undef as empty string' );
129
130 # Test the as_due_date parameter
131 $dt = DateTime->new(
132     year       => 2013,
133     month      => 12,
134     day        => 11,
135     hour       => 23,
136     minute     => 59,
137 );
138 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
139 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 24hr';
140
141 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', dateonly => 1, as_due_date => 1});
142 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 24hr';
143
144 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', as_due_date => 1 });
145 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 12hr';
146
147 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', dateonly => 1, as_due_date => 1});
148 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 12hr';
149
150 # Test as_due_date for hourly loans
151 $dt = DateTime->new(
152     year       => 2013,
153     month      => 12,
154     day        => 11,
155     hour       => 18,
156     minute     => 35,
157 );
158 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
159 cmp_ok $date_string, 'eq', '11/12/2013 18:35', 'as_due_date with hours and timeformat 24hr (non-midnight time)';
160 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr', as_due_date => 1 });
161 cmp_ok $date_string, 'eq', '12/11/2013 06:35 PM', 'as_due_date with hours and timeformat 12hr (non-midnight time)';
162
163 my $now = DateTime->now;
164 is( dt_from_string, $now, "Without parameter, dt_from_string should return today" );
165
166 $module_context->mock(
167     'tz',
168     sub {
169         return DateTime::TimeZone->new( name => 'Europe/Lisbon' );
170     }
171 );
172
173 $dt = dt_from_string('1979-04-01');
174 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
175
176 $module_context->mock(
177     'tz',
178     sub {
179         return DateTime::TimeZone->new( name => 'Europe/Paris' );
180     }
181 );
182
183 $dt = dt_from_string('2014-03-30 02:00:00');
184 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );