Spaces:
No application file
No application file
File size: 6,216 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 |
<?php
declare(strict_types=1);
namespace Mautic\EmailBundle\Tests\Controller;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\ORMException;
use Mautic\CoreBundle\Entity\AuditLog;
use Mautic\CoreBundle\Test\MauticMysqlTestCase;
use Mautic\EmailBundle\Entity\Email;
use Mautic\LeadBundle\Entity\LeadList;
use PHPUnit\Framework\Assert;
use Symfony\Component\DomCrawler\Field\ChoiceFormField;
use Symfony\Component\HttpFoundation\Request;
class EmailFunctionalTest extends MauticMysqlTestCase
{
public function testExcludedSegmentsConflicting(): void
{
$listOne = $this->createLeadList('One');
$listTwo = $this->createLeadList('Two');
$listThree = $this->createLeadList('Three');
$this->em->flush();
$email = $this->createEmail();
$email->addList($listOne);
$email->addExcludedList($listTwo);
$this->em->flush();
$this->em->clear();
$crawler = $this->client->request(Request::METHOD_GET, "/s/emails/edit/{$email->getId()}");
$this->assertResponseOk();
$form = $crawler->selectButton('Save')
->form();
// change lists/excludedLists and submit the form
$form['emailform[excludedLists]']->setValue([$listOne->getId(), $listThree->getId()]); // @phpstan-ignore-line
$crawler = $this->client->submit($form);
$this->assertResponseOk();
Assert::assertStringContainsString('The same segment cannot be excluded and included in the same time.', $crawler->html());
}
public function testExcludedSegmentsFieldIsUpdated(): void
{
$listOne = $this->createLeadList('One');
$listTwo = $this->createLeadList('Two');
$listThree = $this->createLeadList('Three');
$listFour = $this->createLeadList('Four');
$this->em->flush();
$email = $this->createEmail();
$email->addList($listOne);
$email->addExcludedList($listTwo);
$this->em->flush();
$this->em->clear();
$crawler = $this->client->request(Request::METHOD_GET, "/s/emails/edit/{$email->getId()}");
$this->assertResponseOk();
$form = $crawler->selectButton('Save')
->form();
/** @var ChoiceFormField $listsField */
$listsField = $form['emailform[lists]'];
/** @var ChoiceFormField $excludedListsField */
$excludedListsField = $form['emailform[excludedLists]'];
$expectedAvailableOptions = [
$listOne->getId(),
$listTwo->getId(),
$listThree->getId(),
$listFour->getId(),
];
$this->assertChoiceOptions($listsField, $expectedAvailableOptions, [$listOne->getId()]);
$this->assertChoiceOptions($excludedListsField, $expectedAvailableOptions, [$listTwo->getId()]);
// change lists/excludedLists and submit the form
$listsField->setValue([$listOne->getId(), $listFour->getId()]);
$excludedListsField->setValue([$listTwo->getId(), $listThree->getId()]);
$this->client->submit($form);
$this->assertResponseOk();
$email = $this->em->find(Email::class, $email->getId());
// assert lists/excludedLists changed accordingly
$this->assertEmailLists([
$listOne->getId(),
$listFour->getId(),
], $email->getLists());
$this->assertEmailLists([
$listTwo->getId(),
$listThree->getId(),
], $email->getExcludedLists());
// assert audit log
$auditLogs = $this->em->getRepository(AuditLog::class)->findBy([
'bundle' => 'email',
'object' => 'email',
]);
Assert::assertCount(1, $auditLogs);
/** @var AuditLog $auditLog */
$auditLog = reset($auditLogs);
Assert::assertInstanceOf(AuditLog::class, $auditLog);
$details = $auditLog->getDetails();
Assert::assertIsArray($details);
Assert::assertArrayHasKey('lists', $details);
Assert::assertSame([
[$listOne->getId()],
[$listOne->getId(), $listFour->getId()],
], $details['lists']);
Assert::assertArrayHasKey('excludedLists', $details);
Assert::assertSame([
[$listTwo->getId()],
[$listTwo->getId(), $listThree->getId()],
], $details['excludedLists']);
}
/**
* @throws ORMException
*/
private function createLeadList(string $name): LeadList
{
$leadList = new LeadList();
$leadList->setName($name);
$leadList->setPublicName($name);
$leadList->setAlias(mb_strtolower($name));
$this->em->persist($leadList);
return $leadList;
}
/**
* @param mixed[] $expected
* @param mixed[] $actual
*/
private function assertArrayValuesEquals(array $expected, array $actual): void
{
sort($expected);
sort($actual);
Assert::assertEquals($expected, $actual);
}
/**
* @param mixed[] $expectedAvailableOptions
* @param mixed[] $expectedValue
*/
private function assertChoiceOptions(ChoiceFormField $field, array $expectedAvailableOptions, array $expectedValue): void
{
$this->assertArrayValuesEquals($expectedAvailableOptions, $field->availableOptionValues());
$this->assertArrayValuesEquals($expectedValue, $field->getValue());
}
/**
* @param mixed[] $expectedListIds
*/
private function assertEmailLists(array $expectedListIds, Collection $collection): void
{
$this->assertArrayValuesEquals($expectedListIds, $collection->map(function (LeadList $leadList) {
return $leadList->getId();
})->toArray());
}
private function createEmail(): Email
{
$email = new Email();
$email->setName('Email name');
$email->setSubject('Email subject');
$email->setEmailType('list');
$email->setTemplate('some-template');
$email->setCustomHtml('{}');
$this->em->persist($email);
return $email;
}
private function assertResponseOk(): void
{
Assert::assertTrue($this->client->getResponse()->isOk());
}
}
|