[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/test/tests/mail/phpmailer/ -> phpmailer_test.class.php (source)

   1  <?php
   2  /*******************

   3    Unit Test

   4    Type: phpmailer class

   5  ********************/
   6  
   7  lt_include( PLOG_CLASS_PATH."class/test/helpers/lifetypetestcase.class.php" );
   8  lt_include( PLOG_CLASS_PATH."class/mail/phpmailer/class.phpmailer.php" );
   9  
  10  /**

  11   * these were not part of the original test suite but had to be added to avoid plenty of issues with files,

  12   * folders, paths and mail servers. Please modify them to suit your needs

  13   */
  14  define( "TEST_MAIL_HOST", "localhost" );
  15  define( "TEST_MAIL_USER", "please_set@please.set" );
  16  define( "PHPMAILER_TEST_BASE_FOLDER", PLOG_CLASS_PATH."class/test/tests/mail/phpmailer/" );
  17  
  18  /**

  19   * \ingroup Test

  20   *

  21   * Original tests from the PHPMailer package, adapted to work with LifeType's own

  22   * framework for unit testing.

  23   */
  24  class phpmailer_Test extends LifeTypeTestCase
  25  {
  26      /**

  27       * Holds the default phpmailer instance.

  28       * @private

  29       * @type object

  30       */
  31      var $Mail = false;
  32  
  33      /**

  34       * Holds the SMTP mail host.

  35       * @public

  36       * @type string

  37       */
  38      var $Host = "";
  39      
  40      /**

  41       * Holds the change log.

  42       * @private

  43       * @type string array

  44       */
  45      var $ChangeLog = array();
  46      
  47       /**

  48       * Holds the note log.

  49       * @private

  50       * @type string array

  51       */
  52      var $NoteLog = array();   
  53  
  54      /**

  55       * Class constuctor.

  56       */
  57      function phpmailerTest($name) {
  58          /* must define this constructor */

  59          $this->TestCase( $name );
  60      }
  61      
  62      /**

  63       * Run before each test is started.

  64       */
  65      function setUp() {
  66          global $global_vars;
  67          global $INCLUDE_DIR;
  68  
  69          $this->Mail = new PHPMailer();
  70  
  71          $this->Mail->Priority = 3;
  72          $this->Mail->Encoding = "8bit";
  73          $this->Mail->CharSet = "iso-8859-1";
  74          $this->Mail->From = "unit_test@phpmailer.sf.net";
  75          $this->Mail->FromName = "Unit Tester";
  76          $this->Mail->Sender = "";
  77          $this->Mail->Subject = "Unit Test";
  78          $this->Mail->Body = "";
  79          $this->Mail->AltBody = "";
  80          $this->Mail->WordWrap = 0;
  81          $this->Mail->Host = TEST_MAIL_HOST;
  82          $this->Mail->Port = 25;
  83          $this->Mail->Helo = "localhost.localdomain";
  84          $this->Mail->SMTPAuth = false;
  85          $this->Mail->Username = "";
  86          $this->Mail->Password = "";
  87          $this->Mail->PluginDir = $INCLUDE_DIR;
  88          $this->Mail->AddReplyTo("no_reply@phpmailer.sf.net", "Reply Guy");
  89          $this->Mail->Sender = "unit_test@phpmailer.sf.net";
  90          // set the language or else we'll get silly errors

  91          $this->Mail->SetLanguage( 'en', PLOG_CLASS_PATH."class/mail/phpmailer/language/" );
  92  
  93          if(strlen($this->Mail->Host) > 0)
  94              $this->Mail->Mailer = "smtp";
  95          else
  96          {
  97              $this->Mail->Mailer = "mail";
  98              $this->Sender = "unit_test@phpmailer.sf.net";
  99          }
 100          
 101          $this->SetAddress(TEST_MAIL_USER, "Test User");
 102          /*if(strlen($global_vars["mail_cc"]) > 0)

 103              $this->SetAddress($global_vars["mail_cc"], "Carbon User", "cc");*/
 104      }     
 105  
 106      /**

 107       * Run after each test is completed.

 108       */
 109      function tearDown() {
 110          // Clean global variables

 111          $this->Mail = NULL;
 112          $this->ChangeLog = array();
 113          $this->NoteLog = array();
 114      }
 115  
 116  
 117      /**

 118       * Build the body of the message in the appropriate format.

 119       * @private

 120       * @returns void

 121       */
 122      function BuildBody() {
 123          $this->CheckChanges();
 124          
 125          // Determine line endings for message        

 126          if($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0)
 127          {
 128              $eol = "<br/>";
 129              $bullet = "<li>";
 130              $bullet_start = "<ul>";
 131              $bullet_end = "</ul>";
 132          }
 133          else
 134          {
 135              $eol = "\n";
 136              $bullet = " - ";
 137              $bullet_start = "";
 138              $bullet_end = "";
 139          }
 140          
 141          $ReportBody = "";
 142          
 143          $ReportBody .= "---------------------" . $eol;
 144          $ReportBody .= "Unit Test Information" . $eol;
 145          $ReportBody .= "---------------------" . $eol;
 146          $ReportBody .= "phpmailer version: " . $this->Mail->Version . $eol;
 147          $ReportBody .= "Content Type: " . $this->Mail->ContentType . $eol;
 148          
 149          if(strlen($this->Mail->Host) > 0)
 150              $ReportBody .= "Host: " . $this->Mail->Host . $eol;
 151          
 152          // If attachments then create an attachment list

 153          if(count($this->Mail->attachment) > 0)
 154          {
 155              $ReportBody .= "Attachments:" . $eol;
 156              $ReportBody .= $bullet_start;
 157              for($i = 0; $i < count($this->Mail->attachment); $i++)
 158              {
 159                  $ReportBody .= $bullet . "Name: " . $this->Mail->attachment[$i][1] . ", ";
 160                  $ReportBody .= "Encoding: " . $this->Mail->attachment[$i][3] . ", ";
 161                  $ReportBody .= "Type: " . $this->Mail->attachment[$i][4] . $eol;
 162              }
 163              $ReportBody .= $bullet_end . $eol;
 164          }
 165          
 166          // If there are changes then list them

 167          if(count($this->ChangeLog) > 0)
 168          {
 169              $ReportBody .= "Changes" . $eol;
 170              $ReportBody .= "-------" . $eol;
 171  
 172              $ReportBody .= $bullet_start;
 173              for($i = 0; $i < count($this->ChangeLog); $i++)
 174              {
 175                  $ReportBody .= $bullet . $this->ChangeLog[$i][0] . " was changed to [" . 
 176                                 $this->ChangeLog[$i][1] . "]" . $eol;
 177              }
 178              $ReportBody .= $bullet_end . $eol . $eol;
 179          }
 180          
 181          // If there are notes then list them

 182          if(count($this->NoteLog) > 0)
 183          {
 184              $ReportBody .= "Notes" . $eol;
 185              $ReportBody .= "-----" . $eol;
 186  
 187              $ReportBody .= $bullet_start;
 188              for($i = 0; $i < count($this->NoteLog); $i++)
 189              {
 190                  $ReportBody .= $bullet . $this->NoteLog[$i] . $eol;
 191              }
 192              $ReportBody .= $bullet_end;
 193          }
 194          
 195          // Re-attach the original body

 196          $this->Mail->Body .= $eol . $eol . $ReportBody;
 197      }
 198      
 199      /**

 200       * Check which default settings have been changed for the report.

 201       * @private

 202       * @returns void

 203       */
 204      function CheckChanges() {
 205          if($this->Mail->Priority != 3)
 206              $this->AddChange("Priority", $this->Mail->Priority);
 207          if($this->Mail->Encoding != "8bit")
 208              $this->AddChange("Encoding", $this->Mail->Encoding);
 209          if($this->Mail->CharSet != "iso-8859-1")
 210              $this->AddChange("CharSet", $this->Mail->CharSet);
 211          if($this->Mail->Sender != "")
 212              $this->AddChange("Sender", $this->Mail->Sender);
 213          if($this->Mail->WordWrap != 0)
 214              $this->AddChange("WordWrap", $this->Mail->WordWrap);
 215          if($this->Mail->Mailer != "mail")
 216              $this->AddChange("Mailer", $this->Mail->Mailer);
 217          if($this->Mail->Port != 25)
 218              $this->AddChange("Port", $this->Mail->Port);
 219          if($this->Mail->Helo != "localhost.localdomain")
 220              $this->AddChange("Helo", $this->Mail->Helo);
 221          if($this->Mail->SMTPAuth)
 222              $this->AddChange("SMTPAuth", "true");
 223      }
 224      
 225      /**

 226       * Adds a change entry.

 227       * @private

 228       * @returns void

 229       */
 230      function AddChange($sName, $sNewValue) {
 231          $cur = count($this->ChangeLog);
 232          $this->ChangeLog[$cur][0] = $sName;
 233          $this->ChangeLog[$cur][1] = $sNewValue;
 234      }
 235      
 236      /**

 237       * Adds a simple note to the message.

 238       * @public

 239       * @returns void

 240       */
 241      function AddNote($sValue) {
 242          $this->NoteLog[] = $sValue;
 243      }
 244  
 245      /**

 246       * Adds all of the addresses

 247       * @public

 248       * @returns void

 249       */
 250      function SetAddress($sAddress, $sName = "", $sType = "to") {
 251          switch($sType)
 252          {
 253              case "to":
 254                  $this->Mail->AddAddress($sAddress, $sName);
 255                  break;
 256              case "cc":
 257                  $this->Mail->AddCC($sAddress, $sName);
 258                  break;
 259              case "bcc":
 260                  $this->Mail->AddBCC($sAddress, $sName);
 261                  break;
 262          }
 263      }
 264  
 265      /////////////////////////////////////////////////

 266      // UNIT TESTS

 267      /////////////////////////////////////////////////

 268  
 269      /**

 270       * Try a plain message.

 271       */
 272      function test_WordWrap() {
 273  
 274          $this->Mail->WordWrap = 40;
 275          $my_body = "Here is the main body of this message.  It should " .
 276                     "be quite a few lines.  It should be wrapped at the " .
 277                     "40 characters.  Make sure that it is.";
 278          $nBodyLen = strlen($my_body);
 279          $my_body .= "\n\nThis is the above body length: " . $nBodyLen;
 280  
 281          $this->Mail->Body = $my_body;
 282          $this->Mail->Subject .= ": Wordwrap";
 283  
 284          $this->BuildBody();
 285          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 286      }
 287  
 288      /**

 289       * Try a plain message.

 290       */
 291      function test_Low_Priority() {
 292      
 293          $this->Mail->Priority = 5;
 294          $this->Mail->Body = "Here is the main body.  There should be " .
 295                              "a reply to address in this message.";
 296          $this->Mail->Subject .= ": Low Priority";
 297          $this->Mail->AddReplyTo("nobody@nobody.com", "Nobody (Unit Test)");
 298  
 299          $this->BuildBody();
 300          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 301      }
 302  
 303      /**

 304       * Simple plain file attachment test.

 305       */
 306      function test_Multiple_Plain_FileAttachment() {
 307  
 308          $this->Mail->Body = "Here is the text body";
 309          $this->Mail->Subject .= ": Plain + Multiple FileAttachments";
 310  
 311          if(!$this->Mail->AddAttachment( PHPMAILER_TEST_BASE_FOLDER."test.png"))
 312          {
 313              $this->assertTrue(false, $this->Mail->ErrorInfo);
 314              return;
 315          }
 316  
 317          if(!$this->Mail->AddAttachment( PHPMAILER_TEST_BASE_FOLDER."phpmailer_test.class.php", "test.txt"))
 318          {
 319              $this->assertTrue(false, $this->Mail->ErrorInfo);
 320              return;
 321          }
 322  
 323          $this->BuildBody();
 324          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 325      }
 326  
 327      /**

 328       * Simple plain string attachment test.

 329       */
 330      function test_Plain_StringAttachment() {
 331  
 332          $this->Mail->Body = "Here is the text body";
 333          $this->Mail->Subject .= ": Plain + StringAttachment";
 334          
 335          $sAttachment = "These characters are the content of the " .
 336                         "string attachment.\nThis might be taken from a ".
 337                         "database or some other such thing. ";
 338          
 339          $this->Mail->AddStringAttachment($sAttachment, "string_attach.txt");
 340  
 341          $this->BuildBody();
 342          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 343      }
 344  
 345      /**

 346       * Plain quoted-printable message.

 347       */
 348      function test_Quoted_Printable() {
 349  
 350          $this->Mail->Body = "Here is the main body";
 351          $this->Mail->Subject .= ": Plain + Quoted-printable";
 352          $this->Mail->Encoding = "quoted-printable";
 353  
 354          $this->BuildBody();
 355          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 356      }
 357  
 358      /**

 359       * Try a plain message.

 360       */
 361      function test_Html() {
 362      
 363          $this->Mail->IsHTML(true);
 364          $this->Mail->Subject .= ": HTML only";
 365          
 366          $this->Mail->Body = "This is a <b>test message</b> written in HTML. </br>" .
 367                              "Go to <a href=\"http://phpmailer.sourceforge.net/\">" .
 368                              "http://phpmailer.sourceforge.net/</a> for new versions of " .
 369                              "phpmailer.  <p/> Thank you!";
 370  
 371          $this->BuildBody();
 372          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 373      }
 374  
 375      /**

 376       * Simple HTML and attachment test

 377       */
 378      function test_HTML_Attachment() {
 379  
 380          $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
 381          $this->Mail->Subject .= ": HTML + Attachment";
 382          $this->Mail->IsHTML(true);
 383          
 384          if(!$this->Mail->AddAttachment(PHPMAILER_TEST_BASE_FOLDER."phpmailer_test.class.php", "test_attach.txt"))
 385          {
 386              $this->assertTrue(false, $this->Mail->ErrorInfo);
 387              return;
 388          }
 389  
 390          $this->BuildBody();
 391          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 392      }
 393  
 394      /**

 395       * An embedded attachment test.

 396       */
 397      function test_Embedded_Image() {
 398  
 399          $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
 400                       "Here is an image!</a>";
 401          $this->Mail->Subject .= ": Embedded Image";
 402          $this->Mail->IsHTML(true);
 403          
 404          if(!$this->Mail->AddEmbeddedImage(PHPMAILER_TEST_BASE_FOLDER."test.png", "my-attach", "test.png",
 405                                            "base64", "image/png"))
 406          {
 407              $this->assertTrue(false, $this->Mail->ErrorInfo);
 408              return;
 409          }
 410  
 411          $this->BuildBody();
 412          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 413      }
 414  
 415      /**

 416       * An embedded attachment test.

 417       */
 418      function test_Multi_Embedded_Image() {
 419  
 420          $this->Mail->Body = "Embedded Image: <img alt=\"phpmailer\" src=\"cid:my-attach\">" .
 421                       "Here is an image!</a>";
 422          $this->Mail->Subject .= ": Embedded Image + Attachment";
 423          $this->Mail->IsHTML(true);
 424          
 425          if(!$this->Mail->AddEmbeddedImage(PHPMAILER_TEST_BASE_FOLDER."test.png", "my-attach", "test.png",
 426                                            "base64", "image/png"))
 427          {
 428              $this->assertTrue(false, $this->Mail->ErrorInfo);
 429              return;
 430          }
 431  
 432          if(!$this->Mail->AddAttachment(PHPMAILER_TEST_BASE_FOLDER."phpmailer_test.class.php", "test.txt"))
 433          {
 434              $this->assertTrue(false, $this->Mail->ErrorInfo);
 435              return;
 436          }
 437          
 438          $this->BuildBody();
 439          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 440      }
 441  
 442      /**

 443       * Simple multipart/alternative test.

 444       */
 445      function test_AltBody() {
 446  
 447          $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
 448          $this->Mail->AltBody = "Here is the text body of this message.  " .
 449                     "It should be quite a few lines.  It should be wrapped at the " .
 450                     "40 characters.  Make sure that it is.";
 451          $this->Mail->WordWrap = 40;
 452          $this->AddNote("This is a mulipart alternative email");
 453          $this->Mail->Subject .= ": AltBody + Word Wrap";
 454  
 455          $this->BuildBody();
 456          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 457      }
 458  
 459      /**

 460       * Simple HTML and attachment test

 461       */
 462      function test_AltBody_Attachment() {
 463  
 464          $this->Mail->Body = "This is the <b>HTML</b> part of the email.";
 465          $this->Mail->AltBody = "This is the text part of the email.";
 466          $this->Mail->Subject .= ": AltBody + Attachment";
 467          $this->Mail->IsHTML(true);
 468          
 469          if(!$this->Mail->AddAttachment(PHPMAILER_TEST_BASE_FOLDER."phpmailer_test.class.php", "test_attach.txt"))
 470          {
 471              $this->assertTrue(false, $this->Mail->ErrorInfo);
 472              return;
 473          }
 474  
 475          $this->BuildBody();
 476          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 477  
 478          $fp = fopen(PHPMAILER_TEST_BASE_FOLDER."message.txt", "w");
 479          fwrite($fp, $this->Mail->CreateHeader() . $this->Mail->CreateBody());
 480          fclose($fp);
 481      }    
 482  
 483      function test_MultipleSend() {
 484          $this->Mail->Body = "Sending two messages without keepalive";
 485          $this->BuildBody();
 486          $subject = $this->Mail->Subject;
 487  
 488          $this->Mail->Subject = $subject . ": SMTP 1";
 489          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 490          
 491          $this->Mail->Subject = $subject . ": SMTP 2";
 492          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 493      }
 494  
 495      function test_SmtpKeepAlive() {
 496          $this->Mail->Body = "This was done using the SMTP keep-alive.";
 497          $this->BuildBody();
 498          $subject = $this->Mail->Subject;
 499  
 500          $this->Mail->SMTPKeepAlive = true;
 501          $this->Mail->Subject = $subject . ": SMTP keep-alive 1";
 502          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 503          
 504          $this->Mail->Subject = $subject . ": SMTP keep-alive 2";
 505          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 506          $this->Mail->SmtpClose();
 507      }
 508      
 509      /**

 510       * Tests this denial of service attack: 

 511       *    http://www.cybsec.com/vuln/PHPMailer-DOS.pdf

 512       */
 513      function test_DenialOfServiceAttack() {
 514          $this->Mail->Body = "This should no longer cause a denial of service.";
 515          $this->BuildBody();
 516         
 517          $this->Mail->Subject = str_repeat("A", 998);
 518          $this->assertTrue($this->Mail->Send(), $this->Mail->ErrorInfo);
 519      }
 520      
 521      function test_Error() {
 522          $this->Mail->Subject .= ": This should be sent"; 
 523          $this->BuildBody();
 524          $this->Mail->ClearAllRecipients(); // no addresses should cause an error

 525          $this->assertTrue($this->Mail->IsError() == false, "Error found");
 526          $this->assertTrue($this->Mail->Send() == false, "Send succeeded");
 527          $this->assertTrue($this->Mail->IsError(), "No error found");
 528          $this->assertEquals('You must provide at least one ' .
 529                              'recipient email address.', $this->Mail->ErrorInfo);
 530          $this->Mail->AddAddress(TEST_MAIL_USER);
 531          $this->assertTrue($this->Mail->Send(), "Send failed");
 532      }
 533  }  


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics