This repository was archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathMessageTest.php
461 lines (384 loc) · 15 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Mail\Storage;
use Exception as GeneralException;
use PHPUnit\Framework\TestCase;
use Zend\Mail\Exception as MailException;
use Zend\Mail\Storage;
use Zend\Mail\Storage\Exception;
use Zend\Mail\Storage\Message;
use Zend\Mime;
use Zend\Mime\Exception as MimeException;
use Zend\Mail;
/**
* @group Zend_Mail
* @covers Zend\Mail\Storage\Message<extended>
*/
class MessageTest extends TestCase
{
protected $file;
protected $file2;
public function setUp()
{
$this->file = __DIR__ . '/../_files/mail.txt';
$this->file2 = __DIR__ . '/../_files/mail_multi_to.txt';
}
public function testInvalidFile()
{
$this->expectException(GeneralException::class);
new Message(['file' => '/this/file/does/not/exists']);
}
/**
* @dataProvider filesProvider
*/
public function testIsMultipart($params)
{
$message = new Message($params);
$this->assertTrue($message->isMultipart());
}
/**
* @dataProvider filesProvider
*/
public function testGetHeader($params)
{
$message = new Message($params);
$this->assertEquals($message->subject, 'multipart');
}
/**
* @dataProvider filesProvider
*/
public function testGetDecodedHeader($params)
{
$message = new Message($params);
}
/**
* @dataProvider filesProvider
*/
public function testGetHeaderAsArray($params)
{
$message = new Message($params);
$this->assertEquals(['multipart'], $message->getHeader('subject', 'array'), 'getHeader() value not match');
}
public function testGetFirstPart()
{
$message = new Message(['file' => $this->file]);
$this->assertEquals(substr($message->getPart(1)->getContent(), 0, 14), 'The first part');
}
public function testGetFirstPartTwice()
{
$message = new Message(['file' => $this->file]);
$message->getPart(1);
$this->assertEquals(substr($message->getPart(1)->getContent(), 0, 14), 'The first part');
}
public function testGetWrongPart()
{
$this->expectException(GeneralException::class);
$message = new Message(['file' => $this->file]);
$message->getPart(-1);
}
public function testNoHeaderMessage()
{
$message = new Message(['file' => __FILE__]);
$this->assertEquals(substr($message->getContent(), 0, 5), '<?php');
$raw = file_get_contents(__FILE__);
$raw = "\t" . $raw;
$message = new Message(['raw' => $raw]);
$this->assertEquals(substr($message->getContent(), 0, 6), "\t<?php");
}
public function testMultipleHeader()
{
$raw = file_get_contents($this->file);
$raw = "sUBject: test\r\nSubJect: test2\r\n" . $raw;
$message = new Message(['raw' => $raw]);
$this->assertEquals(
'test' . Mime\Mime::LINEEND . 'test2' . Mime\Mime::LINEEND . 'multipart',
$message->getHeader('subject', 'string')
);
$this->assertEquals(
['test', 'test2', 'multipart'],
$message->getHeader('subject', 'array')
);
}
public function testContentTypeDecode()
{
$message = new Message(['file' => $this->file]);
$this->assertEquals(
Mime\Decode::splitContentType($message->ContentType),
['type' => 'multipart/alternative', 'boundary' => 'crazy-multipart']
);
}
public function testSplitEmptyMessage()
{
$this->assertEquals(Mime\Decode::splitMessageStruct('', 'xxx'), null);
}
public function testSplitInvalidMessage()
{
$this->expectException(MimeException\ExceptionInterface::class);
Mime\Decode::splitMessageStruct("--xxx\n", 'xxx');
}
public function testInvalidMailHandler()
{
$this->expectException(Exception\InvalidArgumentException::class);
new Message(['handler' => 1]);
}
public function testMissingId()
{
$this->expectException(Exception\InvalidArgumentException::class);
$mail = new Storage\Mbox(['filename' => __DIR__ . '/../_files/test.mbox/INBOX']);
new Message(['handler' => $mail]);
}
public function testIterator()
{
$message = new Message(['file' => $this->file]);
foreach (new \RecursiveIteratorIterator($message) as $num => $part) {
if ($num == 1) {
// explicit call of __toString() needed for PHP < 5.2
$this->assertEquals(substr($part->__toString(), 0, 14), 'The first part');
}
}
$this->assertEquals($part->contentType, 'text/x-vertical');
}
public function testDecodeString()
{
}
public function testSplitHeader()
{
$header = 'foo; x=y; y="x"';
$this->assertEquals(Mime\Decode::splitHeaderField($header), ['foo', 'x' => 'y', 'y' => 'x']);
$this->assertEquals(Mime\Decode::splitHeaderField($header, 'x'), 'y');
$this->assertEquals(Mime\Decode::splitHeaderField($header, 'y'), 'x');
$this->assertEquals(Mime\Decode::splitHeaderField($header, 'foo', 'foo'), 'foo');
$this->assertEquals(Mime\Decode::splitHeaderField($header, 'foo'), null);
}
public function testSplitInvalidHeader()
{
$this->expectException(MimeException\ExceptionInterface::class);
$header = '';
Mime\Decode::splitHeaderField($header);
}
public function testSplitMessage()
{
$header = 'Test: test';
$body = 'body';
$newlines = ["\r\n", "\n\r", "\n", "\r"];
$decoded_body = null; // "Declare" variable before first "read" usage to avoid IDEs warning
$decoded_headers = null; // "Declare" variable before first "read" usage to avoid IDEs warning
foreach ($newlines as $contentEOL) {
foreach ($newlines as $decodeEOL) {
$content = $header . $contentEOL . $contentEOL . $body;
Mime\Decode::splitMessage($content, $decoded_headers, $decoded_body, $decodeEOL);
$this->assertEquals(['Test' => 'test'], $decoded_headers->toArray());
$this->assertEquals($body, $decoded_body);
}
}
}
public function testToplines()
{
$message = new Message(['headers' => file_get_contents($this->file)]);
$this->assertStringStartsWith('multipart message', $message->getToplines());
}
public function testNoContent()
{
$this->expectException(Exception\RuntimeException::class);
$message = new Message(['raw' => 'Subject: test']);
$message->getContent();
}
public function testEmptyHeader()
{
$message = new Message([]);
$this->assertEquals([], $message->getHeaders()->toArray());
$message = new Message([]);
$subject = null;
$this->expectException('Zend\\Mail\\Exception\\InvalidArgumentException');
$message->subject;
}
public function testWrongHeaderType()
{
// @codingStandardsIgnoreStart
$badMessage = unserialize(
"O:25:\"Zend\Mail\Storage\Message\":9:{s:8:\"\x00*\x00flags\";a:0:{}s:10:\"\x00*\x00headers\";s:16:\"Yellow submarine\";s:10:\"\x00*\x00content\";N;s:11:\"\x00*\x00topLines\";s:0:\"\";s:8:\"\x00*\x00parts\";a:0:{}s:13:\"\x00*\x00countParts\";N;s:15:\"\x00*\x00iterationPos\";i:1;s:7:\"\x00*\x00mail\";N;s:13:\"\x00*\x00messageNum\";i:0;}"
);
// @codingStandardsIgnoreEnd
$this->expectException(MailException\RuntimeException::class);
$badMessage->getHeaders();
}
public function testEmptyBody()
{
$message = new Message([]);
$part = null;
try {
$part = $message->getPart(1);
} catch (Exception\RuntimeException $e) {
// ok
}
if ($part) {
$this->fail('no exception raised while getting part from empty message');
}
$message = new Message([]);
$this->assertEquals(0, $message->countParts());
}
/**
* @group ZF-5209
*/
public function testCheckingHasHeaderFunctionality()
{
$message = new Message(['headers' => ['subject' => 'foo']]);
$this->assertTrue($message->getHeaders()->has('subject'));
$this->assertTrue(isset($message->subject));
$this->assertTrue($message->getHeaders()->has('SuBject'));
$this->assertTrue(isset($message->suBjeCt));
$this->assertFalse($message->getHeaders()->has('From'));
}
public function testWrongMultipart()
{
$this->expectException(Exception\RuntimeException::class);
$message = new Message(['raw' => "Content-Type: multipart/mixed\r\n\r\ncontent"]);
$message->getPart(1);
}
public function testLateFetch()
{
$mail = new Storage\Mbox(['filename' => __DIR__ . '/../_files/test.mbox/INBOX']);
$message = new Message(['handler' => $mail, 'id' => 5]);
$this->assertEquals($message->countParts(), 2);
$this->assertEquals($message->countParts(), 2);
$message = new Message(['handler' => $mail, 'id' => 5]);
$this->assertEquals($message->subject, 'multipart');
$message = new Message(['handler' => $mail, 'id' => 5]);
$this->assertStringStartsWith('multipart message', $message->getContent());
}
public function testManualIterator()
{
$message = new Message(['file' => $this->file]);
$this->assertTrue($message->valid());
$this->assertEquals($message->getChildren(), $message->current());
$this->assertEquals($message->key(), 1);
$message->next();
$this->assertTrue($message->valid());
$this->assertEquals($message->getChildren(), $message->current());
$this->assertEquals($message->key(), 2);
$message->next();
$this->assertFalse($message->valid());
$message->rewind();
$this->assertTrue($message->valid());
$this->assertEquals($message->getChildren(), $message->current());
$this->assertEquals($message->key(), 1);
}
public function testMessageFlagsAreSet()
{
$origFlags = [
'foo' => 'bar',
'baz' => 'bat'
];
$message = new Message(['flags' => $origFlags]);
$messageFlags = $message->getFlags();
$this->assertTrue($message->hasFlag('bar'), var_export($messageFlags, 1));
$this->assertTrue($message->hasFlag('bat'), var_export($messageFlags, 1));
$this->assertEquals(['bar' => 'bar', 'bat' => 'bat'], $messageFlags);
}
public function testGetHeaderFieldSingle()
{
$message = new Message(['file' => $this->file]);
$this->assertEquals($message->getHeaderField('subject'), 'multipart');
}
public function testGetHeaderFieldDefault()
{
$message = new Message(['file' => $this->file]);
$this->assertEquals($message->getHeaderField('content-type'), 'multipart/alternative');
}
public function testGetHeaderFieldNamed()
{
$message = new Message(['file' => $this->file]);
$this->assertEquals($message->getHeaderField('content-type', 'boundary'), 'crazy-multipart');
}
public function testGetHeaderFieldMissing()
{
$message = new Message(['file' => $this->file]);
$this->assertNull($message->getHeaderField('content-type', 'foo'));
}
public function testGetHeaderFieldInvalid()
{
$this->expectException(MailException\ExceptionInterface::class);
$message = new Message(['file' => $this->file]);
$message->getHeaderField('fake-header-name', 'foo');
}
public function testCaseInsensitiveMultipart()
{
$message = new Message(['raw' => "coNTent-TYpe: muLTIpaRT/x-empty\r\n\r\n"]);
$this->assertTrue($message->isMultipart());
}
public function testCaseInsensitiveField()
{
$header = 'test; fOO="this is a test"';
$this->assertEquals(Mime\Decode::splitHeaderField($header, 'Foo'), 'this is a test');
$this->assertEquals(Mime\Decode::splitHeaderField($header, 'bar'), null);
}
public function testSpaceInFieldName()
{
$header = 'test; foo =bar; baz =42';
$this->assertEquals(Mime\Decode::splitHeaderField($header, 'foo'), 'bar');
$this->assertEquals(Mime\Decode::splitHeaderField($header, 'baz'), 42);
}
/**
* @group ZF2-372
*/
public function testStrictParseMessage()
{
$this->expectException('Zend\\Mail\\Exception\\RuntimeException');
$raw = file_get_contents($this->file);
$message = new Message(['raw' => $raw, 'strict' => true]);
}
public function testMultivalueToHeader()
{
$message = new Message(['file' => $this->file2]);
/** @var \Zend\Mail\Header\To $header */
$header = $message->getHeader('to');
$addressList = $header->getAddressList();
$this->assertEquals(2, $addressList->count());
}
/**
* Test case for creating mail with Mail\Message containing \n\n,
* serializing it to text and loading with Storage\Message.
*
* Testcase showing that if mail is created with text body
* that is \n separated, loading such mail with Storage\Message
* causes parse error because it goes into mail body to find headers.
*/
public function testDogFood() {
$text = "Hello, bödi tekst\n\nBye";
$part = new Mime\Part($text);
$part->type = 'text/plain';
$part->charset = 'UTF-8';
$mime = new Mime\Message();
$mime->addPart($part);
$message = new Mail\Message();
$message->setEncoding('UTF-8');
$message->setBody($text);
$raw = $message->toString();
$this->assertContains($text, $raw, "original text is preserved");
$message = new Storage\Message(['raw' => $raw]);
$this->assertNotNull($message);
}
public function filesProvider()
{
$filePath = __DIR__ . '/../_files/mail.txt';
$fileBlankLineOnTop = __DIR__ . '/../_files/mail_blank_top_line.txt';
return [
// Description => [params]
'resource' => [['file' => fopen($filePath, 'r')]],
'file path' => [['file' => $filePath]],
'raw' => [['raw' => file_get_contents($filePath)]],
'file with blank line on top' => [['file' => $fileBlankLineOnTop]],
];
}
}