View | Details | Raw Unified | Return to bug 17015
Collapse All | Expand All

(-)a/t/db_dependent/DiscreteCalendar.t (-1 / +286 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2015 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Test::More tests => 43;
22
use Test::MockModule;
23
24
use C4::Context;
25
use C4::Output;
26
use Koha::DateUtils;
27
use Koha::DiscreteCalendar;
28
29
30
my $module_context = new Test::MockModule('C4::Context');
31
my $today = DateTime->today;
32
my $schema = Koha::Database->new->schema;
33
$schema->storage->txn_begin;
34
35
my $branch1 = "Library1";
36
my $branch2 = "Library2";
37
38
$schema->resultset('DiscreteCalendar')->search({
39
    branchcode  =>'DFLT'
40
})->update_all({
41
    isopened    => 1,
42
    holidaytype => '',
43
    note        => '',
44
    openhour    => '08:00:00',
45
    closehour   => '16:00:00'
46
});
47
48
#Added entries for the branches.
49
Koha::DiscreteCalendar->new( branchcode => 'DFLT' )->add_new_branch('DFLT', $branch1);
50
Koha::DiscreteCalendar->new( branchcode => 'DFLT' )->add_new_branch('DFLT', $branch2);
51
52
isnt($branch1,'', "First branch to do tests. BranchCode => $branch1");
53
isnt($branch1,'', "Second branch  to do tests. BranchCode => $branch2");
54
55
#Reset database copy with default values to be able to do the calculations
56
$schema->resultset('DiscreteCalendar')->search({},
57
    {
58
        where => \["branchcode IN (?,?)", $branch1, $branch2]
59
    }
60
    )->update_all({
61
        isopened    => 1,
62
        holidaytype => '',
63
        note        => '',
64
        openhour    => '08:00:00',
65
        closehour   => '16:00:00'
66
   }
67
);
68
#2 Calendars to make sure branches are treated separately.
69
my $calendar = Koha::DiscreteCalendar->new(branchcode => $branch1);
70
my $calendar2 = Koha::DiscreteCalendar->new(branchcode => $branch2);
71
72
my $single_exception = DateTime->today;
73
$calendar->edit_holiday("Single holiday Today", '', "E", '', '', $single_exception, $single_exception);
74
is($calendar->isOpened($single_exception), 0, "Branch closed today : $single_exception");
75
76
my @single_holidays = $calendar->get_single_holidays();
77
is(scalar @single_holidays, 1, "Set of exception holidays at 1");
78
79
my $days_between_start = DateTime->today;
80
my $days_between_end = DateTime->today->add(days => 6);
81
my $days_between = $calendar->days_between($days_between_start, $days_between_end)->in_units('days');
82
is($days_between, 5, "Days between skips holiday");
83
84
my $fixed_holiday = DateTime->today->add(days => 1);
85
$calendar->edit_holiday("Fixed", '', "R", '', '', $fixed_holiday, $fixed_holiday);
86
is($calendar->isOpened($fixed_holiday), 0, "Branch not opened on $fixed_holiday");
87
88
my @fixed_holidays = $calendar->get_day_month_holidays();
89
is(scalar @fixed_holidays, 1, "Set of Fixed holidays at 1");
90
91
# 1 being mysql DAYOFWEEK() for Sunday
92
$calendar->edit_holiday("Weekly",1, "W", '', '', $today, $today);
93
my @weekly_holidays = $calendar->get_week_days_holidays();
94
is(scalar @weekly_holidays, 1, "Set of weekly holidays at 1");
95
96
my $sunday = DateTime->today->add(days =>( 7 - $today->day_of_week));
97
is($calendar->isOpened($sunday), 0, "Branch not opened on " . $sunday->day_name ."s");
98
99
my $single_exception_range_start = DateTime->today->add(days => 2);
100
my $single_exception_range_end = DateTime->today->add(days => 7);
101
$calendar->edit_holiday("Single holiday range", '', "E", '', '', $single_exception_range_start, $single_exception_range_end);
102
@single_holidays = $calendar->get_single_holidays();
103
is(scalar @single_holidays, 7, "Set of exception holidays at 7");
104
105
my $fixed_holiday_range_start = DateTime->today->add(days => 8);
106
my $fixed_holiday_range_end = DateTime->today->add(days => 13);
107
$calendar->edit_holiday("Fixed holiday range", '', "R", '', '', $fixed_holiday_range_start, $fixed_holiday_range_end);
108
@fixed_holidays = $calendar->get_day_month_holidays();
109
is(scalar @fixed_holidays, 7, "Set of repeatable holidays at 7");
110
111
#Hourly loan
112
# ex :
113
# item due      : 2017-01-24 11:00:00
114
# item returned : 2017-01-26 10:00:00
115
# Branch closed : 2017-01-25
116
# Open/close hours : 8AM to 4PM (8h day)
117
# Hours due : 5 hours on 2017-01-24 + 2 hours on 2017-01-26 = 7hours
118
119
my $open_hours_since_start = DateTime->today->add(days => 40, hours => 11);
120
my $open_hours_since_end = DateTime->today->add(days => 42, hours => 10);
121
my $holiday_between =  DateTime->today->add(days => 41);
122
$calendar->edit_holiday('', '', 'none', '', '', $open_hours_since_start, $open_hours_since_end);
123
124
$calendar->edit_holiday("Single holiday", '', "E", '', '', $holiday_between,$holiday_between);
125
my $open_hours_between = $calendar->open_hours_between($open_hours_since_start, $open_hours_since_end);
126
is($open_hours_between, 7, "7 Hours open between $open_hours_since_start and $open_hours_since_end");
127
128
my $christmas = DateTime->new(
129
    year  => $today->year(),
130
    month => 12,
131
    day   => 25,
132
);
133
$calendar->edit_holiday("Christmas", '', "R", '', '', $christmas, $christmas);
134
is($calendar->isOpened($christmas), 0, "Branch closed on Christmas : $christmas");
135
136
my $newyear = DateTime->new(
137
    year  => $today->year() +1,
138
    month => 1,
139
    day   => 1,
140
);
141
142
$calendar->edit_holiday("New Year", '', "R", '', '', $newyear, $newyear);
143
is($calendar->isOpened($newyear), 0, "Branch closed on New Year : $newyear");
144
145
#Hours between :
146
my $date_due = DateTime->today->add(days =>1);
147
my $date_returned = DateTime->today->add(hours => 8);
148
my $hours_between = $calendar->hours_between($date_due, $date_returned);
149
150
is($hours_between->in_units('hours'), 16, "Date due $date_due, returned $date_returned, 16 hours in advance");
151
152
$date_due = DateTime->today->add(days =>1);
153
$date_returned = DateTime->today->add(days => 2, hours=> 8);
154
$hours_between = $calendar->hours_between($date_due, $date_returned);
155
156
is($hours_between->in_units('hours'), -16, "Date due $date_due, returned $date_returned, 16 hours late");
157
158
159
#delete holidays
160
is($calendar->isOpened($today), 0, "Today is closed");
161
$calendar->edit_holiday('', '', 'none', '', '', $single_exception, $single_exception);
162
is($calendar->isOpened($today), 1, "Today's holiday was removed");
163
164
my $new_open_hours = '08:00';
165
$calendar->edit_holiday('', '', '', $new_open_hours, '', $today, $today);
166
is($calendar->get_date_info($today, $branch1)->{openhour}, '08:00:00', "Open hour changed to 08:00:00");
167
168
isnt($calendar->get_date_info($today, $branch1)->{closehour}, '', "Close hour not removed");
169
170
my $delete_range_end = DateTime->today->add(days => 30);
171
$calendar->edit_holiday('', '', 'none', '', '', $today, $delete_range_end);
172
173
174
is($calendar->isOpened($today), 1, "Today's holiday was already removed");
175
is($calendar->isOpened(DateTime->today->add(days => 1)), 1, "Tomorrow's holiday was removed");
176
is($calendar->isOpened($delete_range_end), 1, "End of range holiday was removed");
177
178
#Check if other branch was affected
179
my @single_holidays_branch2 = $calendar2->get_single_holidays();
180
is(scalar @single_holidays_branch2, 0, "Set of exception holidays at 0 for branch => $branch2");
181
my @fixed_holidays_branch2 = $calendar2->get_day_month_holidays();
182
is(scalar @fixed_holidays_branch2, 0, "Set of Fixed holidays at 0 for branch => $branch2");
183
my @weekly_holidays_branch2 = $calendar2->get_week_days_holidays();
184
is(scalar @weekly_holidays_branch2, 0, "Set of weekly holidays at 0 for branch => $branch2");
185
186
187
#Tests for addDate()
188
189
my $one_day_dur = DateTime::Duration->new( days => 1 );
190
my $negative_one_day_dur = DateTime::Duration->new( days => - 1 );
191
my $two_day_dur = DateTime::Duration->new( days => 2 );
192
my $seven_day_dur = DateTime::Duration->new( days => 7 );
193
194
#Preference useDaysMode Calendar
195
$calendar->{days_mode} = 'Calendar';
196
$single_exception->add(days => 1);
197
my $tomorrow = DateTime->today->add(days => 1);
198
$calendar->edit_holiday("Single holiday Today", '', "E", '', '', $tomorrow, $tomorrow);
199
200
is($calendar->addDate( $today, $one_day_dur, 'days' )->ymd(),
201
    $today->add(days => 2)->ymd(),
202
    'Single day add (Calendar)' );
203
204
is($calendar->addDate( $today, $two_day_dur, 'days' )->ymd(),
205
    $today->add(days => 2)->ymd(),
206
    'Two days add, skips holiday (Calendar)' );
207
208
cmp_ok($calendar->addDate( $today, $seven_day_dur, 'days' )->ymd(), 'eq',
209
    $today->add(days => 7)->ymd(),
210
    'Add 7 days (Calendar)' );
211
#Closed Sunday
212
$calendar->edit_holiday("Weekly",1, "W", '', '', $today, $today);
213
is( $calendar->addDate( $sunday, $one_day_dur, 'days' )->day_of_week, 1,
214
    'addDate skips closed Sunday (Calendar)' );
215
#to remove the closed sundays
216
$today = DateTime->today;
217
$calendar->edit_holiday('', '', 'none', '', '', $today, $delete_range_end);
218
219
is( $calendar->addDate($today, $negative_one_day_dur , 'days')->ymd(),
220
    $today->add(days => - 1)->ymd(),
221
    'Negative call to addDate (Calendar)' );
222
223
#Preference useDaysMode DateDue
224
$calendar->{days_mode} = 'Datedue';
225
#clean everything
226
$today = DateTime->today;
227
$calendar->edit_holiday('', '', 'none', '', '', $today, $delete_range_end);
228
$calendar->edit_holiday("Single holiday Today", '', "E", '', '', $tomorrow, $tomorrow);
229
230
is($calendar->addDate( $today, $one_day_dur, 'days' )->ymd(),
231
    $today->add(days => 2)->ymd(),
232
    'Single day add' );
233
234
is($calendar->addDate( $today, $two_day_dur, 'days' )->ymd(),
235
    $today->add(days => 2)->ymd(),
236
    'Two days add, skips holiday (Datedue)' );
237
238
cmp_ok($calendar->addDate( $today, $seven_day_dur, 'days' )->ymd(), 'eq',
239
    $today->add(days => 7)->ymd(),
240
    'Add 7 days (Datedue)' );
241
#Closed Sunday
242
$calendar->edit_holiday("Weekly",1, "W", '', '', $today, $today);
243
is( $calendar->addDate( $sunday, $one_day_dur, 'days' )->day_of_week, 1,
244
    'addDate skips closed Sunday (Datedue)' );
245
#to remove the closed sundays
246
$today = DateTime->today;
247
$calendar->edit_holiday('', '', 'none', '', '', $today, $delete_range_end);
248
249
is( $calendar->addDate($today, $negative_one_day_dur , 'days')->ymd(),
250
    $today->add(days => - 1)->ymd(),
251
    'Negative call to addDate (Datedue)' );
252
253
#Preference useDaysMode Days
254
$calendar->{days_mode} = 'Days';
255
#clean everything
256
$today = DateTime->today;
257
$calendar->edit_holiday('', '', 'none', '', '', $today, $delete_range_end);
258
$calendar->edit_holiday("Single holiday Today", '', "E", '', '', $tomorrow, $tomorrow);
259
is($calendar->addDate( $today, $one_day_dur, 'days' )->ymd(),
260
    $today->add(days => 1)->ymd(),
261
    'Single day add' );
262
263
is($calendar->addDate( $today, $two_day_dur, 'days' )->ymd(),
264
    $today->add(days => 2)->ymd(),
265
    'Two days add, skips holiday (Days)' );
266
267
cmp_ok($calendar->addDate( $today, $seven_day_dur, 'days' )->ymd(), 'eq',
268
    $today->add(days => 7)->ymd(),
269
    'Add 7 days (Days)' );
270
#Closed Sunday
271
$calendar->edit_holiday("Weekly",1, "W", '', '', $today, $today);
272
is( $calendar->addDate( $sunday, $one_day_dur, 'days' )->day_of_week, 1,
273
    'addDate skips closed Sunday (Days)' );
274
275
is( $calendar->addDate($today, $negative_one_day_dur , 'days')->ymd(),
276
    $today->add(days => - 1)->ymd(),
277
    'Negative call to addDate (Days)' );
278
279
#copying a branch to an other
280
is($calendar2->isOpened($tomorrow), 1, "$branch2 opened tomorrow");
281
$calendar->copyToBranch($branch2);
282
is($calendar2->isOpened($tomorrow), 0, "$branch2 close tomorrow after copying from $branch1");
283
284
$schema->storage->txn_rollback;
285
286
1;

Return to bug 17015