-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
MessageTest.php
209 lines (187 loc) · 7.98 KB
/
MessageTest.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
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
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Mailer;
use Cake\Mailer\Message;
use Cake\TestSuite\TestCase;
use TestApp\Mailer\TestMessage;
/**
* MessageTest class
*/
class MessageTest extends TestCase
{
/**
* @var \Cake\Mailer\Message
*/
protected $message;
public function setUp(): void
{
parent::setUp();
$this->message = new Message();
}
/**
* testWrap method
*
* @return void
*/
public function testWrap()
{
$renderer = new TestMessage();
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
$result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
$expected = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
'',
];
$this->assertSame($expected, $result);
$text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
$result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
$expected = [
'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
'amet.',
'',
];
$this->assertSame($expected, $result);
$text = '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula pellentesque accumsan amet.<hr></p>';
$result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
$expected = [
'<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
'pellentesque accumsan amet.<hr></p>',
'',
];
$this->assertSame($expected, $result);
$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac <a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
$result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
$expected = [
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
'<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
'nisi, vehicula pellentesque accumsan amet.',
'',
];
$this->assertSame($expected, $result);
$text = 'Lorem ipsum <a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">ok</a>';
$result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
$expected = [
'Lorem ipsum',
'<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
'ok</a>',
'',
];
$this->assertSame($expected, $result);
$text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
$result = $renderer->doWrap($text, Message::LINE_LENGTH_SHOULD);
$expected = [
'Lorem ipsum',
'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
'ok.',
'',
];
$this->assertSame($expected, $result);
}
/**
* testHeaders method
*
* @return void
*/
public function testHeaders()
{
$this->message->setMessageId(false);
$this->message->setHeaders(['X-Something' => 'nice']);
$expected = [
'X-Something' => 'nice',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '8bit',
];
$this->assertSame($expected, $this->message->getHeaders());
$this->message->addHeaders(['X-Something' => 'very nice', 'X-Other' => 'cool']);
$expected = [
'X-Something' => 'very nice',
'X-Other' => 'cool',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '8bit',
];
$this->assertSame($expected, $this->message->getHeaders());
$this->message->setFrom('cake@cakephp.org');
$this->assertSame($expected, $this->message->getHeaders());
$expected = [
'From' => 'cake@cakephp.org',
'X-Something' => 'very nice',
'X-Other' => 'cool',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '8bit',
];
$this->assertSame($expected, $this->message->getHeaders(['from' => true]));
$this->message->setFrom('cake@cakephp.org', 'CakePHP');
$expected['From'] = 'CakePHP <cake@cakephp.org>';
$this->assertSame($expected, $this->message->getHeaders(['from' => true]));
$this->message->setTo(['cake@cakephp.org', 'php@cakephp.org' => 'CakePHP']);
$expected = [
'From' => 'CakePHP <cake@cakephp.org>',
'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
'X-Something' => 'very nice',
'X-Other' => 'cool',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '8bit',
];
$this->assertSame($expected, $this->message->getHeaders(['from' => true, 'to' => true]));
$this->message->setCharset('ISO-2022-JP');
$expected = [
'From' => 'CakePHP <cake@cakephp.org>',
'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
'X-Something' => 'very nice',
'X-Other' => 'cool',
'Date' => date(DATE_RFC2822),
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=ISO-2022-JP',
'Content-Transfer-Encoding' => '7bit',
];
$this->assertSame($expected, $this->message->getHeaders(['from' => true, 'to' => true]));
$result = $this->message->setHeaders([]);
$this->assertInstanceOf(Message::class, $result);
$this->message->setHeaders(['o:tag' => ['foo']]);
$this->message->addHeaders(['o:tag' => ['bar']]);
$result = $this->message->getHeaders();
$this->assertEquals(['foo', 'bar'], $result['o:tag']);
}
/**
* testHeadersString method
*
* @return void
*/
public function testHeadersString()
{
$this->message->setMessageId(false);
$this->message->setHeaders(['X-Something' => 'nice']);
$expected = [
'X-Something: nice',
'Date: ' . date(DATE_RFC2822),
'MIME-Version: 1.0',
'Content-Type: text/plain; charset=UTF-8',
'Content-Transfer-Encoding: 8bit',
];
$this->assertSame(implode("\r\n", $expected), $this->message->getHeadersString());
}
}