Skip to content
This repository
Browse code

Adding EmailComponent::_getSocket() so EmailComponent + smtp is easie…

…r to test.

Fixing issue where hosts with portnames could cause smtp emails to fail.
Added tests, fixed an existing test to not depend on a local mailserver.
Fixes #1433
  • Loading branch information...
commit 8754d11aed8c5b88fb94dcd50db765cb4c0c46b5 1 parent f45f2e4
Mark Story authored January 06, 2011
16  cake/libs/controller/components/email.php
@@ -837,6 +837,18 @@ function _mail() {
837 837
 		return @mail($to, $this->_encode($this->subject), $message, $header, $this->additionalParams);
838 838
 	}
839 839
 
  840
+
  841
+/**
  842
+ * Helper method to get socket, overridden in tests
  843
+ *
  844
+ * @param array $config Config data for the socket.
  845
+ * @return void
  846
+ * @access protected
  847
+ */
  848
+	function _getSocket($config) {
  849
+		$this->__smtpConnection =& new CakeSocket($config);
  850
+	}
  851
+
840 852
 /**
841 853
  * Sends out email via SMTP
842 854
  *
@@ -853,7 +865,7 @@ function _smtp() {
853 865
 			'timeout' => 30
854 866
 		);
855 867
 		$this->smtpOptions = array_merge($defaults, $this->smtpOptions);
856  
-		$this->__smtpConnection =& new CakeSocket($this->smtpOptions);
  868
+		$this->_getSocket($this->smtpOptions);
857 869
 
858 870
 		if (!$this->__smtpConnection->connect()) {
859 871
 			$this->smtpError = $this->__smtpConnection->lastError();
@@ -867,7 +879,7 @@ function _smtp() {
867 879
 		if (isset($this->smtpOptions['client'])) {
868 880
 			$host = $this->smtpOptions['client'];
869 881
 		} elseif (!empty($httpHost)) {
870  
-			$host = $httpHost;
  882
+			list($host) = explode(':', $httpHost);
871 883
 		} else {
872 884
 			$host = 'localhost';
873 885
 		}
95  cake/tests/cases/libs/controller/components/email.test.php
@@ -20,6 +20,9 @@
20 20
  * @license       http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
21 21
  */
22 22
 App::import('Component', 'Email');
  23
+App::import('Core', 'CakeSocket');
  24
+
  25
+Mock::generate('CakeSocket', 'MockEmailSocket');
23 26
 
24 27
 /**
25 28
  * EmailTestComponent class
@@ -29,6 +32,7 @@
29 32
  */
