Bug 11541: (follow-up) update tests
[koha.git] / t / DateUtils.t
1 use strict;
2 use warnings;
3 use 5.010;
4 use DateTime;
5 use DateTime::TimeZone;
6
7 use C4::Context;
8 use Test::More tests => 31;
9 use Test::MockModule;
10
11 BEGIN { use_ok('Koha::DateUtils'); }
12
13 my $tz = C4::Context->tz;
14
15 isa_ok( $tz, 'DateTime::TimeZone', 'Context returns timezone object' );
16
17 my $testdate_iso = '2011-06-16';                   # Bloomsday 2011
18 my $dt = dt_from_string( $testdate_iso, 'iso' );
19
20 isa_ok( $dt, 'DateTime', 'dt_from_string returns a DateTime object' );
21
22 cmp_ok( $dt->ymd(), 'eq', $testdate_iso, 'Returned object matches input' );
23
24 $dt->set_hour(12);
25 $dt->set_minute(0);
26
27 my $date_string;
28
29 my $module_context = new Test::MockModule('C4::Context');
30 $module_context->mock(
31     'preference',
32     sub {
33         return 'us';
34     }
35 );
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_due preserves non midnight HH:SS';
73
74 $dt->set_hour(23);
75 $dt->set_minute(59);
76 $date_string = output_pref_due({ dt => $dt, dateformat => 'metric', timeformat => '24hr' });
77 cmp_ok $date_string, 'eq', '16/06/2011',
78   'output_pref_due truncates HH:SS at midnight';
79
80 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
81 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
82 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
83 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
84     'Returned Dublin object matches input' );
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 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
116 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
117
118 $formatted = format_sqldatetime( undef, 'metric' );
119 cmp_ok( $formatted, 'eq', q{},
120     'format_sqldatetime formats undef as empty string' );
121
122 $formatted = format_sqlduedatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
123 cmp_ok(
124     $formatted, 'eq',
125     '16/06/2011 12:00',
126     'format_sqlduedatetime conversion for hourly loans'
127 );
128
129 $formatted = format_sqlduedatetime( '2011-06-16 23:59:07', 'metric', '24hr' );
130 cmp_ok( $formatted, 'eq', '16/06/2011',
131     'format_sqlduedatetime conversion for daily loans' );