Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 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 $this->$method();
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
		if (!class_exists('MockSocket')) {
85
			$this->getMock('CakeSocket', array('read', 'write', 'connect', 'enableCrypto'), array(), 'MockSocket');
86
		}
87
		$this->socket = new MockSocket();
88
 
89
		$this->SmtpTransport = new SmtpTestTransport();
90
		$this->SmtpTransport->setSocket($this->socket);
91
		$this->SmtpTransport->config(array('client' => 'localhost'));
92
	}
93
 
94
/**
95
 * testConnectEhlo method
96
 *
97
 * @return void
98
 */
99
	public function testConnectEhlo() {
100
		$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
101
		$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
102
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
103
		$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
104
		$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
105
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
106
		$this->SmtpTransport->connect();
107
	}
108
 
109
/**
110
 * testConnectEhloTls method
111
 *
112
 * @return void
113
 */
114
	public function testConnectEhloTls() {
115
		$this->SmtpTransport->config(array('tls' => true));
116
		$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
117
		$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
118
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
119
		$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
120
		$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
121
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
122
		$this->socket->expects($this->at(5))->method('write')->with("STARTTLS\r\n");
123
		$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
124
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("220 Server ready\r\n"));
125
		$this->socket->expects($this->at(8))->method('other')->with('tls')->will($this->returnValue(true));
126
		$this->socket->expects($this->at(9))->method('write')->with("EHLO localhost\r\n");
127
		$this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
128
		$this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 Accepted\r\n"));
129
		$this->SmtpTransport->connect();
130
	}
131
 
132
/**
133
 * testConnectEhloTlsOnNonTlsServer method
134
 *
135
 * @expectedException SocketException
136
 * @return void
137
 */
138
	public function testConnectEhloTlsOnNonTlsServer() {
139
		$this->SmtpTransport->config(array('tls' => true));
140
		$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
141
		$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
142
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
143
		$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
144
		$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
145
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
146
		$this->socket->expects($this->at(5))->method('write')->with("STARTTLS\r\n");
147
		$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
148
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
149
		$this->SmtpTransport->connect();
150
	}
151
 
152
/**
153
 * testConnectEhloNoTlsOnRequiredTlsServer method
154
 *
155
 * @expectedException SocketException
156
 * @return void
157
 */
158
	public function testConnectEhloNoTlsOnRequiredTlsServer() {
159
		$this->SmtpTransport->config(array('tls' => false, 'username' => 'user', 'password' => 'pass'));
160
		$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
161
		$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
162
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
163
		$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
164
		$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
165
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("250 Accepted\r\n"));
166
		$this->socket->expects($this->at(5))->method('read')->with("AUTH LOGIN\r\n");
167
		$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
168
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized authentication type\r\n"));
169
		$this->SmtpTransport->connect();
170
		$this->SmtpTransport->auth();
171
	}
172
 
173
/**
174
 * testConnectHelo method
175
 *
176
 * @return void
177
 */
178
	public function testConnectHelo() {
179
		$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
180
		$this->socket->expects($this->at(0))->method('read')->will($this->returnValue(false));
181
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
182
		$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
183
		$this->socket->expects($this->at(3))->method('read')->will($this->returnValue(false));
184
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
185
		$this->socket->expects($this->at(5))->method('write')->with("HELO localhost\r\n");
186
		$this->socket->expects($this->at(6))->method('read')->will($this->returnValue(false));
187
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250 Accepted\r\n"));
188
		$this->SmtpTransport->connect();
189
	}
190
 
191
/**
192
 * testConnectFail method
193
 *
194
 * @expectedException SocketException
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
 * testAuthNoAuth method
231
 *
232
 * @return void
233
 */
234
	public function testAuthNoAuth() {
235
		$this->socket->expects($this->never())->method('write')->with("AUTH LOGIN\r\n");
236
		$this->SmtpTransport->config(array('username' => null, 'password' => null));
237
		$this->SmtpTransport->auth();
238
	}
239
 
240
/**
241
 * testRcpt method
242
 *
243
 * @return void
244
 */
245
	public function testRcpt() {
246
		$email = new CakeEmail();
247
		$email->from('noreply@cakephp.org', 'CakePHP Test');
248
		$email->to('cake@cakephp.org', 'CakePHP');
249
		$email->bcc('phpnut@cakephp.org');
250
		$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
251
 
252
		$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\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("250 OK\r\n"));
255
		$this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
256
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
257
		$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
258
		$this->socket->expects($this->at(6))->method('write')->with("RCPT TO:<mark@cakephp.org>\r\n");
