Spaces:
No application file
No application file
File size: 8,715 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
<?php
namespace Mautic\CampaignBundle\Tests\Command;
use Mautic\CampaignBundle\Entity\LeadEventLog;
use Mautic\CampaignBundle\Entity\LeadEventLogRepository;
use Mautic\CampaignBundle\Executioner\ScheduledExecutioner;
use Mautic\CampaignBundle\Tests\Functional\Fixtures\FixtureHelper;
use Mautic\CoreBundle\Helper\DateTimeHelper;
use PHPUnit\Framework\Assert;
class ExecuteEventCommandTest extends AbstractCampaignCommand
{
public function testEventsAreExecutedForInactiveEventWithSingleContact(): void
{
putenv('CAMPAIGN_EXECUTIONER_SCHEDULER_ACKNOWLEDGE_SECONDS=1');
$this->testSymfonyCommand('mautic:campaigns:trigger', ['-i' => 1, '--contact-ids' => '1,2,3']);
// There should be three events scheduled
$byEvent = $this->getCampaignEventLogs([2]);
$this->assertCount(3, $byEvent[2]);
$logIds = [];
foreach ($byEvent[2] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Event is not scheduled for lead ID '.$log['lead_id']);
}
$logIds[] = $log['id'];
}
$this->testSymfonyCommand('mautic:campaigns:execute', ['--scheduled-log-ids' => implode(',', $logIds)]);
// There should still be three events scheduled
$byEvent = $this->getCampaignEventLogs([2]);
$this->assertCount(3, $byEvent[2]);
foreach ($byEvent[2] as $log) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Event is not scheduled for lead ID '.$log['lead_id']);
}
}
// Pop off the last so we can test that only the two given are executed
$lastId = array_pop($logIds);
// Wait 6 seconds to go past scheduled time
static::getContainer()->get(ScheduledExecutioner::class)->setNowTime(new \DateTime('+'.self::CONDITION_SECONDS.' seconds'));
$this->testSymfonyCommand('mautic:campaigns:execute', ['--scheduled-log-ids' => implode(',', $logIds)]);
// The events should have executed
$byEvent = $this->getCampaignEventLogs([2]);
$this->assertCount(3, $byEvent[2]);
foreach ($byEvent[2] as $log) {
// Lasta
if ($log['id'] === $lastId) {
if (0 === (int) $log['is_scheduled']) {
$this->fail('Event is not scheduled when it should be for lead ID '.$log['lead_id']);
}
continue;
}
if (1 === (int) $log['is_scheduled']) {
$this->fail('Event is still scheduled for lead ID '.$log['lead_id']);
}
}
putenv('CAMPAIGN_EXECUTIONER_SCHEDULER_ACKNOWLEDGE_SECONDS=0');
}
public function testRepublishScheduledCampaignEventActionWhenEventFailedBecauseCampaignWasUnpublished(): void
{
$fixtureHelper = new FixtureHelper($this->em);
$contact = $fixtureHelper->createContact('[email protected]');
$campaign = $fixtureHelper->createCampaign('Scheduled event test');
$fixtureHelper->addContactToCampaign($contact, $campaign);
$fixtureHelper->createCampaignWithScheduledEvent($campaign);
$this->em->flush();
$commandResult = $this->testSymfonyCommand('mautic:campaigns:trigger', ['--campaign-id' => $campaign->getId()]);
Assert::assertStringContainsString('1 total event was scheduled', $commandResult->getDisplay());
$campaign->setIsPublished(false);
$this->em->persist($campaign);
$this->em->flush();
$this->em->clear();
$leadEventLogRepository = $this->em->getRepository(LeadEventLog::class);
\assert($leadEventLogRepository instanceof LeadEventLogRepository);
$log = $leadEventLogRepository->findOneBy(['lead' => $contact, 'campaign' => $campaign]);
\assert($log instanceof LeadEventLog);
Assert::assertTrue($log->getIsScheduled());
// Time machine so we don't have to wait for that long.
$log->setTriggerDate(new \DateTime('2 days ago'));
$log->setDateTriggered(new \DateTime('2 days ago'));
$log->setIsScheduled(true);
$this->em->persist($log);
$this->em->flush();
$this->em->clear();
$commandResult = $this->testSymfonyCommand('mautic:campaigns:execute', ['--scheduled-log-ids' => $log->getId()]);
Assert::assertStringContainsString('0 total events(s) to be processed', $commandResult->getDisplay());
Assert::assertStringContainsString('0 total events were executed', $commandResult->getDisplay());
Assert::assertStringContainsString('0 total events were scheduled', $commandResult->getDisplay());
$log = $leadEventLogRepository->findOneBy(['lead' => $contact, 'campaign' => $campaign]);
\assert($log instanceof LeadEventLog);
Assert::assertTrue($log->getIsScheduled());
Assert::assertSame([], $log->getMetadata());
}
public function testRepublishScheduledCampaignEventActionWhenEventFailedBecauseCampaignPublishDownIsInThePast(): void
{
$fixtureHelper = new FixtureHelper($this->em);
$contact = $fixtureHelper->createContact('[email protected]');
$campaign = $fixtureHelper->createCampaign('Scheduled event test');
$fixtureHelper->addContactToCampaign($contact, $campaign);
$fixtureHelper->createCampaignWithScheduledEvent($campaign);
$this->em->flush();
$commandResult = $this->testSymfonyCommand('mautic:campaigns:trigger', ['--campaign-id' => $campaign->getId()]);
Assert::assertStringContainsString('1 total event was scheduled', $commandResult->getDisplay());
$campaign->setPublishUp(new \DateTime('3 days ago'));
$campaign->setPublishDown(new \DateTime('1 days ago'));
$this->em->persist($campaign);
$this->em->flush();
$this->em->clear();
$leadEventLogRepository = $this->em->getRepository(LeadEventLog::class);
\assert($leadEventLogRepository instanceof LeadEventLogRepository);
$log = $leadEventLogRepository->findOneBy(['lead' => $contact, 'campaign' => $campaign]);
\assert($log instanceof LeadEventLog);
Assert::assertTrue($log->getIsScheduled());
// Time machine so we don't have to wait for that long.
$log->setTriggerDate(new \DateTime('2 days ago'));
$log->setDateTriggered(new \DateTime('2 days ago'));
$log->setIsScheduled(true);
$this->em->persist($log);
$this->em->flush();
$this->em->clear();
$commandResult = $this->testSymfonyCommand('mautic:campaigns:execute', ['--scheduled-log-ids' => $log->getId()]);
Assert::assertStringContainsString('1 total events(s) to be processed', $commandResult->getDisplay());
Assert::assertStringContainsString('0 total events were executed', $commandResult->getDisplay());
Assert::assertStringContainsString('0 total events were scheduled', $commandResult->getDisplay());
}
public function testScheduledCampaignEventActionIfScheduledAtDefined(): void
{
$interval = 5;
$unit = 'i';
$fixtureHelper = new FixtureHelper($this->em);
$contact = $fixtureHelper->createContact('[email protected]');
$campaign = $fixtureHelper->createCampaign('Scheduled event test');
$fixtureHelper->addContactToCampaign($contact, $campaign);
$hour = new \DateTime();
$hour->add((new DateTimeHelper())->buildInterval($interval, $unit));
$fixtureHelper->createCampaignWithScheduledEvent($campaign, $interval, $unit, $hour);
$this->em->flush();
$commandResult = $this->testSymfonyCommand('mautic:campaigns:trigger', ['--campaign-id' => $campaign->getId()]);
Assert::assertStringContainsString('1 total event was scheduled', $commandResult->getDisplay());
$leadEventLogRepository = $this->em->getRepository(LeadEventLog::class);
\assert($leadEventLogRepository instanceof LeadEventLogRepository);
$log = $leadEventLogRepository->findOneBy(['lead' => $contact, 'campaign' => $campaign]);
\assert($log instanceof LeadEventLog);
Assert::assertTrue($log->getIsScheduled());
$commandResult = $this->testSymfonyCommand('mautic:campaigns:execute', ['--scheduled-log-ids' => $log->getId()]);
Assert::assertStringContainsString('1 total events(s) to be processed', $commandResult->getDisplay());
Assert::assertStringContainsString('1 total event was scheduled', $commandResult->getDisplay());
Assert::assertStringContainsString('0 total events were executed', $commandResult->getDisplay());
}
}
|