Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 1
<?php
2
/**
3
 * ErrorHandlerTest 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.Error
15
 * @since         CakePHP(tm) v 1.2.0.5432
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('ErrorHandler', 'Error');
20
App::uses('Controller', 'Controller');
21
App::uses('Router', 'Routing');
22
 
23
/**
24
 * A faulty ExceptionRenderer to test nesting.
25
 */
26
class FaultyExceptionRenderer extends ExceptionRenderer {
27
 
28
/**
29
 * Dummy rendering implementation.
30
 *
31
 * @return void
32
 * @throws Exception
33
 */
34
	public function render() {
35
		throw new Exception('Error from renderer.');
36
	}
37
 
38
}
39
 
40
/**
41
 * ErrorHandlerTest class
42
 *
43
 * @package       Cake.Test.Case.Error
44
 */
45
class ErrorHandlerTest extends CakeTestCase {
46
 
47
	protected $_restoreError = false;
48
 
49
/**
50
 * setup create a request object to get out of router later.
51
 *
52
 * @return void
53
 */
54
	public function setUp() {
55
		parent::setUp();
56
		App::build(array(
57
			'View' => array(
58
				CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
59
			)
60
		), App::RESET);
61
		Router::reload();
62
 
63
		$request = new CakeRequest(null, false);
64
		$request->base = '';
65
		Router::setRequestInfo($request);
66
		Configure::write('debug', 2);
67
 
68
		CakeLog::disable('stdout');
69
		CakeLog::disable('stderr');
70
	}
71
 
72
/**
73
 * tearDown
74
 *
75
 * @return void
76
 */
77
	public function tearDown() {
78
		parent::tearDown();
79
		if ($this->_restoreError) {
80
			restore_error_handler();
81
		}
82
		CakeLog::enable('stdout');
83
		CakeLog::enable('stderr');
84
	}
85
 
86
/**
87
 * test error handling when debug is on, an error should be printed from Debugger.
88
 *
89
 * @return void
90
 */
91
	public function testHandleErrorDebugOn() {
92
		set_error_handler('ErrorHandler::handleError');
93
		$this->_restoreError = true;
94
 
95
		ob_start();
96
		$wrong .= '';
97
		$result = ob_get_clean();
98
 
99
		$this->assertRegExp('/<pre class="cake-error">/', $result);
100
		$this->assertRegExp('/<b>Notice<\/b>/', $result);
101
		$this->assertRegExp('/variable:\s+wrong/', $result);
102
	}
103
 
104
/**
105
 * provides errors for mapping tests.
106
 *
107
 * @return void
108
 */
109
	public static function errorProvider() {
110
		return array(
111
			array(E_USER_NOTICE, 'Notice'),
112
			array(E_USER_WARNING, 'Warning'),
113
		);
114
	}
115
 
116
/**
117
 * test error mappings
118
 *
119
 * @dataProvider errorProvider
120
 * @return void
121
 */
122
	public function testErrorMapping($error, $expected) {
123
		set_error_handler('ErrorHandler::handleError');
124
		$this->_restoreError = true;
125
 
126
		ob_start();
127
		trigger_error('Test error', $error);
128
 
129
		$result = ob_get_clean();
130
		$this->assertRegExp('/<b>' . $expected . '<\/b>/', $result);
131
	}
132
 
133
/**
134
 * test error prepended by @
135
 *
136
 * @return void
137
 */
138
	public function testErrorSuppressed() {
139
		set_error_handler('ErrorHandler::handleError');
140
		$this->_restoreError = true;
141
 
142
		ob_start();
143
		//@codingStandardsIgnoreStart
144
		@include 'invalid.file';
145
		//@codingStandardsIgnoreEnd
146
		$result = ob_get_clean();
147
		$this->assertTrue(empty($result));
148
	}
149
 
150
/**
151
 * Test that errors go into CakeLog when debug = 0.
152
 *
153
 * @return void
154
 */
155
	public function testHandleErrorDebugOff() {
156
		Configure::write('debug', 0);
157
		Configure::write('Error.trace', false);
158
		if (file_exists(LOGS . 'debug.log')) {
159
			unlink(LOGS . 'debug.log');
160
		}
161
 
162
		set_error_handler('ErrorHandler::handleError');
163
		$this->_restoreError = true;
164
 
165
		$out .= '';
166
 
167
		$result = file(LOGS . 'debug.log');
168
		$this->assertEquals(1, count($result));
169
		$this->assertRegExp(
170
			'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
171
			$result[0]
172
		);
173
		if (file_exists(LOGS . 'debug.log')) {
174
			unlink(LOGS . 'debug.log');
175
		}
176
	}
177
 
178
/**
179
 * Test that errors going into CakeLog include traces.
180
 *
181
 * @return void
182
 */
183
	public function testHandleErrorLoggingTrace() {
184
		Configure::write('debug', 0);
185
		Configure::write('Error.trace', true);
186
		if (file_exists(LOGS . 'debug.log')) {
187
			unlink(LOGS . 'debug.log');
188
		}
189
 
190
		set_error_handler('ErrorHandler::handleError');
191
		$this->_restoreError = true;
192
 
193
		$out .= '';
194
 
195
		$result = file(LOGS . 'debug.log');
196
		$this->assertRegExp(
197
			'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
198
			$result[0]
199
		);
200
		$this->assertRegExp('/^Trace:/', $result[1]);
201
		$this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
202
		if (file_exists(LOGS . 'debug.log')) {
203
			unlink(LOGS . 'debug.log');
204
		}
205
	}
206
 
207
/**
208
 * test handleException generating a page.
209
 *
210
 * @return void
211
 */
212
	public function testHandleException() {
213
		$error = new NotFoundException('Kaboom!');
214
		ob_start();
215
		ErrorHandler::handleException($error);
216
		$result = ob_get_clean();
217
		$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
218
	}
219
 
220
/**
221
 * test handleException generating log.
222
 *
223
 * @return void
224
 */
225
	public function testHandleExceptionLog() {
226
		if (file_exists(LOGS . 'error.log')) {
227
			unlink(LOGS . 'error.log');
228
		}
229
		Configure::write('Exception.log', true);
230
		$error = new NotFoundException('Kaboom!');
231
 
232
		ob_start();
233
		ErrorHandler::handleException($error);
234
		$result = ob_get_clean();
235
		$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
236
 
237
		$log = file(LOGS . 'error.log');
238
		$this->assertContains('[NotFoundException] Kaboom!', $log[0], 'message missing.');
239
		$this->assertContains('ErrorHandlerTest->testHandleExceptionLog', $log[2], 'Stack trace missing.');
240
	}
241
 
242
/**
243
 * test handleException generating log.
244
 *
245
 * @return void
246
 */
247
	public function testHandleExceptionLogSkipping() {
248
		if (file_exists(LOGS . 'error.log')) {
249
			unlink(LOGS . 'error.log');
250
		}
251
		Configure::write('Exception.log', true);
252
		Configure::write('Exception.skipLog', array('NotFoundException'));
253
		$notFound = new NotFoundException('Kaboom!');
254
		$forbidden = new ForbiddenException('Fooled you!');
255
 
256
		ob_start();
257
		ErrorHandler::handleException($notFound);
258
		$result = ob_get_clean();
259
		$this->assertRegExp('/Kaboom!/', $result, 'message missing.');
260
 
261
		ob_start();
262
		ErrorHandler::handleException($forbidden);
263
		$result = ob_get_clean();
264
		$this->assertRegExp('/Fooled you!/', $result, 'message missing.');
265
 
266
		$log = file(LOGS . 'error.log');
267
		$this->assertNotContains('[NotFoundException] Kaboom!', $log[0], 'message should not be logged.');
268
		$this->assertContains('[ForbiddenException] Fooled you!', $log[0], 'message missing.');
269
	}
270
 
271
/**
272
 * tests it is possible to load a plugin exception renderer
273
 *
274
 * @return void
275
 */
276
	public function testLoadPluginHandler() {
277
		App::build(array(
278
			'Plugin' => array(
279
				CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
280
			)
281
		), App::RESET);
282
		CakePlugin::load('TestPlugin');
283
		Configure::write('Exception.renderer', 'TestPlugin.TestPluginExceptionRenderer');
284
		$error = new NotFoundException('Kaboom!');
285
		ob_start();
286
		ErrorHandler::handleException($error);
287
		$result = ob_get_clean();
288
		$this->assertEquals('Rendered by test plugin', $result);
289
		CakePlugin::unload();
290
	}
291
 
292
/**
293
 * test handleFatalError generating a page.
294
 *
295
 * These tests start two buffers as handleFatalError blows the outer one up.
296
 *
297
 * @return void
298
 */
299
	public function testHandleFatalErrorPage() {
300
		$line = __LINE__;
301
 
302
		ob_start();
303
		ob_start();
304
		Configure::write('debug', 1);
305
		ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
306
		$result = ob_get_clean();
307
		$this->assertContains('Something wrong', $result, 'message missing.');
308
		$this->assertContains(__FILE__, $result, 'filename missing.');
309
		$this->assertContains((string)$line, $result, 'line missing.');
310
 
311
		ob_start();
312
		ob_start();
313
		Configure::write('debug', 0);
314
		ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
315
		$result = ob_get_clean();
316
		$this->assertNotContains('Something wrong', $result, 'message must not appear.');
317
		$this->assertNotContains(__FILE__, $result, 'filename must not appear.');
318
		$this->assertContains('An Internal Error Has Occurred', $result);
319
	}
320
 
321
/**
322
 * test handleException generating log.
323
 *
324
 * @return void
325
 */
326
	public function testHandleFatalErrorLog() {
327
		if (file_exists(LOGS . 'error.log')) {
328
			unlink(LOGS . 'error.log');
329
		}
330
 
331
		ob_start();
332
		ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
333
		ob_clean();
334
 
335
		$log = file(LOGS . 'error.log');
336
		$this->assertContains(__FILE__, $log[0], 'missing filename');
337
		$this->assertContains('[FatalErrorException] Something wrong', $log[1], 'message missing.');
338
	}
339
 
340
/**
341
 * testExceptionRendererNestingDebug method
342
 *
343
 * @return void
344
 */
345
	public function testExceptionRendererNestingDebug() {
346
		Configure::write('debug', 2);
347
		Configure::write('Exception.renderer', 'FaultyExceptionRenderer');
348
 
349
		$result = false;
350
		try {
351
			ob_start();
352
			ob_start();
353
			ErrorHandler::handleFatalError(E_USER_ERROR, 'Initial error', __FILE__, __LINE__);
354
		} catch (Exception $e) {
355
			$result = $e instanceof FatalErrorException;
356
		}
357
 
358
		restore_error_handler();
359
		$this->assertTrue($result);
360
	}
361
 
362
/**
363
 * testExceptionRendererNestingProduction method
364
 *
365
 * @return void
366
 */
367
	public function testExceptionRendererNestingProduction() {
368
		Configure::write('debug', 0);
369
		Configure::write('Exception.renderer', 'FaultyExceptionRenderer');
370
 
371
		$result = false;
372
		try {
373
			ob_start();
374
			ob_start();
375
			ErrorHandler::handleFatalError(E_USER_ERROR, 'Initial error', __FILE__, __LINE__);
376
		} catch (Exception $e) {
377
			$result = $e instanceof InternalErrorException;
378
		}
379
 
380
		restore_error_handler();
381
		$this->assertTrue($result);
382
	}
383
 
384
}