Skip to content

Commit

Permalink
ScheduleElement: Sync start time & the first recurrence of a rule
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab authored and nilmerg committed Apr 28, 2023
1 parent b86723b commit 6d20102
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/FormElement/ScheduleElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ public function getValue($name = null, $default = null)
$rule->endAt(parent::getValue('end'));
}

// Sync the start time and first recurrence of the rule
if (! $this->hasCronExpression() && $this->getFrequency() !== static::NO_REPEAT) {
$nextDue = $rule->getNextRecurrences($start)->current() ?? $start;
$rule->startAt($nextDue);
}

return $rule;
}

Expand Down
22 changes: 17 additions & 5 deletions tests/ScheduleElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public function testCustomDailyFrequencyWithEnd()

public function testCustomWeeklyFrequency()
{
$start = new DateTime('2023-02-07T15:17:07');
$start = new DateTime('2023-02-08T15:17:07');
$frequency = (new RRule('FREQ=WEEKLY;INTERVAL=4;BYDAY=WE'))->startAt($start);
$element = $this->assembleElement(['value' => $frequency]);

Expand All @@ -346,7 +346,7 @@ public function testCustomWeeklyFrequency()

public function testCustomWeeklyFrequencyWithEnd()
{
$start = new DateTime('2023-02-07T15:17:07');
$start = new DateTime('2023-02-08T15:17:07');
$end = new DateTime('2023-02-10T18:00:00');
$frequency = (new RRule('FREQ=WEEKLY;INTERVAL=4;BYDAY=WE'))
->startAt($start)
Expand Down Expand Up @@ -374,7 +374,7 @@ public function testCustomWeeklyFrequencyWithEnd()

public function testCustomMonthlyFrequency()
{
$start = new DateTime('2023-02-07T15:17:07');
$start = new DateTime('2023-02-08T15:17:07');
$frequency = (new RRule('FREQ=MONTHLY;INTERVAL=1;BYMONTHDAY=8,17,18,27'))->startAt($start);
$element = $this->assembleElement(['value' => $frequency]);

Expand Down Expand Up @@ -422,7 +422,7 @@ public function testCustomMonthlyFrequency()

public function testCustomMonthlyFrequencyWithEnd()
{
$start = new DateTime('2023-02-07T15:17:07');
$start = new DateTime('2023-02-08T15:17:07');
$end = new DateTime('2023-02-10T18:00:00');
$frequency = (new RRule('FREQ=MONTHLY;INTERVAL=1;BYMONTHDAY=8,17,18,27'))
->startAt($start)
Expand Down Expand Up @@ -474,7 +474,7 @@ public function testCustomMonthlyFrequencyWithEnd()

public function testCustomOnTheEachMonthFrequency()
{
$start = new DateTime('2023-02-07T15:17:07');
$start = new DateTime('2023-02-08T15:17:07');
$end = new DateTime('2023-02-10T18:00:00');
$frequency = (new RRule('FREQ=MONTHLY;INTERVAL=1;BYDAY=2WE'))
->startAt($start)
Expand Down Expand Up @@ -591,4 +591,16 @@ public function testCustomOnTheEachYearFrequencyWithEnd()

$this->assertEquals($frequency, $element->getValue());
}

public function testRecurrenceStartIsSyncedCorrectly()
{
$start = new DateTime('2023-04-27T11:00:00');
$frequency = (new RRule('FREQ=WEEKLY;INTERVAL=2;BYDAY=SA,SU'))->startAt($start);

$value = $this->assembleElement(['value' => $frequency])->getValue();

// The initial start date is April 27, but the frequency only triggers on Saturday/Sunday,
// so the first recurrence is on April 29 and the start date should've been synced.
$this->assertEquals(new DateTime('2023-04-29T11:00:00'), $value->getStart());
}
}

0 comments on commit 6d20102

Please sign in to comment.