Skip to content

Commit

Permalink
Consider interval delay in specific time also
Browse files Browse the repository at this point in the history
  • Loading branch information
dadarya0 authored and rahuld-dev committed Jan 23, 2024
1 parent 3be38f3 commit 894929c
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 4 deletions.
16 changes: 12 additions & 4 deletions app/bundles/CampaignBundle/Executioner/Scheduler/Mode/Interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,19 @@ private function getExecutionDateTimeFromHour(Lead $contact, \DateTimeInterface
}
}

/** @var \DateTime $groupExecutionDate */
$groupExecutionDate = clone $compareFromDateTime;
$groupExecutionDate->setTimezone($this->getDefaultTimezone());
if (!isset($groupExecutionDate)) {
$groupExecutionDate = clone $compareFromDateTime;
$groupExecutionDate->setTimezone($this->getDefaultTimezone());
}

$groupExecutionDate->setTime($groupHour->format('H'), $groupHour->format('i'));
$testGroupHour = clone $groupExecutionDate;
$testGroupHour->setTime($groupHour->format('H'), $groupHour->format('i'));

if ($groupExecutionDate <= $testGroupHour) {
return $testGroupHour;
} else {
$groupExecutionDate->modify('+1 day')->setTime($groupHour->format('H'), $groupHour->format('i'));
}

return $groupExecutionDate;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
<?php

declare(strict_types=1);

namespace Mautic\CampaignBundle\Tests\Functional\Campaign;

use Mautic\CampaignBundle\Entity\Campaign;
use Mautic\CampaignBundle\Entity\Event;
use Mautic\CampaignBundle\Entity\Lead as CampaignLead;
use Mautic\CampaignBundle\Entity\LeadEventLog;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Entity\Lead;

