configParams[self::CAMPAIGN_SUMMARY_PARAM] = in_array($this->getName(), $functionForUseSummary); $this->configParams[self::CAMPAIGN_RANGE_PARAM] = in_array($this->getName(), $functionForUseRange); parent::setUp(); $this->campaignModel = static::getContainer()->get('mautic.model.factory')->getModel('campaign'); $this->campaignLeadsLabel = static::getContainer()->get('translator')->trans('mautic.campaign.campaign.leads'); $this->configParams['delete_campaign_event_log_in_background'] = false; } public function testCampaignContactCountThroughStatsWithSummary(): void { $this->campaignContactCountThroughStats(); } public function testCampaignContactCountThroughStatsWithoutSummary(): void { $this->campaignContactCountThroughStats(); } public function testCampaignContactCountOnCanvasWithoutSummaryAndRange(): void { $this->campaignContactCountOnCanvas(); } public function testCampaignContactCountOnCanvasWithSummaryWithoutRange(): void { $this->campaignContactCountOnCanvas(); } public function testCampaignContactCountOnCanvasWithoutSummaryWithRange(): void { $this->campaignContactCountOnCanvas(); } public function testCampaignContactCountOnCanvasWithSummaryAndRange(): void { $this->campaignContactCountOnCanvas(); } public function testCampaignCountsBeforeSummarizeCommandWithoutSummaryAndRange(): void { $this->getCountAndDetails(false, false, 100, 2, 0); } public function testCampaignCountsBeforeSummarizeCommandWithSummaryWithoutRange(): void { $this->getCountAndDetails(false, false, 0, 0, 0); } public function testCampaignCountsBeforeSummarizeCommandWithoutSummaryWithRange(): void { $this->getCountAndDetails(false, false, 100, 2, 0); } public function testCampaignCountsBeforeSummarizeCommandWithSummaryAndRange(): void { $this->getCountAndDetails(false, false, 0, 0, 0); } public function testCampaignCountsAfterSummarizeCommandWithoutSummaryAndRange(): void { $this->getCountAndDetails(false, true, 100, 2, 0); } public function testCampaignCountsAfterSummarizeCommandWithSummaryWithoutRange(): void { $this->getCountAndDetails(false, true, 100, 2, 0); } public function testCampaignCountsAfterSummarizeCommandWithoutSummaryWithRange(): void { $this->getCountAndDetails(false, true, 100, 2, 0); } public function testCampaignCountsAfterSummarizeCommandWithSummaryAndRange(): void { $this->getCountAndDetails(false, true, 100, 2, 0); } public function testCampaignPendingCountsWithoutSummaryAndRange(): void { $this->getCountAndDetails(true, true, 100, 2, 1); } public function testCampaignPendingCountsWithSummaryWithoutRange(): void { $this->getCountAndDetails(true, true, 100, 2, 1); } public function testCampaignPendingCountsWithoutSummaryWithRange(): void { $this->getCountAndDetails(true, true, 100, 2, 1); } public function testCampaignPendingCountsWithSummaryAndRange(): void { $this->getCountAndDetails(true, true, 100, 2, 1); } private function getStatTotalContacts(int $campaignId): int { $from = date('Y-m-d', strtotime('-2 months')); $to = date('Y-m-d', strtotime('-1 month')); $stats = $this->campaignModel->getCampaignMetricsLineChartData( null, new \DateTime($from), new \DateTime($to), null, ['campaign_id' => $campaignId] ); $datasets = $stats['datasets'] ?? []; return $this->processTotalContactStats($datasets); } private function getCanvasTotalContacts(int $campaignId): int { $crawler = $this->getCrawlers($campaignId); $canvasJson = trim($crawler->filter('canvas')->html()); $canvasData = json_decode($canvasJson, true); $datasets = $canvasData['datasets'] ?? []; return $this->processTotalContactStats($datasets); } /** * @param array> $datasets */ private function processTotalContactStats(array $datasets): int { $totalContacts = 0; foreach ($datasets as $dataset) { if ($dataset['label'] === $this->campaignLeadsLabel) { $data = $dataset['data'] ?? []; $totalContacts = array_sum($data); break; } } return $totalContacts; } private function getCrawlers(int $campaignId): Crawler { $from = date('F d, Y', strtotime('-2 months')); $to = date('F d, Y', strtotime('-1 month')); $parameters = [ 'daterange' => [ 'date_from' => $from, 'date_to' => $to, ], ]; return $this->client->request(Request::METHOD_POST, '/s/campaigns/view/'.$campaignId, $parameters); } /** * @return array */ private function getActionCounts(int $campaignId): array { $crawler = $this->getCrawlers($campaignId); $successPercent = trim($crawler->filter('#actions-container')->filter('span')->eq(0)->html()); $completed = trim($crawler->filter('#actions-container')->filter('span')->eq(1)->html()); $pending = trim($crawler->filter('#actions-container')->filter('span')->eq(2)->html()); return [ 'successPercent' => $successPercent, 'completed' => $completed, 'pending' => $pending, ]; } private function campaignContactCountThroughStats(): void { $campaign = $this->saveSomeCampaignLeadEventLogs(); $campaignId = $campaign->getId(); $totalContacts = $this->getStatTotalContacts($campaignId); Assert::assertSame(2, $totalContacts); } private function campaignContactCountOnCanvas(): void { $campaign = $this->saveSomeCampaignLeadEventLogs(); $campaignId = $campaign->getId(); $totalContacts = $this->getCanvasTotalContacts($campaignId); Assert::assertSame(2, $totalContacts); } private function getCountAndDetails(bool $emulatePendingCount, bool $runCommand, int $expectedSuccessPercent, int $expectedCompleted, int $expectedPending): void { $campaign = $this->saveSomeCampaignLeadEventLogs($emulatePendingCount); $campaignId = $campaign->getId(); if ($runCommand) { $this->testSymfonyCommand( SummarizeCommand::NAME, [ '--env' => 'test', '--max-hours' => 768, ] ); } $actionCounts = $this->getActionCounts($campaignId); Assert::assertSame($expectedSuccessPercent.'%', $actionCounts['successPercent']); Assert::assertSame($expectedCompleted, (int) $actionCounts['completed']); Assert::assertSame($expectedPending, (int) $actionCounts['pending']); } public function testDeleteCampaign(): void { $lead = $this->createLead(); $campaign = $this->createCampaign(); $event = $this->createEvent('Event 1', $campaign); $this->createEventLog($lead, $event, $campaign); $this->client->request(Request::METHOD_POST, '/s/campaigns/delete/'.$campaign->getId()); $response = $this->client->getResponse(); Assert::assertSame(Response::HTTP_OK, $response->getStatusCode(), $response->getContent()); $eventLogs = $this->em->getRepository(LeadEventLog::class)->findAll(); Assert::assertCount(0, $eventLogs); } private function createLead(): Lead { $lead = new Lead(); $lead->setFirstname('Test'); $this->em->persist($lead); $this->em->flush(); return $lead; } private function createCampaign(): Campaign { $campaign = new Campaign(); $campaign->setName('My campaign'); $this->em->persist($campaign); $this->em->flush(); return $campaign; } private function createEvent(string $name, Campaign $campaign): Event { $event = new Event(); $event->setName($name); $event->setCampaign($campaign); $event->setType('email.send'); $event->setEventType('action'); $this->em->persist($event); $this->em->flush(); return $event; } private function createEventLog(Lead $lead, Event $event, Campaign $campaign): LeadEventLog { $leadEventLog = new LeadEventLog(); $leadEventLog->setLead($lead); $leadEventLog->setEvent($event); $leadEventLog->setCampaign($campaign); $this->em->persist($leadEventLog); $this->em->flush(); return $leadEventLog; } }