|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Azine\EmailBundle\Tests\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Azine\EmailBundle\Services\AzineEmailTwigExtension; |
|
6
|
|
|
|
|
7
|
|
|
class AzineEmailTwigExtensionTest extends \PHPUnit\Framework\TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
private $longText = 'Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.'; |
|
10
|
|
|
|
|
11
|
|
|
public function testFilters() |
|
12
|
|
|
{ |
|
13
|
|
|
$twigExtension = $this->getAzineEmailTwigExtensionWithMocks(); |
|
14
|
|
|
$filters = $twigExtension->getFilters(); |
|
15
|
|
|
$filterNames = array(); |
|
16
|
|
|
$this->assertSame(5, sizeof($filters), 'Unexpected number of Twig filters'); |
|
17
|
|
|
|
|
18
|
|
|
foreach ($filters as $filter) { |
|
19
|
|
|
/* @var $filter \Twig_SimpleFilter */ |
|
20
|
|
|
$this->assertTrue($filter instanceof \Twig_SimpleFilter, 'Twig_SimpleFilter expected as filter'); |
|
21
|
|
|
$filterNames[] = $filter->getName(); |
|
22
|
|
|
} |
|
23
|
|
|
$this->assertContains('textWrap', $filterNames, 'The filter textWrap should exist.'); |
|
24
|
|
|
$this->assertContains('urlEncodeText', $filterNames, 'The filter urlEncodeText should exist.'); |
|
25
|
|
|
$this->assertContains('addCampaignParamsForTemplate', $filterNames, 'The filter addCampaignParamsForTemplate should exist.'); |
|
26
|
|
|
|
|
27
|
|
|
$this->assertSame('azine_email_bundle_twig_extension', $twigExtension->getName()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testTextWrap() |
|
31
|
|
|
{ |
|
32
|
|
|
$twigExtension = $this->getAzineEmailTwigExtensionWithMocks(); |
|
33
|
|
|
$wrapped = $twigExtension->textWrap($this->longText); |
|
34
|
|
|
$nlIndex = strpos($wrapped, "\n"); |
|
35
|
|
|
$this->assertLessThanOrEqual(75, $nlIndex); |
|
36
|
|
|
$this->assertGreaterThan(65, $nlIndex); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testTextWrap60() |
|
40
|
|
|
{ |
|
41
|
|
|
$twigExtension = $this->getAzineEmailTwigExtensionWithMocks(); |
|
42
|
|
|
$wrapped = $twigExtension->textWrap($this->longText, 60); |
|
43
|
|
|
$nlIndex = strpos($wrapped, "\n"); |
|
44
|
|
|
$this->assertLessThanOrEqual(60, $nlIndex); |
|
45
|
|
|
$this->assertGreaterThan(55, $nlIndex); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testTextWrap100() |
|
49
|
|
|
{ |
|
50
|
|
|
$twigExtension = $this->getAzineEmailTwigExtensionWithMocks(); |
|
51
|
|
|
$wrapped = $twigExtension->textWrap($this->longText, 100); |
|
52
|
|
|
$nlIndex = strpos($wrapped, "\n"); |
|
53
|
|
|
$this->assertLessThanOrEqual(100, $nlIndex); |
|
54
|
|
|
$this->assertGreaterThan(90, $nlIndex); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testUrlEncodeText() |
|
58
|
|
|
{ |
|
59
|
|
|
$twigExtension = $this->getAzineEmailTwigExtensionWithMocks(); |
|
60
|
|
|
|
|
61
|
|
|
$percent = '%'; |
|
62
|
|
|
$amp = '&'; |
|
63
|
|
|
$backslash = '\\'; |
|
64
|
|
|
$lineBreak = ' |
|
65
|
|
|
'; |
|
66
|
|
|
|
|
67
|
|
|
$textWithSpecialChars = "blabla $percent $amp $backslash $lineBreak blabla $percent $amp $backslash $lineBreak "; |
|
68
|
|
|
|
|
69
|
|
|
$textUrlEncoded = $twigExtension->urlEncodeText($textWithSpecialChars); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertFalse(strpos($textUrlEncoded, $amp)); |
|
72
|
|
|
$this->assertFalse(strpos($textUrlEncoded, $backslash)); |
|
73
|
|
|
$this->assertFalse(strpos($textUrlEncoded, $lineBreak)); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertStringCount('%0D%0A', $textUrlEncoded, 2); |
|
76
|
|
|
$this->assertStringCount('%20', $textUrlEncoded, 10); |
|
77
|
|
|
$this->assertStringCount('%26', $textUrlEncoded, 2); |
|
78
|
|
|
$this->assertStringCount('%5C', $textUrlEncoded, 2); |
|
79
|
|
|
$this->assertStringCount('%25', $textUrlEncoded, 2); |
|
80
|
|
|
$this->assertStringCount('%', $textUrlEncoded, 20); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function testStripAndConvertTags() |
|
84
|
|
|
{ |
|
85
|
|
|
$html = "Some text<a href=\"http://acme.com/link1\"><div><img src=\"http://acme.com/img.jpg\">link text1</div></a> |
|
86
|
|
|
Some more text |
|
87
|
|
|
<a href='http://acme.com/link2'> |
|
88
|
|
|
<div> |
|
89
|
|
|
<img src='http://acme.com/img.jpg'> |
|
90
|
|
|
link text2 |
|
91
|
|
|
</div> |
|
92
|
|
|
</a> |
|
93
|
|
|
Even some more text |
|
94
|
|
|
<a href=\"http://acme.com/link3\">acme.com</a> or <a href=\"http://acme.com/link4\">here</a> |
|
95
|
|
|
End of text |
|
96
|
|
|
"; |
|
97
|
|
|
|
|
98
|
|
|
$twigExtension = $this->getAzineEmailTwigExtensionWithMocks(); |
|
99
|
|
|
$txt = $twigExtension->stripAndConvertTags($html); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertContains('link text1: http://acme.com/link1', $txt, "Link with html as link-text didn't work as expected."); |
|
102
|
|
|
$this->assertContains('link text2: http://acme.com/link2', $txt, "Link with html and linebreaks as link-text didn't work as expected."); |
|
103
|
|
|
$this->assertContains('http://acme.com/link3', $txt, "Link with url as link-text didn't work as expected."); |
|
104
|
|
|
$this->assertNotContains('link text3: http://acme.com/link3', $txt, "Link with url as link-text didn't work as expected."); |
|
105
|
|
|
$this->assertContains('here: http://acme.com/link4', $txt, "Link with url as link-text didn't work as expected."); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return AzineEmailTwigExtension |
|
110
|
|
|
*/ |
|
111
|
|
|
private function getAzineEmailTwigExtensionWithMocks() |
|
112
|
|
|
{ |
|
113
|
|
|
$templateProvider = $this->getMockBuilder("Azine\EmailBundle\Services\TemplateProviderInterface")->disableOriginalConstructor()->getMock(); |
|
114
|
|
|
$translator = $this->getMockBuilder("Symfony\Bundle\FrameworkBundle\Translation\Translator")->disableOriginalConstructor()->getMock(); |
|
115
|
|
|
|
|
116
|
|
|
return new AzineEmailTwigExtension($templateProvider, $translator); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $needle |
|
121
|
|
|
* @param int $expectedCount |
|
122
|
|
|
*/ |
|
123
|
|
|
private function assertStringCount($needle, $haystack, $expectedCount) |
|
124
|
|
|
{ |
|
125
|
|
|
$count = 0; |
|
126
|
|
|
str_replace($needle, '--', $haystack, $count); |
|
127
|
|
|
$this->assertSame($expectedCount, $count, "Found $needle $count times instead of $expectedCount"); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|