Bug 5549 : Koha::Calendar::addDate should not loop on -1
authorColin Campbell <colin.campbell@ptfs-europe.com>
Fri, 24 Jun 2011 13:28:22 +0000 (14:28 +0100)
committerChris Cormack <chrisc@catalyst.net.nz>
Tue, 20 Mar 2012 00:26:36 +0000 (13:26 +1300)
Control variable of loop should be absolute value
to avoid looping when backtracking

Koha/Calendar.pm
t/Kalendar.t

index bdd4b8f..a3ad60c 100644 (file)
@@ -87,6 +87,7 @@ sub addDate {
     if ( ref $add_duration ne 'DateTime::Duration' ) {
         $add_duration = DateTime::Duration->new( days => $add_duration );
     }
+    $unit ||= q{};    # default days ?
     my $days_mode = $self->{days_mode};
     Readonly::Scalar my $return_by_hour => 10;
     my $day_dur = DateTime::Duration->new( days => 1 );
@@ -114,7 +115,7 @@ sub addDate {
             }
 
         } else {
-            my $days = $add_duration->in_units('days');
+            my $days = abs $add_duration->in_units('days');
             while ($days) {
                 $base_date->add_duration($day_dur);
                 if ( $self->is_holiday($base_date) ) {
index ef80ca9..f955231 100755 (executable)
@@ -5,7 +5,7 @@ use DateTime;
 use DateTime::TimeZone;
 
 use C4::Context;
-use Test::More tests => 8;    # last test to print
+use Test::More tests => 9;
 
 BEGIN { use_ok('Koha::Calendar'); }
 
@@ -57,3 +57,6 @@ 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 );
+cmp_ok( $dt->ymd(), 'cmp', '2011-06-15', 'Negative call to addDate' );