Spaces:
No application file
No application file
File size: 7,051 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 |
<?php
namespace Mautic\LeadBundle\Tests\Model;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\LeadList;
use Mautic\LeadBundle\Entity\LeadListRepository;
use Mautic\LeadBundle\Entity\LeadRepository;
use Mautic\LeadBundle\Model\ListModel;
use Mautic\UserBundle\Entity\User;
use PHPUnit\Framework\Assert;
class ListModelFunctionalTest extends MauticMysqlTestCase
{
public function testPublicSegmentsInContactPreferences(): void
{
$user = $this->em->getRepository(User::class)->findBy([], [], 1)[0];
$firstLeadList = $this->createLeadList($user, 'First', true);
$secondLeadList = $this->createLeadList($user, 'Second', false);
$thirdLeadList = $this->createLeadList($user, 'Third', true);
$this->em->flush();
/** @var LeadListRepository $repo */
$repo = $this->em->getRepository(LeadList::class);
$lists = $repo->getGlobalLists();
Assert::assertCount(2, $lists);
Assert::assertArrayHasKey($firstLeadList->getId(), $lists);
Assert::assertArrayHasKey($thirdLeadList->getId(), $lists);
Assert::assertArrayNotHasKey(
$secondLeadList->getId(),
$lists,
'Non-global lists should not be returned by the `getGlobalLists()` method.'
);
}
public function testSegmentLineChartData(): void
{
/** @var ListModel $segmentModel */
$segmentModel = static::getContainer()->get('mautic.lead.model.list');
/** @var LeadRepository $contactRepository */
$contactRepository = $this->em->getRepository(Lead::class);
$segment = new LeadList();
$segment->setName('Segment A');
$segmentModel->saveEntity($segment);
$contacts = [new Lead(), new Lead(), new Lead(), new Lead()];
$contactRepository->saveEntities($contacts);
$segmentModel->addLead($contacts[0], $segment); // Emulating adding by a filter.
$segmentModel->addLead($contacts[1], $segment); // Emulating adding by a filter.
$segmentModel->addLead($contacts[2], $segment, true); // Manually added.
$segmentModel->addLead($contacts[3], $segment, true); // Manually added.
$data = $segmentModel->getSegmentContactsLineChartData(
'd',
new \DateTime('1 month ago', new \DateTimeZone('UTC')),
new \DateTime('now', new \DateTimeZone('UTC')),
null,
['leadlist_id' => ['value' => $segment->getId(), 'list_column_name' => 't.lead_id']]
);
Assert::assertSame('added', strtolower($data['datasets'][0]['label']));
Assert::assertSame('removed', strtolower($data['datasets'][1]['label']));
Assert::assertSame('total', strtolower($data['datasets'][2]['label']));
Assert::assertSame(4, (int) end($data['datasets'][0]['data'])); // Added for today.
Assert::assertSame(0, (int) end($data['datasets'][1]['data'])); // Removed for today.
Assert::assertSame(4, (int) end($data['datasets'][2]['data'])); // Total for today.
// To make this interesting, lets' remove some contacts to see what happens.
$segmentModel->removeLead($contacts[1], $segment); // Emulating removing by a filter.
$segmentModel->removeLead($contacts[2], $segment, true); // Manually removed.
$data = $segmentModel->getSegmentContactsLineChartData(
'd',
new \DateTime('1 month ago', new \DateTimeZone('UTC')),
new \DateTime('now', new \DateTimeZone('UTC')),
null,
['leadlist_id' => ['value' => $segment->getId(), 'list_column_name' => 't.lead_id']]
);
Assert::assertSame(4, (int) end($data['datasets'][0]['data'])); // Added for today.
Assert::assertSame(2, (int) end($data['datasets'][1]['data'])); // Removed for today.
Assert::assertSame(2, (int) end($data['datasets'][2]['data'])); // Total for today.
}
public function testSegmentLineChartDataWithoutFetchDataFromLeadListTable(): void
{
/** @var ListModel $segmentModel */
$segmentModel = static::getContainer()->get('mautic.lead.model.list');
/** @var LeadRepository $contactRepository */
$contactRepository = $this->em->getRepository(Lead::class);
$segment = new LeadList();
$segment->setName('Segment A');
$segmentModel->saveEntity($segment);
$contacts = [new Lead()];
$contactRepository->saveEntities($contacts);
// Adding record in mautic_lead_lists_leads before 11 second from mautic_lead_event_log
// using old code there should be double records means 2 but now it will show only 1 contact
$segmentModel->addLead($contacts[0], $segment, true, false, 1, new \DateTime('-11 seconds', new \DateTimeZone('UTC'))); // Emulating adding by a filter.
$data = $segmentModel->getSegmentContactsLineChartData(
'd',
new \DateTime('-2 days', new \DateTimeZone('UTC')),
new \DateTime('now', new \DateTimeZone('UTC')),
null,
['leadlist_id' => ['value' => $segment->getId(), 'list_column_name' => 't.lead_id']]
);
// using old code there should be only 1 label added but now there should be all 3 labels
Assert::assertSame('added', strtolower($data['datasets'][0]['label']));
Assert::assertSame('removed', strtolower($data['datasets'][1]['label']));
Assert::assertSame('total', strtolower($data['datasets'][2]['label']));
Assert::assertSame(1, (int) end($data['datasets'][0]['data'])); // Added for today.
Assert::assertSame(0, (int) end($data['datasets'][1]['data'])); // Removed for today.
Assert::assertSame(1, (int) end($data['datasets'][2]['data'])); // Total for today.
// To make this interesting, lets' remove some contacts to see what happens.
$segmentModel->removeLead($contacts[0], $segment, true);
$data = $segmentModel->getSegmentContactsLineChartData(
'd',
new \DateTime('-2 days', new \DateTimeZone('UTC')),
new \DateTime('now', new \DateTimeZone('UTC')),
null,
['leadlist_id' => ['value' => $segment->getId(), 'list_column_name' => 't.lead_id']]
);
Assert::assertSame(1, (int) end($data['datasets'][0]['data'])); // Added for today.
Assert::assertSame(1, (int) end($data['datasets'][1]['data'])); // Removed for today.
Assert::assertSame(0, (int) end($data['datasets'][2]['data'])); // Total for today.
}
private function createLeadList(User $user, string $name, bool $isGlobal): LeadList
{
$leadList = new LeadList();
$leadList->setName($name);
$leadList->setPublicName('Public'.$name);
$leadList->setAlias(mb_strtolower($name));
$leadList->setCreatedBy($user);
$leadList->setIsGlobal($isGlobal);
$this->em->persist($leadList);
return $leadList;
}
}
|