Spaces:
No application file
No application file
File size: 5,312 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 |
<?php
namespace Mautic\ChannelBundle\Tests\PreferenceBuilder;
use Doctrine\Common\Collections\ArrayCollection;
use Mautic\CampaignBundle\Entity\Event;
use Mautic\CampaignBundle\Entity\LeadEventLog;
use Mautic\ChannelBundle\PreferenceBuilder\ChannelPreferences;
use Mautic\ChannelBundle\PreferenceBuilder\PreferenceBuilder;
use Mautic\LeadBundle\Entity\DoNotContact;
use Mautic\LeadBundle\Entity\Lead;
use Psr\Log\NullLogger;
class PreferenceBuilderTest extends \PHPUnit\Framework\TestCase
{
public function testChannelsArePrioritized(): void
{
$lead = $this->getMockBuilder(Lead::class)
->getMock();
$lead->expects($this->once())
->method('getChannelRules')
->willReturn(
[
'sms' => [
'dnc' => DoNotContact::IS_CONTACTABLE,
],
'email' => [
'dnc' => DoNotContact::IS_CONTACTABLE,
],
]
);
$log = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log->method('getLead')
->willReturn($lead);
$log->method('getId')
->willReturn(1);
$lead2 = $this->getMockBuilder(Lead::class)
->getMock();
$lead2->expects($this->once())
->method('getChannelRules')
->willReturn(
[
'email' => [
'dnc' => DoNotContact::IS_CONTACTABLE,
],
'sms' => [
'dnc' => DoNotContact::UNSUBSCRIBED,
],
]
);
$log2 = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log2->method('getLead')
->willReturn($lead2);
$log2->method('getId')
->willReturn(2);
$logs = new ArrayCollection([$log, $log2]);
$event = new Event();
$builder = new PreferenceBuilder($logs, $event, ['email' => [], 'sms' => [], 'push' => []], new NullLogger());
$preferences = $builder->getChannelPreferences();
$this->assertCount(3, $preferences);
$this->assertTrue(isset($preferences['email']));
$this->assertTrue(isset($preferences['sms']));
$this->assertTrue(isset($preferences['push']));
/** @var ChannelPreferences $emailLogs */
$email = $preferences['email'];
// First priority
$emailLogs = $email->getLogsByPriority(1);
$this->assertCount(1, $emailLogs);
$this->assertEquals(2, $emailLogs->first()->getId());
// Second priority
$emailLogs = $email->getLogsByPriority(2);
$this->assertCount(1, $emailLogs);
$this->assertEquals(1, $emailLogs->first()->getId());
// First priority for SMS which should just be one
/** @var ChannelPreferences $smsLogs */
$sms = $preferences['sms'];
$smsLogs = $sms->getLogsByPriority(1);
$this->assertCount(1, $smsLogs);
$this->assertEquals(1, $smsLogs->first()->getId());
// None for second priority because of DNC
$smsLogs = $sms->getLogsByPriority(2);
$this->assertCount(0, $smsLogs);
// No one had push enabled but it should be defined
$push = $preferences['push'];
$pushLogs = $push->getLogsByPriority(1);
$this->assertCount(0, $pushLogs);
}
public function testLogIsRemovedFromAllChannels(): void
{
$lead = $this->getMockBuilder(Lead::class)
->getMock();
$lead->expects($this->once())
->method('getChannelRules')
->willReturn(
[
'sms' => [
'dnc' => DoNotContact::IS_CONTACTABLE,
],
'email' => [
'dnc' => DoNotContact::IS_CONTACTABLE,
],
]
);
$log = $this->getMockBuilder(LeadEventLog::class)
->getMock();
$log->method('getLead')
->willReturn($lead);
$log->method('getId')
->willReturn(1);
$logs = new ArrayCollection([$log]);
$event = new Event();
$builder = new PreferenceBuilder($logs, $event, ['email' => [], 'sms' => [], 'push' => []], new NullLogger());
$preferences = $builder->getChannelPreferences();
/** @var ChannelPreferences $sms */
$sms = $preferences['sms'];
$smsLogs = $sms->getLogsByPriority(1);
$this->assertCount(1, $smsLogs);
/** @var ChannelPreferences $email */
$email = $preferences['email'];
$emailLogs = $email->getLogsByPriority(2);
$this->assertCount(1, $emailLogs);
$builder->removeLogFromAllChannels($log);
$preferences = $builder->getChannelPreferences();
/** @var ChannelPreferences $sms */
$sms = $preferences['sms'];
$smsLogs = $sms->getLogsByPriority(1);
$this->assertCount(0, $smsLogs);
/** @var ChannelPreferences $email */
$email = $preferences['email'];
$emailLogs = $email->getLogsByPriority(2);
$this->assertCount(0, $emailLogs);
}
}
|