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