Bug 11148: Add a as_due_date parameter to the output_pref routine
[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 my $dear_dirty_dublin = DateTime::TimeZone->new( name => 'Europe/Dublin' );
71 my $new_dt = dt_from_string( '16/06/2011', 'metric', $dear_dirty_dublin );
72 isa_ok( $new_dt, 'DateTime', 'Create DateTime with different timezone' );
73 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso,
74     'Returned Dublin object matches input' );
75
76 $new_dt = dt_from_string( '2011-06-16 12:00', 'sql' );
77 isa_ok( $new_dt, 'DateTime', 'Create DateTime from (mysql) sql' );
78 cmp_ok( $new_dt->ymd(), 'eq', $testdate_iso, 'sql returns correct date' );
79
80 $new_dt = dt_from_string( $dt, 'iso' );
81 isa_ok( $new_dt, 'DateTime', 'Passed a DateTime dt_from_string returns it' );
82
83 # C4::Dates allowed 00th of the month
84
85 my $ymd = '2012-01-01';
86 my $dt0 = dt_from_string( '00/01/2012', 'metric' );
87 isa_ok( $dt0, 'DateTime',
88     'dt_from_string returns a DateTime object passed a zero metric day' );
89 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects metric day 0' );
90
91 $dt0 = dt_from_string( '01/00/2012', 'us' );
92 isa_ok( $dt0, 'DateTime',
93     'dt_from_string returns a DateTime object passed a zero us day' );
94 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects us day 0' );
95
96 $dt0 = dt_from_string( '2012-01-00', 'iso' );
97 isa_ok( $dt0, 'DateTime',
98     'dt_from_string returns a DateTime object passed a zero iso day' );
99 cmp_ok( $dt0->ymd(), 'eq', $ymd, 'Returned object corrects iso day 0' );
100
101 # Return undef if passed mysql 0 dates
102 $dt0 = dt_from_string( '0000-00-00', 'iso' );
103 is( $dt0, undef, "undefined returned for 0 iso date" );
104
105 my $formatted = format_sqldatetime( '2011-06-16 12:00:07', 'metric', '24hr' );
106 cmp_ok( $formatted, 'eq', '16/06/2011 12:00', 'format_sqldatetime conversion' );
107
108 $formatted = format_sqldatetime( undef, 'metric' );
109 cmp_ok( $formatted, 'eq', q{},
110     'format_sqldatetime formats undef as empty string' );
111
112 # Test the as_due_date parameter
113 $dt = DateTime->new(
114     year       => 2013,
115     month      => 12,
116     day        => 11,
117     hour       => 23,
118     minute     => 59,
119 );
120 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', as_due_date => 1 });
121 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 24hr';
122
123 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '24hr', dateonly => 1, as_due_date => 1});
124 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 24hr';
125
126 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', as_due_date => 1 });
127 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date with hours and timeformat 12hr';
128
129 $date_string = output_pref({ dt => $dt, dateformat => 'metric', timeformat => '12hr', dateonly => 1, as_due_date => 1});
130 cmp_ok $date_string, 'eq', '11/12/2013', 'as_due_date without hours and timeformat 12hr';