Spaces:
No application file
No application file
File size: 8,652 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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
<?php
declare(strict_types=1);
namespace Mautic\EmailBundle\Tests\Entity;
use Doctrine\ORM\ORMException;
use Mautic\CategoryBundle\Entity\Category;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\EmailBundle\Entity\Email;
use Mautic\EmailBundle\Entity\EmailRepository;
use Mautic\LeadBundle\Entity\DoNotContact;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Entity\LeadCategory;
use Mautic\LeadBundle\Entity\LeadList;
use Mautic\LeadBundle\Entity\ListLead;
use PHPUnit\Framework\Assert;
class EmailRepositoryFunctionalTest extends MauticMysqlTestCase
{
private EmailRepository $emailRepository;
protected function setUp(): void
{
parent::setUp();
/** @var EmailRepository $repository */
$repository = $this->em->getRepository(Email::class);
$this->emailRepository = $repository;
}
public function testGetDoNotEmailListEmpty(): void
{
$result = $this->emailRepository->getDoNotEmailList();
Assert::assertSame([], $result);
}
public function testGetDoNotEmailListNotEmpty(): void
{
$lead = new Lead();
$lead->setEmail('[email protected]');
$this->em->persist($lead);
$doNotContact = new DoNotContact();
$doNotContact->setLead($lead);
$doNotContact->setDateAdded(new \DateTime());
$doNotContact->setChannel('email');
$this->em->persist($doNotContact);
$this->em->flush();
// no $leadIds
$result = $this->emailRepository->getDoNotEmailList();
Assert::assertSame([$lead->getId() => $lead->getEmail()], $result);
// matching $leadIds
$result = $this->emailRepository->getDoNotEmailList([$lead->getId()]);
Assert::assertSame([$lead->getId() => $lead->getEmail()], $result);
// mismatching $leadIds
$result = $this->emailRepository->getDoNotEmailList([-1]);
Assert::assertSame([], $result);
}
public function testCheckDoNotEmailNonExistent(): void
{
$result = $this->emailRepository->checkDoNotEmail('[email protected]');
Assert::assertFalse($result);
}
public function testCheckDoNotEmailExistent(): void
{
$lead = new Lead();
$lead->setEmail('[email protected]');
$this->em->persist($lead);
$doNotContact = new DoNotContact();
$doNotContact->setLead($lead);
$doNotContact->setDateAdded(new \DateTime());
$doNotContact->setChannel('email');
$doNotContact->setReason(1);
$doNotContact->setComments('Some comment');
$this->em->persist($doNotContact);
$this->em->flush();
$result = $this->emailRepository->checkDoNotEmail('[email protected]');
Assert::assertSame([
'id' => (string) $doNotContact->getId(),
'unsubscribed' => true,
'bounced' => false,
'manual' => false,
'comments' => $doNotContact->getComments(),
], $result);
}
public function testGetEmailPendingQueryWithSubscribedCategory(): void
{
// create some leads
$leadOne = $this->createLead('one');
$leadTwo = $this->createLead('two');
$leadThree = $this->createLead('three');
$leadFour = $this->createLead('four');
// create some categories
$catOne = $this->createCategory('one');
$catTwo = $this->createCategory('two');
$catThree = $this->createCategory('three');
// lead to subscribe categories
$this->subscribeCategory($leadOne, true, $catOne, $catTwo);
$this->subscribeCategory($leadTwo, true, $catOne, $catThree);
$this->subscribeCategory($leadThree, true, $catTwo, $catThree);
$this->subscribeCategory($leadFour, true, $catOne, $catThree);
// lead to unsubscribe categories
$this->subscribeCategory($leadOne, false, $catThree);
$sourceListOne = $this->createLeadList('Source', $leadOne, $leadTwo, $leadThree, $leadFour);
// create an email with included/excluded lists
$email = new Email();
$email->setName('Email');
$email->setSubject('Subject');
$email->setEmailType('list');
$email->addList($sourceListOne);
$email->setCategory($catThree);
$this->em->persist($email);
$this->em->flush();
$this->em->clear();
$result = $this->emailRepository->getEmailPendingQuery($email->getId())
->executeQuery()
->fetchAllAssociative();
$actualLeadIds = array_map('intval', array_column($result, 'id'));
sort($actualLeadIds);
$expectedLeadIds = [$leadTwo->getId(), $leadThree->getId(), $leadFour->getId()];
sort($expectedLeadIds);
$this->assertSame($expectedLeadIds, $actualLeadIds);
}
public function testGetEmailPendingQueryWithExcludedLists(): void
{
// create some leads
$leadOne = $this->createLead('one');
$leadTwo = $this->createLead('two');
$leadThree = $this->createLead('three');
$leadFour = $this->createLead('four');
$leadFive = $this->createLead('five');
$leadSix = $this->createLead('six');
// add some leads in lists for inclusion
$sourceListOne = $this->createLeadList('Source', $leadOne, $leadTwo, $leadThree);
$sourceListTwo = $this->createLeadList('Source', $leadOne, $leadFour, $leadFive, $leadSix);
// add some leads in lists for exclusion
$excludeListOne = $this->createLeadList('Exclude', $leadTwo, $leadSix);
$excludeListTwo = $this->createLeadList('Exclude', $leadTwo, $leadThree);
// create an email with included/excluded lists
$email = new Email();
$email->setName('Email');
$email->setSubject('Subject');
$email->setEmailType('list');
$email->addList($sourceListOne);
$email->addList($sourceListTwo);
$email->addExcludedList($excludeListOne);
$email->addExcludedList($excludeListTwo);
$this->em->persist($email);
$this->em->flush();
$this->em->clear();
$actualLeadIds = $this->emailRepository->getEmailPendingQuery($email->getId())
->executeQuery()
->fetchFirstColumn();
sort($actualLeadIds);
$expectedLeadIds = [$leadOne->getId(), $leadFour->getId(), $leadFive->getId()];
$expectedLeadIds = array_map(fn (int $id) => (string) $id, $expectedLeadIds);
sort($expectedLeadIds);
Assert::assertSame($expectedLeadIds, $actualLeadIds);
}
/**
* @throws ORMException
*/
private function createLead(string $lastName): Lead
{
$lead = new Lead();
$lead->setLastname($lastName);
$lead->setEmail(sprintf('%[email protected]', $lastName));
$this->em->persist($lead);
return $lead;
}
/**
* @param Lead ...$leads
*
* @throws ORMException
*/
private function createLeadList(string $name, ...$leads): LeadList
{
$leadList = new LeadList();
$leadList->setName($name);
$leadList->setPublicName($name);
$leadList->setAlias(mb_strtolower($name));
$this->em->persist($leadList);
foreach ($leads as $lead) {
$this->addLeadToList($lead, $leadList);
}
return $leadList;
}
private function addLeadToList(Lead $leadOne, LeadList $sourceList): void
{
$listLead = new ListLead();
$listLead->setLead($leadOne);
$listLead->setList($sourceList);
$listLead->setDateAdded(new \DateTime());
$this->em->persist($listLead);
}
private function createCategory(string $string): Category
{
$category = new Category();
$category->setTitle('Category '.$string);
$category->setAlias('category-'.$string);
$category->setBundle('global');
$this->em->persist($category);
return $category;
}
/**
* @param Category ...$categories
*/
private function subscribeCategory(Lead $lead, bool $subscribed, ...$categories): void
{
foreach ($categories as $category) {
$leadCategory = new LeadCategory();
$leadCategory->setLead($lead);
$leadCategory->setCategory($category);
$leadCategory->setDateAdded(new \DateTime());
$leadCategory->setManuallyAdded($subscribed);
$leadCategory->setManuallyRemoved(!$subscribed);
$this->em->persist($leadCategory);
}
$this->em->flush();
}
}
|