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