Initial commit: CloudOps infrastructure platform
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mautic\ChannelBundle\Tests\Model;
|
||||
|
||||
use Mautic\ChannelBundle\Model\ChannelActionModel;
|
||||
use Mautic\LeadBundle\Entity\DoNotContact as DNC;
|
||||
use Mautic\LeadBundle\Entity\Lead;
|
||||
use Mautic\LeadBundle\Model\DoNotContact;
|
||||
use Mautic\LeadBundle\Model\LeadModel;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class ChannelActionModelTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
private \PHPUnit\Framework\MockObject\MockObject $contactMock5;
|
||||
|
||||
private \PHPUnit\Framework\MockObject\MockObject $contactMock6;
|
||||
|
||||
private \PHPUnit\Framework\MockObject\MockObject $contactModelMock;
|
||||
|
||||
private \PHPUnit\Framework\MockObject\MockObject $doNotContactMock;
|
||||
|
||||
private \PHPUnit\Framework\MockObject\MockObject $translatorMock;
|
||||
|
||||
private ChannelActionModel $actionModel;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->contactMock5 = $this->createMock(Lead::class);
|
||||
$this->contactMock6 = $this->createMock(Lead::class);
|
||||
$this->contactModelMock = $this->createMock(LeadModel::class);
|
||||
$this->doNotContactMock = $this->createMock(DoNotContact::class);
|
||||
$this->translatorMock = $this->createMock(TranslatorInterface::class);
|
||||
$this->actionModel = new ChannelActionModel(
|
||||
$this->contactModelMock,
|
||||
$this->doNotContactMock,
|
||||
$this->translatorMock
|
||||
);
|
||||
|
||||
$this->contactMock5->method('getId')->willReturn(5);
|
||||
}
|
||||
|
||||
public function testUpdateEntityAccess(): void
|
||||
{
|
||||
$contacts = [5, 6];
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getLeadsByIds')
|
||||
->with($contacts)
|
||||
->willReturn([$this->contactMock5, $this->contactMock6]);
|
||||
$matcher = $this->exactly(2);
|
||||
|
||||
$this->contactModelMock->expects($matcher)
|
||||
->method('canEditContact')->willReturnCallback(function (...$parameters) use ($matcher) {
|
||||
if (1 === $matcher->numberOfInvocations()) {
|
||||
$this->assertSame($this->contactMock5, $parameters[0]);
|
||||
}
|
||||
if (2 === $matcher->numberOfInvocations()) {
|
||||
$this->assertSame($this->contactMock6, $parameters[0]);
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
$this->contactModelMock->expects($this->never())
|
||||
->method('getContactChannels');
|
||||
|
||||
$this->actionModel->update($contacts, []);
|
||||
}
|
||||
|
||||
public function testSubscribeContactToEmailChannel(): void
|
||||
{
|
||||
$contacts = [5];
|
||||
$subscribedChannels = ['email', 'sms']; // Subscribe contact to these channels
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getLeadsByIds')
|
||||
->with($contacts)
|
||||
->willReturn([$this->contactMock5]);
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('canEditContact')
|
||||
->with($this->contactMock5)
|
||||
->willReturn(true);
|
||||
|
||||
// Contact is already subscribed to the SMS channel but not to email
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getContactChannels')
|
||||
->with($this->contactMock5)
|
||||
->willReturn(['sms' => 'sms']);
|
||||
|
||||
$this->doNotContactMock->expects($this->once())
|
||||
->method('isContactable')
|
||||
->with($this->contactMock5, 'email')
|
||||
->willReturn(DNC::IS_CONTACTABLE);
|
||||
|
||||
$this->doNotContactMock->expects($this->once())
|
||||
->method('removeDncForContact')
|
||||
->with(5, 'email');
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getPreferenceChannels')
|
||||
->willReturn(['Email' => 'email', 'Text Message' => 'sms']);
|
||||
|
||||
$this->doNotContactMock->expects($this->never())
|
||||
->method('addDncForContact');
|
||||
|
||||
$this->actionModel->update($contacts, $subscribedChannels);
|
||||
}
|
||||
|
||||
public function testSubscribeContactWhoUnsubscribedToEmailChannel(): void
|
||||
{
|
||||
$contacts = [5];
|
||||
$subscribedChannels = ['email', 'sms']; // Subscribe contact to these channels
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getLeadsByIds')
|
||||
->with($contacts)
|
||||
->willReturn([$this->contactMock5]);
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('canEditContact')
|
||||
->with($this->contactMock5)
|
||||
->willReturn(true);
|
||||
|
||||
// Contact is already subscribed to the SMS channel but not to email
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getContactChannels')
|
||||
->with($this->contactMock5)
|
||||
->willReturn(['sms' => 'sms']);
|
||||
|
||||
$this->doNotContactMock->expects($this->once())
|
||||
->method('isContactable')
|
||||
->with($this->contactMock5, 'email')
|
||||
->willReturn(DNC::UNSUBSCRIBED);
|
||||
|
||||
$this->doNotContactMock->expects($this->never())
|
||||
->method('removeDncForContact');
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getPreferenceChannels')
|
||||
->willReturn(['Email' => 'email', 'Text Message' => 'sms']);
|
||||
|
||||
$this->doNotContactMock->expects($this->never())
|
||||
->method('addDncForContact');
|
||||
|
||||
$this->actionModel->update($contacts, $subscribedChannels);
|
||||
}
|
||||
|
||||
public function testUnsubscribeContactFromSmsChannel(): void
|
||||
{
|
||||
$contacts = [5];
|
||||
$subscribedChannels = []; // Unsubscribe contact from missing
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getLeadsByIds')
|
||||
->with($contacts)
|
||||
->willReturn([$this->contactMock5]);
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('canEditContact')
|
||||
->with($this->contactMock5)
|
||||
->willReturn(true);
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getContactChannels')
|
||||
->with($this->contactMock5)
|
||||
->willReturn(['sms' => 'sms']);
|
||||
|
||||
$this->doNotContactMock->expects($this->never())
|
||||
->method('isContactable');
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getPreferenceChannels')
|
||||
->willReturn(['Email' => 'email', 'Text Message' => 'sms']);
|
||||
$matcher = $this->exactly(2);
|
||||
|
||||
$this->doNotContactMock->expects($matcher)
|
||||
->method('addDncForContact')->willReturnCallback(function (...$parameters) use ($matcher) {
|
||||
if (1 === $matcher->numberOfInvocations()) {
|
||||
$this->assertSame(5, $parameters[0]);
|
||||
$this->assertSame('email', $parameters[1]);
|
||||
$this->assertSame(DNC::MANUAL, $parameters[2]);
|
||||
}
|
||||
if (2 === $matcher->numberOfInvocations()) {
|
||||
$this->assertSame(5, $parameters[0]);
|
||||
$this->assertSame('sms', $parameters[1]);
|
||||
$this->assertSame(DNC::MANUAL, $parameters[2]);
|
||||
}
|
||||
});
|
||||
|
||||
$this->actionModel->update($contacts, $subscribedChannels);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\ChannelBundle\Tests\Model;
|
||||
|
||||
use Doctrine\Common\Collections\AbstractLazyCollection;
|
||||
use Mautic\ChannelBundle\Model\FrequencyActionModel;
|
||||
use Mautic\LeadBundle\Entity\FrequencyRule;
|
||||
use Mautic\LeadBundle\Entity\FrequencyRuleRepository;
|
||||
use Mautic\LeadBundle\Entity\Lead;
|
||||
use Mautic\LeadBundle\Model\LeadModel;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
|
||||
class FrequencyActionModelTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var MockObject|Lead
|
||||
*/
|
||||
private MockObject $contactMock5;
|
||||
|
||||
/**
|
||||
* @var MockObject|LeadModel
|
||||
*/
|
||||
private MockObject $contactModelMock;
|
||||
|
||||
/**
|
||||
* @var MockObject|FrequencyRuleRepository
|
||||
*/
|
||||
private MockObject $frequencyRepoMock;
|
||||
|
||||
/**
|
||||
* @var MockObject|FrequencyRule
|
||||
*/
|
||||
private MockObject $frequencyRuleEmailMock;
|
||||
|
||||
/**
|
||||
* @var MockObject|FrequencyRule
|
||||
*/
|
||||
private MockObject $frequencyRuleSmsMock;
|
||||
|
||||
private FrequencyActionModel $actionModel;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->contactMock5 = $this->createMock(Lead::class);
|
||||
$this->contactModelMock = $this->createMock(LeadModel::class);
|
||||
$this->frequencyRepoMock = $this->createMock(FrequencyRuleRepository::class);
|
||||
$this->frequencyRuleEmailMock = $this->createMock(FrequencyRule::class);
|
||||
$this->frequencyRuleSmsMock = $this->createMock(FrequencyRule::class);
|
||||
$collectionMock = $this->createMock(AbstractLazyCollection::class);
|
||||
$this->actionModel = new FrequencyActionModel(
|
||||
$this->contactModelMock,
|
||||
$this->frequencyRepoMock
|
||||
);
|
||||
|
||||
$collectionMock->method('toArray')
|
||||
->willReturn([
|
||||
'email' => $this->frequencyRuleEmailMock,
|
||||
'sms' => $this->frequencyRuleSmsMock,
|
||||
]);
|
||||
|
||||
$this->contactMock5->method('getFrequencyRules')->willReturn($collectionMock);
|
||||
}
|
||||
|
||||
public function testUpdateWhenEntityAccess(): void
|
||||
{
|
||||
$contacts = [5];
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getLeadsByIds')
|
||||
->with($contacts)
|
||||
->willReturn([$this->contactMock5]);
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('canEditContact')
|
||||
->with($this->contactMock5)
|
||||
->willReturn(false);
|
||||
|
||||
$this->contactModelMock->expects($this->never())
|
||||
->method('getPreferenceChannels');
|
||||
|
||||
$this->actionModel->update($contacts, [], '');
|
||||
}
|
||||
|
||||
public function testUpdate(): void
|
||||
{
|
||||
$contacts = [5];
|
||||
$params = [
|
||||
'subscribed_channels' => ['email', 'sms'],
|
||||
'frequency_number_email' => '2',
|
||||
'frequency_time_email' => 'WEEK',
|
||||
'preferred_channel' => 'email',
|
||||
'contact_pause_start_date_email' => '2018-05-13',
|
||||
'contact_pause_end_date_email' => '2018-05-26',
|
||||
'frequency_number_sms' => '',
|
||||
'frequency_time_sms' => '',
|
||||
'contact_pause_start_date_sms' => '',
|
||||
'contact_pause_end_date_sms' => '',
|
||||
];
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getLeadsByIds')
|
||||
->with($contacts)
|
||||
->willReturn([$this->contactMock5]);
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('canEditContact')
|
||||
->with($this->contactMock5)
|
||||
->willReturn(true);
|
||||
|
||||
$this->contactModelMock->expects($this->once())
|
||||
->method('getPreferenceChannels')
|
||||
->willReturn([
|
||||
'Email' => 'email',
|
||||
'Text Message' => 'sms',
|
||||
]);
|
||||
|
||||
$this->frequencyRuleEmailMock->expects($this->once())
|
||||
->method('setChannel')
|
||||
->with('email');
|
||||
|
||||
$this->frequencyRuleEmailMock->expects($this->once())
|
||||
->method('setLead')
|
||||
->with($this->contactMock5);
|
||||
|
||||
$this->frequencyRuleEmailMock->expects($this->once())
|
||||
->method('setDateAdded');
|
||||
|
||||
$this->frequencyRuleEmailMock->expects($this->once())
|
||||
->method('setFrequencyNumber')
|
||||
->with('2');
|
||||
|
||||
$this->frequencyRuleEmailMock->expects($this->once())
|
||||
->method('setFrequencyTime')
|
||||
->with('WEEK');
|
||||
|
||||
$this->frequencyRuleEmailMock->expects($this->once())
|
||||
->method('setPauseFromDate')
|
||||
->with(new \DateTime('2018-05-13T00:00:00.000000+0000'));
|
||||
|
||||
$this->frequencyRuleEmailMock->expects($this->once())
|
||||
->method('setPauseToDate')
|
||||
->with(new \DateTime('2018-05-26T00:00:00.000000+0000'));
|
||||
|
||||
$this->frequencyRuleEmailMock->expects($this->once())
|
||||
->method('setPreferredChannel')
|
||||
->with(true);
|
||||
$matcher = $this->exactly(2);
|
||||
|
||||
$this->contactMock5->expects($matcher)
|
||||
->method('addFrequencyRule')->willReturnCallback(function (...$parameters) use ($matcher) {
|
||||
if (1 === $matcher->numberOfInvocations()) {
|
||||
$this->assertEquals($this->frequencyRuleEmailMock, $parameters[0]);
|
||||
}
|
||||
if (2 === $matcher->numberOfInvocations()) {
|
||||
$this->assertEquals($this->frequencyRuleEmailMock, $parameters[0]);
|
||||
}
|
||||
});
|
||||
$matcher = $this->exactly(2);
|
||||
|
||||
$this->frequencyRepoMock->expects($matcher)
|
||||
->method('saveEntity')->willReturnCallback(function (...$parameters) use ($matcher) {
|
||||
if (1 === $matcher->numberOfInvocations()) {
|
||||
$this->assertSame($this->frequencyRuleEmailMock, $parameters[0]);
|
||||
}
|
||||
if (2 === $matcher->numberOfInvocations()) {
|
||||
$this->assertSame($this->frequencyRuleSmsMock, $parameters[0]);
|
||||
}
|
||||
});
|
||||
|
||||
$this->frequencyRuleSmsMock->expects($this->once())
|
||||
->method('setChannel')
|
||||
->with('sms');
|
||||
|
||||
$this->frequencyRuleSmsMock->expects($this->once())
|
||||
->method('setLead')
|
||||
->with($this->contactMock5);
|
||||
|
||||
$this->frequencyRuleSmsMock->expects($this->once())
|
||||
->method('setDateAdded');
|
||||
|
||||
$this->frequencyRuleSmsMock->expects($this->once())
|
||||
->method('setFrequencyNumber')
|
||||
->with(null);
|
||||
|
||||
$this->frequencyRuleSmsMock->expects($this->once())
|
||||
->method('setFrequencyTime')
|
||||
->with(null);
|
||||
|
||||
$this->frequencyRuleSmsMock->expects($this->never())
|
||||
->method('setPauseFromDate');
|
||||
|
||||
$this->frequencyRuleSmsMock->expects($this->never())
|
||||
->method('setPauseToDate');
|
||||
|
||||
$this->frequencyRuleSmsMock->expects($this->once())
|
||||
->method('setPreferredChannel')
|
||||
->with(false);
|
||||
|
||||
$this->actionModel->update($contacts, $params, 'email');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
namespace Mautic\ChannelBundle\Tests\Model;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Mautic\ChannelBundle\Entity\MessageQueue;
|
||||
use Mautic\ChannelBundle\Entity\MessageQueueRepository;
|
||||
use Mautic\ChannelBundle\Model\MessageQueueModel;
|
||||
use Mautic\CoreBundle\Helper\CoreParametersHelper;
|
||||
use Mautic\CoreBundle\Helper\UserHelper;
|
||||
use Mautic\CoreBundle\Security\Permissions\CorePermissions;
|
||||
use Mautic\CoreBundle\Translation\Translator;
|
||||
use Mautic\LeadBundle\Entity\Lead;
|
||||
use Mautic\LeadBundle\Entity\LeadRepository;
|
||||
use Mautic\LeadBundle\Model\CompanyModel;
|
||||
use Mautic\LeadBundle\Model\LeadModel;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
class MessageQueueModelTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const DATE = '2019-07-07 15:00:00';
|
||||
|
||||
/**
|
||||
* @var MessageQueueModel
|
||||
*/
|
||||
protected $messageQueue;
|
||||
|
||||
/**
|
||||
* @var MessageQueue
|
||||
*/
|
||||
protected $message;
|
||||
|
||||
/** @var MockObject|LeadModel */
|
||||
protected $leadModel;
|
||||
|
||||
/** @var MockObject|CompanyModel */
|
||||
protected $companyModel;
|
||||
|
||||
/** @var MockObject|EntityManagerInterface */
|
||||
protected $entityManager;
|
||||
|
||||
/** @var MockObject|MessageQueueRepository */
|
||||
protected $messageQueueRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->leadModel = $this->createMock(LeadModel::class);
|
||||
$this->companyModel = $this->createMock(CompanyModel::class);
|
||||
$this->entityManager = $this->createMock(EntityManagerInterface::class);
|
||||
$this->messageQueueRepository = $this->createMock(MessageQueueRepository::class);
|
||||
$coreHelper = $this->createMock(CoreParametersHelper::class);
|
||||
|
||||
$this->messageQueue = new MessageQueueModel(
|
||||
$this->leadModel,
|
||||
$this->companyModel,
|
||||
$coreHelper,
|
||||
$this->entityManager,
|
||||
$this->createMock(CorePermissions::class),
|
||||
$this->createMock(EventDispatcherInterface::class),
|
||||
$this->createMock(UrlGeneratorInterface::class),
|
||||
$this->createMock(Translator::class),
|
||||
$this->createMock(UserHelper::class),
|
||||
$this->createMock(LoggerInterface::class)
|
||||
);
|
||||
|
||||
$this->entityManager->method('getRepository')->willReturn($this->messageQueueRepository);
|
||||
|
||||
$message = new MessageQueue();
|
||||
$scheduleDate = new \DateTime(self::DATE);
|
||||
$message->setScheduledDate($scheduleDate);
|
||||
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
public function testRescheduleMessageIntervalDay(): void
|
||||
{
|
||||
$interval = new \DateInterval('P2D');
|
||||
$this->prepareRescheduleMessageIntervalTest($interval);
|
||||
}
|
||||
|
||||
public function testRescheduleMessageIntervalWeek(): void
|
||||
{
|
||||
$interval = new \DateInterval('P4W');
|
||||
$this->prepareRescheduleMessageIntervalTest($interval);
|
||||
}
|
||||
|
||||
public function testRescheduleMessageIntervalMonth(): void
|
||||
{
|
||||
$interval = new \DateInterval('P8M');
|
||||
$this->prepareRescheduleMessageIntervalTest($interval);
|
||||
}
|
||||
|
||||
public function testRescheduleMessageNoInterval(): void
|
||||
{
|
||||
$interval = new \DateInterval('PT0S');
|
||||
$this->prepareRescheduleMessageIntervalTest($interval);
|
||||
}
|
||||
|
||||
protected function prepareRescheduleMessageIntervalTest(\DateInterval $interval)
|
||||
{
|
||||
$oldScheduleDate = $this->message->getScheduledDate();
|
||||
$this->messageQueue->reschedule($this->message, $interval);
|
||||
$scheduleDate = $this->message->getScheduledDate();
|
||||
/** @var \DateTime $oldScheduleDate */
|
||||
$oldScheduleDate->add($interval);
|
||||
|
||||
$this->assertEquals($oldScheduleDate, $scheduleDate);
|
||||
$this->assertNotSame($oldScheduleDate, $scheduleDate);
|
||||
}
|
||||
|
||||
public function testSendMessagesWithNullEvent(): void
|
||||
{
|
||||
$queue = $this->message;
|
||||
$lead = new Lead();
|
||||
$lead->setId(1);
|
||||
$queue->setLead($lead);
|
||||
|
||||
$contactData = [
|
||||
1 => [
|
||||
'firstname' => 'John',
|
||||
'email' => 'john.doe@example.com',
|
||||
],
|
||||
];
|
||||
|
||||
$leadRepository = $this->createMock(LeadRepository::class);
|
||||
$this->leadModel->method('getRepository')->willReturn($leadRepository);
|
||||
$leadRepository->method('getContacts')->willReturn($contactData);
|
||||
|
||||
$this->entityManager->expects($this->exactly(1))
|
||||
->method('detach');
|
||||
|
||||
$this->messageQueueRepository->method('getQueuedMessages')
|
||||
->willReturn([$queue]);
|
||||
|
||||
$this->messageQueue->sendMessages('email', 1);
|
||||
}
|
||||
|
||||
public function testProcessMessageQueueLeadFieldsShouldNotContainCompany(): void
|
||||
{
|
||||
$queue = $this->message;
|
||||
|
||||
$lead = new Lead();
|
||||
$lead->setId(1);
|
||||
$queue->setLead($lead);
|
||||
|
||||
$contactData = [
|
||||
1 => [
|
||||
'firstname' => 'John',
|
||||
'email' => 'john.doe@example.com',
|
||||
],
|
||||
];
|
||||
|
||||
$leadRepository = $this->createMock(LeadRepository::class);
|
||||
$this->leadModel->method('getRepository')->willReturn($leadRepository);
|
||||
$leadRepository->method('getContacts')->willReturn($contactData);
|
||||
|
||||
$this->messageQueue->processMessageQueue($queue);
|
||||
$this->assertArrayNotHasKey('companies', $queue->getLead()->getFields());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user