30 33
 class EmailTestComponent extends EmailComponent {
31 34
 
  35
+	var $smtpSend = '';
32 36
 /**
33 37
  * smtpSend method override for testing
34 38
  *
@@ -40,6 +44,19 @@ function smtpSend($data, $code = '250') {
40 44
 	}
41 45
 
42 46
 /**
  47
+ * undocumented function
  48
+ *
  49
+ * @return void
  50
+ */
  51
+	function _smtpSend($data, $code = '250') {
  52
+		if ($this->_debug) {
  53
+			$this->smtpSend .= $data . "\n";
  54
+			return true;
  55
+		}
  56
+		return parent::_smtpSend($data, $code);
  57
+	}
  58
+
  59
+/**
43 60
  * Convenience setter method for testing.
44 61
  *
45 62
  * @access public
@@ -50,6 +67,18 @@ function setConnectionSocket(&$socket) {
50 67
 	}
51 68
 
52 69
 /**
  70
+ * Allows mocks to be used with tests.
  71
+ *
  72
+ * @param array $config 
  73
+ * @return void
  74
+ */
  75
+	function _getSocket($config) {
  76
+		if (empty($this->__smtpConnection)) {
  77
+			parent::_getSocket($config);
  78
+		}
  79
+	}
  80
+
  81
+/**
53 82
  * Convenience getter method for testing.
54 83
  *
55 84
  * @access public
@@ -408,46 +437,60 @@ function testSmtpEhlo() {
408 437
  * @return void
409 438
  */
410 439
 	function testSmtpSendMultipleTo() {
411  
-		if ($this->skipIf(!@fsockopen('localhost', 25), '%s No SMTP server running on localhost')) {
412  
-			return;
413  
-		}
414 440
 		$this->Controller->EmailTest->reset();
415 441
 		$this->Controller->EmailTest->to = array('postmaster@localhost', 'root@localhost');
416 442
 		$this->Controller->EmailTest->from = 'noreply@example.com';
417 443
 		$this->Controller->EmailTest->subject = 'Cake SMTP multiple To test';
418 444
 		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
419 445
 		$this->Controller->EmailTest->template = null;
420  
-
  446
+		$this->Controller->EmailTest->_debug = true;
  447
+		$this->Controller->EmailTest->sendAs = 'text';
421 448
 		$this->Controller->EmailTest->delivery = 'smtp';
  449
+		
  450
+		$socket = new MockEmailSocket();
  451
+		$socket->setReturnValue('connect', true);
  452
+		$this->Controller->EmailTest->setConnectionSocket($socket);
  453
+
422 454
 		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
423 455
 
424  
-		$this->Controller->EmailTest->_debug = true;
425  
-		$this->Controller->EmailTest->sendAs = 'text';
426  
-		$expect = <<<TEMPDOC
427  
-<pre>Host: localhost
428  
-Port: 25
429  
-Timeout: 30
430  
-To: postmaster@localhost, root@localhost
431  
-From: noreply@example.com
432  
-Subject: Cake SMTP multiple To test
433  
-Header:
  456
+		$this->assertPattern('/EHLO localhost\n/', $this->Controller->EmailTest->smtpSend);
  457
+		$this->assertPattern('/MAIL FROM: <noreply@example\.com>\n/', $this->Controller->EmailTest->smtpSend);
  458
+		$this->assertPattern('/RCPT TO: <postmaster@localhost>\n/', $this->Controller->EmailTest->smtpSend);
  459
+		$this->assertPattern('/RCPT TO: <root@localhost>\n/', $this->Controller->EmailTest->smtpSend);
  460
+		$this->assertPattern(
  461
+			'/To: postmaster@localhost, root@localhost[\n\r]/', 
  462
+			$this->Controller->EmailTest->smtpSend
  463
+		);
  464
+	}
434 465
 
435  
-To: postmaster@localhost, root@localhost
436  
-From: noreply@example.com
437  
-Reply-To: noreply@example.com
438  
-Subject: Cake SMTP multiple To test
439  
-X-Mailer: CakePHP Email Component
440  
-Content-Type: text/plain; charset=UTF-8
441  
-Content-Transfer-Encoding: 7bitParameters:
  466
+/**
  467
+ * test sending smtp from a host using a port.
  468
+ *
  469
+ * @return void
  470
+ */
  471
+	function testSmtpSendHostWithPort() {
  472
+		$bkp = env('HTTP_HOST');
  473
+		$_SERVER['HTTP_HOST'] = 'localhost:8080';
442 474
 
443  
-Message:
  475
+		$this->Controller->EmailTest->reset();
  476
+		$this->Controller->EmailTest->to = array('root@localhost');
  477
+		$this->Controller->EmailTest->from = 'noreply@example.com';
  478
+		$this->Controller->EmailTest->subject = 'Cake SMTP host test';
  479
+		$this->Controller->EmailTest->replyTo = 'noreply@example.com';
  480
+		$this->Controller->EmailTest->template = null;
  481
+		$this->Controller->EmailTest->delivery = 'smtp';
  482
+		$this->Controller->EmailTest->sendAs = 'text';
  483
+		$this->Controller->EmailTest->_debug = true;
444 484
 
445  
-This is the body of the message
  485
+		$socket = new MockEmailSocket();
  486
+		$socket->setReturnValue('connect', true);
446 487
 
447  
-</pre>
448  
-TEMPDOC;
  488
+		$this->Controller->EmailTest->setConnectionSocket($socket);
449 489
 		$this->assertTrue($this->Controller->EmailTest->send('This is the body of the message'));
450  
-		$this->assertEqual($this->Controller->Session->read('Message.email.message'), $this->__osFix($expect));
  490
+
  491
+		$this->assertPattern('/EHLO localhost\n/', $this->Controller->EmailTest->smtpSend);
  492
+
  493
+		$_SERVER['HTTP_HOST'] = $bkp;
451 494
 	}
452 495
 
453 496
 /**

0 notes on commit 8754d11

Please sign in to comment.
Something went wrong with that request. Please try again.