Ensure that weekly recurrence counting works if two incidences follow on two
authorGunnar Wrobel <p@rdus.de>
Tue, 10 Nov 2009 09:57:32 +0000 (10:57 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 10 Nov 2009 09:57:32 +0000 (10:57 +0100)
successive days.

Ensure that weekly recurrence counting works if an incidence lies directly at
the beginning of the week.

Ensure that weekly recurrence counting excludes events that happened in the
start week but lay before the start day of the recurrence.

framework/Date/lib/Horde/Date/Recurrence.php
framework/Date/test/Horde/Date/RecurrenceTest.php

index 3d9ffd9..d5a1c9b 100644 (file)
@@ -418,6 +418,18 @@ class Horde_Date_Recurrence
 
             if ($this->hasRecurCount()) {
                 $recurrences = 0;
+                /**
+                 * Correct the number of recurrences by the number of events
+                 * that lay between the start of the start week and the
+                 * recurrence start.
+                 */
+                $next = clone $start_week;
+                while ($next->compareDateTime($this->start) < 0) {
+                    if ($this->recurOnDay((int)pow(2, $next->dayOfWeek()))) {
+                        $recurrences--;
+                    }
+                    ++$next->mday;
+                }
                 if ($repeats > 0) {
                     $weekdays = $this->recurData;
                     $total_recurrences_per_week = 0;
@@ -432,12 +444,12 @@ class Horde_Date_Recurrence
             }
 
             $next = clone $start_week;
-            $next->mday += $recur;
+            $next->mday += $recur - 1;
             while ($next->compareDateTime($after) < 0 &&
                    $next->compareDateTime($after_week_end) < 0) {
                 ++$next->mday;
                 if ($this->hasRecurCount()
-                    && $next->compareDateTime($this->start) >= 0
+                    && $next->compareDateTime($after) < 0
                     && $this->recurOnDay((int)pow(2, $next->dayOfWeek()))) {
                     $recurrences++;
                 }
index 267a3e1..ca60813 100644 (file)
@@ -139,7 +139,7 @@ class Horde_Date_RecurrenceTest extends PHPUnit_Framework_TestCase
 
     public function testWeeklyCountWithMultipleIncidencesPerWeekIfTheFirstIncidenceInThisWeekHasAlreadyPassed()
     {
-        $r = new Horde_Date_Recurrence('2007-02-27 10:00:00');
+        $r = new Horde_Date_Recurrence('2007-03-03 10:00:00');
         $r->setRecurType(Horde_Date_Recurrence::RECUR_WEEKLY);
         $r->setRecurOnDay(Horde_Date::MASK_MONDAY | Horde_Date::MASK_SATURDAY);
         $r->setRecurInterval(1);
@@ -198,6 +198,48 @@ class Horde_Date_RecurrenceTest extends PHPUnit_Framework_TestCase
                             $this->_getRecurrences($r));
     }
 
+    public function testWeeklyCountWithMultipleIncidencesPerWeekIfNextIncidenceIsNextDay()
+    {
+        $r = new Horde_Date_Recurrence('2009-11-11 06:00:00');
+        $r->setRecurType(Horde_Date_Recurrence::RECUR_WEEKLY);
+        $r->setRecurOnDay(Horde_Date::MASK_WEDNESDAY | Horde_Date::MASK_THURSDAY);
+        $r->setRecurInterval(1);
+        $r->setRecurCount(6);
+        $this->assertEquals('W1 WE TH #6', $r->toRRule10($this->ical));
+        $this->assertEquals('FREQ=WEEKLY;INTERVAL=1;BYDAY=WE,TH;COUNT=6', $r->toRRule20($this->ical));
+        $this->assertEquals(array('2009-11-11 06:00:00',
+                                  '2009-11-12 06:00:00',
+                                  '2009-11-18 06:00:00',
+                                  '2009-11-19 06:00:00',
+                                  '2009-11-25 06:00:00',
+                                  '2009-11-26 06:00:00'),
+                            $this->_getRecurrences($r));
+    }
+
+    public function testWeeklyCountWithMultipleIncidencesPerWeekIfNextIncidenceIsBeginningOfWeek()
+    {
+        $r = new Horde_Date_Recurrence('2009-11-09 06:00:00');
+        $r->setRecurType(Horde_Date_Recurrence::RECUR_WEEKLY);
+        $r->setRecurOnDay(
+            Horde_Date::MASK_MONDAY |
+            Horde_Date::MASK_TUESDAY |
+            Horde_Date::MASK_WEDNESDAY |
+            Horde_Date::MASK_THURSDAY |
+            Horde_Date::MASK_FRIDAY
+        );
+        $r->setRecurInterval(1);
+        $r->setRecurCount(6);
+        $this->assertEquals('W1 MO TU WE TH FR #6', $r->toRRule10($this->ical));
+        $this->assertEquals('FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR;COUNT=6', $r->toRRule20($this->ical));
+        $this->assertEquals(array('2009-11-09 06:00:00',
+                                  '2009-11-10 06:00:00',
+                                  '2009-11-11 06:00:00',
+                                  '2009-11-12 06:00:00',
+                                  '2009-11-13 06:00:00',
+                                  '2009-11-16 06:00:00'),
+                            $this->_getRecurrences($r));
+    }
+
     public function testWeeklyCountWithMultipleIncidencesPerWeekAndCountIsOne()
     {
         $r = new Horde_Date_Recurrence('2007-03-01 10:00:00');