Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 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
 * @return void
134
 */
135
	public function testConnectEhloTlsOnNonTlsServer() {
136
		$this->SmtpTransport->config(array('tls' => true));
137
		$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
138
		$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
139
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
140
		$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
141
		$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
142
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
143
		$this->socket->expects($this->at(5))->method('write')->with("STARTTLS\r\n");
144
		$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
145
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
146
		$this->SmtpTransport->connect();
147
	}
148
 
149
/**
150
 * testConnectEhloNoTlsOnRequiredTlsServer method
151
 *
152
 * @expectedException SocketException
153
 * @return void
154
 */
155
	public function testConnectEhloNoTlsOnRequiredTlsServer() {
156
		$this->SmtpTransport->config(array('tls' => false, 'username' => 'user', 'password' => 'pass'));
157
		$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
158
		$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
159
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
160
		$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
161
		$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
162
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
163
		$this->socket->expects($this->at(5))->method('write')->with("AUTH LOGIN\r\n");
164
		$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
165
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized authentication type\r\n"));
166
		$this->SmtpTransport->connect();
167
		$this->SmtpTransport->auth();
168
	}
169
 
170
/**
171
 * testConnectHelo method
172
 *
173
 * @return void
174
 */
175
	public function testConnectHelo() {
176
		$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
177
		$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
178
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
179
		$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
180
		$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
181
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
182
		$this->socket->expects($this->at(5))->method('write')->with("HELO localhost\r\n");
183
		$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
184
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250 Accepted\r\n"));
185
		$this->SmtpTransport->connect();
186
	}
187
 
188
/**
189
 * testConnectFail method
190
 *
191
 * @expectedException SocketException
192
 * @return void
193
 */
194
	public function testConnectFail() {
195
		$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
196
		$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
197
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
198
		$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
199
		$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
200
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
201
		$this->socket->expects($this->at(5))->method('write')->with("HELO localhost\r\n");
202
		$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
203
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
204
		$this->SmtpTransport->connect();
205
	}
206
 
207
/**
208
 * testAuth method
209
 *
210
 * @return void
211
 */
212
	public function testAuth() {
213
		$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
214
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
215
		$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("334 Login\r\n"));
216
		$this->socket->expects($this->at(3))->method('write')->with("bWFyaw==\r\n");
217
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
218
		$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("334 Pass\r\n"));
219
		$this->socket->expects($this->at(6))->method('write')->with("c3Rvcnk=\r\n");
220
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
221
		$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("235 OK\r\n"));
222
		$this->SmtpTransport->config(array('username' => 'mark', 'password' => 'story'));
223
		$this->SmtpTransport->auth();
224
	}
225
 
226
/**
227
 * testAuthNoAuth method
228
 *
229
 * @return void
230
 */
231
	public function testAuthNoAuth() {
232
		$this->socket->expects($this->any())->method('write')->with($this->logicalNot($this->stringContains('AUTH LOGIN')));
233
 
234
		$this->SmtpTransport->config(array('username' => null, 'password' => null));
235
		$this->SmtpTransport->auth();
236
	}
237
 
238
/**
239
 * testRcpt method
240
 *
241
 * @return void
242
 */
243
	public function testRcpt() {
244
		$email = new CakeEmail();
245
		$email->from('noreply@cakephp.org', 'CakePHP Test');
246
		$email->to('cake@cakephp.org', 'CakePHP');
247
		$email->bcc('phpnut@cakephp.org');
248
		$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
249
 
250
		$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
251
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
252
		$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
253
		$this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
254
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
255
		$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
256
		$this->socket->expects($this->at(6))->method('write')->with("RCPT TO:<mark@cakephp.org>\r\n");
257
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
258
		$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250 OK\r\n"));
259
		$this->socket->expects($this->at(9))->method('write')->with("RCPT TO:<juan@cakephp.org>\r\n");
260
		$this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
261
		$this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 OK\r\n"));
262
		$this->socket->expects($this->at(12))->method('write')->with("RCPT TO:<phpnut@cakephp.org>\r\n");
263
		$this->socket->expects($this->at(13))->method('read')->will($this->returnValue(false));
264
		$this->socket->expects($this->at(14))->method('read')->will($this->returnValue("250 OK\r\n"));
265
 
266
		$this->SmtpTransport->setCakeEmail($email);
267
		$this->SmtpTransport->sendRcpt();
268
	}
