|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Mailer\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Yiisoft\Mailer\Template; |
|
6
|
|
|
use Yiisoft\View\View; |
|
7
|
|
|
|
|
8
|
|
|
class TemplateTest extends TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
public function setUp(): void |
|
11
|
|
|
{ |
|
12
|
|
|
parent::setUp(); |
|
13
|
|
|
|
|
14
|
|
|
$filePath = $this->getTestFilePath(); |
|
15
|
|
|
if (!file_exists($filePath)) { |
|
16
|
|
|
mkdir($filePath, 0777, true); |
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @dataProvider setUpData |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testSetup(string $viewPath, $viewName, string $htmlLayout, string $textLayout): void |
|
24
|
|
|
{ |
|
25
|
|
|
$template = new Template($this->get(View::class), $viewPath, $viewName); |
|
26
|
|
|
$template->setHtmlLayout($htmlLayout); |
|
27
|
|
|
$template->setTextLayout($textLayout); |
|
28
|
|
|
$this->assertSame($viewPath, $this->getObjectPropertyValue($template, 'viewPath')); |
|
29
|
|
|
$this->assertSame($viewName, $this->getObjectPropertyValue($template, 'viewName')); |
|
30
|
|
|
$this->assertSame($textLayout, $this->getObjectPropertyValue($template, 'textLayout')); |
|
31
|
|
|
$this->assertSame($textLayout, $this->getObjectPropertyValue($template, 'textLayout')); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function setUpData(): array |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
['/tmp/foo', 'bar', '', ''], |
|
38
|
|
|
['/tmp/bar', 'baz', 'layouts/html', 'layouts/text'], |
|
39
|
|
|
['/tmp/bar', ['html' => 'html', 'text' => 'text'], 'layouts/html', 'layouts/text'], |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return Template |
|
45
|
|
|
*/ |
|
46
|
|
|
public function createTemplate(string $viewPath, $viewName): Template |
|
47
|
|
|
{ |
|
48
|
|
|
$template = new Template($this->get(View::class), $viewPath, $viewName); |
|
49
|
|
|
|
|
50
|
|
|
return $template; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testRender(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$viewPath = $this->getTestFilePath(); |
|
56
|
|
|
$viewName = 'test_view'; |
|
57
|
|
|
$template = $this->createTemplate($viewPath, $viewName); |
|
58
|
|
|
$viewFileName = $viewPath . DIRECTORY_SEPARATOR . $viewName . '.php'; |
|
59
|
|
|
$viewFileContent = '<?php echo $testParam; ?>'; |
|
60
|
|
|
$this->saveFile($viewFileName, $viewFileContent); |
|
61
|
|
|
|
|
62
|
|
|
$parameters = [ |
|
63
|
|
|
'testParam' => 'test output' |
|
64
|
|
|
]; |
|
65
|
|
|
$renderResult = $template->render($viewName, $parameters); |
|
66
|
|
|
$this->assertEquals($parameters['testParam'], $renderResult); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @depends testRender |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testRenderLayout(): void |
|
73
|
|
|
{ |
|
74
|
|
|
$viewPath = $this->getTestFilePath(); |
|
75
|
|
|
|
|
76
|
|
|
$viewName = 'test_view2'; |
|
77
|
|
|
$template = $this->createTemplate($viewPath, $viewName); |
|
78
|
|
|
$viewFileName = $viewPath . DIRECTORY_SEPARATOR . $viewName . '.php'; |
|
79
|
|
|
$viewFileContent = 'view file content'; |
|
80
|
|
|
$this->saveFile($viewFileName, $viewFileContent); |
|
81
|
|
|
|
|
82
|
|
|
$layoutName = 'test_layout'; |
|
83
|
|
|
$layoutFileName = $viewPath . DIRECTORY_SEPARATOR . $layoutName . '.php'; |
|
84
|
|
|
$layoutFileContent = 'Begin Layout <?php echo $content; ?> End Layout'; |
|
85
|
|
|
$this->saveFile($layoutFileName, $layoutFileContent); |
|
86
|
|
|
|
|
87
|
|
|
$renderResult = $template->render($viewName, [], $layoutName); |
|
88
|
|
|
$this->assertEquals('Begin Layout ' . $viewFileContent . ' End Layout', $renderResult); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @depends testRenderLayout |
|
93
|
|
|
*/ |
|
94
|
|
|
public function testCompose(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$viewPath = $this->getTestFilePath(); |
|
97
|
|
|
$htmlViewName = 'test_html_view'; |
|
98
|
|
|
$textViewName = 'test_text_view'; |
|
99
|
|
|
$viewName = [ |
|
100
|
|
|
'html' => $htmlViewName, |
|
101
|
|
|
'text' => $textViewName, |
|
102
|
|
|
]; |
|
103
|
|
|
$template = $this->createTemplate($viewPath, $viewName); |
|
104
|
|
|
|
|
105
|
|
|
$template->setHtmlLayout(''); |
|
106
|
|
|
$template->setTextLayout(''); |
|
107
|
|
|
|
|
108
|
|
|
$htmlViewFileName = $viewPath . DIRECTORY_SEPARATOR . $htmlViewName . '.php'; |
|
109
|
|
|
$htmlViewFileContent = 'HTML <b>view file</b> content'; |
|
110
|
|
|
$this->saveFile($htmlViewFileName, $htmlViewFileContent); |
|
111
|
|
|
|
|
112
|
|
|
$textViewFileName = $viewPath . DIRECTORY_SEPARATOR . $textViewName . '.php'; |
|
113
|
|
|
$textViewFileContent = 'Plain text view file content'; |
|
114
|
|
|
$this->saveFile($textViewFileName, $textViewFileContent); |
|
115
|
|
|
|
|
116
|
|
|
$message = $this->createMessage(); |
|
117
|
|
|
$template->compose($message); |
|
118
|
|
|
$this->assertEquals($htmlViewFileContent, $message->getHtmlBody(), 'Unable to render html!'); |
|
119
|
|
|
$this->assertEquals($textViewFileContent, $message->getTextBody(), 'Unable to render text!'); |
|
120
|
|
|
|
|
121
|
|
|
$message = $this->createMessage(); |
|
122
|
|
|
$template2 = $this->createTemplate($viewPath, $htmlViewName); |
|
123
|
|
|
$template2->compose($message); |
|
124
|
|
|
$this->assertEquals($htmlViewFileContent, $message->getHtmlBody(), 'Unable to render html by direct view!'); |
|
125
|
|
|
$this->assertEquals(strip_tags($htmlViewFileContent), $message->getTextBody(), 'Unable to render text by direct view!'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function htmlAndPlainProvider(): array |
|
129
|
|
|
{ |
|
130
|
|
|
return [ |
|
131
|
|
|
[ |
|
132
|
|
|
'HTML <b>view file</b> content <a href="http://yiifresh.com/index.php?r=site%2Freset-password&token=abcdef">http://yiifresh.com/index.php?r=site%2Freset-password&token=abcdef</a>', |
|
133
|
|
|
'HTML view file content http://yiifresh.com/index.php?r=site%2Freset-password&token=abcdef', |
|
134
|
|
|
], |
|
135
|
|
|
[ |
|
136
|
|
|
<<<HTML |
|
137
|
|
|
<html><head><style type="text/css">.content{color: #112345;}</style><title>TEST</title></head> |
|
138
|
|
|
<body> |
|
139
|
|
|
<style type="text/css">.content{color: #112345;}</style> |
|
140
|
|
|
<p> First paragraph |
|
141
|
|
|
second line |
|
142
|
|
|
|
|
143
|
|
|
<a href="http://yiifresh.com/index.php?r=site%2Freset-password&token=abcdef">http://yiifresh.com/index.php?r=site%2Freset-password&token=abcdef</a> |
|
144
|
|
|
|
|
145
|
|
|
</p><script type="text/javascript">alert("hi")</script> |
|
146
|
|
|
|
|
147
|
|
|
<p>Test Lorem ipsum...</p> |
|
148
|
|
|
</body> |
|
149
|
|
|
</html> |
|
150
|
|
|
HTML |
|
151
|
|
|
,<<<TEXT |
|
152
|
|
|
First paragraph |
|
153
|
|
|
second line |
|
154
|
|
|
|
|
155
|
|
|
http://yiifresh.com/index.php?r=site%2Freset-password&token=abcdef |
|
156
|
|
|
|
|
157
|
|
|
Test Lorem ipsum... |
|
158
|
|
|
TEXT |
|
159
|
|
|
], |
|
160
|
|
|
]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @dataProvider htmlAndPlainProvider |
|
165
|
|
|
* @depends testCompose |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $htmlViewFileContent |
|
168
|
|
|
* @param string $expectedTextRendering |
|
169
|
|
|
*/ |
|
170
|
|
|
public function testComposePlainTextFallback(string $htmlViewFileContent, string $expectedTextRendering): void |
|
171
|
|
|
{ |
|
172
|
|
|
$viewPath = $this->getTestFilePath(); |
|
173
|
|
|
$htmlViewName = 'test_html_view'; |
|
174
|
|
|
$template = $this->createTemplate($viewPath, $htmlViewName); |
|
175
|
|
|
|
|
176
|
|
|
$htmlViewFileName = $viewPath . DIRECTORY_SEPARATOR . $htmlViewName . '.php'; |
|
177
|
|
|
$this->saveFile($htmlViewFileName, $htmlViewFileContent); |
|
178
|
|
|
|
|
179
|
|
|
$message = $this->createMessage(); |
|
180
|
|
|
$template->compose($message); |
|
181
|
|
|
$this->assertEqualsWithoutLE($htmlViewFileContent, $message->getHtmlBody(), 'Unable to render html!'); |
|
182
|
|
|
$this->assertEqualsWithoutLE($expectedTextRendering, $message->getTextBody(), 'Unable to render text!'); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|