259
		$this->socket->expects($this->at(7))->method('read')->will($this->returnValue(false));
260
		$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250 OK\r\n"));
261
		$this->socket->expects($this->at(9))->method('write')->with("RCPT TO:<juan@cakephp.org>\r\n");
262
		$this->socket->expects($this->at(10))->method('read')->will($this->returnValue(false));
263
		$this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 OK\r\n"));
264
		$this->socket->expects($this->at(12))->method('write')->with("RCPT TO:<phpnut@cakephp.org>\r\n");
265
		$this->socket->expects($this->at(13))->method('read')->will($this->returnValue(false));
266
		$this->socket->expects($this->at(14))->method('read')->will($this->returnValue("250 OK\r\n"));
267
 
268
		$this->SmtpTransport->setCakeEmail($email);
269
		$this->SmtpTransport->sendRcpt();
270
	}
271
 
272
/**
273
 * testRcptWithReturnPath method
274
 *
275
 * @return void
276
 */
277
	public function testRcptWithReturnPath() {
278
		$email = new CakeEmail();
279
		$email->from('noreply@cakephp.org', 'CakePHP Test');
280
		$email->to('cake@cakephp.org', 'CakePHP');
281
		$email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
282
 
283
		$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<pleasereply@cakephp.org>\r\n");
284
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
285
		$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n"));
286
		$this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
287
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
288
		$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
289
 
290
		$this->SmtpTransport->setCakeEmail($email);
291
		$this->SmtpTransport->sendRcpt();
292
	}
293
 
294
/**
295
 * testSendData method
296
 *
297
 * @return void
298
 */
299
	public function testSendData() {
300
		$this->getMock('CakeEmail', array('message'), array(), 'SmtpCakeEmail');
301
		$email = new SmtpCakeEmail();
302
		$email->from('noreply@cakephp.org', 'CakePHP Test');
303
		$email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
304
		$email->to('cake@cakephp.org', 'CakePHP');
305
		$email->cc(array('mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso'));
306
		$email->bcc('phpnut@cakephp.org');
307
		$email->messageID('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
308
		$email->subject('Testing SMTP');
309
		$date = date(DATE_RFC2822);
310
		$email->setHeaders(array('X-Mailer' => SmtpCakeEmail::EMAIL_CLIENT, 'Date' => $date));
311
		$email->expects($this->any())->method('message')->will($this->returnValue(array('First Line', 'Second Line', '.Third Line', '')));
312
 
313
		$data = "From: CakePHP Test <noreply@cakephp.org>\r\n";
314
		$data .= "To: CakePHP <cake@cakephp.org>\r\n";
315
		$data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>\r\n";
316
		$data .= "X-Mailer: CakePHP Email\r\n";
317
		$data .= "Date: " . $date . "\r\n";
318
		$data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n";
319
		$data .= "Subject: Testing SMTP\r\n";
320
		$data .= "MIME-Version: 1.0\r\n";
321
		$data .= "Content-Type: text/plain; charset=UTF-8\r\n";
322
		$data .= "Content-Transfer-Encoding: 8bit\r\n";
323
		$data .= "\r\n";
324
		$data .= "First Line\r\n";
325
		$data .= "Second Line\r\n";
326
		$data .= "..Third Line\r\n"; // RFC5321 4.5.2.Transparency
327
		$data .= "\r\n";
328
		$data .= "\r\n\r\n.\r\n";
329
 
330
		$this->socket->expects($this->at(0))->method('write')->with("DATA\r\n");
331
		$this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false));
332
		$this->socket->expects($this->at(2))->method('read')->will($this->returnValue("354 OK\r\n"));
333
		$this->socket->expects($this->at(3))->method('write')->with($data);
334
		$this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false));
335
		$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
336
 
337
		$this->SmtpTransport->setCakeEmail($email);
338
		$this->SmtpTransport->sendData();
339
	}
340
 
341
/**
342
 * testQuit method
343
 *
344
 * @return void
345
 */
346
	public function testQuit() {
347
		$this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
348
		$this->SmtpTransport->disconnect();
349
	}
350
 
351
/**
352
 * testEmptyConfigArray method
353
 *
354
 * @return void
355
 */
356
	public function testEmptyConfigArray() {
357
		$expected = $this->SmtpTransport->config(array(
358
			'client' => 'myhost.com',
359
			'port' => 666
360
		));
361
 
362
		$this->assertEquals(666, $expected['port']);
363
 
364
		$result = $this->SmtpTransport->config(array());
365
		$this->assertEquals($expected, $result);
366
	}
367
 
368
}