Bug 18336: Atomic update
[koha.git] / t / DateUtils.t
1 use Modern::Perl;
2 use DateTime;
3 use DateTime::TimeZone;
4
5 use C4::Context;
6
7 use Test::More tests => 67;
8
9 use Test::MockModule;
10 use Test::Warn;
11 use Time::HiRes qw/ gettimeofday /;
12 use Try::Tiny;
13
14 use t::lib::Mocks;
15
16 BEGIN { use_ok('Koha::DateUtils'); }
17
18 t::lib::Mocks::mock_preference('dateformat', 'us');
19 t::lib::Mocks::mock_preference('TimeFormat', 'This_is_not_used_but_called');
20
21 my $tz = C4::Context->tz;
22
23 isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
24
25 my $testdate_iso = '2011-06-16';                   # Bloomsday 2011
26 my $dt = dt_from_string( $testdate_iso, 'iso' );
27
28 isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
29
30 cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
31
32 $dt->set_hour(12);
33 $dt->set_minute(0);
34
35 my $date_string;
36
37 my $dateformat = C4::Context->preference('dateformat');
38 cmp_ok  output_pref({ dt => $dt, dateformat => $dateformat }),
39         'eq',
40         output_pref($dt),
41         'output_pref gives an hashref or a dt';
42
43 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '24hr' });
44 cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
45
46 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '12hr' });
47 cmp_ok $date_string, 'eq', '2011-06-16 12:00 PM', 'iso output 12hr';
48
49 $date_string = output_pref({ dt => $dt, dateformat => 'rfc3339' });
50 like($date_string, qr/2011-06-16T12:00:00\+|-\d\d:\d\d/, 'RFC3339 output');
51
52 # "notime" doesn't actually mean anything in this context, but we
53 # can't pass undef or output_pref will try to access the database
54 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => 'notime', dateonly => 1 });
55 cmp_ok $date_string, 'eq', '2011-06-16', 'iso output (date only)';
56
57 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '24hr' });
58 cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
59
60 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr' });
61 cmp_ok $date_string, 'eq', '06/16/2011 12:00 PM', 'us output 12hr';
62
63 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => 'notime', dateonly => 1 });
64 cmp_ok $date_string, 'eq', '06/16/2011', 'us output (date only)';
65
66 # metric should return the French Revolutionary Calendar Really
67 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
68 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
69
70 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => 'notime', dateonly => 1 });
71 cmp_ok $date_string, 'eq', '16/06/2011', 'metric output (date only)';
72
73 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
74 cmp_ok $date_string, 'eq', '16/06/2011 12:00',
75   'output_pref preserves non midnight HH:SS';
76
77 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
78 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
79 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
80 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
81     'Returned Dublin object matches input' );
82
83 SKIP: {
84     skip "Don't test execution time of dt_from_string on slow servers", 6 if $ENV{SLOW_SERVER};
85     for ( qw/ 2014-01-01 2100-01-01 9999-01-01 / ) {
86         my $duration = gettimeofday();
87         $new_dt = dt_from_string($_, 'iso', $dear_dirty_dublin);
88         $duration = gettimeofday() - $duration;
89         cmp_ok $duration, '<', 1, "Create DateTime with dt_from_string() for $_ with TZ in less than 1s";
90         $duration = gettimeofday();
91         output_pref( { dt => $new_dt } );
92         $duration = gettimeofday() - $duration;
93         cmp_ok $duration, '<', 1, "Create DateTime with output_pref() for $_ with TZ in less than 1s";
94     }
95 };
96
97 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
98 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
99 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
100
101 $new_dt = dt_from_string( $dt, 'iso' );
102 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
103
104 my $ymd = '2012-01-01';
105 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
106 isa_ok( $dt0, 'DateTime',
107     'dt_from_string returns a DateTime object passed a zero metric day' );
108 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
109
110 $dt0 = dt_from_string( '01/00/2012', 'us' );
111 isa_ok( $dt0, 'DateTime',
112     'dt_from_string returns a DateTime object passed a zero us day' );
113 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
114
115 $dt0 = dt_from_string( '2012-01-00', 'iso' );
116 isa_ok( $dt0, 'DateTime',
117     'dt_from_string returns a DateTime object passed a zero iso day' );
118 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
119
120 $dt0 = dt_from_string( '2012-01-00T12:00:00Z', 'rfc3339' );
121 isa_ok( $dt0, 'DateTime',
122     'dt_from_string returns a DateTime object passed a zero rfc3339 day' );
123 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects rfc3339 day 0' );
124
125 # Return undef if passed mysql 0 dates
126 $dt0 = dt_from_string( '0000-00-00', 'iso' );
127 is( $dt0, undef, "undefined returned for 0 iso date" );
128
129 # Return undef if passed mysql 9999-* date
130 my $dt9999 = dt_from_string( '9999-12-31', 'sql' );
131 is( $dt9999->ymd(), '9999-12-31', "dt_from_string should return a DateTime object for 9999-12-31" );
132
133 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
134 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
135
136 $formatted = format_sqldatetime( undef, 'metric' );
137 cmp_ok( $formatted, 'eq', q{},
138     'format_sqldatetime formats undef as empty string' );
139
140 # Test the as_due_date parameter
141 $dt = DateTime->new(
142     year       => 2013,
143     month      => 12,
144     day        => 11,
145     hour       => 23,
146     minute     => 59,
147 );
148 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
149 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 24hr';
150
151 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', dateonly => 1, as_due_date => 1});
152 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 24hr';
153
154 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', as_due_date => 1 });
155 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 12hr';
156
157 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', dateonly => 1, as_due_date => 1});
158 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 12hr';
159
160 # Test as_due_date for hourly loans
161 $dt = DateTime->new(
162     year       => 2013,
163     month      => 12,
164     day        => 11,
165     hour       => 18,
166     minute     => 35,
167 );
168 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
169 cmp_ok $date_string, 'eq', '11/12/2013 18:35', 'as_due_date with hours and timeformat 24hr (non-midnight time)';
170 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr', as_due_date => 1 });
171 cmp_ok $date_string, 'eq', '12/11/2013 06:35 PM', 'as_due_date with hours and timeformat 12hr (non-midnight time)';
172
173 my $now = DateTime->now;
174 is( dt_from_string, $now, "Without parameter, dt_from_string should return today" );
175
176 my $module_context = new Test::MockModule('C4::Context');
177 $module_context->mock(
178     'tz',
179     sub {
180         return DateTime::TimeZone->new( name => 'Europe/Lisbon' );
181     }
182 );
183
184 $dt = dt_from_string('1979-04-01');
185 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
186
187 $module_context->mock(
188     'tz',
189     sub {
190         return DateTime::TimeZone->new( name => 'Europe/Paris' );
191     }
192 );
193
194 $dt = dt_from_string('2014-03-30 02:00:00');
195 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
196
197 # Test dt_from_string
198 t::lib::Mocks::mock_preference('dateformat', 'metric');
199 t::lib::Mocks::mock_preference('TimeFormat', '24hr');
200
201 # dt_from_string should take into account the dateformat pref, or the given parameter
202 $dt = dt_from_string('31/01/2015');
203 is( ref($dt), 'DateTime', '31/01/2015 is a correct date in metric format' );
204 is( output_pref( { dt => $dt, dateonly => 1 } ), '31/01/2015' );
205 $dt = eval { dt_from_string( '31/01/2015', 'iso' ); };
206 is( ref($dt), '', '31/01/2015 is not a correct date in iso format' );
207 $dt = eval { dt_from_string( '01/01/2015', 'iso' ); };
208 is( ref($dt), '', '01/01/2015 is not a correct date in iso format' );
209 $dt = eval { dt_from_string( '01/01/2015', 'rfc3339' ); };
210 is( ref($dt), '', '01/01/2015 is not a correct date in rfc3339 format' );
211 $dt = eval { dt_from_string( '31/01/2015', 'us' ); };
212 is( ref($dt), '', '31/01/2015 is not a correct date in us format' );
213 $dt = dt_from_string( '01/01/2015', 'us' );
214 is( ref($dt), 'DateTime', '01/01/2015 is a correct date in us format' );
215 $dt = dt_from_string( '01.01.2015', 'dmydot' );
216 is( ref($dt), 'DateTime', '01.01.2015 is a correct date in dmydot format' );
217
218
219 # default value for hh and mm is 00:00
220 $dt = dt_from_string('31/01/2015');
221 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' );
222
223 $dt = dt_from_string('31/01/2015 12:34');
224 is( output_pref( { dt => $dt } ), '31/01/2015 12:34', 'dt_from_string should match hh:mm' );
225
226 $dt = dt_from_string('31/01/2015 12:34:56');
227 is( output_pref( { dt => $dt } ), '31/01/2015 12:34', 'dt_from_string should match hh:mm:ss' );
228
229 # date before 1900
230 $dt = dt_from_string('01/01/1900');
231 is( output_pref( { dt => $dt, dateonly => 1 } ), '01/01/1900', 'dt_from_string should manage date < 1900' );
232
233 # fallback
234 $dt = dt_from_string('2015-01-31 01:02:03');
235 is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' );
236
237 # output_pref with no parameters, single parameter (no hash)
238 is( output_pref(), undef, 'Call output_pref without parameters' );
239 try {
240     output_pref( 'no_dt' );
241     ok( 0, 'Passed single invalid dt to output_pref' );
242 } catch {
243     is( ref($_), 'Koha::Exceptions::WrongParameter',
244         'Passed single invalid dt to output_pref' );
245 };
246
247 # pass invalid dt via hash
248 try {
249     output_pref({ dt => 'no_dt' });
250     ok( 0, 'Passed invalid dt in hash to output_pref' );
251 } catch {
252     is( ref($_), 'Koha::Exceptions::WrongParameter',
253         'Passed invalid dt in hash to output_pref' );
254 };
255
256 # output_pref with str parameter
257 is( output_pref( { 'str' => $testdate_iso, dateformat => 'iso', dateonly => 1 } ), $testdate_iso, 'output_pref should handle correctly the iso parameter' );
258 my $output_for_invalid_date;
259 try {
260     $output_for_invalid_date = output_pref({ str => 'invalid_date' });
261     ok( 0, 'Invalid date string passed to output_pref' );
262 } catch {
263     is( ref($_), 'Koha::Exceptions::WrongParameter',
264         'Invalid date string passed to output_pref' );
265 };
266 is( $output_for_invalid_date, undef, 'No return value from output_pref' );
267 try {
268     output_pref({ 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 });
269     ok( 0, 'output_pref should carp if str and dt parameters are passed together' );
270 } catch {
271     is( ref($_), 'Koha::Exceptions::WrongParameter', 'output_pref should throw an exception if str and dt parameters are passed together' );
272 };
273
274 # End of tests