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 => 16;
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 $dbh->do("DELETE FROM special_holidays");
48 Koha::Caches->get_instance->flush_all;
50 # Artificially set timezone
51 my $timezone = 'America/Santiago';
56 my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
57 my $calendar = Koha::Calendar->new( branchcode => $branch );
59 C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
63 title => 'Invalid date',
64 description => 'Invalid date description',
67 my $exception_holiday = $calendar->exception_holidays->iterator->next;
68 my $now_dt = DateTime->now;
71 eval { $diff = $calendar->days_between( $now_dt, $exception_holiday ) };
74 qr/Invalid local time for date in time zone: America\/Santiago/,
75 'Avoid invalid datetime due to DST'
78 $schema->storage->txn_rollback;
81 $schema->storage->txn_begin;
83 # Create two fresh branches for the tests
84 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
85 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
87 C4::Calendar->new( branchcode => $branch_1 )->insert_week_day_holiday(
90 description => 'Sundays',
93 my $holiday2add = dt_from_string("2015-01-01");
94 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
95 day => $holiday2add->day(),
96 month => $holiday2add->month(),
97 year => $holiday2add->year(),
99 description => "New Year's Day",
101 $holiday2add = dt_from_string("2014-12-25");
102 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
103 day => $holiday2add->day(),
104 month => $holiday2add->month(),
105 year => $holiday2add->year(),
107 description => 'Christmas',
110 my $koha_calendar = Koha::Calendar->new( branchcode => $branch_1 );
111 my $c4_calendar = C4::Calendar->new( branchcode => $branch_1 );
113 isa_ok( $koha_calendar, 'Koha::Calendar', 'Koha::Calendar class returned' );
114 isa_ok( $c4_calendar, 'C4::Calendar', 'C4::Calendar class returned' );
116 my $sunday = DateTime->new(
121 my $monday = DateTime->new(
126 my $christmas = DateTime->new(
131 my $newyear = DateTime->new(
137 is( $koha_calendar->is_holiday($sunday), 1, 'Sunday is a closed day' );
138 is( $koha_calendar->is_holiday($monday), 0, 'Monday is not a closed day' );
139 is( $koha_calendar->is_holiday($christmas), 1, 'Christmas is a closed day' );
140 is( $koha_calendar->is_holiday($newyear), 1, 'New Years day is a closed day' );
142 $dbh->do("DELETE FROM repeatable_holidays");
143 $dbh->do("DELETE FROM special_holidays");
145 my $custom_holiday = DateTime->new(
151 my $today = dt_from_string();
152 C4::Calendar->new( branchcode => $branch_2 )->insert_single_holiday(
153 day => $today->day(),
154 month => $today->month(),
155 year => $today->year(),
157 description => "$today",
160 is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" );
161 is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1");
163 # Few tests for exception holidays
164 my ( $diff, $cal, $special );
165 $dbh->do("DELETE FROM special_holidays");
166 _add_exception( $today, $branch_1, 'Today' );
167 $cal = Koha::Calendar->new( branchcode => $branch_1 );
168 $special = $cal->exception_holidays;
169 is( $special->count, 1, 'One exception holiday added' );
171 my $tomorrow= dt_from_string();
172 $tomorrow->add_duration( DateTime::Duration->new(days => 1) );
173 _add_exception( $tomorrow, $branch_1, 'Tomorrow' );
174 $cal = Koha::Calendar->new( branchcode => $branch_1 );
175 $special = $cal->exception_holidays;
176 is( $special->count, 2, 'Set of exception holidays contains two dates' );
178 $diff = $today->delta_days( $special->min )->in_units('days');
179 is( $diff, 0, 'Lowest exception holiday is today' );
180 $diff = $tomorrow->delta_days( $special->max )->in_units('days');
181 is( $diff, 0, 'Highest exception holiday is tomorrow' );
183 C4::Calendar->new( branchcode => $branch_1 )->delete_holiday(
184 weekday => $tomorrow->day_of_week,
185 day => $tomorrow->day,
186 month => $tomorrow->month,
187 year => $tomorrow->year,
189 $cal = Koha::Calendar->new( branchcode => $branch_1 );
190 $special = $cal->exception_holidays;
191 is( $special->count, 1, 'Set of exception holidays back to one' );
194 my ( $dt, $branch, $descr ) = @_;
195 C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
200 description => $descr,
204 $schema->storage->txn_rollback;