SmtpTransportTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Network\Email;
  16. use Cake\Network\Email\Email;
  17. use Cake\Network\Email\SmtpTransport;
  18. use Cake\Network\Socket;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Help to test SmtpTransport
  22. *
  23. */
  24. class SmtpTestTransport extends SmtpTransport
  25. {
  26. /**
  27. * Helper to change the socket
  28. *
  29. * @param Socket $socket
  30. * @return void
  31. */
  32. public function setSocket(Socket $socket)
  33. {
  34. $this->_socket = $socket;
  35. }
  36. /**
  37. * Disabled the socket change
  38. *
  39. * @return void
  40. */
  41. protected function _generateSocket()
  42. {
  43. }
  44. /**
  45. * Magic function to call protected methods
  46. *
  47. * @param string $method
  48. * @param string $args
  49. * @return mixed
  50. */
  51. public function __call($method, $args)
  52. {
  53. $method = '_' . $method;
  54. return call_user_func_array([$this, $method], $args);
  55. }
  56. }
  57. /**
  58. * Test case
  59. *
  60. */
  61. class SmtpTransportTest extends TestCase
  62. {
  63. /**
  64. * Setup
  65. *
  66. * @return void
  67. */
  68. public function setUp()
  69. {
  70. parent::setUp();
  71. $this->socket = $this->getMock(
  72. 'Cake\Network\Socket',
  73. ['read', 'write', 'connect', 'disconnect', 'enableCrypto']
  74. );
  75. $this->SmtpTransport = new SmtpTestTransport();
  76. $this->SmtpTransport->setSocket($this->socket);
  77. $this->SmtpTransport->config(['client' => 'localhost']);
  78. }
  79. /**
  80. * testConnectEhlo method
  81. *
  82. * @return void
  83. */
  84. public function testConnectEhlo()
  85. {
  86. $this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
  87. $this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
  88. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
  89. $this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
  90. $this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
  91. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
  92. $this->SmtpTransport->connect();
  93. }
  94. /**
  95. * testConnectEhloTls method
  96. *
  97. * @return void
  98. */
  99. public function testConnectEhloTls()
  100. {
  101. $this->SmtpTransport->config(['tls' => true]);
  102. $this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
  103. $this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
  104. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
  105. $this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
  106. $this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
  107. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
  108. $this->socket->expects($this->at(5))->method('write')->with("STARTTLS\r\n");
  109. $this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
  110. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("220 Server ready\r\n"));
  111. $this->socket->expects($this->at(8))->method('enableCrypto')->with('tls')->will($this->returnValue(true));
  112. $this->socket->expects($this->at(9))->method('write')->with("EHLO localhost\r\n");
  113. $this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
  114. $this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 Accepted\r\n"));
  115. $this->SmtpTransport->connect();
  116. }
  117. /**
  118. * testConnectEhloTlsOnNonTlsServer method
  119. *
  120. * @expectedException \Cake\Network\Exception\SocketException
  121. * @expectedExceptionMessage SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.
  122. * @return void
  123. */
  124. public function testConnectEhloTlsOnNonTlsServer()
  125. {
  126. $this->SmtpTransport->config(['tls' => true]);
  127. $this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
  128. $this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
  129. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
  130. $this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
  131. $this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
  132. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
  133. $this->socket->expects($this->at(5))->method('write')->with("STARTTLS\r\n");
  134. $this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
  135. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
  136. $this->SmtpTransport->connect();
  137. }
  138. /**
  139. * testConnectEhloNoTlsOnRequiredTlsServer method
  140. *
  141. * @expectedException \Cake\Network\Exception\SocketException
  142. * @expectedExceptionMessage SMTP authentication method not allowed, check if SMTP server requires TLS.
  143. * @return void
  144. */
  145. public function testConnectEhloNoTlsOnRequiredTlsServer()
  146. {
  147. $this->SmtpTransport->config(['tls' => false, 'username' => 'user', 'password' => 'pass']);
  148. $this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
  149. $this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
  150. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
  151. $this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
  152. $this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
  153. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
  154. $this->socket->expects($this->at(5))->method('write')->with("AUTH LOGIN\r\n");
  155. $this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
  156. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized authentication type\r\n"));
  157. $this->SmtpTransport->connect();
  158. }
  159. /**
  160. * testConnectHelo method
  161. *
  162. * @return void
  163. */
  164. public function testConnectHelo()
  165. {
  166. $this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
  167. $this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
  168. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
  169. $this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
  170. $this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
  171. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
  172. $this->socket->expects($this->at(5))->method('write')->with("HELO localhost\r\n");
  173. $this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
  174. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250 Accepted\r\n"));
  175. $this->SmtpTransport->connect();
  176. }
  177. /**
  178. * testConnectFail method
  179. *
  180. * @expectedException \Cake\Network\Exception\SocketException
  181. * @expectedExceptionMessage SMTP server did not accept the connection.
  182. * @return void
  183. */
  184. public function testConnectFail()
  185. {
  186. $this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
  187. $this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
  188. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
  189. $this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
  190. $this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
  191. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
  192. $this->socket->expects($this->at(5))->method('write')->with("HELO localhost\r\n");
  193. $this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
  194. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
  195. $this->SmtpTransport->connect();
  196. }
  197. /**
  198. * testAuth method
  199. *
  200. * @return void
  201. */
  202. public function testAuth()
  203. {
  204. $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
  205. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  206. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("334 Login\r\n"));
  207. $this->socket->expects($this->at(3))->method('write')->with("bWFyaw==\r\n");
  208. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
  209. $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("334 Pass\r\n"));
  210. $this->socket->expects($this->at(6))->method('write')->with("c3Rvcnk=\r\n");
  211. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
  212. $this->socket->expects($this->at(8))->method('read')->will($this->returnValue("235 OK\r\n"));
  213. $this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
  214. $this->SmtpTransport->auth();
  215. }
  216. /**
  217. * testAuthNotRecognized method
  218. *
  219. * @expectedException \Cake\Network\Exception\SocketException
  220. * @expectedExceptionMessage AUTH command not recognized or not implemented, SMTP server may not require authentication.
  221. * @return void
  222. */
  223. public function testAuthNotRecognized()
  224. {
  225. $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
  226. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  227. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
  228. $this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
  229. $this->SmtpTransport->auth();
  230. }
  231. /**
  232. * testAuthNotImplemented method
  233. *
  234. * @expectedException \Cake\Network\Exception\SocketException
  235. * @expectedExceptionMessage AUTH command not recognized or not implemented, SMTP server may not require authentication.
  236. * @return void
  237. */
  238. public function testAuthNotImplemented()
  239. {
  240. $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
  241. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  242. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("502 5.3.3 Command not implemented\r\n"));
  243. $this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
  244. $this->SmtpTransport->auth();
  245. }
  246. /**
  247. * testAuthBadSequence method
  248. *
  249. * @expectedException \Cake\Network\Exception\SocketException
  250. * @expectedExceptionMessage SMTP Error: 503 5.5.1 Already authenticated
  251. * @return void
  252. */
  253. public function testAuthBadSequence()
  254. {
  255. $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
  256. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  257. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("503 5.5.1 Already authenticated\r\n"));
  258. $this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
  259. $this->SmtpTransport->auth();
  260. }
  261. /**
  262. * testAuthBadUsername method
  263. *
  264. * @expectedException \Cake\Network\Exception\SocketException
  265. * @expectedExceptionMessage SMTP server did not accept the username.
  266. * @return void
  267. */
  268. public function testAuthBadUsername()
  269. {
  270. $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
  271. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  272. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("334 Login\r\n"));
  273. $this->socket->expects($this->at(3))->method('write')->with("bWFyaw==\r\n");
  274. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
  275. $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
  276. $this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
  277. $this->SmtpTransport->auth();
  278. }
  279. /**
  280. * testAuthBadPassword method
  281. *
  282. * @expectedException \Cake\Network\Exception\SocketException
  283. * @expectedExceptionMessage SMTP server did not accept the password.
  284. * @return void
  285. */
  286. public function testAuthBadPassword()
  287. {
  288. $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
  289. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  290. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("334 Login\r\n"));
  291. $this->socket->expects($this->at(3))->method('write')->with("bWFyaw==\r\n");
  292. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
  293. $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("334 Pass\r\n"));
  294. $this->socket->expects($this->at(6))->method('write')->with("c3Rvcnk=\r\n");
  295. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
  296. $this->socket->expects($this->at(8))->method('read')->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
  297. $this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
  298. $this->SmtpTransport->auth();
  299. }
  300. /**
  301. * testRcpt method
  302. *
  303. * @return void
  304. */
  305. public function testRcpt()
  306. {
  307. $email = new Email();
  308. $email->from('noreply@cakephp.org', 'CakePHP Test');
  309. $email->to('cake@cakephp.org', 'CakePHP');
  310. $email->bcc('phpnut@cakephp.org');
  311. $email->cc(['mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso']);
  312. $this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
  313. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  314. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
  315. $this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
  316. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
  317. $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
  318. $this->socket->expects($this->at(6))->method('write')->with("RCPT TO:<mark@cakephp.org>\r\n");
  319. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
  320. $this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250 OK\r\n"));
  321. $this->socket->expects($this->at(9))->method('write')->with("RCPT TO:<juan@cakephp.org>\r\n");
  322. $this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
  323. $this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 OK\r\n"));
  324. $this->socket->expects($this->at(12))->method('write')->with("RCPT TO:<phpnut@cakephp.org>\r\n");
  325. $this->socket->expects($this->at(13))->method('read')->will($this->returnValue(false));
  326. $this->socket->expects($this->at(14))->method('read')->will($this->returnValue("250 OK\r\n"));
  327. $this->SmtpTransport->sendRcpt($email);
  328. }
  329. /**
  330. * testRcptWithReturnPath method
  331. *
  332. * @return void
  333. */
  334. public function testRcptWithReturnPath()
  335. {
  336. $email = new Email();
  337. $email->from('noreply@cakephp.org', 'CakePHP Test');
  338. $email->to('cake@cakephp.org', 'CakePHP');
  339. $email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
  340. $this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<pleasereply@cakephp.org>\r\n");
  341. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  342. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
  343. $this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
  344. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
  345. $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
  346. $this->SmtpTransport->sendRcpt($email);
  347. }
  348. /**
  349. * testSendData method
  350. *
  351. * @return void
  352. */
  353. public function testSendData()
  354. {
  355. $email = $this->getMock('Cake\Network\Email\Email', ['message']);
  356. $email->from('noreply@cakephp.org', 'CakePHP Test');
  357. $email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
  358. $email->to('cake@cakephp.org', 'CakePHP');
  359. $email->cc(['mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso']);
  360. $email->bcc('phpnut@cakephp.org');
  361. $email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
  362. $email->subject('Testing SMTP');
  363. $date = date(DATE_RFC2822);
  364. $email->setHeaders(['Date' => $date]);
  365. $email->expects($this->once())
  366. ->method('message')
  367. ->will($this->returnValue(['First Line', 'Second Line', '.Third Line', '']));
  368. $data = "From: CakePHP Test <noreply@cakephp.org>\r\n";
  369. $data .= "Return-Path: CakePHP Return <pleasereply@cakephp.org>\r\n";
  370. $data .= "To: CakePHP <cake@cakephp.org>\r\n";
  371. $data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>\r\n";
  372. $data .= "Date: " . $date . "\r\n";
  373. $data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n";
  374. $data .= "Subject: Testing SMTP\r\n";
  375. $data .= "MIME-Version: 1.0\r\n";
  376. $data .= "Content-Type: text/plain; charset=UTF-8\r\n";
  377. $data .= "Content-Transfer-Encoding: 8bit\r\n";
  378. $data .= "\r\n";
  379. $data .= "First Line\r\n";
  380. $data .= "Second Line\r\n";
  381. $data .= "..Third Line\r\n"; // RFC5321 4.5.2.Transparency
  382. $data .= "\r\n";
  383. $data .= "\r\n\r\n.\r\n";
  384. $this->socket->expects($this->at(0))->method('write')->with("DATA\r\n");
  385. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  386. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("354 OK\r\n"));
  387. $this->socket->expects($this->at(3))->method('write')->with($data);
  388. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
  389. $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
  390. $this->SmtpTransport->sendData($email);
  391. }
  392. /**
  393. * testQuit method
  394. *
  395. * @return void
  396. */
  397. public function testQuit()
  398. {
  399. $this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
  400. $this->socket->connected = true;
  401. $this->SmtpTransport->disconnect();
  402. }
  403. /**
  404. * testEmptyConfigArray method
  405. *
  406. * @return void
  407. */
  408. public function testEmptyConfigArray()
  409. {
  410. $this->SmtpTransport->config([
  411. 'client' => 'myhost.com',
  412. 'port' => 666
  413. ]);
  414. $expected = $this->SmtpTransport->config();
  415. $this->assertEquals(666, $expected['port']);
  416. $this->SmtpTransport->config([]);
  417. $result = $this->SmtpTransport->config();
  418. $this->assertEquals($expected, $result);
  419. }
  420. /**
  421. * testGetLastResponse method
  422. *
  423. * @return void
  424. */
  425. public function testGetLastResponse()
  426. {
  427. $this->assertEmpty($this->SmtpTransport->getLastResponse());
  428. $this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
  429. $this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
  430. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
  431. $this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
  432. $this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
  433. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250-PIPELINING\r\n"));
  434. $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250-SIZE 102400000\r\n"));
  435. $this->socket->expects($this->at(6))->method('read')->will($this->returnValue("250-VRFY\r\n"));
  436. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250-ETRN\r\n"));
  437. $this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250-STARTTLS\r\n"));
  438. $this->socket->expects($this->at(9))->method('read')->will($this->returnValue("250-AUTH PLAIN LOGIN\r\n"));
  439. $this->socket->expects($this->at(10))->method('read')->will($this->returnValue("250-AUTH=PLAIN LOGIN\r\n"));
  440. $this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250-ENHANCEDSTATUSCODES\r\n"));
  441. $this->socket->expects($this->at(12))->method('read')->will($this->returnValue("250-8BITMIME\r\n"));
  442. $this->socket->expects($this->at(13))->method('read')->will($this->returnValue("250 DSN\r\n"));
  443. $this->SmtpTransport->connect();
  444. $expected = [
  445. ['code' => '250', 'message' => 'PIPELINING'],
  446. ['code' => '250', 'message' => 'SIZE 102400000'],
  447. ['code' => '250', 'message' => 'VRFY'],
  448. ['code' => '250', 'message' => 'ETRN'],
  449. ['code' => '250', 'message' => 'STARTTLS'],
  450. ['code' => '250', 'message' => 'AUTH PLAIN LOGIN'],
  451. ['code' => '250', 'message' => 'AUTH=PLAIN LOGIN'],
  452. ['code' => '250', 'message' => 'ENHANCEDSTATUSCODES'],
  453. ['code' => '250', 'message' => '8BITMIME'],
  454. ['code' => '250', 'message' => 'DSN']
  455. ];
  456. $result = $this->SmtpTransport->getLastResponse();
  457. $this->assertEquals($expected, $result);
  458. $email = new Email();
  459. $email->from('noreply@cakephp.org', 'CakePHP Test');
  460. $email->to('cake@cakephp.org', 'CakePHP');
  461. $this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
  462. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  463. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
  464. $this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
  465. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
  466. $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
  467. $this->SmtpTransport->sendRcpt($email);
  468. $expected = [
  469. ['code' => '250', 'message' => 'OK'],
  470. ];
  471. $result = $this->SmtpTransport->getLastResponse();
  472. $this->assertEquals($expected, $result);
  473. }
  474. /**
  475. * testBufferResponseLines method
  476. *
  477. * @return void
  478. */
  479. public function testBufferResponseLines()
  480. {
  481. $responseLines = [
  482. '123',
  483. "456\tFOO",
  484. 'FOOBAR',
  485. '250-PIPELINING',
  486. '250-ENHANCEDSTATUSCODES',
  487. '250-8BITMIME',
  488. '250 DSN',
  489. ];
  490. $this->SmtpTransport->bufferResponseLines($responseLines);
  491. $expected = [
  492. ['code' => '123', 'message' => null],
  493. ['code' => '250', 'message' => 'PIPELINING'],
  494. ['code' => '250', 'message' => 'ENHANCEDSTATUSCODES'],
  495. ['code' => '250', 'message' => '8BITMIME'],
  496. ['code' => '250', 'message' => 'DSN']
  497. ];
  498. $result = $this->SmtpTransport->getLastResponse();
  499. $this->assertEquals($expected, $result);
  500. }
  501. /**
  502. * testExplicitConnectAlreadyConnected method
  503. *
  504. * @return void
  505. */
  506. public function testExplicitConnectAlreadyConnected()
  507. {
  508. $this->socket->expects($this->never())->method('connect');
  509. $this->socket->connected = true;
  510. $this->SmtpTransport->connect();
  511. }
  512. /**
  513. * testConnected method
  514. *
  515. * @return void
  516. */
  517. public function testConnected()
  518. {
  519. $this->socket->connected = true;
  520. $this->assertTrue($this->SmtpTransport->connected());
  521. $this->socket->connected = false;
  522. $this->assertFalse($this->SmtpTransport->connected());
  523. }
  524. /**
  525. * testAutoDisconnect method
  526. *
  527. * @return void
  528. */
  529. public function testAutoDisconnect()
  530. {
  531. $this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
  532. $this->socket->expects($this->at(1))->method('disconnect');
  533. $this->socket->connected = true;
  534. unset($this->SmtpTransport);
  535. }
  536. /**
  537. * testExplicitDisconnect method
  538. *
  539. * @return void
  540. */
  541. public function testExplicitDisconnect()
  542. {
  543. $this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
  544. $this->socket->expects($this->at(1))->method('disconnect');
  545. $this->socket->connected = true;
  546. $this->SmtpTransport->disconnect();
  547. }
  548. /**
  549. * testExplicitDisconnectNotConnected method
  550. *
  551. * @return void
  552. */
  553. public function testExplicitDisconnectNotConnected()
  554. {
  555. $callback = function ($arg) {
  556. $this->assertNotEquals("QUIT\r\n", $arg);
  557. };
  558. $this->socket->expects($this->any())->method('write')->will($this->returnCallback($callback));
  559. $this->socket->expects($this->never())->method('disconnect');
  560. $this->SmtpTransport->disconnect();
  561. }
  562. /**
  563. * testKeepAlive method
  564. *
  565. * @return void
  566. */
  567. public function testKeepAlive()
  568. {
  569. $this->SmtpTransport->config(['keepAlive' => true]);
  570. $email = $this->getMock('Cake\Network\Email\Email', ['message']);
  571. $email->from('noreply@cakephp.org', 'CakePHP Test');
  572. $email->to('cake@cakephp.org', 'CakePHP');
  573. $email->expects($this->exactly(2))->method('message')->will($this->returnValue(['First Line']));
  574. $callback = function ($arg) {
  575. $this->assertNotEquals("QUIT\r\n", $arg);
  576. };
  577. $this->socket->expects($this->any())->method('write')->will($this->returnCallback($callback));
  578. $this->socket->expects($this->never())->method('disconnect');
  579. $this->socket->expects($this->at(0))->method('connect')->will($this->returnValue(true));
  580. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  581. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
  582. $this->socket->expects($this->at(3))->method('write')->with("EHLO localhost\r\n");
  583. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
  584. $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
  585. $this->socket->expects($this->at(6))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
  586. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
  587. $this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250 OK\r\n"));
  588. $this->socket->expects($this->at(9))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
  589. $this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
  590. $this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 OK\r\n"));
  591. $this->socket->expects($this->at(12))->method('write')->with("DATA\r\n");
  592. $this->socket->expects($this->at(13))->method('read')->will($this->returnValue(false));
  593. $this->socket->expects($this->at(14))->method('read')->will($this->returnValue("354 OK\r\n"));
  594. $this->socket->expects($this->at(15))->method('write')->with($this->stringContains('First Line'));
  595. $this->socket->expects($this->at(16))->method('read')->will($this->returnValue(false));
  596. $this->socket->expects($this->at(17))->method('read')->will($this->returnValue("250 OK\r\n"));
  597. $this->socket->expects($this->at(18))->method('write')->with("RSET\r\n");
  598. $this->socket->expects($this->at(19))->method('read')->will($this->returnValue(false));
  599. $this->socket->expects($this->at(20))->method('read')->will($this->returnValue("250 OK\r\n"));
  600. $this->socket->expects($this->at(21))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
  601. $this->socket->expects($this->at(22))->method('read')->will($this->returnValue(false));
  602. $this->socket->expects($this->at(23))->method('read')->will($this->returnValue("250 OK\r\n"));
  603. $this->socket->expects($this->at(24))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
  604. $this->socket->expects($this->at(25))->method('read')->will($this->returnValue(false));
  605. $this->socket->expects($this->at(26))->method('read')->will($this->returnValue("250 OK\r\n"));
  606. $this->socket->expects($this->at(27))->method('write')->with("DATA\r\n");
  607. $this->socket->expects($this->at(28))->method('read')->will($this->returnValue(false));
  608. $this->socket->expects($this->at(29))->method('read')->will($this->returnValue("354 OK\r\n"));
  609. $this->socket->expects($this->at(15))->method('write')->with($this->stringContains('First Line'));
  610. $this->socket->expects($this->at(31))->method('read')->will($this->returnValue(false));
  611. $this->socket->expects($this->at(32))->method('read')->will($this->returnValue("250 OK\r\n"));
  612. $this->SmtpTransport->send($email);
  613. $this->socket->connected = true;
  614. $this->SmtpTransport->send($email);
  615. }
  616. /**
  617. * testSendDefaults method
  618. *
  619. * @return void
  620. */
  621. public function testSendDefaults()
  622. {
  623. $email = $this->getMock('Cake\Network\Email\Email', ['message']);
  624. $email->from('noreply@cakephp.org', 'CakePHP Test');
  625. $email->to('cake@cakephp.org', 'CakePHP');
  626. $email->expects($this->once())->method('message')->will($this->returnValue(['First Line']));
  627. $this->socket->expects($this->at(0))->method('connect')->will($this->returnValue(true));
  628. $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
  629. $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
  630. $this->socket->expects($this->at(3))->method('write')->with("EHLO localhost\r\n");
  631. $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
  632. $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
  633. $this->socket->expects($this->at(6))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
  634. $this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
  635. $this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250 OK\r\n"));
  636. $this->socket->expects($this->at(9))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
  637. $this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
  638. $this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 OK\r\n"));
  639. $this->socket->expects($this->at(12))->method('write')->with("DATA\r\n");
  640. $this->socket->expects($this->at(13))->method('read')->will($this->returnValue(false));
  641. $this->socket->expects($this->at(14))->method('read')->will($this->returnValue("354 OK\r\n"));
  642. $this->socket->expects($this->at(15))->method('write')->with($this->stringContains('First Line'));
  643. $this->socket->expects($this->at(16))->method('read')->will($this->returnValue(false));
  644. $this->socket->expects($this->at(17))->method('read')->will($this->returnValue("250 OK\r\n"));
  645. $this->socket->expects($this->at(18))->method('write')->with("QUIT\r\n");
  646. $this->socket->expects($this->at(19))->method('disconnect');
  647. $this->SmtpTransport->send($email);
  648. }
  649. }