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