Bug 14870: (followup) Remove stray C4::Dates
[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 => 59;
7 use Test::MockModule;
8 use Test::Warn;
9 use Time::HiRes qw/ gettimeofday /;
10 use t::lib::Mocks;
11
12 BEGIN { use_ok('Koha::DateUtils'); }
13
14 t::lib::Mocks::mock_preference('dateformat', 'us');
15 t::lib::Mocks::mock_preference('TimeFormat', 'This_is_not_used_but_called');
16
17 my $tz = C4::Context->tz;
18
19 isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
20
21 my $testdate_iso = '2011-06-16';                   # Bloomsday 2011
22 my $dt = dt_from_string( $testdate_iso, 'iso' );
23
24 isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
25
26 cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
27
28 $dt->set_hour(12);
29 $dt->set_minute(0);
30
31 my $date_string;
32
33 my $dateformat = C4::Context->preference('dateformat');
34 cmp_ok  output_pref({ dt => $dt, dateformat => $dateformat }),
35         'eq',
36         output_pref($dt),
37         'output_pref gives an hashref or a dt';
38
39 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '24hr' });
40 cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
41
42 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '12hr' });
43 cmp_ok $date_string, 'eq', '2011-06-16 12:00 PM', 'iso output 12hr';
44
45 # "notime" doesn't actually mean anything in this context, but we
46 # can't pass undef or output_pref will try to access the database
47 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => 'notime', dateonly => 1 });
48 cmp_ok $date_string, 'eq', '2011-06-16', 'iso output (date only)';
49
50 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '24hr' });
51 cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
52
53 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr' });
54 cmp_ok $date_string, 'eq', '06/16/2011 12:00 PM', 'us output 12hr';
55
56 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => 'notime', dateonly => 1 });
57 cmp_ok $date_string, 'eq', '06/16/2011', 'us output (date only)';
58
59 # metric should return the French Revolutionary Calendar Really
60 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
61 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
62
63 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => 'notime', dateonly => 1 });
64 cmp_ok $date_string, 'eq', '16/06/2011', 'metric output (date only)';
65
66 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
67 cmp_ok $date_string, 'eq', '16/06/2011 12:00',
68   'output_pref preserves non midnight HH:SS';
69
70 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
71 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
72 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
73 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
74     'Returned Dublin object matches input' );
75
76 for ( qw/ 2014-01-01 2100-01-01 9999-01-01 / ) {
77     my $duration = gettimeofday();
78     $new_dt = dt_from_string($_, 'iso', $dear_dirty_dublin);
79     $duration = gettimeofday() - $duration;
80     cmp_ok $duration, '<', 1, "Create DateTime with dt_from_string() for $_ with TZ in less than 1s";
81     $duration = gettimeofday();
82     output_pref( { dt => $new_dt } );
83     $duration = gettimeofday() - $duration;
84     cmp_ok $duration, '<', 1, "Create DateTime with output_pref() for $_ with TZ in less than 1s";
85 }
86
87 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
88 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
89 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
90
91 $new_dt = dt_from_string( $dt, 'iso' );
92 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
93
94 my $ymd = '2012-01-01';
95 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
96 isa_ok( $dt0, 'DateTime',
97     'dt_from_string returns a DateTime object passed a zero metric day' );
98 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
99
100 $dt0 = dt_from_string( '01/00/2012', 'us' );
101 isa_ok( $dt0, 'DateTime',
102     'dt_from_string returns a DateTime object passed a zero us day' );
103 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
104
105 $dt0 = dt_from_string( '2012-01-00', 'iso' );
106 isa_ok( $dt0, 'DateTime',
107     'dt_from_string returns a DateTime object passed a zero iso day' );
108 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
109
110 # Return undef if passed mysql 0 dates
111 $dt0 = dt_from_string( '0000-00-00', 'iso' );
112 is( $dt0, undef, "undefined returned for 0 iso date" );
113
114 # Return undef if passed mysql 9999-* date
115 my $dt9999 = dt_from_string( '9999-12-31', 'sql' );
116 is( $dt9999->ymd(), '9999-12-31', "dt_from_string should return a DateTime object for 9999-12-31" );
117
118 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
119 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
120
121 $formatted = format_sqldatetime( undef, 'metric' );
122 cmp_ok( $formatted, 'eq', q{},
123     'format_sqldatetime formats undef as empty string' );
124
125 # Test the as_due_date parameter
126 $dt = DateTime->new(
127     year       => 2013,
128     month      => 12,
129     day        => 11,
130     hour       => 23,
131     minute     => 59,
132 );
133 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
134 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 24hr';
135
136 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', dateonly => 1, as_due_date => 1});
137 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 24hr';
138
139 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', as_due_date => 1 });
140 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 12hr';
141
142 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', dateonly => 1, as_due_date => 1});
143 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 12hr';
144
145 # Test as_due_date for hourly loans
146 $dt = DateTime->new(
147     year       => 2013,
148     month      => 12,
149     day        => 11,
150     hour       => 18,
151     minute     => 35,
152 );
153 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
154 cmp_ok $date_string, 'eq', '11/12/2013 18:35', 'as_due_date with hours and timeformat 24hr (non-midnight time)';
155 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr', as_due_date => 1 });
156 cmp_ok $date_string, 'eq', '12/11/2013 06:35 PM', 'as_due_date with hours and timeformat 12hr (non-midnight time)';
157
158 my $now = DateTime->now;
159 is( dt_from_string, $now, "Without parameter, dt_from_string should return today" );
160
161 my $module_context = new Test::MockModule('C4::Context');
162 $module_context->mock(
163     'tz',
164     sub {
165         return DateTime::TimeZone->new( name => 'Europe/Lisbon' );
166     }
167 );
168
169 $dt = dt_from_string('1979-04-01');
170 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
171
172 $module_context->mock(
173     'tz',
174     sub {
175         return DateTime::TimeZone->new( name => 'Europe/Paris' );
176     }
177 );
178
179 $dt = dt_from_string('2014-03-30 02:00:00');
180 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
181
182 # Test dt_from_string
183 t::lib::Mocks::mock_preference('dateformat', 'metric');
184 t::lib::Mocks::mock_preference('TimeFormat', '24hr');
185
186 # dt_from_string should take into account the dateformat pref, or the given parameter
187 $dt = dt_from_string('31/01/2015');
188 is( ref($dt), 'DateTime', '31/01/2015 is a correct date in metric format' );
189 is( output_pref( { dt => $dt, dateonly => 1 } ), '31/01/2015' );
190 $dt = eval { dt_from_string( '31/01/2015', 'iso' ); };
191 is( ref($dt), '', '31/01/2015 is not a correct date in iso format' );
192 $dt = eval { dt_from_string( '01/01/2015', 'iso' ); };
193 is( ref($dt), '', '01/01/2015 is not a correct date in iso format' );
194 $dt = eval { dt_from_string( '31/01/2015', 'us' ); };
195 is( ref($dt), '', '31/01/2015 is not a correct date in us format' );
196 $dt = dt_from_string( '01/01/2015', 'us' );
197 is( ref($dt), 'DateTime', '01/01/2015 is a correct date in us format' );
198
199
200 # default value for hh and mm is 00:00
201 $dt = dt_from_string('31/01/2015');
202 is( output_pref( { dt => $dt } ), '31/01/2015 00:00', 'dt_from_string should generate a DT object with 00:00 as default hh:mm' );
203
204 $dt = dt_from_string('31/01/2015 12:34');
205 is( output_pref( { dt => $dt } ), '31/01/2015 12:34', 'dt_from_string should match hh:mm' );
206
207 $dt = dt_from_string('31/01/2015 12:34:56');
208 is( output_pref( { dt => $dt } ), '31/01/2015 12:34', 'dt_from_string should match hh:mm:ss' );
209
210 # date before 1900
211 $dt = dt_from_string('01/01/1900');
212 is( output_pref( { dt => $dt, dateonly => 1 } ), '01/01/1900', 'dt_from_string should manage date < 1900' );
213
214 # fallback
215 $dt = dt_from_string('2015-01-31 01:02:03');
216 is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' );
217
218 # output_pref with str parameter
219 is( output_pref( { 'str' => $testdate_iso, dateformat => 'iso', dateonly => 1 } ), $testdate_iso, 'output_pref should handle correctly the iso parameter' );
220 my $output_for_invalid_date;
221 warning_like { $output_for_invalid_date = output_pref( { str => 'invalid_date' } ) }
222              { carped => qr[^Invalid date 'invalid_date' passed to output_pref] },
223              'output_pref should carp if an invalid date is passed for the str parameter';
224 is( $output_for_invalid_date, undef, 'output_pref should return undef if an invalid date is passed' );
225 warning_is { output_pref( { 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 } ) }
226            { carped => 'output_pref should not be called with both dt and str parameters' },
227            'output_pref should carp if str and dt parameters are passed together';