Huge rewrite of default parameters, ext-network continued:
[mailer.git] / inc / phpmailer / test / phpmailer_test.php
1 <?php\r
2 /*******************\r
3   Unit Test\r
4   Type: phpmailer class\r
5 ********************/\r
6 \r
7 $INCLUDE_DIR = "../";\r
8 \r
9 require("phpunit.php");\r
10 require($INCLUDE_DIR . "class.phpmailer.php");\r
11 error_reporting(E_ALL);\r
12 \r
13 /**\r
14  * Performs authentication tests\r
15  */\r
16 class phpmailerTest extends TestCase\r
17 {\r
18     /**\r
19      * Holds the default phpmailer instance.\r
20      * @private\r
21      * @type object\r
22      */\r
23     var $Mail = false;\r
24 \r
25     /**\r
26      * Holds the SMTP mail host.\r
27      * @public\r
28      * @type string\r
29      */\r
30     var $Host = "";\r
31     \r
32     /**\r
33      * Holds the change log.\r
34      * @private\r
35      * @type string array\r
36      */\r
37     var $ChangeLog = array();\r
38     \r
39      /**\r
40      * Holds the note log.\r
41      * @private\r
42      * @type string array\r
43      */\r
44     var $NoteLog = array();   \r
45 \r
46     /**\r
47      * Class constuctor.\r
48      */\r
49     function phpmailerTest($name) {\r
50         /* must define this constructor */\r
51         $this->TestCase( $name );\r
52     }\r
53     \r
54     /**\r
55      * Run before each test is started.\r
56      */\r
57     function setUp() {\r
58         global $global_vars;\r
59         global $INCLUDE_DIR;\r
60 \r
61         $this->Mail = new PHPMailer();\r
62 \r
63         $this->Mail->Priority = 3;\r
64         $this->Mail->Encoding = "8bit";\r
65         $this->Mail->CharSet = "iso-8859-1";\r
66         $this->Mail->From = "unit_test@phpmailer.sf.net";\r
67         $this->Mail->FromName = "Unit Tester";\r
68         $this->Mail->Sender = "";\r
69         $this->Mail->Subject = "Unit Test";\r
70         $this->Mail->Body = "";\r
71         $this->Mail->AltBody = "";\r
72         $this->Mail->WordWrap = '0';\r
73         $this->Mail->Host = $global_vars["mail_host"];\r
74         $this->Mail->Port = 25;\r
75         $this->Mail->Helo = "localhost.localdomain";\r
76         $this->Mail->SMTPAuth = false;\r
77         $this->Mail->Username = "";\r
78         $this->Mail->Password = "";\r
79         $this->Mail->PluginDir = $INCLUDE_DIR;\r
80                 $this->Mail->AddReplyTo("no_reply@phpmailer.sf.net", "Reply Guy");\r
81         $this->Mail->Sender = "unit_test@phpmailer.sf.net";\r
82 \r
83         if(strlen($this->Mail->Host) > 0)\r
84             $this->Mail->Mailer = "smtp";\r
85         else\r
86         {\r
87             $this->Mail->Mailer = "mail";\r
88             $this->Sender = "unit_test@phpmailer.sf.net";\r
89         }\r
90         \r
91         global $global_vars;\r
92         $this->SetAddress($global_vars["mail_to"], "Test User");\r
93         if(strlen($global_vars["mail_cc"]) > 0)\r
94             $this->SetAddress($global_vars["mail_cc"], "Carbon User", "cc");\r
95     }     \r
96 \r
97     /**\r
98      * Run after each test is completed.\r
99      */\r
100     function tearDown() {\r
101         // Clean global variables\r
102         $this->Mail = NULL;\r
103         $this->ChangeLog = array();\r
104         $this->NoteLog = array();\r
105     }\r
106 \r
107 \r
108     /**\r
109      * Build the body of the message in the appropriate format.\r
110      * @private\r
111      * @returns void\r
112      */\r
113     function BuildBody() {\r
114         $this->CheckChanges();\r
115         \r
116         // Determine line endings for message        \r
117         if($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0)\r
118         {\r
119             $eol = "<br/>";\r
120             $bullet = "<li>";\r
121             $bullet_start = "<ul>";\r
122             $bullet_end = "</ul>";\r
123         }\r
124         else\r
125         {\r
126             $eol = "\n";\r
127             $bullet = " - ";\r
128             $bullet_start = "";\r
129             $bullet_end = "";\r
130         }\r
131         \r
132         $ReportBody = "";\r
133         \r
134         $ReportBody .= "---------------------" . $eol;\r
135         $ReportBody .= "Unit Test Information" . $eol;\r
136         $ReportBody .= "---------------------" . $eol;\r
137         $ReportBody .= "phpmailer version: " . $this->Mail->Version . $eol;\r
138         $ReportBody .= "Content Type: " . $this->Mail->ContentType . $eol;\r
139         \r
140         if(strlen($this->Mail->Host) > 0)\r
141             $ReportBody .= "Host: " . $this->Mail->Host . $eol;\r
142         \r
143         // If attachments then create an attachment list\r
144         if(count($this->Mail->attachment) > 0)\r
145         {\r
146             $ReportBody .= "Attachments:" . $eol;\r
147             $ReportBody .= $bullet_start;\r
148             for($i = '0'; $i < count($this->Mail->attachment); $i++)\r
149             {\r
150                 $ReportBody .= $bullet . "Name: " . $this->Mail->attachment[$i][1] . ", ";\r
151                 $ReportBody .= "Encoding: " . $this->Mail->attachment[$i][3] . ", ";\r
152                 $ReportBody .= "Type: " . $this->Mail->attachment[$i][4] . $eol;\r
153             }\r
154             $ReportBody .= $bullet_end . $eol;\r
155         }\r
156         \r
157         // If there are changes then list them\r
158         if(count($this->ChangeLog) > 0)\r
159         {\r
160             $ReportBody .= "Changes" . $eol;\r
161             $ReportBody .= "-------" . $eol;\r
162 \r
163             $ReportBody .= $bullet_start;\r
164             for($i = '0'; $i < count($this->ChangeLog); $i++)\r
165             {\r
166                 $ReportBody .= $bullet . $this->ChangeLog[$i][0] . " was changed to [" . \r
167                                $this->ChangeLog[$i][1] . "]" . $eol;\r
168             }\r
169             $ReportBody .= $bullet_end . $eol . $eol;\r
170         }\r
171         \r
172         // If there are notes then list them\r
173         if(count($this->NoteLog) > 0)\r
174         {\r
175             $ReportBody .= "Notes" . $eol;\r
176             $ReportBody .= "-----" . $eol;\r
177 \r
178             $ReportBody .= $bullet_start;\r
179             for($i = '0'; $i < count($this->NoteLog); $i++)\r
180             {\r
181                 $ReportBody .= $bullet . $this->NoteLog[$i] . $eol;\r
182             }\r
183             $ReportBody .= $bullet_end;\r
184         }\r
185         \r
186         // Re-attach the original body\r
187         $this->Mail->Body .= $eol . $eol . $ReportBody;\r
188     }\r
189     \r
190     /**\r
191      * Check which default settings have been changed for the report.\r
192      * @private\r
193      * @returns void\r
194      */\r
195     function CheckChanges() {\r
196         if($this->Mail->Priority != 3)\r
197             $this->AddChange("Priority", $this->Mail->Priority);\r
198         if($this->Mail->Encoding != "8bit")\r
199             $this->AddChange("Encoding", $this->Mail->Encoding);\r
200         if($this->Mail->CharSet != "iso-8859-1")\r
201             $this->AddChange("CharSet", $this->Mail->CharSet);\r
202         if($this->Mail->Sender != "")\r
203             $this->AddChange("Sender", $this->Mail->Sender);\r
204         if($this->Mail->WordWrap != 0)\r
205             $this->AddChange("WordWrap", $this->Mail->WordWrap);\r
206         if($this->Mail->Mailer != "mail")\r
207             $this->AddChange("Mailer", $this->Mail->Mailer);\r
208         if($this->Mail->Port != 25)\r
209             $this->AddChange("Port", $this->Mail->Port);\r
210         if($this->Mail->Helo != "localhost.localdomain")\r
211             $this->AddChange("Helo", $this->Mail->Helo);\r
212         if($this->Mail->SMTPAuth)\r
213             $this->AddChange("SMTPAuth", "true");\r
214     }\r
215     \r
216     /**\r
217      * Adds a change entry.\r
218      * @private\r
219      * @returns void\r
220      */\r
221     function AddChange($sName, $sNewValue) {\r
222         $cur = count($this->ChangeLog);\r
223         $this->ChangeLog[$cur][0] = $sName;\r
224         $this->ChangeLog[$cur][1] = $sNewValue;\r
225     }\r
226     \r
227     /**\r
228      * Adds a simple note to the message.\r
229      * @public\r
230      * @returns void\r
231      */\r
232     function AddNote($sValue) {\r
233         $this->NoteLog[] = $sValue;\r
234     }\r
235 \r
236     /**\r
237      * Adds all of the addresses\r
238      * @public\r
239      * @returns void\r
240      */\r
241     function SetAddress($sAddress, $sName = "", $sType = "to") {\r
242         switch($sType)\r
243         {\r
244             case "to":\r
245                 $this->Mail->AddAddress($sAddress, $sName);\r
246                 break;\r
247             case "cc":\r
248                 $this->Mail->AddCC($sAddress, $sName);\r
249                 break;\r
250             case "bcc":\r
251                 $this->Mail->AddBCC($sAddress, $sName);\r
252                 break;\r
253         }\r
254     }\r
255 \r
256     /////////////////////////////////////////////////\r
257     // UNIT TESTS\r
258     /////////////////////////////////////////////////\r
259 \r
260     /**\r
261      * Try a plain message.\r
262      */\r
263     function test_WordWrap() {\r
264 \r
265         $this->Mail->WordWrap = 40;\r
266         $my_body = "Here is the main body of this message.  It should " .\r
267                    "be quite a few lines.  It should be wrapped at the " .\r
268                    "40 characters.  Make sure that it is.";\r
269         $nBodyLen = strlen($my_body);\r
270         $my_body .= "\n\nThis is the above body length: " . $nBodyLen;\r
271 \r
272         $this->Mail->Body = $my_body;\r
273         $this->Mail->Subject .= ": Wordwrap";\r
274 \r
275         $this->BuildBody();\r
276         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
277     }\r
278 \r
279     /**\r
280      * Try a plain message.\r
281      */\r
282     function test_Low_Priority() {\r
283     \r
284         $this->Mail->Priority = 5;\r
285         $this->Mail->Body = "Here is the main body.  There should be " .\r
286                             "a reply to address in this message.";\r
287         $this->Mail->Subject .= ": Low Priority";\r
288         $this->Mail->AddReplyTo("nobody@nobody.com", "Nobody (Unit Test)");\r
289 \r
290         $this->BuildBody();\r
291         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
292     }\r
293 \r
294     /**\r
295      * Simple plain file attachment test.\r
296      */\r
297     function test_Multiple_Plain_FileAttachment() {\r
298 \r
299         $this->Mail->Body = "Here is the text body";\r
300         $this->Mail->Subject .= ": Plain + Multiple FileAttachments";\r
301 \r
302         if(!$this->Mail->AddAttachment("test.png"))\r
303         {\r
304             $this->assert(false, $this->Mail->ErrorInfo);\r
305             return;\r
306         }\r
307 \r
308         if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))\r
309         {\r
310             $this->assert(false, $this->Mail->ErrorInfo);\r
311             return;\r
312         }\r
313 \r
314         $this->BuildBody();\r
315         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
316     }\r
317 \r
318     /**\r
319      * Simple plain string attachment test.\r
320      */\r
321     function test_Plain_StringAttachment() {\r
322 \r
323         $this->Mail->Body = "Here is the text body";\r
324         $this->Mail->Subject .= ": Plain + StringAttachment";\r
325         \r
326         $sAttachment = "These characters are the content of the " .\r
327                        "string attachment.\nThis might be taken from a ".\r
328                        "database or some other such thing. ";\r
329         \r
330         $this->Mail->AddStringAttachment($sAttachment, "string_attach.txt");\r
331 \r
332         $this->BuildBody();\r
333         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
334     }\r
335 \r
336     /**\r
337      * Plain quoted-printable message.\r
338      */\r
339     function test_Quoted_Printable() {\r
340 \r
341         $this->Mail->Body = "Here is the main body";\r
342         $this->Mail->Subject .= ": Plain + Quoted-printable";\r
343         $this->Mail->Encoding = "quoted-printable";\r
344 \r
345         $this->BuildBody();\r
346         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
347     }\r
348 \r
349     /**\r
350      * Try a plain message.\r
351      */\r
352     function test_Html() {\r
353     \r
354         $this->Mail->IsHTML(true);\r
355         $this->Mail->Subject .= ": HTML only";\r
356         \r
357         $this->Mail->Body = "This is a <b>test message</b> written in HTML. </br>" .\r
358                             "Go to <a href=\"http://phpmailer.sourceforge.net/\">" .\r
359                             "http://phpmailer.sourceforge.net/</a> for new versions of " .\r
360                             "phpmailer.  <p/> Thank you!";\r
361 \r
362         $this->BuildBody();\r
363         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
364     }\r
365 \r
366     /**\r
367      * Simple HTML and attachment test\r
368      */\r
369     function test_HTML_Attachment() {\r
370 \r
371         $this->Mail->Body = "This is the <b>HTML</b> part of the email.";\r
372         $this->Mail->Subject .= ": HTML + Attachment";\r
373         $this->Mail->IsHTML(true);\r
374         \r
375         if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))\r
376         {\r
377             $this->assert(false, $this->Mail->ErrorInfo);\r
378             return;\r
379         }\r
380 \r
381         $this->BuildBody();\r
382         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
383     }\r
384 \r
385     /**\r
386      * An embedded attachment test.\r
387      */\r
388     function test_Embedded_Image() {\r
389 \r
390         $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .\r
391                      "Here is an image!</a>";\r
392         $this->Mail->Subject .= ": Embedded Image";\r
393         $this->Mail->IsHTML(true);\r
394         \r
395         if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",\r
396                                           "base64", "image/png"))\r
397         {\r
398             $this->assert(false, $this->Mail->ErrorInfo);\r
399             return;\r
400         }\r
401 \r
402         $this->BuildBody();\r
403         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
404     }\r
405 \r
406     /**\r
407      * An embedded attachment test.\r
408      */\r
409     function test_Multi_Embedded_Image() {\r
410 \r
411         $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .\r
412                      "Here is an image!</a>";\r
413         $this->Mail->Subject .= ": Embedded Image + Attachment";\r
414         $this->Mail->IsHTML(true);\r
415         \r
416         if(!$this->Mail->AddEmbeddedImage("test.png", "my-attach", "test.png",\r
417                                           "base64", "image/png"))\r
418         {\r
419             $this->assert(false, $this->Mail->ErrorInfo);\r
420             return;\r
421         }\r
422 \r
423         if(!$this->Mail->AddAttachment("phpmailer_test.php", "test.txt"))\r
424         {\r
425             $this->assert(false, $this->Mail->ErrorInfo);\r
426             return;\r
427         }\r
428         \r
429         $this->BuildBody();\r
430         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
431     }\r
432 \r
433     /**\r
434      * Simple multipart/alternative test.\r
435      */\r
436     function test_AltBody() {\r
437 \r
438         $this->Mail->Body = "This is the <b>HTML</b> part of the email.";\r
439         $this->Mail->AltBody = "Here is the text body of this message.  " .\r
440                    "It should be quite a few lines.  It should be wrapped at the " .\r
441                    "40 characters.  Make sure that it is.";\r
442         $this->Mail->WordWrap = 40;\r
443         $this->AddNote("This is a mulipart alternative email");\r
444         $this->Mail->Subject .= ": AltBody + Word Wrap";\r
445 \r
446         $this->BuildBody();\r
447         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
448     }\r
449 \r
450     /**\r
451      * Simple HTML and attachment test\r
452      */\r
453     function test_AltBody_Attachment() {\r
454 \r
455         $this->Mail->Body = "This is the <b>HTML</b> part of the email.";\r
456         $this->Mail->AltBody = "This is the text part of the email.";\r
457         $this->Mail->Subject .= ": AltBody + Attachment";\r
458         $this->Mail->IsHTML(true);\r
459         \r
460         if(!$this->Mail->AddAttachment("phpmailer_test.php", "test_attach.txt"))\r
461         {\r
462             $this->assert(false, $this->Mail->ErrorInfo);\r
463             return;\r
464         }\r
465 \r
466         $this->BuildBody();\r
467         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
468 \r
469         $fp = fopen("message.txt", "w");\r
470         fwrite($fp, $this->Mail->CreateHeader() . $this->Mail->CreateBody());\r
471         fclose($fp);\r
472     }    \r
473 \r
474     function test_MultipleSend() {\r
475         $this->Mail->Body = "Sending two messages without keepalive";\r
476         $this->BuildBody();\r
477         $subject = $this->Mail->Subject;\r
478 \r
479         $this->Mail->Subject = $subject . ": SMTP 1";\r
480         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
481         \r
482         $this->Mail->Subject = $subject . ": SMTP 2";\r
483         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
484     }\r
485 \r
486     function test_SmtpKeepAlive() {\r
487         $this->Mail->Body = "This was done using the SMTP keep-alive.";\r
488         $this->BuildBody();\r
489         $subject = $this->Mail->Subject;\r
490 \r
491         $this->Mail->SMTPKeepAlive = true;\r
492         $this->Mail->Subject = $subject . ": SMTP keep-alive 1";\r
493         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
494         \r
495         $this->Mail->Subject = $subject . ": SMTP keep-alive 2";\r
496         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
497         $this->Mail->SmtpClose();\r
498     }\r
499     \r
500     /**\r
501      * Tests this denial of service attack: \r
502      *    http://www.cybsec.com/vuln/PHPMailer-DOS.pdf\r
503      */\r
504     function test_DenialOfServiceAttack() {\r
505         $this->Mail->Body = "This should no longer cause a denial of service.";\r
506         $this->BuildBody();\r
507        \r
508         $this->Mail->Subject = str_repeat("A", 998);\r
509         $this->assert($this->Mail->Send(), $this->Mail->ErrorInfo);\r
510     }\r
511     \r
512     function test_Error() {\r
513         $this->Mail->Subject .= ": This should be sent"; \r
514         $this->BuildBody();\r
515         $this->Mail->ClearAllRecipients(); // no addresses should cause an error\r
516         $this->assert($this->Mail->IsError() == false, "Error found");\r
517         $this->assert($this->Mail->Send() == false, "Send succeeded");\r
518         $this->assert($this->Mail->IsError(), "No error found");\r
519         $this->assertEquals('You must provide at least one ' .\r
520                             'recipient email address.', $this->Mail->ErrorInfo);\r
521         $this->Mail->AddAddress(get("mail_to"));\r
522         $this->assert($this->Mail->Send(), "Send failed");\r
523     }\r
524 }  \r
525  \r
526 /**\r
527  * Create and run test instance.\r
528  */\r
529  \r
530 if(isset($HTTP_GET_VARS))\r
531     $global_vars = $HTTP_GET_VARS;\r
532 else\r
533     $global_vars = $_REQUEST;\r
534 \r
535 if(isset($global_vars["submitted"]))\r
536 {\r
537     echo "Test results:<br>";\r
538     $suite = new TestSuite( "phpmailerTest" );\r
539     \r
540     $testRunner = new TestRunner;\r
541     $testRunner->run($suite);\r
542     echo "<hr noshade/>";\r
543 }\r
544 \r
545 function get($sName) {\r
546     global $global_vars;\r
547     if(isset($global_vars[$sName]))\r
548         return $global_vars[$sName];\r
549     else\r
550         return "";\r
551 }\r
552 \r
553 ?>\r
554 \r
555 <html>\r
556 <body>\r
557 <h3>phpmailer Unit Test</h3>\r
558 By entering a SMTP hostname it will automatically perform tests with SMTP.\r
559 \r
560 <form name="phpmailer_unit" action="phpmailer_test.php" method="get">\r
561 <input type="hidden" name="submitted" value="1"/>\r
562 To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>\r
563 <br/>\r
564 Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>\r
565 <br/>\r
566 SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>\r
567 <p/>\r
568 <input type="submit" value="Run Test"/>\r
569 \r
570 </form>\r
571 </body>\r
572 </html>\r