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

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

Return to bug 17015