3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 17;
23 use DateTime::TimeZone;
25 use t::lib::TestBuilder;
32 use_ok('Koha::Calendar');
33 use_ok('C4::Calendar');
36 my $schema = Koha::Database->new->schema;
37 my $dbh = C4::Context->dbh;
38 my $builder = t::lib::TestBuilder->new;
40 subtest 'exception_holidays() tests' => sub {
44 $schema->storage->txn_begin;
46 my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
47 my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
49 $dbh->do("DELETE FROM special_holidays");
51 Koha::Caches->get_instance->flush_all;
53 my $holiday2add = dt_from_string("2030-07-07");
54 C4::Calendar->new( branchcode => $branch )->insert_day_month_holiday(
55 day => $holiday2add->day(),
56 month => $holiday2add->month(),
57 year => $holiday2add->year(),
59 description => "This is a holiday, for now",
62 C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
66 title => 'Not a holiday',
67 description => 'This date should not be a holiday',
70 my $calendar = Koha::Calendar->new( branchcode => $branch2 );
71 $calendar->exception_holidays(); #This sets exception holiday in cache
72 $calendar = Koha::Calendar->new( branchcode => $branch );
73 is( $calendar->is_holiday( dt_from_string('2020-07-07') ), 0, "The date is not a holiday");
75 $dbh->do("DELETE FROM special_holidays");
77 Koha::Caches->get_instance->flush_all;
79 # Artificially set timezone
80 my $timezone = 'America/Santiago';
85 $calendar = Koha::Calendar->new( branchcode => $branch );
87 C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
91 title => 'Invalid date',
92 description => 'Invalid date description',
95 my $exception_holiday = $calendar->exception_holidays->iterator->next;
96 my $now_dt = DateTime->now;
99 eval { $diff = $calendar->days_between( $now_dt, $exception_holiday ) };
102 qr/Invalid local time for date in time zone: America\/Santiago/,
103 'Avoid invalid datetime due to DST'
106 $schema->storage->txn_rollback;
109 $schema->storage->txn_begin;
111 # Create two fresh branches for the tests
112 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
113 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
115 C4::Calendar->new( branchcode => $branch_1 )->insert_week_day_holiday(
118 description => 'Sundays',
121 my $holiday2add = dt_from_string("2015-01-01");
122 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
123 day => $holiday2add->day(),
124 month => $holiday2add->month(),
125 year => $holiday2add->year(),
127 description => "New Year's Day",
129 $holiday2add = dt_from_string("2014-12-25");
130 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
131 day => $holiday2add->day(),
132 month => $holiday2add->month(),
133 year => $holiday2add->year(),
135 description => 'Christmas',
138 my $koha_calendar = Koha::Calendar->new( branchcode => $branch_1 );
139 my $c4_calendar = C4::Calendar->new( branchcode => $branch_1 );
141 isa_ok( $koha_calendar, 'Koha::Calendar', 'Koha::Calendar class returned' );
142 isa_ok( $c4_calendar, 'C4::Calendar', 'C4::Calendar class returned' );
144 my $sunday = DateTime->new(
149 my $monday = DateTime->new(
154 my $christmas = DateTime->new(
159 my $newyear = DateTime->new(
165 is( $koha_calendar->is_holiday($sunday), 1, 'Sunday is a closed day' );
166 is( $koha_calendar->is_holiday($monday), 0, 'Monday is not a closed day' );
167 is( $koha_calendar->is_holiday($christmas), 1, 'Christmas is a closed day' );
168 is( $koha_calendar->is_holiday($newyear), 1, 'New Years day is a closed day' );
170 $dbh->do("DELETE FROM repeatable_holidays");
171 $dbh->do("DELETE FROM special_holidays");
173 my $custom_holiday = DateTime->new(
179 my $today = dt_from_string();
180 C4::Calendar->new( branchcode => $branch_2 )->insert_single_holiday(
181 day => $today->day(),
182 month => $today->month(),
183 year => $today->year(),
185 description => "$today",
188 is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" );
189 is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1");
191 # Few tests for exception holidays
192 my ( $diff, $cal, $special );
193 $dbh->do("DELETE FROM special_holidays");
194 _add_exception( $today, $branch_1, 'Today' );
195 $cal = Koha::Calendar->new( branchcode => $branch_1 );
196 $special = $cal->exception_holidays;
197 is( $special->count, 1, 'One exception holiday added' );
199 my $tomorrow= dt_from_string();
200 $tomorrow->add_duration( DateTime::Duration->new(days => 1) );
201 _add_exception( $tomorrow, $branch_1, 'Tomorrow' );
202 $cal = Koha::Calendar->new( branchcode => $branch_1 );
203 $special = $cal->exception_holidays;
204 is( $special->count, 2, 'Set of exception holidays contains two dates' );
206 $diff = $today->delta_days( $special->min )->in_units('days');
207 is( $diff, 0, 'Lowest exception holiday is today' );
208 $diff = $tomorrow->delta_days( $special->max )->in_units('days');
209 is( $diff, 0, 'Highest exception holiday is tomorrow' );
211 C4::Calendar->new( branchcode => $branch_1 )->delete_holiday(
212 weekday => $tomorrow->day_of_week,
213 day => $tomorrow->day,
214 month => $tomorrow->month,
215 year => $tomorrow->year,
217 $cal = Koha::Calendar->new( branchcode => $branch_1 );
218 $special = $cal->exception_holidays;
219 is( $special->count, 1, 'Set of exception holidays back to one' );
222 my ( $dt, $branch, $descr ) = @_;
223 C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
228 description => $descr,
232 $schema->storage->txn_rollback;
234 subtest 'copy_to_branch' => sub {
238 $schema->storage->txn_begin;
240 my $branch1 = $builder->build( { source => 'Branch' } )->{ branchcode };
241 my $calendar1 = C4::Calendar->new( branchcode => $branch1 );
242 my $sunday = dt_from_string("2020-03-15");
243 $calendar1->insert_week_day_holiday(
246 description => 'Sundays',
249 my $day_month = dt_from_string("2020-03-17");
250 $calendar1->insert_day_month_holiday(
251 day => $day_month->day(),
252 month => $day_month->month(),
253 year => $day_month->year(),
258 my $future_date = dt_from_string("9999-12-31");
259 $calendar1->insert_single_holiday(
260 day => $future_date->day(),
261 month => $future_date->month(),
262 year => $future_date->year(),
267 my $future_exception = dt_from_string("9999-12-30");
268 $calendar1->insert_exception_holiday(
269 day => $future_exception->day(),
270 month => $future_exception->month(),
271 year => $future_exception->year(),
276 my $past_date = dt_from_string("2019-11-20");
277 $calendar1->insert_single_holiday(
278 day => $past_date->day(),
279 month => $past_date->month(),
280 year => $past_date->year(),
285 my $past_exception = dt_from_string("2020-03-09");
286 $calendar1->insert_exception_holiday(
287 day => $past_exception->day(),
288 month => $past_exception->month(),
289 year => $past_exception->year(),
294 my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
296 C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
298 my $calendar2 = C4::Calendar->new( branchcode => $branch2 );
299 my $exceptions = $calendar2->get_exception_holidays;
301 is( $calendar2->isHoliday( $sunday->day, $sunday->month, $sunday->year ), 1, "Weekday holiday copied to branch 2" );
302 is( $calendar2->isHoliday( $day_month->day, $day_month->month, $day_month->year ), 1, "Day/month holiday copied to branch 2" );
303 is( $calendar2->isHoliday( $future_date->day, $future_date->month, $future_date->year ), 1, "Single holiday copied to branch 2" );
304 is( ( grep { $_->{date} eq "9999-12-30"} values %$exceptions ), 1, "Exception holiday copied to branch 2" );
305 is( $calendar2->isHoliday( $past_date->day, $past_date->month, $past_date->year ), 0, "Don't copy past single holidays" );
306 is( ( grep { $_->{date} eq "2020-03-09"} values %$exceptions ), 0, "Don't copy past exception holidays " );
308 C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
310 #Select all rows with same values from database
311 my $dbh = C4::Context->dbh;
312 my $get_repeatable_holidays = "SELECT a.* FROM repeatable_holidays a
313 JOIN (SELECT branchcode, weekday, day, month, COUNT(*)
314 FROM repeatable_holidays
315 GROUP BY branchcode, weekday, day, month HAVING count(*) > 1) b
316 ON a.branchcode = b.branchcode
317 AND ( a.weekday = b.weekday OR (a.day = b.day AND a.month = b.month))
318 ORDER BY a.branchcode;";
319 my $sth = $dbh->prepare($get_repeatable_holidays);
322 my @repeatable_holidays;
323 while(my $row = $sth->fetchrow_hashref){
324 push @repeatable_holidays, $row
327 is( scalar(@repeatable_holidays), 0, "None of the repeatable holidays were doubled");
329 my $get_special_holidays = "SELECT a.* FROM special_holidays a
330 JOIN (SELECT branchcode, day, month, year, isexception, COUNT(*)
331 FROM special_holidays
332 GROUP BY branchcode, day, month, year, isexception HAVING count(*) > 1) b
333 ON a.branchcode = b.branchcode
334 AND a.day = b.day AND a.month = b.month AND a.year = b.year AND a.isexception = b.isexception
335 ORDER BY a.branchcode;";
336 $sth = $dbh->prepare($get_special_holidays);
339 my @special_holidays;
340 while(my $row = $sth->fetchrow_hashref){
341 push @special_holidays, $row
344 is( scalar(@special_holidays), 0, "None of the special holidays were doubled");
346 $schema->storage->txn_rollback;
351 Koha::Caches->get_instance->flush_all;