Bug 766: Create a plugin routine to build dropdown list
[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 => 34;
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 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 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
81 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
82 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
83
84 $new_dt = dt_from_string( $dt, 'iso' );
85 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
86
87 # C4::Dates allowed 00th of the month
88
89 my $ymd = '2012-01-01';
90 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
91 isa_ok( $dt0, 'DateTime',
92     'dt_from_string returns a DateTime object passed a zero metric day' );
93 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
94
95 $dt0 = dt_from_string( '01/00/2012', 'us' );
96 isa_ok( $dt0, 'DateTime',
97     'dt_from_string returns a DateTime object passed a zero us day' );
98 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
99
100 $dt0 = dt_from_string( '2012-01-00', 'iso' );
101 isa_ok( $dt0, 'DateTime',
102     'dt_from_string returns a DateTime object passed a zero iso day' );
103 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
104
105 # Return undef if passed mysql 0 dates
106 $dt0 = dt_from_string( '0000-00-00', 'iso' );
107 is( $dt0, undef, "undefined returned for 0 iso date" );
108
109 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
110 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
111
112 $formatted = format_sqldatetime( undef, 'metric' );
113 cmp_ok( $formatted, 'eq', q{},
114     'format_sqldatetime formats undef as empty string' );
115
116 # Test the as_due_date parameter
117 $dt = DateTime->new(
118     year       => 2013,
119     month      => 12,
120     day        => 11,
121     hour       => 23,
122     minute     => 59,
123 );
124 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
125 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 24hr';
126
127 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', dateonly => 1, as_due_date => 1});
128 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 24hr';
129
130 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', as_due_date => 1 });
131 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 12hr';
132
133 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', dateonly => 1, as_due_date => 1});
134 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 12hr';
135
136 # Test as_due_date for hourly loans
137 $dt = DateTime->new(
138     year       => 2013,
139     month      => 12,
140     day        => 11,
141     hour       => 18,
142     minute     => 35,
143 );
144 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
145 cmp_ok $date_string, 'eq', '11/12/2013 18:35', 'as_due_date with hours and timeformat 24hr (non-midnight time)';
146 $date_string = output_pref({ dt => $dt, dateformat => 'us', timeformat => '12hr', as_due_date => 1 });
147 cmp_ok $date_string, 'eq', '12/11/2013 06:35 PM', 'as_due_date with hours and timeformat 12hr (non-midnight time)';