269
 
270
/**
271
 * testRcptWithReturnPath method
272
 *
273
 * @return void
274
 */
275
	public function testRcptWithReturnPath() {
276
		$email = new CakeEmail();
277
		$email->from('noreply@cakephp.org', 'CakePHP Test');
278
		$email->to('cake@cakephp.org', 'CakePHP');
279
		$email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
280
 
281
		$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<pleasereply@cakephp.org>\r\n");
282
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
283
		$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
284
		$this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
285
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
286
		$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
287
 
288
		$this->SmtpTransport->setCakeEmail($email);
289
		$this->SmtpTransport->sendRcpt();
290
	}
291
 
292
/**
293
 * testSendData method
294
 *
295
 * @return void
296
 */
297
	public function testSendData() {
298
		$email = $this->getMock('CakeEmail', array('message'), array(), 'SmtpCakeEmail');
299
		$email->from('noreply@cakephp.org', 'CakePHP Test');
300
		$email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
301
		$email->to('cake@cakephp.org', 'CakePHP');
302
		$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
303
		$email->bcc('phpnut@cakephp.org');
304
		$email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
305
		$email->subject('Testing SMTP');
306
		$date = date(DATE_RFC2822);
307
		$email->setHeaders(array('X-Mailer' => SmtpCakeEmail::EMAIL_CLIENT, 'Date' => $date));
308
		$email->expects($this->once())->method('message')->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', '')));
309
 
310
		$data = "From: CakePHP Test <noreply@cakephp.org>\r\n";
311
		$data .= "To: CakePHP <cake@cakephp.org>\r\n";
312
		$data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>\r\n";
313
		$data .= "X-Mailer: CakePHP Email\r\n";
314
		$data .= "Date: " . $date . "\r\n";
315
		$data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n";
316
		$data .= "Subject: Testing SMTP\r\n";
317
		$data .= "MIME-Version: 1.0\r\n";
318
		$data .= "Content-Type: text/plain; charset=UTF-8\r\n";
319
		$data .= "Content-Transfer-Encoding: 8bit\r\n";
320
		$data .= "\r\n";
321
		$data .= "First Line\r\n";
322
		$data .= "Second Line\r\n";
323
		$data .= "..Third Line\r\n"; // RFC5321 4.5.2.Transparency
324
		$data .= "\r\n";
325
		$data .= "\r\n\r\n.\r\n";
326
 
327
		$this->socket->expects($this->at(0))->method('write')->with("DATA\r\n");
328
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
329
		$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("354 OK\r\n"));
330
		$this->socket->expects($this->at(3))->method('write')->with($data);
331
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
332
		$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
333
 
334
		$this->SmtpTransport->setCakeEmail($email);
335
		$this->SmtpTransport->sendData();
336
	}
337
 
338
/**
339
 * testQuit method
340
 *
341
 * @return void
342
 */
343
	public function testQuit() {
344
		$this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
345
		$this->SmtpTransport->disconnect();
346
	}
347
 
348
/**
349
 * testEmptyConfigArray method
350
 *
351
 * @return void
352
 */
353
	public function testEmptyConfigArray() {
354
		$expected = $this->SmtpTransport->config(array(
355
			'client' => 'myhost.com',
356
			'port' => 666
357
		));
358
 
359
		$this->assertEquals(666, $expected['port']);
360
 
361
		$result = $this->SmtpTransport->config(array());
362
		$this->assertEquals($expected, $result);
363
	}
