Bug 17476: Add a way to bypass dt_from_string processing time for slow servers
[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 => 60;
8
9 use Test::MockModule;
10 use Test::Warn;
11 use Time::HiRes qw/ gettimeofday /;
12 use t::lib::Mocks;
13
14 BEGIN { use_ok('Koha::DateUtils'); }
15
16 t::lib::Mocks::mock_preference('dateformat', 'us');
17 t::lib::Mocks::mock_preference('TimeFormat', 'This_is_not_used_but_called');
18
19 my $tz = C4::Context->tz;
20
21 isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
22
23 my $testdate_iso = '2011-06-16';                   # Bloomsday 2011
24 my $dt = dt_from_string( $testdate_iso, 'iso' );
25
26 isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
27
28 cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
29
30 $dt->set_hour(12);
31 $dt->set_minute(0);
32
33 my $date_string;
34
35 my $dateformat = C4::Context->preference('dateformat');
36 cmp_ok  output_pref({ dt => $dt, dateformat => $dateformat }),
37         'eq',
38         output_pref($dt),
39         'output_pref gives an hashref or a dt';
40
41 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '24hr' });
42 cmp_ok $date_string, 'eq', '2011-06-16 12:00', 'iso output';
43
44 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => '12hr' });
45 cmp_ok $date_string, 'eq', '2011-06-16 12:00 PM', 'iso output 12hr';
46
47 # "notime" doesn't actually mean anything in this context, but we
48 # can't pass undef or output_pref will try to access the database
49 $date_string = output_pref({ dt => $dt, dateformat => 'iso', timeformat => 'notime', dateonly => 1 });
50 cmp_ok $date_string, 'eq', '2011-06-16', 'iso output (date only)';
51
52 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '24hr' });
53 cmp_ok $date_string, 'eq', '06/16/2011 12:00', 'us output';
54
55 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr' });
56 cmp_ok $date_string, 'eq', '06/16/2011 12:00 PM', 'us output 12hr';
57
58 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => 'notime', dateonly => 1 });
59 cmp_ok $date_string, 'eq', '06/16/2011', 'us output (date only)';
60
61 # metric should return the French Revolutionary Calendar Really
62 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
63 cmp_ok $date_string, 'eq', '16/06/2011 12:00', 'metric output';
64
65 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => 'notime', dateonly => 1 });
66 cmp_ok $date_string, 'eq', '16/06/2011', 'metric output (date only)';
67
68 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
69 cmp_ok $date_string, 'eq', '16/06/2011 12:00',
70   'output_pref preserves non midnight HH:SS';
71
72 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
73 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
74 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
75 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
76     'Returned Dublin object matches input' );
77
78 SKIP: {
79     skip "Don't test execution time of dt_from_string on slow servers", 6 if $ENV{SLOW_SERVER};
80     for ( qw/ 2014-01-01 2100-01-01 9999-01-01 / ) {
81         my $duration = gettimeofday();
82         $new_dt = dt_from_string($_, 'iso', $dear_dirty_dublin);
83         $duration = gettimeofday() - $duration;
84         cmp_ok $duration, '<', 1, "Create DateTime with dt_from_string() for $_ with TZ in less than 1s";
85         $duration = gettimeofday();
86         output_pref( { dt => $new_dt } );
87         $duration = gettimeofday() - $duration;
88         cmp_ok $duration, '<', 1, "Create DateTime with output_pref() for $_ with TZ in less than 1s";
89     }
90 };
91
92 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
93 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
94 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
95
96 $new_dt = dt_from_string( $dt, 'iso' );
97 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
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', 'sql' );
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 my $module_context = new Test::MockModule('C4::Context');
167 $module_context->mock(
168     'tz',
169     sub {
170         return DateTime::TimeZone->new( name => 'Europe/Lisbon' );
171     }
172 );
173
174 $dt = dt_from_string('1979-04-01');
175 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
176
177 $module_context->mock(
178     'tz',
179     sub {
180         return DateTime::TimeZone->new( name => 'Europe/Paris' );
181     }
182 );
183
184 $dt = dt_from_string('2014-03-30 02:00:00');
185 isa_ok( $dt, 'DateTime', 'dt_from_string should return a DateTime object if a DST is given' );
186
187 # Test dt_from_string
188 t::lib::Mocks::mock_preference('dateformat', 'metric');
189 t::lib::Mocks::mock_preference('TimeFormat', '24hr');
190
191 # dt_from_string should take into account the dateformat pref, or the given parameter
192 $dt = dt_from_string('31/01/2015');
193 is( ref($dt), 'DateTime', '31/01/2015 is a correct date in metric format' );
194 is( output_pref( { dt => $dt, dateonly => 1 } ), '31/01/2015' );
195 $dt = eval { dt_from_string( '31/01/2015', 'iso' ); };
196 is( ref($dt), '', '31/01/2015 is not a correct date in iso format' );
197 $dt = eval { dt_from_string( '01/01/2015', 'iso' ); };
198 is( ref($dt), '', '01/01/2015 is not a correct date in iso format' );
199 $dt = eval { dt_from_string( '31/01/2015', 'us' ); };
200 is( ref($dt), '', '31/01/2015 is not a correct date in us format' );
201 $dt = dt_from_string( '01/01/2015', 'us' );
202 is( ref($dt), 'DateTime', '01/01/2015 is a correct date in us format' );
203 $dt = dt_from_string( '01.01.2015', 'dmydot' );
204 is( ref($dt), 'DateTime', '01.01.2015 is a correct date in dmydot format' );
205
206
207 # default value for hh and mm is 00:00
208 $dt = dt_from_string('31/01/2015');
209 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' );
210
211 $dt = dt_from_string('31/01/2015 12:34');
212 is( output_pref( { dt => $dt } ), '31/01/2015 12:34', 'dt_from_string should match hh:mm' );
213
214 $dt = dt_from_string('31/01/2015 12:34:56');
215 is( output_pref( { dt => $dt } ), '31/01/2015 12:34', 'dt_from_string should match hh:mm:ss' );
216
217 # date before 1900
218 $dt = dt_from_string('01/01/1900');
219 is( output_pref( { dt => $dt, dateonly => 1 } ), '01/01/1900', 'dt_from_string should manage date < 1900' );
220
221 # fallback
222 $dt = dt_from_string('2015-01-31 01:02:03');
223 is( output_pref( {dt => $dt} ), '31/01/2015 01:02', 'dt_from_string should fallback to sql format' );
224
225 # output_pref with str parameter
226 is( output_pref( { 'str' => $testdate_iso, dateformat => 'iso', dateonly => 1 } ), $testdate_iso, 'output_pref should handle correctly the iso parameter' );
227 my $output_for_invalid_date;
228 warning_like { $output_for_invalid_date = output_pref( { str => 'invalid_date' } ) }
229              { carped => qr[^Invalid date 'invalid_date' passed to output_pref] },
230              'output_pref should carp if an invalid date is passed for the str parameter';
231 is( $output_for_invalid_date, undef, 'output_pref should return undef if an invalid date is passed' );
232 warning_is { output_pref( { 'str' => $testdate_iso, dt => $dt, dateformat => 'iso', dateonly => 1 } ) }
233            { carped => 'output_pref should not be called with both dt and str parameters' },
234            'output_pref should carp if str and dt parameters are passed together';