class CampaignActionJumpToEventWithIntervalTriggerModeFunctionalTest extends MauticMysqlTestCase
{
/**
* @dataProvider dataForCampaignWithJumpToEventWithIntervalTriggerMode
*/
public function testCampaignWithJumpToEventWithIntervalTriggerMode(Event $adjustPointEvent, string $format, int $expected): void
{
// Create Campaign
$campaign = new Campaign();
$campaign->setName('Campaign With Jump');
$campaign->setIsPublished(true);
$campaign->setAllowRestart(true);

$this->em->persist($campaign);

// Create event: Condition
$fieldValueEvent = new Event();
$fieldValueEvent->setCampaign($campaign);
$fieldValueEvent->setName('Field Value');
$fieldValueEvent->setType('lead.field_value');
$fieldValueEvent->setEventType(Event::TYPE_CONDITION);
$fieldValueEvent->setTriggerMode(Event::TRIGGER_MODE_IMMEDIATE);
$fieldValueEvent->setProperties([
'field' => 'firstname',
'operator' => '!empty',
'value' => null,
'properties' => [
'field' => 'firstname',
'operator' => '!empty',
'value' => null,
],
]);
$fieldValueEvent->setOrder(1);

$this->em->persist($fieldValueEvent);
$this->em->flush();

// Event: Adjust point
$adjustPointEvent->setCampaign($campaign);
$adjustPointEvent->setParent($fieldValueEvent);

$this->em->persist($adjustPointEvent);
$this->em->flush();

// Create event: Jump to action
$jumpToEvent = new Event();
$jumpToEvent->setCampaign($campaign);
$jumpToEvent->setName('Jump to Condition');
$jumpToEvent->setType('campaign.jump_to_event');
$jumpToEvent->setEventType(Event::TYPE_ACTION);
$jumpToEvent->setTriggerMode(Event::TRIGGER_MODE_IMMEDIATE);
$jumpToEvent->setProperties(['jumpToEvent' => $adjustPointEvent->getId()]);
$jumpToEvent->setParent($fieldValueEvent);
$jumpToEvent->setDecisionPath('yes');
$jumpToEvent->setOrder(3);

$this->em->persist($jumpToEvent);
$this->em->flush();

// Create Lead
$lead = new Lead();
$lead->setFirstname('First Name');
$this->em->persist($lead);

// Create Campaign Lead
$campaignLead = new CampaignLead();
$campaignLead->setCampaign($campaign);
$campaignLead->setLead($lead);
$campaignLead->setDateAdded(new \DateTime());

$this->em->persist($campaignLead);
$this->em->flush();
$this->em->clear();

// Execute Campaign
$this->runCommand(
'mautic:campaigns:trigger',
['--campaign-id' => $campaign->getId()]
);

// Search the logs
$leadEventLogRepo = $this->em->getRepository(LeadEventLog::class);

$adjustEventLog = $leadEventLogRepo->findOneBy(['event' => $adjustPointEvent->getId()]);

$triggeredDate = $adjustEventLog->getTriggerDate();
$dateTriggered = $adjustEventLog->getDateTriggered();

$this->assertTrue($adjustEventLog->getIsScheduled());
$this->assertEqualsWithDelta($expected, $dateTriggered->diff($triggeredDate)->format($format), 1);
}

/**
* @return iterable<mixed>
*/
public function dataForCampaignWithJumpToEventWithIntervalTriggerMode(): iterable
{
$now = new \DateTimeImmutable();

$event = new Event();
$event->setName('Adjust points');
$event->setEventType(Event::TYPE_ACTION);
$event->setTriggerMode(Event::TRIGGER_MODE_INTERVAL);
$event->setType('lead.changepoints');
$event->setDecisionPath('no');
$event->setTriggerInterval(0);
$event->setTriggerIntervalUnit('i');
$event->setOrder(2);

$adjustPointEvent = clone $event;
$adjustPointEvent->setTriggerInterval(10);
$adjustPointEvent->setTriggerIntervalUnit('i');

yield 'Points Interval with 10 minutes' => [
$adjustPointEvent,
'%i',
10,
];

$adjustPointEvent = clone $event;
$adjustPointEvent->setTriggerHour($now->modify('-1 hour')->format('H:i'));

yield 'Points at a relative time: Scheduled at - before one hour' => [
$adjustPointEvent,
'%h',
23,
];

$adjustPointEvent = clone $event;
$adjustPointEvent->setTriggerDate();
$adjustPointEvent->setTriggerInterval(1);
$adjustPointEvent->setTriggerIntervalUnit('H');
$adjustPointEvent->setTriggerHour($now->modify('-1 hour')->format('H:i'));

yield 'Points at a relative time: Scheduled at - before one hour with delay of 1 hour' => [
$adjustPointEvent,
'%h',
0,
];

$adjustPointEvent = clone $event;
$adjustPointEvent->setTriggerDate();
$adjustPointEvent->setTriggerRestrictedStartHour($now->modify('+2 hour')->format('H:i'));
$adjustPointEvent->setTriggerRestrictedStopHour($now->modify('+3 hour')->format('H:i'));

yield 'Points at a relative time: Between future start and stop time on same day' => [
$adjustPointEvent,
'%h',
2,
];

$adjustPointEvent = clone $event;
$adjustPointEvent->setTriggerRestrictedStartHour($now->modify('-2 hour')->format('H:i'));
$adjustPointEvent->setTriggerRestrictedStopHour($now->modify('-1 hour')->format('H:i'));

yield 'Points at a relative time: Between passed time' => [
$adjustPointEvent,
'%h',
22,
];

$adjustPointEvent = clone $event;
$adjustPointEvent->setTriggerRestrictedStartHour($now->modify('+3 hour')->format('H:i'));
$adjustPointEvent->setTriggerRestrictedStopHour($now->modify('+4 hour')->format('H:i'));

yield 'Points at a relative time: Between future time' => [
$adjustPointEvent,
'%h',
3,
];

$adjustPointEvent = clone $event;
$adjustPointEvent->setTriggerInterval(1);
$adjustPointEvent->setTriggerIntervalUnit('h');
$adjustPointEvent->setTriggerRestrictedDaysOfWeek([0, 1, 2, 3, 4, 5, 6]);

yield 'Points at a relative time: One hour interval and All Days' => [
$adjustPointEvent,
'%h',
1,
];

$adjustPointEvent = clone $event;
$adjustPointEvent->setTriggerMode(Event::TRIGGER_MODE_DATE);
$adjustPointEvent->setTriggerDate($now->modify('+5 hour'));

yield 'Points at specific date/time' => [
$adjustPointEvent,
'%h',
5,
];
}
}

0 comments on commit 894929c

Please sign in to comment.