364
 
365
/**
366
 * testGetLastResponse method
367
 *
368
 * @return void
369
 */
370
	public function testGetLastResponse() {
371
		$this->assertEmpty($this->SmtpTransport->getLastResponse());
372
 
373
		$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
374
		$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
375
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
376
		$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
377
		$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
378
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250-PIPELINING\r\n"));
379
		$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250-SIZE 102400000\r\n"));
380
		$this->socket->expects($this->at(6))->method('read')->will($this->returnValue("250-VRFY\r\n"));
381
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250-ETRN\r\n"));
382
		$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250-STARTTLS\r\n"));
383
		$this->socket->expects($this->at(9))->method('read')->will($this->returnValue("250-AUTH PLAIN LOGIN\r\n"));
384
		$this->socket->expects($this->at(10))->method('read')->will($this->returnValue("250-AUTH=PLAIN LOGIN\r\n"));
385
		$this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250-ENHANCEDSTATUSCODES\r\n"));
386
		$this->socket->expects($this->at(12))->method('read')->will($this->returnValue("250-8BITMIME\r\n"));
387
		$this->socket->expects($this->at(13))->method('read')->will($this->returnValue("250 DSN\r\n"));
388
		$this->SmtpTransport->connect();
389
 
390
		$expected = array(
391
			array('code' => '250', 'message' => 'PIPELINING'),
392
			array('code' => '250', 'message' => 'SIZE 102400000'),
393
			array('code' => '250', 'message' => 'VRFY'),
394
			array('code' => '250', 'message' => 'ETRN'),
395
			array('code' => '250', 'message' => 'STARTTLS'),
396
			array('code' => '250', 'message' => 'AUTH PLAIN LOGIN'),
397
			array('code' => '250', 'message' => 'AUTH=PLAIN LOGIN'),
398
			array('code' => '250', 'message' => 'ENHANCEDSTATUSCODES'),
399
			array('code' => '250', 'message' => '8BITMIME'),
400
			array('code' => '250', 'message' => 'DSN')
401
		);
402
		$result = $this->SmtpTransport->getLastResponse();
403
		$this->assertEquals($expected, $result);
404
 
405
		$email = new CakeEmail();
406
		$email->from('noreply@cakephp.org', 'CakePHP Test');
407
		$email->to('cake@cakephp.org', 'CakePHP');
408
 
409
		$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
410
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
411
		$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
412
		$this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
413
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
414
		$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
415
 
416
		$this->SmtpTransport->setCakeEmail($email);
417
		$this->SmtpTransport->sendRcpt();
418
 
419
		$expected = array(
420
			array('code' => '250', 'message' => 'OK'),
421
		);
422
		$result = $this->SmtpTransport->getLastResponse();
423
		$this->assertEquals($expected, $result);
424
	}
425
 
426
/**
427
 * testBufferResponseLines method
428
 *
429
 * @return void
430
 */
431
	public function testBufferResponseLines() {
432
		$reponseLines = array(
433
			'123',
434
			"456\tFOO",
435
			'FOOBAR',
436
			'250-PIPELINING',
437
			'250-ENHANCEDSTATUSCODES',
438
			'250-8BITMIME',
439
			'250 DSN',
440
		);
441
		$this->SmtpTransport->bufferResponseLines($reponseLines);
442
 
443
		$expected = array(
444
			array('code' => '123', 'message' => null),
445
			array('code' => '250', 'message' => 'PIPELINING'),
446
			array('code' => '250', 'message' => 'ENHANCEDSTATUSCODES'),
447
			array('code' => '250', 'message' => '8BITMIME'),
448
			array('code' => '250', 'message' => 'DSN')
449
		);
450
		$result = $this->SmtpTransport->getLastResponse();
451
		$this->assertEquals($expected, $result);
452
	}
453
}