Bug 17909: Followup - fix typos
[koha.git] / t / db_dependent / Holidays.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 16;
21
22 use DateTime;
23 use DateTime::TimeZone;
24
25 use t::lib::TestBuilder;
26 use C4::Context;
27 use C4::Branch;
28 use Koha::Database;
29 use Koha::DateUtils;
30
31
32 BEGIN {
33     use_ok('Koha::Calendar');
34     use_ok('C4::Calendar');
35 }
36
37 my $schema = Koha::Database->new->schema;
38 my $dbh = C4::Context->dbh;
39 my $builder = t::lib::TestBuilder->new;
40
41 subtest 'exception_holidays() tests' => sub {
42
43     plan tests => 1;
44
45     $schema->storage->txn_begin;
46
47     $dbh->do("DELETE FROM special_holidays");
48
49     # Artificially set timezone
50     my $timezone = 'America/Santiago';
51     $ENV{TZ} = $timezone;
52     use POSIX qw(tzset);
53     tzset;
54
55     my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
56     my $calendar = Koha::Calendar->new( branchcode => $branch );
57
58     C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
59         day         => 6,
60         month       => 9,
61         year        => 2015,
62         title       => 'Invalid date',
63         description => 'Invalid date description',
64     );
65
66     my $exception_holiday = $calendar->exception_holidays->iterator->next;
67     my $now_dt            = DateTime->now;
68
69     my $diff;
70     eval { $diff = $calendar->days_between( $now_dt, $exception_holiday ) };
71     unlike(
72         $@,
73         qr/Invalid local time for date in time zone: America\/Santiago/,
74         'Avoid invalid datetime due to DST'
75     );
76
77     $schema->storage->txn_rollback;
78 };
79
80 $schema->storage->txn_begin;
81
82 # Create two fresh branches for the tests
83 my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
84 my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
85
86 C4::Calendar->new( branchcode => $branch_1 )->insert_week_day_holiday(
87     weekday     => 0,
88     title       => '',
89     description => 'Sundays',
90 );
91
92 my $holiday2add = dt_from_string("2015-01-01");
93 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
94     day         => $holiday2add->day(),
95     month       => $holiday2add->month(),
96     year        => $holiday2add->year(),
97     title       => '',
98     description => "New Year's Day",
99 );
100 $holiday2add = dt_from_string("2014-12-25");
101 C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
102     day         => $holiday2add->day(),
103     month       => $holiday2add->month(),
104     year        => $holiday2add->year(),
105     title       => '',
106     description => 'Christmas',
107 );
108
109 my $koha_calendar = Koha::Calendar->new( branchcode => $branch_1 );
110 my $c4_calendar = C4::Calendar->new( branchcode => $branch_1 );
111
112 isa_ok( $koha_calendar, 'Koha::Calendar', 'Koha::Calendar class returned' );
113 isa_ok( $c4_calendar,   'C4::Calendar',   'C4::Calendar class returned' );
114
115 my $sunday = DateTime->new(
116     year  => 2011,
117     month => 6,
118     day   => 26,
119 );
120 my $monday = DateTime->new(
121     year  => 2011,
122     month => 6,
123     day   => 27,
124 );
125 my $christmas = DateTime->new(
126     year  => 2032,
127     month => 12,
128     day   => 25,
129 );
130 my $newyear = DateTime->new(
131     year  => 2035,
132     month => 1,
133     day   => 1,
134 );
135
136 is( $koha_calendar->is_holiday($sunday),    1, 'Sunday is a closed day' );
137 is( $koha_calendar->is_holiday($monday),    0, 'Monday is not a closed day' );
138 is( $koha_calendar->is_holiday($christmas), 1, 'Christmas is a closed day' );
139 is( $koha_calendar->is_holiday($newyear),   1, 'New Years day is a closed day' );
140
141 $dbh->do("DELETE FROM repeatable_holidays");
142 $dbh->do("DELETE FROM special_holidays");
143
144 my $custom_holiday = DateTime->new(
145     year  => 2013,
146     month => 11,
147     day   => 12,
148 );
149
150 my $today = dt_from_string();
151 C4::Calendar->new( branchcode => $branch_2 )->insert_single_holiday(
152     day         => $today->day(),
153     month       => $today->month(),
154     year        => $today->year(),
155     title       => "$today",
156     description => "$today",
157 );
158
159 is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" );
160 is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1");
161
162 # Few tests for exception holidays
163 my ( $diff, $cal, $special );
164 $dbh->do("DELETE FROM special_holidays");
165 _add_exception( $today, $branch_1, 'Today' );
166 $cal = Koha::Calendar->new( branchcode => $branch_1 );
167 $special = $cal->exception_holidays;
168 is( $special->count, 1, 'One exception holiday added' );
169
170 my $tomorrow= dt_from_string();
171 $tomorrow->add_duration( DateTime::Duration->new(days => 1) );
172 _add_exception( $tomorrow, $branch_1, 'Tomorrow' );
173 $cal = Koha::Calendar->new( branchcode => $branch_1 );
174 $special = $cal->exception_holidays;
175 is( $special->count, 2, 'Set of exception holidays contains two dates' );
176
177 $diff = $today->delta_days( $special->min )->in_units('days');
178 is( $diff, 0, 'Lowest exception holiday is today' );
179 $diff = $tomorrow->delta_days( $special->max )->in_units('days');
180 is( $diff, 0, 'Highest exception holiday is tomorrow' );
181
182 C4::Calendar->new( branchcode => $branch_1 )->delete_holiday(
183     weekday => $tomorrow->day_of_week,
184     day     => $tomorrow->day,
185     month   => $tomorrow->month,
186     year    => $tomorrow->year,
187 );
188 $cal = Koha::Calendar->new( branchcode => $branch_1 );
189 $special = $cal->exception_holidays;
190 is( $special->count, 1, 'Set of exception holidays back to one' );
191
192 sub _add_exception {
193     my ( $dt, $branch, $descr ) = @_;
194     C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
195         day         => $dt->day,
196         month       => $dt->month,
197         year        => $dt->year,
198         title       => $descr,
199         description => $descr,
200     );
201 }
202
203 $schema->storage->txn_rollback;
204
205 1;