Spaces:
No application file
No application file
File size: 10,376 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
<?php
declare(strict_types=1);
namespace Mautic\DynamicContentBundle\Tests\Unit\Helper;
use Mautic\CampaignBundle\Executioner\RealTimeExecutioner;
use Mautic\CoreBundle\Event\TokenReplacementEvent;
use Mautic\DynamicContentBundle\DynamicContentEvents;
use Mautic\DynamicContentBundle\Entity\DynamicContent;
use Mautic\DynamicContentBundle\Event\ContactFiltersEvaluateEvent;
use Mautic\DynamicContentBundle\Helper\DynamicContentHelper;
use Mautic\DynamicContentBundle\Model\DynamicContentModel;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Model\LeadModel;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcher;
class DynamicContentHelperTest extends \PHPUnit\Framework\TestCase
{
/**
* @var MockObject&DynamicContentModel
*/
private MockObject $mockModel;
/**
* @var MockObject&RealTimeExecutioner
*/
private MockObject $realTimeExecutioner;
/**
* @var MockObject&EventDispatcher
*/
private MockObject $mockDispatcher;
/**
* @var MockObject&LeadModel
*/
private MockObject $leadModel;
private DynamicContentHelper $helper;
protected function setUp(): void
{
$this->mockModel = $this->createMock(DynamicContentModel::class);
$this->realTimeExecutioner = $this->createMock(RealTimeExecutioner::class);
$this->mockDispatcher = $this->createMock(EventDispatcher::class);
$this->leadModel = $this->createMock(LeadModel::class);
$this->helper = new DynamicContentHelper(
$this->mockModel,
$this->realTimeExecutioner,
$this->mockDispatcher,
$this->leadModel,
);
}
public function testGetDwcBySlotNameWithPublished(): void
{
$this->mockModel->expects($this->exactly(2))
->method('getEntities')
->withConsecutive(
[
[
'filter' => [
'where' => [
[
'col' => 'e.slotName',
'expr' => 'eq',
'val' => 'test',
],
[
'col' => 'e.isPublished',
'expr' => 'eq',
'val' => 1,
],
],
],
'ignore_paginator' => true,
],
],
[
[
'filter' => [
'where' => [
[
'col' => 'e.slotName',
'expr' => 'eq',
'val' => 'secondtest',
],
],
],
'ignore_paginator' => true,
],
]
)
->willReturnOnConsecutiveCalls(['some entity'], []);
// Only get published
$this->assertCount(1, $this->helper->getDwcsBySlotName('test', true));
// Get all
$this->assertCount(0, $this->helper->getDwcsBySlotName('secondtest'));
}
public function testGetDynamicContentSlotForLeadWithListenerFindingMatch(): void
{
$slotName = 'test';
$contact = new Lead();
$contact->setFields(['email' => '[email protected]', 'id' => 123]);
$slot = new DynamicContent();
$slot->setName($slotName);
$slot->setIsCampaignBased(false);
// Setting filter that is not known to Mautic, but is for a plugin.
$slot->setFilters([['field' => 'unicorn', 'type' => 'text', 'operator' => '=', 'filter' => 'magic']]);
$slot->setContent('<p>test</p>');
$this->mockModel->method('getEntities')
->willReturn([$slot]);
$this->mockModel->method('getTranslatedEntity')
->willReturn([$slot, $slot]);
$this->leadModel->method('getEntity')
->with(123)
->willReturn($contact);
$this->mockDispatcher->method('hasListeners')->willReturn(true);
$this->mockDispatcher->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[
$this->callback(
function (ContactFiltersEvaluateEvent $event) use ($contact, $slot) {
$this->assertSame($contact, $event->getContact());
$this->assertSame($slot->getFilters(), $event->getFilters());
$event->setIsEvaluated(true);
$event->setIsMatched(true); // Match found in a subscriber.
return true;
}
),
DynamicContentEvents::ON_CONTACTS_FILTER_EVALUATE,
],
[
$this->callback(
function (TokenReplacementEvent $event) use ($contact, $slot) {
$this->assertSame($contact, $event->getLead());
$this->assertSame($slot->getContent(), $event->getContent());
return true;
}
),
DynamicContentEvents::TOKEN_REPLACEMENT,
]
);
Assert::assertSame(
'<p>test</p>',
$this->helper->getDynamicContentSlotForLead($slotName, $contact)
);
}
public function testGetDynamicContentSlotForLeadWithListenerNotFindingMatch(): void
{
$slotName = 'test';
$contact = new Lead();
$contact->setFields(['email' => '[email protected]', 'id' => 123]);
$slot = new DynamicContent();
$slot->setName($slotName);
$slot->setIsCampaignBased(false);
// Setting filter that is not known to Mautic, nor any plugin.
$slot->setFilters([['field' => 'unicorn', 'type' => 'text', 'operator' => '=', 'filter' => 'magic']]);
$slot->setContent('<p>test</p>');
$this->mockModel->method('getEntities')
->willReturn([$slot]);
$this->mockModel->method('getTranslatedEntity')
->willReturn([$slot, $slot]);
$this->leadModel->method('getEntity')
->with(123)
->willReturn($contact);
$this->mockDispatcher->method('hasListeners')->willReturn(true);
$this->mockDispatcher->expects($this->once())
->method('dispatch')
->withConsecutive(
[
$this->callback(
function (ContactFiltersEvaluateEvent $event) use ($contact, $slot) {
$this->assertSame($contact, $event->getContact());
$this->assertSame($slot->getFilters(), $event->getFilters());
// Match not found in any subscriber.
return true;
}
),
DynamicContentEvents::ON_CONTACTS_FILTER_EVALUATE,
]
);
Assert::assertSame(
'', // No content returned as the filter did not match anything.
$this->helper->getDynamicContentSlotForLead($slotName, $contact)
);
}
public function testGetDynamicContentSlotForLeadWithNoListenerWithMatchingFilter(): void
{
$slotName = 'test';
$contact = new Lead();
$contact->setFields(['email' => '[email protected]', 'id' => 123]);
$slot = new DynamicContent();
$slot->setName($slotName);
$slot->setIsCampaignBased(false);
$slot->setFilters([['field' => 'email', 'type' => 'email', 'operator' => '=', 'filter' => '[email protected]']]);
$slot->setContent('<p>test</p>');
$this->mockModel->method('getEntities')
->willReturn([$slot]);
$this->mockModel->method('getTranslatedEntity')
->willReturn([$slot, $slot]);
$this->leadModel->method('getEntity')
->with(123)
->willReturn($contact);
$this->mockDispatcher->method('hasListeners')->willReturn(false);
$this->mockDispatcher->expects($this->once())
->method('dispatch')
->withConsecutive(
[
$this->callback(
function (TokenReplacementEvent $event) use ($contact, $slot) {
$this->assertSame($contact, $event->getLead());
$this->assertSame($slot->getContent(), $event->getContent());
return true;
}
),
DynamicContentEvents::TOKEN_REPLACEMENT,
]
);
Assert::assertSame(
'<p>test</p>',
$this->helper->getDynamicContentSlotForLead($slotName, $contact)
);
}
public function testGetDynamicContentSlotForLeadWithNoListenerWithNotMatchingFilter(): void
{
$slotName = 'test';
$contact = new Lead();
$contact->setFields(['email' => '[email protected]', 'id' => 123]);
$slot = new DynamicContent();
$slot->setName($slotName);
$slot->setIsCampaignBased(false);
$slot->setFilters([['field' => 'email', 'type' => 'email', 'operator' => '=', 'filter' => '[email protected]']]);
$slot->setContent('<p>test</p>');
$this->mockModel->method('getEntities')
->willReturn([$slot]);
$this->mockModel->method('getTranslatedEntity')
->willReturn([$slot, $slot]);
$this->leadModel->method('getEntity')
->with(123)
->willReturn($contact);
$this->mockDispatcher->method('hasListeners')->willReturn(false);
$this->mockDispatcher->expects($this->never())->method('dispatch');
Assert::assertSame(
'',
$this->helper->getDynamicContentSlotForLead($slotName, $contact)
);
}
}
|