Tomas Cohen Arazi
7b18e2146c
useDaysMode=Datedue wasn't used as advertised in the docs. Added next_open_day and prev_open_day subs to Koha::Calendar and some tests for them. - Koha::Calendar->addDate was rewritten in a more sane way (also split into addHours and addDays for convenience). - Fixed a bug introduced in Bug 8966 regarding dt truncation and dtSets->contains - Minor docs typos - Use the passed Calendar mode or default to 'Calendar' in Koha::Calendar->_mockinit. - Tests I'm writing some db-dependent tests for is_holiday, and hopefully for CalcDateDue so any rewrite/followup doesn't break things. Regards To+ Sponsored-by: Universidad Nacional de Córdoba Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de> Passed-QA-by: Paul Poulain <paul.poulain@biblibre.com> Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
182 lines
5.7 KiB
Perl
Executable file
182 lines
5.7 KiB
Perl
Executable file
#!/usr/bin/env perl
|
|
|
|
use strict;
|
|
use warnings;
|
|
use DateTime;
|
|
use DateTime::Duration;
|
|
use Test::More tests => 22;
|
|
use Koha::DateUtils;
|
|
|
|
BEGIN {
|
|
use_ok('Koha::Calendar');
|
|
|
|
# This was the only test C4 had
|
|
# Remove when no longer used
|
|
use_ok('C4::Calendar');
|
|
}
|
|
|
|
my $cal = Koha::Calendar->new( TEST_MODE => 1 );
|
|
|
|
isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
|
|
|
|
my $saturday = DateTime->new(
|
|
year => 2011,
|
|
month => 6,
|
|
day => 25,
|
|
time_zone => 'Europe/London',
|
|
);
|
|
my $sunday = DateTime->new(
|
|
year => 2011,
|
|
month => 6,
|
|
day => 26,
|
|
time_zone => 'Europe/London',
|
|
);
|
|
my $monday = DateTime->new(
|
|
year => 2011,
|
|
month => 6,
|
|
day => 27,
|
|
time_zone => 'Europe/London',
|
|
);
|
|
my $bloomsday = DateTime->new(
|
|
year => 2011,
|
|
month => 6,
|
|
day => 16,
|
|
time_zone => 'Europe/London',
|
|
); # should be a holiday
|
|
my $special = DateTime->new(
|
|
year => 2011,
|
|
month => 6,
|
|
day => 1,
|
|
time_zone => 'Europe/London',
|
|
); # should be a holiday
|
|
my $notspecial = DateTime->new(
|
|
year => 2011,
|
|
month => 6,
|
|
day => 2,
|
|
time_zone => 'Europe/London',
|
|
); # should NOT be a holiday
|
|
|
|
is( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' ); # wee free test;
|
|
is( $cal->is_holiday($monday), 0, 'Monday is not a closed day' ); # alas
|
|
is( $cal->is_holiday($bloomsday), 1, 'month/day closed day test' );
|
|
is( $cal->is_holiday($special), 1, 'special closed day test' );
|
|
is( $cal->is_holiday($notspecial), 0, 'open day test' );
|
|
|
|
my $dt = $cal->addDate( $saturday, 1, 'days' );
|
|
is( $dt->day_of_week, 1, 'addDate skips closed Sunday' );
|
|
|
|
$dt = $cal->addDate( $bloomsday, -1 );
|
|
is( $dt->ymd(), '2011-06-15', 'Negative call to addDate' );
|
|
|
|
my $test_dt = DateTime->new( # Monday
|
|
year => 2012,
|
|
month => 7,
|
|
day => 23,
|
|
hour => 11,
|
|
minute => 53,
|
|
time_zone => 'Europe/London',
|
|
);
|
|
|
|
my $later_dt = DateTime->new( # Monday
|
|
year => 2012,
|
|
month => 9,
|
|
day => 17,
|
|
hour => 17,
|
|
minute => 30,
|
|
time_zone => 'Europe/London',
|
|
);
|
|
|
|
my $daycount = $cal->days_between( $test_dt, $later_dt );
|
|
cmp_ok( $daycount->in_units('days'),
|
|
'==', 48, 'days_between calculates correctly' );
|
|
|
|
my $ret;
|
|
|
|
$cal->set_daysmode('Calendar');
|
|
|
|
# see bugzilla #8966
|
|
is( $cal->is_holiday($later_dt), 0, 'is holiday for the next test' );
|
|
cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'Date should be the same after is_holiday' );
|
|
|
|
# example tests for bug report
|
|
$cal->clear_weekly_closed_days();
|
|
|
|
$daycount = $cal->days_between( dt_from_string('2012-01-10','iso'),
|
|
dt_from_string("2012-05-05",'iso') )->in_units('days');
|
|
cmp_ok( $daycount, '==', 116, 'test larger intervals' );
|
|
$daycount = $cal->days_between( dt_from_string("2012-01-01",'iso'),
|
|
dt_from_string("2012-05-05",'iso') )->in_units('days');
|
|
cmp_ok( $daycount, '==', 125, 'test positive intervals' );
|
|
my $daycount2 = $cal->days_between( dt_from_string("2012-05-05",'iso'),
|
|
dt_from_string("2012-01-01",'iso') )->in_units('days');
|
|
cmp_ok( $daycount2, '==', $daycount, 'test parameter order not relevant' );
|
|
$daycount = $cal->days_between( dt_from_string("2012-07-01",'iso'),
|
|
dt_from_string("2012-07-15",'iso') )->in_units('days');
|
|
cmp_ok( $daycount, '==', 14, 'days_between calculates correctly' );
|
|
$cal->add_holiday( dt_from_string('2012-07-06','iso') );
|
|
$daycount = $cal->days_between( dt_from_string("2012-07-01",'iso'),
|
|
dt_from_string("2012-07-15",'iso') )->in_units('days');
|
|
cmp_ok( $daycount, '==', 13, 'holiday correctly recognized' );
|
|
|
|
$cal->add_holiday( dt_from_string('2012-07-07','iso') );
|
|
$daycount = $cal->days_between( dt_from_string("2012-07-01",'iso'),
|
|
dt_from_string("2012-07-15",'iso') )->in_units('days');
|
|
cmp_ok( $daycount, '==', 12, 'multiple holidays correctly recognized' );
|
|
|
|
my $one_day_dur = DateTime::Duration->new( days => 1 );
|
|
my $two_day_dur = DateTime::Duration->new( days => 2 );
|
|
my $seven_day_dur = DateTime::Duration->new( days => 7 );
|
|
|
|
subtest '\'Datedue\' tests' => sub {
|
|
my $cal = Koha::Calendar->new( TEST_MODE => 1 ,
|
|
days_mode => 'Datedue');
|
|
|
|
$cal->add_holiday( dt_from_string('2012-07-04','iso') );
|
|
$dt = dt_from_string( '2012-07-03','iso' );
|
|
|
|
is($cal->addDate( $dt, $one_day_dur, 'days' ),
|
|
dt_from_string('2012-07-05','iso'),
|
|
'Single day add (Datedue, matches holiday, shift)' );
|
|
|
|
is($cal->addDate( $dt, $two_day_dur, 'days' ),
|
|
dt_from_string('2012-07-05','iso'),
|
|
'Two days add, skips holiday (Datedue)' );
|
|
|
|
cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
|
|
'2012-07-30T11:53:00',
|
|
'Add 7 days (Datedue)' );
|
|
};
|
|
|
|
|
|
subtest '\'Calendar\' tests' => sub {
|
|
my $cal = Koha::Calendar->new( TEST_MODE => 1,
|
|
days_mode => 'Calendar' );
|
|
|
|
$cal->add_holiday( dt_from_string('2012-07-04','iso') );
|
|
$dt = dt_from_string('2012-07-03','iso');
|
|
|
|
is($cal->addDate( $dt, $one_day_dur, 'days' ),
|
|
dt_from_string('2012-07-05','iso'),
|
|
'Single day add (Calendar)' );
|
|
|
|
cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
|
|
'2012-07-31T11:53:00',
|
|
'Add 7 days (Calendar)' );
|
|
};
|
|
|
|
|
|
subtest '\'Days\' tests' => sub {
|
|
my $cal = Koha::Calendar->new( TEST_MODE => 1,
|
|
days_mode => 'Days' );
|
|
|
|
$cal->add_holiday( dt_from_string('2012-07-04','iso') );
|
|
$dt = dt_from_string('2012-07-03','iso');
|
|
|
|
is($cal->addDate( $dt, $one_day_dur, 'days' ),
|
|
dt_from_string('2012-07-04','iso'),
|
|
'Single day add (Days)' );
|
|
|
|
cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
|
|
'2012-07-30T11:53:00',
|
|
'Add 7 days (Days)' );
|
|
};
|