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