Merge branch 'bug_2832' into 3.12-master
[koha.git] / t / Calendar.t
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5 use DateTime;
6 use DateTime::Duration;
7 use Test::More tests => 26;
8 use Koha::DateUtils;
9
10 BEGIN {
11     use_ok('Koha::Calendar');
12
13     # This was the only test C4 had
14     # Remove when no longer used
15     use_ok('C4::Calendar');
16 }
17
18 my $cal = Koha::Calendar->new( TEST_MODE => 1 );
19
20 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
21
22 my $saturday = DateTime->new(
23     year      => 2011,
24     month     => 6,
25     day       => 25,
26     time_zone => 'Europe/London',
27 );
28 my $sunday = DateTime->new(
29     year      => 2011,
30     month     => 6,
31     day       => 26,
32     time_zone => 'Europe/London',
33 );
34 my $monday = DateTime->new(
35     year      => 2011,
36     month     => 6,
37     day       => 27,
38     time_zone => 'Europe/London',
39 );
40 my $bloomsday = DateTime->new(
41     year      => 2011,
42     month     => 6,
43     day       => 16,
44     time_zone => 'Europe/London',
45 );    # should be a holiday
46 my $special = DateTime->new(
47     year      => 2011,
48     month     => 6,
49     day       => 1,
50     time_zone => 'Europe/London',
51 );    # should be a holiday
52 my $notspecial = DateTime->new(
53     year      => 2011,
54     month     => 6,
55     day       => 2,
56     time_zone => 'Europe/London',
57 );    # should NOT be a holiday
58
59 is( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );   # wee free test;
60 is( $cal->is_holiday($monday),     0, 'Monday is not a closed day' );    # alas
61 is( $cal->is_holiday($bloomsday),  1, 'month/day closed day test' );
62 is( $cal->is_holiday($special),    1, 'special closed day test' );
63 is( $cal->is_holiday($notspecial), 0, 'open day test' );
64
65 my $dt = $cal->addDate( $saturday, 1, 'days' );
66 is( $dt->day_of_week, 1, 'addDate skips closed Sunday' );
67
68 $dt = $cal->addDate( $bloomsday, -1 );
69 is( $dt->ymd(), '2011-06-15', 'Negative call to addDate' );
70
71 my $test_dt = DateTime->new(    # Monday
72     year      => 2012,
73     month     => 7,
74     day       => 23,
75     hour      => 11,
76     minute    => 53,
77     time_zone => 'Europe/London',
78 );
79
80 my $later_dt = DateTime->new(    # Monday
81     year      => 2012,
82     month     => 9,
83     day       => 17,
84     hour      => 17,
85     minute    => 30,
86     time_zone => 'Europe/London',
87 );
88
89 my $daycount = $cal->days_between( $test_dt, $later_dt );
90 cmp_ok( $daycount->in_units('days'),
91     '==', 48, 'days_between calculates correctly' );
92
93 my $ret;
94
95 $cal->set_daysmode('Calendar');
96
97 # see bugzilla #8966
98 is( $cal->is_holiday($later_dt), 0, 'is holiday for the next test' );
99 cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'Date should be the same after is_holiday' );
100
101 # example tests for bug report
102 $cal->clear_weekly_closed_days();
103
104 $daycount = $cal->days_between( dt_from_string('2012-01-10','iso'),
105     dt_from_string("2012-05-05",'iso') )->in_units('days');
106 cmp_ok( $daycount, '==', 116, 'test larger intervals' );
107 $daycount = $cal->days_between( dt_from_string("2012-01-01",'iso'),
108     dt_from_string("2012-05-05",'iso') )->in_units('days');
109 cmp_ok( $daycount, '==', 125, 'test positive intervals' );
110 my $daycount2 = $cal->days_between( dt_from_string("2012-05-05",'iso'),
111     dt_from_string("2012-01-01",'iso') )->in_units('days');
112 cmp_ok( $daycount2, '==', $daycount, 'test parameter order not relevant' );
113 $daycount = $cal->days_between( dt_from_string("2012-07-01",'iso'),
114     dt_from_string("2012-07-15",'iso') )->in_units('days');
115 cmp_ok( $daycount, '==', 14, 'days_between calculates correctly' );
116 $cal->add_holiday( dt_from_string('2012-07-06','iso') );
117 $daycount = $cal->days_between( dt_from_string("2012-07-01",'iso'),
118     dt_from_string("2012-07-15",'iso') )->in_units('days');
119 cmp_ok( $daycount, '==', 13, 'holiday correctly recognized' );
120
121 $cal->add_holiday( dt_from_string('2012-07-07','iso') );
122 $daycount = $cal->days_between( dt_from_string("2012-07-01",'iso'),
123     dt_from_string("2012-07-15",'iso') )->in_units('days');
124 cmp_ok( $daycount, '==', 12, 'multiple holidays correctly recognized' );
125
126 my $one_day_dur = DateTime::Duration->new( days => 1 );
127 my $two_day_dur = DateTime::Duration->new( days => 2 );
128 my $seven_day_dur = DateTime::Duration->new( days => 7 );
129
130     ## 'Datedue' tests
131     $cal = Koha::Calendar->new( TEST_MODE => 1 ,
132                                 days_mode => 'Datedue');
133
134     $cal->add_holiday( dt_from_string('2012-07-04','iso') );
135     $dt = dt_from_string( '2012-07-03','iso' );
136
137     is($cal->addDate( $dt, $one_day_dur, 'days' ),
138         dt_from_string('2012-07-05','iso'),
139         'Single day add (Datedue, matches holiday, shift)' );
140
141     is($cal->addDate( $dt, $two_day_dur, 'days' ),
142         dt_from_string('2012-07-05','iso'),
143         'Two days add, skips holiday (Datedue)' );
144
145     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
146         '2012-07-30T11:53:00',
147         'Add 7 days (Datedue)' );
148
149
150
151     ## 'Calendar' tests'
152     $cal = Koha::Calendar->new( TEST_MODE => 1,
153                                 days_mode => 'Calendar' );
154
155     $cal->add_holiday( dt_from_string('2012-07-04','iso') );
156     $dt = dt_from_string('2012-07-03','iso');
157
158     is($cal->addDate( $dt, $one_day_dur, 'days' ),
159         dt_from_string('2012-07-05','iso'),
160         'Single day add (Calendar)' );
161
162     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
163        '2012-07-31T11:53:00',
164        'Add 7 days (Calendar)' );
165
166
167
168     ## 'Days' tests
169     $cal = Koha::Calendar->new( TEST_MODE => 1,
170                                 days_mode => 'Days' );
171
172     $cal->add_holiday( dt_from_string('2012-07-04','iso') );
173     $dt = dt_from_string('2012-07-03','iso');
174
175     is($cal->addDate( $dt, $one_day_dur, 'days' ),
176         dt_from_string('2012-07-04','iso'),
177         'Single day add (Days)' );
178
179     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
180         '2012-07-30T11:53:00',
181         'Add 7 days (Days)' );