-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
TokenSubscriberTest.php
132 lines (114 loc) · 4.72 KB
/
TokenSubscriberTest.php
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
<?php
/*
* @copyright 2016 Mautic Contributors. All rights reserved
* @author Mautic, Inc.
*
* @link https://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Mautic\EmailBundle\Tests\EventListener;
use Mautic\CoreBundle\Factory\MauticFactory;
use Mautic\EmailBundle\Entity\Email;
use Mautic\EmailBundle\Event\EmailSendEvent;
use Mautic\EmailBundle\EventListener\TokenSubscriber;
use Mautic\EmailBundle\Helper\MailHelper;
use Mautic\LeadBundle\Entity\Lead;
use Mautic\LeadBundle\Helper\PrimaryCompanyHelper;
use Symfony\Component\EventDispatcher\EventDispatcher;
class TokenSubscriberTest extends \PHPUnit\Framework\TestCase
{
public function testDynamicContentCustomTokens()
{
$mockFactory = $this->getMockBuilder(MauticFactory::class)
->disableOriginalConstructor()
->getMock();
$swiftMailer = $this->getMockBuilder(\Swift_Mailer::class)
->disableOriginalConstructor()
->getMock();
$tokens = [
'{test}' => 'value',
];
$mailHelper = new MailHelper($mockFactory, $swiftMailer);
$mailHelper->setTokens($tokens);
$email = new Email();
$email->setCustomHtml(
<<<'CONTENT'
<html xmlns="http://www.w3.org/1999/xhtml">
<body style="margin: 0px; cursor: auto;" class="ui-sortable">
<div data-section-wrapper="1">
<center>
<table data-section="1" style="width: 600;" width="600" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>
<div data-slot-container="1" style="min-height: 30px">
<div data-slot="text"><br /><h2>Hello there!</h2><br />{test} test We haven't heard from you for a while...<a href="https://google.com">check this link</a><br /><br />{unsubscribe_text} | {webview_text}</div>{dynamiccontent="Dynamic Content 2"}<div data-slot="codemode">
<div id="codemodeHtmlContainer">
<p>Place your content here {test}</p></div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</center>
</div>
</body></html>
CONTENT
)
->setDynamicContent(
[
[
'tokenName' => 'Dynamic Content 1',
'content' => 'Default Dynamic Content',
'filters' => [
[
'content' => null,
'filters' => [
],
],
],
],
[
'tokenName' => 'Dynamic Content 2',
'content' => 'DEC {test}',
'filters' => [
],
],
]
);
$mailHelper->setEmail($email);
$lead = new Lead();
$lead->setEmail('hello@someone.com');
$mailHelper->setLead($lead);
$dispatcher = new EventDispatcher();
$primaryCompanyHelper = $this->createMock(PrimaryCompanyHelper::class);
$primaryCompanyHelper->method('getProfileFieldsWithPrimaryCompany')
->willReturn(['email' => 'hello@someone.com']);
/** @var TokenSubscriber $subscriber */
$subscriber = $this->getMockBuilder(TokenSubscriber::class)
->setConstructorArgs([$dispatcher, $primaryCompanyHelper])
->onlyMethods(null)
->getMock();
$dispatcher->addSubscriber($subscriber);
$event = new EmailSendEvent($mailHelper);
$subscriber->decodeTokens($event);
$eventTokens = $event->getTokens(false);
$this->assertEquals(
$eventTokens,
[
'{dynamiccontent="Dynamic Content 1"}' => 'Default Dynamic Content',
'{dynamiccontent="Dynamic Content 2"}' => 'DEC value',
]
);
$mailHelper->addTokens($eventTokens);
$mailerTokens = $mailHelper->getTokens();
$mailHelper->message->setBody($email->getCustomHtml());
MailHelper::searchReplaceTokens(array_keys($mailerTokens), $mailerTokens, $mailHelper->message);
$parsedBody = $mailHelper->message->getBody();
$this->assertNotFalse(strpos($parsedBody, 'DEC value'));
$this->assertNotFalse(strpos($parsedBody, 'value test We'));
$this->assertNotFalse(strpos($parsedBody, 'Place your content here value'));
}
}