SmtpTransportTest.php 31 KB

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