-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
MessageTest.php
991 lines (851 loc) · 35.6 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
<?php
namespace LaminasTest\Mail;
use Laminas\Mail\Address;
use Laminas\Mail\AddressList;
use Laminas\Mail\Exception;
use Laminas\Mail\Header;
use Laminas\Mail\Header\ContentType;
use Laminas\Mail\Header\GenericHeader;
use Laminas\Mail\Headers;
use Laminas\Mail\Message;
use Laminas\Mime\Message as MimeMessage;
use Laminas\Mime\Mime;
use Laminas\Mime\Part as MimePart;
use PHPUnit\Framework\TestCase;
use stdClass;
use function count;
use function date;
use function file_get_contents;
use function implode;
use function substr;
/**
* @group Laminas_Mail
* @covers Laminas\Mail\Message<extended>
*/
class MessageTest extends TestCase
{
/** @var Message */
public $message;
public function setUp(): void
{
$this->message = new Message();
}
public function testInvalidByDefault(): void
{
$this->assertFalse($this->message->isValid());
}
public function testSetsOrigDateHeaderByDefault(): void
{
$headers = $this->message->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
$this->assertTrue($headers->has('date'));
$header = $headers->get('date');
$date = date('r');
$date = substr($date, 0, 16);
$test = $header->getFieldValue();
$test = substr($test, 0, 16);
$this->assertEquals($date, $test);
}
public function testAddingFromAddressMarksAsValid(): void
{
$this->message->addFrom('[email protected]');
$this->assertTrue($this->message->isValid());
}
public function testHeadersMethodReturnsHeadersObject(): void
{
$headers = $this->message->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
}
public function testToMethodReturnsAddressListObject(): void
{
$this->message->addTo('[email protected]');
$to = $this->message->getTo();
$this->assertInstanceOf(AddressList::class, $to);
}
public function testToAddressListLivesInHeaders(): void
{
$this->message->addTo('[email protected]');
$to = $this->message->getTo();
$headers = $this->message->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
$this->assertTrue($headers->has('to'));
$header = $headers->get('to');
$this->assertSame($header->getAddressList(), $to);
}
public function testFromMethodReturnsAddressListObject(): void
{
$this->message->addFrom('[email protected]');
$from = $this->message->getFrom();
$this->assertInstanceOf(AddressList::class, $from);
}
public function testFromAddressListLivesInHeaders(): void
{
$this->message->addFrom('[email protected]');
$from = $this->message->getFrom();
$headers = $this->message->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
$this->assertTrue($headers->has('from'));
$header = $headers->get('from');
$this->assertSame($header->getAddressList(), $from);
}
public function testCcMethodReturnsAddressListObject(): void
{
$this->message->addCc('[email protected]');
$cc = $this->message->getCc();
$this->assertInstanceOf(AddressList::class, $cc);
}
public function testCcAddressListLivesInHeaders(): void
{
$this->message->addCc('[email protected]');
$cc = $this->message->getCc();
$headers = $this->message->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
$this->assertTrue($headers->has('cc'));
$header = $headers->get('cc');
$this->assertSame($header->getAddressList(), $cc);
}
public function testBccMethodReturnsAddressListObject(): void
{
$this->message->addBcc('[email protected]');
$bcc = $this->message->getBcc();
$this->assertInstanceOf(AddressList::class, $bcc);
}
public function testBccAddressListLivesInHeaders(): void
{
$this->message->addBcc('[email protected]');
$bcc = $this->message->getBcc();
$headers = $this->message->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
$this->assertTrue($headers->has('bcc'));
$header = $headers->get('bcc');
$this->assertSame($header->getAddressList(), $bcc);
}
public function testReplyToMethodReturnsAddressListObject(): void
{
$this->message->addReplyTo('[email protected]');
$replyTo = $this->message->getReplyTo();
$this->assertInstanceOf(AddressList::class, $replyTo);
}
public function testReplyToAddressListLivesInHeaders(): void
{
$this->message->addReplyTo('[email protected]');
$replyTo = $this->message->getReplyTo();
$headers = $this->message->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
$this->assertTrue($headers->has('reply-to'));
$header = $headers->get('reply-to');
$this->assertSame($header->getAddressList(), $replyTo);
}
public function testSenderIsNullByDefault(): void
{
$this->assertNull($this->message->getSender());
}
public function testNullSenderDoesNotCreateHeader(): void
{
$sender = $this->message->getSender();
$headers = $this->message->getHeaders();
$this->assertFalse($headers->has('sender'));
}
public function testSettingSenderCreatesAddressObject(): void
{
$this->message->setSender('[email protected]');
$sender = $this->message->getSender();
$this->assertInstanceOf(Address::class, $sender);
}
public function testCanSpecifyNameWhenSettingSender(): void
{
$this->message->setSender('[email protected]', 'Example Test');
$sender = $this->message->getSender();
$this->assertInstanceOf(Address::class, $sender);
$this->assertEquals('Example Test', $sender->getName());
}
public function testCanProvideAddressObjectWhenSettingSender(): void
{
$sender = new Address('[email protected]');
$this->message->setSender($sender);
$test = $this->message->getSender();
$this->assertSame($sender, $test);
}
public function testSenderAccessorsProxyToSenderHeader(): void
{
$header = new Header\Sender();
$this->message->getHeaders()->addHeader($header);
$address = new Address('[email protected]', 'Example Test');
$this->message->setSender($address);
$this->assertSame($address, $header->getAddress());
}
public function testCanAddFromAddressUsingName(): void
{
$this->message->addFrom('[email protected]', 'Example Test');
$addresses = $this->message->getFrom();
$this->assertEquals(1, count($addresses));
$address = $addresses->current();
$this->assertEquals('[email protected]', $address->getEmail());
$this->assertEquals('Example Test', $address->getName());
}
public function testCanAddFromAddressUsingEmailAndNameAsString(): void
{
$this->message->addFrom('Example Test <[email protected]>');
$addresses = $this->message->getFrom();
$this->assertEquals(1, count($addresses));
$address = $addresses->current();
$this->assertEquals('[email protected]', $address->getEmail());
$this->assertEquals('Example Test', $address->getName());
}
public function testCanAddFromAddressUsingAddressObject(): void
{
$address = new Address('[email protected]', 'Example Test');
$this->message->addFrom($address);
$addresses = $this->message->getFrom();
$this->assertEquals(1, count($addresses));
$test = $addresses->current();
$this->assertSame($address, $test);
}
public function testCanAddManyFromAddressesUsingArray(): void
{
$addresses = [
'[email protected]' => 'Laminas Contributors List',
new Address('[email protected]', 'Laminas Announce List'),
];
$this->message->addFrom($addresses);
$from = $this->message->getFrom();
$this->assertEquals(3, count($from));
$this->assertTrue($from->has('[email protected]'));
$this->assertTrue($from->has('[email protected]'));
$this->assertTrue($from->has('[email protected]'));
}
public function testCanAddManyFromAddressesUsingAddressListObject(): void
{
$list = new AddressList();
$list->add('[email protected]');
$this->message->addFrom('[email protected]');
$this->message->addFrom($list);
$from = $this->message->getFrom();
$this->assertEquals(2, count($from));
$this->assertTrue($from->has('[email protected]'));
$this->assertTrue($from->has('[email protected]'));
}
public function testCanSetFromListFromAddressList(): void
{
$list = new AddressList();
$list->add('[email protected]');
$this->message->addFrom('[email protected]');
$this->message->setFrom($list);
$from = $this->message->getFrom();
$this->assertEquals(1, count($from));
$this->assertFalse($from->has('[email protected]'));
$this->assertTrue($from->has('[email protected]'));
}
public function testCanAddCcAddressUsingName(): void
{
$this->message->addCc('[email protected]', 'Example Test');
$addresses = $this->message->getCc();
$this->assertEquals(1, count($addresses));
$address = $addresses->current();
$this->assertEquals('[email protected]', $address->getEmail());
$this->assertEquals('Example Test', $address->getName());
}
public function testCanAddCcAddressUsingAddressObject(): void
{
$address = new Address('[email protected]', 'Example Test');
$this->message->addCc($address);
$addresses = $this->message->getCc();
$this->assertEquals(1, count($addresses));
$test = $addresses->current();
$this->assertSame($address, $test);
}
public function testCanAddManyCcAddressesUsingArray(): void
{
$addresses = [
'[email protected]' => 'Laminas Contributors List',
new Address('[email protected]', 'Laminas Announce List'),
];
$this->message->addCc($addresses);
$cc = $this->message->getCc();
$this->assertEquals(3, count($cc));
$this->assertTrue($cc->has('[email protected]'));
$this->assertTrue($cc->has('[email protected]'));
$this->assertTrue($cc->has('[email protected]'));
}
public function testCanAddManyCcAddressesUsingAddressListObject(): void
{
$list = new AddressList();
$list->add('[email protected]');
$this->message->addCc('[email protected]');
$this->message->addCc($list);
$cc = $this->message->getCc();
$this->assertEquals(2, count($cc));
$this->assertTrue($cc->has('[email protected]'));
$this->assertTrue($cc->has('[email protected]'));
}
public function testCanSetCcListFromAddressList(): void
{
$list = new AddressList();
$list->add('[email protected]');
$this->message->addCc('[email protected]');
$this->message->setCc($list);
$cc = $this->message->getCc();
$this->assertEquals(1, count($cc));
$this->assertFalse($cc->has('[email protected]'));
$this->assertTrue($cc->has('[email protected]'));
}
public function testCanAddBccAddressUsingName(): void
{
$this->message->addBcc('[email protected]', 'Example Test');
$addresses = $this->message->getBcc();
$this->assertEquals(1, count($addresses));
$address = $addresses->current();
$this->assertEquals('[email protected]', $address->getEmail());
$this->assertEquals('Example Test', $address->getName());
}
public function testCanAddBccAddressUsingAddressObject(): void
{
$address = new Address('[email protected]', 'Example Test');
$this->message->addBcc($address);
$addresses = $this->message->getBcc();
$this->assertEquals(1, count($addresses));
$test = $addresses->current();
$this->assertSame($address, $test);
}
public function testCanAddManyBccAddressesUsingArray(): void
{
$addresses = [
'[email protected]' => 'Laminas Contributors List',
new Address('[email protected]', 'Laminas Announce List'),
];
$this->message->addBcc($addresses);
$bcc = $this->message->getBcc();
$this->assertEquals(3, count($bcc));
$this->assertTrue($bcc->has('[email protected]'));
$this->assertTrue($bcc->has('[email protected]'));
$this->assertTrue($bcc->has('[email protected]'));
}
public function testCanAddManyBccAddressesUsingAddressListObject(): void
{
$list = new AddressList();
$list->add('[email protected]');
$this->message->addBcc('[email protected]');
$this->message->addBcc($list);
$bcc = $this->message->getBcc();
$this->assertEquals(2, count($bcc));
$this->assertTrue($bcc->has('[email protected]'));
$this->assertTrue($bcc->has('[email protected]'));
}
public function testCanSetBccListFromAddressList(): void
{
$list = new AddressList();
$list->add('[email protected]');
$this->message->addBcc('[email protected]');
$this->message->setBcc($list);
$bcc = $this->message->getBcc();
$this->assertEquals(1, count($bcc));
$this->assertFalse($bcc->has('[email protected]'));
$this->assertTrue($bcc->has('[email protected]'));
}
public function testCanAddReplyToAddressUsingName(): void
{
$this->message->addReplyTo('[email protected]', 'Example Test');
$addresses = $this->message->getReplyTo();
$this->assertEquals(1, count($addresses));
$address = $addresses->current();
$this->assertEquals('[email protected]', $address->getEmail());
$this->assertEquals('Example Test', $address->getName());
}
public function testCanAddReplyToAddressUsingAddressObject(): void
{
$address = new Address('[email protected]', 'Example Test');
$this->message->addReplyTo($address);
$addresses = $this->message->getReplyTo();
$this->assertEquals(1, count($addresses));
$test = $addresses->current();
$this->assertSame($address, $test);
}
public function testCanAddManyReplyToAddressesUsingArray(): void
{
$addresses = [
'[email protected]' => 'Laminas Contributors List',
new Address('[email protected]', 'Laminas Announce List'),
];
$this->message->addReplyTo($addresses);
$replyTo = $this->message->getReplyTo();
$this->assertEquals(3, count($replyTo));
$this->assertTrue($replyTo->has('[email protected]'));
$this->assertTrue($replyTo->has('[email protected]'));
$this->assertTrue($replyTo->has('[email protected]'));
}
public function testCanAddManyReplyToAddressesUsingAddressListObject(): void
{
$list = new AddressList();
$list->add('[email protected]');
$this->message->addReplyTo('[email protected]');
$this->message->addReplyTo($list);
$replyTo = $this->message->getReplyTo();
$this->assertEquals(2, count($replyTo));
$this->assertTrue($replyTo->has('[email protected]'));
$this->assertTrue($replyTo->has('[email protected]'));
}
public function testCanSetReplyToListFromAddressList(): void
{
$list = new AddressList();
$list->add('[email protected]');
$this->message->addReplyTo('[email protected]');
$this->message->setReplyTo($list);
$replyTo = $this->message->getReplyTo();
$this->assertEquals(1, count($replyTo));
$this->assertFalse($replyTo->has('[email protected]'));
$this->assertTrue($replyTo->has('[email protected]'));
}
public function testSubjectIsEmptyByDefault(): void
{
$this->assertNull($this->message->getSubject());
}
public function testSubjectIsMutable(): void
{
$this->message->setSubject('test subject');
$subject = $this->message->getSubject();
$this->assertEquals('test subject', $subject);
}
public function testSubjectIsMutableReplaceExisting(): void
{
$this->message->setSubject('test subject');
$this->message->setSubject('new subject');
$this->assertSame('new subject', $this->message->getSubject());
}
public function testSettingSubjectProxiesToHeader(): void
{
$this->message->setSubject('test subject');
$headers = $this->message->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
$this->assertTrue($headers->has('subject'));
$header = $headers->get('subject');
$this->assertEquals('test subject', $header->getFieldValue());
}
public function testBodyIsEmptyByDefault(): void
{
$this->assertNull($this->message->getBody());
}
public function testMaySetBodyFromString(): void
{
$this->message->setBody('body');
$this->assertEquals('body', $this->message->getBody());
}
public function testMaySetBodyFromStringSerializableObject(): void
{
$object = new TestAsset\StringSerializableObject('body');
$this->message->setBody($object);
$this->assertSame($object, $this->message->getBody());
$this->assertEquals('body', $this->message->getBodyText());
}
public function testMaySetBodyFromMimeMessage(): void
{
$body = new MimeMessage();
$this->message->setBody($body);
$this->assertSame($body, $this->message->getBody());
}
public function testMaySetNullBody(): void
{
$this->message->setBody(null);
$this->assertNull($this->message->getBody());
}
public static function invalidBodyValues(): array
{
return [
[['foo']],
[true],
[false],
[new stdClass()],
];
}
/**
* @dataProvider invalidBodyValues
*/
public function testSettingNonScalarNonMimeNonStringSerializableValueForBodyRaisesException(mixed $body): void
{
$this->expectException(Exception\InvalidArgumentException::class);
$this->message->setBody($body);
}
public function testSettingBodyFromSinglePartMimeMessageSetsAppropriateHeaders(): void
{
$mime = new Mime('foo-bar');
$part = new MimePart('<b>foo</b>');
$part->type = 'text/html';
$body = new MimeMessage();
$body->setMime($mime);
$body->addPart($part);
$this->message->setBody($body);
$headers = $this->message->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
$this->assertTrue($headers->has('mime-version'));
$header = $headers->get('mime-version');
$this->assertEquals('1.0', $header->getFieldValue());
$this->assertTrue($headers->has('content-type'));
$header = $headers->get('content-type');
$this->assertEquals('text/html', $header->getFieldValue());
}
public function testSettingUtf8MailBodyFromSinglePartMimeUtf8MessageSetsAppropriateHeaders(): void
{
$mime = new Mime('foo-bar');
$part = new MimePart('UTF-8 TestString: AaÜüÄäÖöß');
$part->type = Mime::TYPE_TEXT;
$part->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
$part->charset = 'utf-8';
$body = new MimeMessage();
$body->setMime($mime);
$body->addPart($part);
$this->message->setEncoding('UTF-8');
$this->message->setBody($body);
$this->assertStringContainsString(
'Content-Type: text/plain;' . Headers::FOLDING . 'charset="utf-8"' . Headers::EOL
. 'Content-Transfer-Encoding: quoted-printable' . Headers::EOL,
$this->message->getHeaders()->toString()
);
}
public function testSettingBodyFromMultiPartMimeMessageSetsAppropriateHeaders(): void
{
$mime = new Mime('foo-bar');
$text = new MimePart('foo');
$text->type = 'text/plain';
$html = new MimePart('<b>foo</b>');
$html->type = 'text/html';
$body = new MimeMessage();
$body->setMime($mime);
$body->addPart($text);
$body->addPart($html);
$this->message->setBody($body);
$headers = $this->message->getHeaders();
$this->assertInstanceOf(Headers::class, $headers);
$this->assertTrue($headers->has('mime-version'));
$header = $headers->get('mime-version');
$this->assertEquals('1.0', $header->getFieldValue());
$this->assertTrue($headers->has('content-type'));
$header = $headers->get('content-type');
$this->assertEquals("multipart/mixed;\r\n boundary=\"foo-bar\"", $header->getFieldValue());
}
public function testRetrievingBodyTextFromMessageWithMultiPartMimeBodyReturnsMimeSerialization(): void
{
$mime = new Mime('foo-bar');
$text = new MimePart('foo');
$text->type = 'text/plain';
$html = new MimePart('<b>foo</b>');
$html->type = 'text/html';
$body = new MimeMessage();
$body->setMime($mime);
$body->addPart($text);
$body->addPart($html);
$this->message->setBody($body);
$text = $this->message->getBodyText();
$this->assertEquals($body->generateMessage(Headers::EOL), $text);
$this->assertStringContainsString('--foo-bar', $text);
$this->assertStringContainsString('--foo-bar--', $text);
$this->assertStringContainsString('Content-Type: text/plain', $text);
$this->assertStringContainsString('Content-Type: text/html', $text);
}
public function testEncodingIsAsciiByDefault(): void
{
$this->assertEquals('ASCII', $this->message->getEncoding());
}
public function testEncodingIsMutable(): void
{
$this->message->setEncoding('UTF-8');
$this->assertEquals('UTF-8', $this->message->getEncoding());
}
public function testMessageReturnsNonEncodedSubject(): void
{
$this->message->setSubject('This is a subject');
$this->message->setEncoding('UTF-8');
$this->assertEquals('This is a subject', $this->message->getSubject());
}
public function testSettingNonAsciiEncodingForcesMimeEncodingOfSomeHeaders(): void
{
$this->message->addTo('[email protected]', 'Laminas DevTeam');
$this->message->addFrom('[email protected]', "Matthew Weier O'Phinney");
$this->message->addCc('[email protected]', 'Laminas Contributors List');
$this->message->addBcc('[email protected]', 'Laminas CR Team');
$this->message->setSubject('This is a subject');
$this->message->setEncoding('UTF-8');
$test = $this->message->getHeaders()->toString();
$expected = '=?UTF-8?Q?Laminas=20DevTeam?=';
$this->assertStringContainsString($expected, $test);
$this->assertStringContainsString('<[email protected]>', $test);
$expected = "=?UTF-8?Q?Matthew=20Weier=20O'Phinney?=";
$this->assertStringContainsString($expected, $test, $test);
$this->assertStringContainsString('<[email protected]>', $test);
$expected = '=?UTF-8?Q?Laminas=20Contributors=20List?=';
$this->assertStringContainsString($expected, $test);
$this->assertStringContainsString('<[email protected]>', $test);
$expected = '=?UTF-8?Q?Laminas=20CR=20Team?=';
$this->assertStringContainsString($expected, $test);
$this->assertStringContainsString('<[email protected]>', $test);
$expected = 'Subject: =?UTF-8?Q?This=20is=20a=20subject?=';
$this->assertStringContainsString($expected, $test);
}
/**
* @see https://zendframework.com/issues/browse/ZF2-507
*/
public function testDefaultDateHeaderEncodingIsAlwaysAscii(): void
{
$this->message->setEncoding('utf-8');
$headers = $this->message->getHeaders();
$header = $headers->get('date');
$date = date('r');
$date = substr($date, 0, 16);
$test = $header->getFieldValue();
$test = substr($test, 0, 16);
$this->assertEquals($date, $test);
}
public function testRestoreFromSerializedString(): void
{
$this->message->addTo('[email protected]', 'Example Test');
$this->message->addFrom('[email protected]', "Matthew Weier O'Phinney");
$this->message->addCc('[email protected]', 'Laminas Contributors List');
$this->message->setSubject('This is a subject');
$this->message->setBody('foo');
$serialized = $this->message->toString();
$restoredMessage = Message::fromString($serialized);
$this->assertEquals($serialized, $restoredMessage->toString());
}
/**
* @group 45
*/
public function testCanRestoreFromSerializedStringWhenBodyContainsMultipleNewlines(): void
{
$this->message->addTo('[email protected]', 'Example Test');
$this->message->addFrom('[email protected]', "Matthew Weier O'Phinney");
$this->message->addCc('[email protected]', 'Laminas Contributors List');
$this->message->setSubject('This is a subject');
$this->message->setBody("foo\n\ntest");
$serialized = $this->message->toString();
$restoredMessage = Message::fromString($serialized);
$this->assertEquals($serialized, $restoredMessage->toString());
}
/**
* @see https://zendframework.com/issues/browse/ZF-5962
*/
public function testPassEmptyArrayIntoSetPartsOfMimeMessageShouldReturnEmptyBodyString(): void
{
$mimeMessage = new MimeMessage();
$mimeMessage->setParts([]);
$this->message->setBody($mimeMessage);
$this->assertEquals('', $this->message->getBodyText());
}
public function messageRecipients(): array
{
return [
'setFrom' => ['setFrom'],
'addFrom' => ['addFrom'],
'setTo' => ['setTo'],
'addTo' => ['addTo'],
'setCc' => ['setCc'],
'addCc' => ['addCc'],
'setBcc' => ['setBcc'],
'addBcc' => ['addBcc'],
'setReplyTo' => ['setReplyTo'],
'setSender' => ['setSender'],
];
}
/**
* @group ZF2015-04
* @dataProvider messageRecipients
*/
public function testExceptionWhenAttemptingToSerializeMessageWithCRLFInjectionViaHeader(
string $recipientMethod
): void {
$subject = [
'test1',
'Content-Type: text/html; charset = "iso-8859-1"',
'',
'<html><body><iframe src="http://example.com/"></iframe></body></html> <!--',
];
$this->expectException(Exception\InvalidArgumentException::class);
$this->message->{$recipientMethod}(implode(Headers::EOL, $subject));
}
/**
* @group ZF2015-04
*/
public function testDetectsCRLFInjectionViaSubject(): void
{
$subject = [
'test1',
'Content-Type: text/html; charset = "iso-8859-1"',
'',
'<html><body><iframe src="http://example.com/"></iframe></body></html> <!--',
];
$this->message->setSubject(implode(Headers::EOL, $subject));
$serializedHeaders = $this->message->getHeaders()->toString();
$this->assertStringContainsString('example', $serializedHeaders);
$this->assertStringNotContainsString("\r\n<html>", $serializedHeaders);
}
public function testHeaderUnfoldingWorksAsExpectedForMultipartMessages(): void
{
$text = new MimePart('Test content');
$text->type = Mime::TYPE_TEXT;
$text->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
$text->disposition = Mime::DISPOSITION_INLINE;
$text->charset = 'UTF-8';
$html = new MimePart('<b>Test content</b>');
$html->type = Mime::TYPE_HTML;
$html->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
$html->disposition = Mime::DISPOSITION_INLINE;
$html->charset = 'UTF-8';
$multipartContent = new MimeMessage();
$multipartContent->addPart($text);
$multipartContent->addPart($html);
$multipartPart = new MimePart($multipartContent->generateMessage());
$multipartPart->charset = 'UTF-8';
$multipartPart->type = 'multipart/alternative';
$multipartPart->boundary = $multipartContent->getMime()->boundary();
$message = new MimeMessage();
$message->addPart($multipartPart);
$this->message->getHeaders()->addHeaderLine('Content-Transfer-Encoding', Mime::ENCODING_QUOTEDPRINTABLE);
$this->message->setBody($message);
$contentType = $this->message->getHeaders()->get('Content-Type');
$this->assertInstanceOf(ContentType::class, $contentType);
$this->assertStringContainsString('multipart/alternative', $contentType->getFieldValue());
$this->assertStringContainsString($multipartContent->getMime()->boundary(), $contentType->getFieldValue());
}
/**
* @group 19
*/
public function testCanParseMultipartReport(): void
{
$raw = file_get_contents(__DIR__ . '/_files/laminas-mail-19.eml');
$message = Message::fromString($raw);
$this->assertInstanceOf(Message::class, $message);
$this->assertIsString($message->getBody());
$headers = $message->getHeaders();
$this->assertCount(8, $headers);
$this->assertTrue($headers->has('Date'));
$this->assertTrue($headers->has('From'));
$this->assertTrue($headers->has('Message-Id'));
$this->assertTrue($headers->has('To'));
$this->assertTrue($headers->has('MIME-Version'));
$this->assertTrue($headers->has('Content-Type'));
$this->assertTrue($headers->has('Subject'));
$this->assertTrue($headers->has('Auto-Submitted'));
$contentType = $headers->get('Content-Type');
$this->assertEquals('multipart/report', $contentType->getType());
}
public function testMailHeaderContainsZeroValue(): void
{
$message =
"From: [email protected]\r\n"
. "To: [email protected]\r\n"
. "Subject: plain text email example\r\n"
. "X-Spam-Score: 0\r\n"
. "X-Some-Value: 1\r\n"
. "\r\n"
. "I am a test message\r\n";
$msg = Message::fromString($message);
$this->assertStringContainsString('X-Spam-Score: 0', $msg->toString());
}
/**
* @ref CVE-2016-10033 which targeted WordPress
*/
public function testSecondCodeInjectionInFromHeader(): void
{
$message = new Message();
$this->expectException(Exception\InvalidArgumentException::class);
// @codingStandardsIgnoreStart
$message->setFrom('user@xenial(tmp1 -be ${run{${substr{0}{1}{$spool_directory}}usr${substr{0}{1}{$spool_directory}}bin${substr{0}{1}{$spool_directory}}touch${substr{10}{1}{$tod_log}}${substr{0}{1}{$spool_directory}}tmp${substr{0}{1}{$spool_directory}}test}} tmp2)', 'Sender\'s name');
// @codingStandardsIgnoreEnd
}
public function testMessageSubjectFromString(): void
{
$rawMessage = 'Subject: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=';
$mail = Message::fromString($rawMessage);
$this->assertStringContainsString(
'Subject: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20?=' . "\r\n"
. ' =?UTF-8?Q?accented=20vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=' . "\r\n",
$mail->toString()
);
}
public function testMessageSubjectSetSubject(): void
{
$mail = new Message();
$mail->setSubject('Non “ascii” characters like accented vowels òàùèéì');
$this->assertStringContainsString(
'Subject: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20?=' . "\r\n"
. ' =?UTF-8?Q?accented=20vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=' . "\r\n",
$mail->toString()
);
}
public function testCorrectHeaderEncodingAddHeader(): void
{
$mail = new Message();
$header = new GenericHeader('X-Test', 'Non “ascii” characters like accented vowels òàùèéì');
$mail->getHeaders()->addHeader($header);
$this->assertStringContainsString(
'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20?=' . "\r\n"
. ' =?UTF-8?Q?accented=20vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=' . "\r\n",
$mail->toString()
);
}
public function testCorrectHeaderEncodingSetHeaders(): void
{
$mail = new Message();
$header = new GenericHeader('X-Test', 'Non “ascii” characters like accented vowels òàùèéì');
$headers = new Headers();
$headers->addHeader($header);
$mail->setHeaders($headers);
$this->assertStringContainsString(
'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20?=' . "\r\n"
. ' =?UTF-8?Q?accented=20vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=' . "\r\n",
$mail->toString()
);
}
public function testCorrectHeaderEncodingFromString(): void
{
$mail = new Message();
$str = 'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=';
$header = GenericHeader::fromString($str);
$mail->getHeaders()->addHeader($header);
$this->assertStringContainsString(
'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20?=' . "\r\n"
. ' =?UTF-8?Q?accented=20vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=',
$mail->toString()
);
}
public function testCorrectHeaderEncodingFromStringAndSetHeaders(): void
{
$mail = new Message();
$str = 'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=';
$header = GenericHeader::fromString($str);
$headers = new Headers();
$headers->addHeader($header);
$mail->setHeaders($headers);
$this->assertStringContainsString(
'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20?=' . "\r\n"
. ' =?UTF-8?Q?accented=20vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=',
$mail->toString()
);
}
public function testMessageSubjectEncodingWhenEncodingSetAfterTheSubject(): void
{
$mail = new Message();
$mail->setSubject('hello world');
$mail->setEncoding('UTF-8');
$this->assertSame('UTF-8', $mail->getHeaders()->get('subject')->getEncoding());
$this->assertSame(
'Subject: =?UTF-8?Q?hello=20world?=',
$mail->getHeaders()->get('subject')->toString()
);
}
public function testMessageSubjectEncodingWhenEcodingSetBeforeTheSubject(): void
{
$mail = new Message();
$mail->setEncoding('UTF-8');
$mail->setSubject('hello world');
$this->assertSame('UTF-8', $mail->getHeaders()->get('subject')->getEncoding());
$this->assertSame(
'Subject: =?UTF-8?Q?hello=20world?=',
$mail->getHeaders()->get('subject')->toString()
);
}
}