6 use DateTime::Duration;
7 use Test::More tests => 26;
11 use_ok('Koha::Calendar');
13 # This was the only test C4 had
14 # Remove when no longer used
15 use_ok('C4::Calendar');
18 my $cal = Koha::Calendar->new( TEST_MODE => 1 );
20 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
22 my $saturday = DateTime->new(
26 time_zone => 'Europe/London',
28 my $sunday = DateTime->new(
32 time_zone => 'Europe/London',
34 my $monday = DateTime->new(
38 time_zone => 'Europe/London',
40 my $bloomsday = DateTime->new(
44 time_zone => 'Europe/London',
45 ); # should be a holiday
46 my $special = DateTime->new(
50 time_zone => 'Europe/London',
51 ); # should be a holiday
52 my $notspecial = DateTime->new(
56 time_zone => 'Europe/London',
57 ); # should NOT be a holiday
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' );
65 my $dt = $cal->addDate( $saturday, 1, 'days' );
66 is( $dt->day_of_week, 1, 'addDate skips closed Sunday' );
68 $dt = $cal->addDate( $bloomsday, -1 );
69 is( $dt->ymd(), '2011-06-15', 'Negative call to addDate' );
71 my $test_dt = DateTime->new( # Monday
77 time_zone => 'Europe/London',
80 my $later_dt = DateTime->new( # Monday
86 time_zone => 'Europe/London',
89 my $daycount = $cal->days_between( $test_dt, $later_dt );
90 cmp_ok( $daycount->in_units('days'),
91 '==', 48, 'days_between calculates correctly' );
95 $cal->set_daysmode('Calendar');
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' );
101 # example tests for bug report
102 $cal->clear_weekly_closed_days();
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' );
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' );
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 );
131 $cal = Koha::Calendar->new( TEST_MODE => 1 ,
132 days_mode => 'Datedue');
134 $cal->add_holiday( dt_from_string('2012-07-04','iso') );
135 $dt = dt_from_string( '2012-07-03','iso' );
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)' );
141 is($cal->addDate( $dt, $two_day_dur, 'days' ),
142 dt_from_string('2012-07-05','iso'),
143 'Two days add, skips holiday (Datedue)' );
145 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
146 '2012-07-30T11:53:00',
147 'Add 7 days (Datedue)' );
152 $cal = Koha::Calendar->new( TEST_MODE => 1,
153 days_mode => 'Calendar' );
155 $cal->add_holiday( dt_from_string('2012-07-04','iso') );
156 $dt = dt_from_string('2012-07-03','iso');
158 is($cal->addDate( $dt, $one_day_dur, 'days' ),
159 dt_from_string('2012-07-05','iso'),
160 'Single day add (Calendar)' );
162 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
163 '2012-07-31T11:53:00',
164 'Add 7 days (Calendar)' );
169 $cal = Koha::Calendar->new( TEST_MODE => 1,
170 days_mode => 'Days' );
172 $cal->add_holiday( dt_from_string('2012-07-04','iso') );
173 $dt = dt_from_string('2012-07-03','iso');
175 is($cal->addDate( $dt, $one_day_dur, 'days' ),
176 dt_from_string('2012-07-04','iso'),
177 'Single day add (Days)' );
179 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
180 '2012-07-30T11:53:00',
181 'Add 7 days (Days)' );