3
* ExceptionRendererTest file
7
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
8
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice
13
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
15
* @package Cake.Test.Case.Error
16
* @since CakePHP(tm) v 2.0
17
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20
App::uses('ExceptionRenderer', 'Error');
21
App::uses('Controller', 'Controller');
22
App::uses('AppController', 'Controller');
23
App::uses('Component', 'Controller');
24
App::uses('Router', 'Routing');
27
* Short description for class.
29
* @package Cake.Test.Case.Error
31
class AuthBlueberryUser extends CakeTestModel {
36
* @var string 'AuthBlueberryUser'
38
public $name = 'AuthBlueberryUser';
45
public $useTable = false;
49
* BlueberryComponent class
51
* @package Cake.Test.Case.Error
53
class BlueberryComponent extends Component {
60
public $testName = null;
67
public function initialize(Controller $controller) {
68
$this->testName = 'BlueberryComponent';
74
* TestErrorController class
76
* @package Cake.Test.Case.Error
78
class TestErrorController extends Controller {
85
public $uses = array();
92
public $components = array('Blueberry');
99
public function beforeRender() {
100
echo $this->Blueberry->testName;
108
public function index() {
109
$this->autoRender = false;
116
* MyCustomExceptionRenderer class
118
* @package Cake.Test.Case.Error
120
class MyCustomExceptionRenderer extends ExceptionRenderer {
123
* custom error message type.
127
public function missingWidgetThing() {
128
echo 'widget thing is missing';
134
* Exception class for testing app error handlers and custom errors.
136
* @package Cake.Test.Case.Error
138
class MissingWidgetThingException extends NotFoundException {
143
* ExceptionRendererTest class
145
* @package Cake.Test.Case.Error
147
class ExceptionRendererTest extends CakeTestCase {
149
protected $_restoreError = false;
152
* setup create a request object to get out of router later.
156
public function setUp() {
160
CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
165
$request = new CakeRequest(null, false);
167
Router::setRequestInfo($request);
168
Configure::write('debug', 2);
176
public function tearDown() {
178
if ($this->_restoreError) {
179
restore_error_handler();
184
* Mocks out the response on the ExceptionRenderer object so headers aren't modified.
188
protected function _mockResponse($error) {
189
$error->controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
194
* test that methods declared in an ExceptionRenderer subclass are not converted
195
* into error400 when debug > 0
199
public function testSubclassMethodsNotBeingConvertedToError() {
200
Configure::write('debug', 2);
202
$exception = new MissingWidgetThingException('Widget not found');
203
$ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
206
$ExceptionRenderer->render();
207
$result = ob_get_clean();
209
$this->assertEquals('widget thing is missing', $result);
213
* test that subclass methods are not converted when debug = 0
217
public function testSubclassMethodsNotBeingConvertedDebug0() {
218
Configure::write('debug', 0);
219
$exception = new MissingWidgetThingException('Widget not found');
220
$ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
222
$this->assertEquals('missingWidgetThing', $ExceptionRenderer->method);
225
$ExceptionRenderer->render();
226
$result = ob_get_clean();
228
$this->assertEquals('widget thing is missing', $result, 'Method declared in subclass converted to error400');
232
* test that ExceptionRenderer subclasses properly convert framework errors.
236
public function testSubclassConvertingFrameworkErrors() {
237
Configure::write('debug', 0);
239
$exception = new MissingControllerException('PostsController');
240
$ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
242
$this->assertEquals('error400', $ExceptionRenderer->method);
245
$ExceptionRenderer->render();
246
$result = ob_get_clean();
248
$this->assertRegExp('/Not Found/', $result, 'Method declared in error handler not converted to error400. %s');
252
* test things in the constructor.
256
public function testConstruction() {
257
$exception = new NotFoundException('Page not found');
258
$ExceptionRenderer = new ExceptionRenderer($exception);
260
$this->assertInstanceOf('CakeErrorController', $ExceptionRenderer->controller);
261
$this->assertEquals('error400', $ExceptionRenderer->method);
262
$this->assertEquals($exception, $ExceptionRenderer->error);
266
* test that method gets coerced when debug = 0
270
public function testErrorMethodCoercion() {
271
Configure::write('debug', 0);
272
$exception = new MissingActionException('Page not found');
273
$ExceptionRenderer = new ExceptionRenderer($exception);
275
$this->assertInstanceOf('CakeErrorController', $ExceptionRenderer->controller);
276
$this->assertEquals('error400', $ExceptionRenderer->method);
277
$this->assertEquals($exception, $ExceptionRenderer->error);
281
* test that helpers in custom CakeErrorController are not lost
283
public function testCakeErrorHelpersNotLost() {
284
$testApp = CAKE . 'Test' . DS . 'test_app' . DS;
286
'Controller' => array(
287
$testApp . 'Controller' . DS
289
'View/Helper' => array(
290
$testApp . 'View' . DS . 'Helper' . DS
292
'View/Layouts' => array(
293
$testApp . 'View' . DS . 'Layouts' . DS
296
$testApp . 'Error' . DS
300
App::uses('TestAppsExceptionRenderer', 'Error');
301
$exception = new SocketException('socket exception');
302
$renderer = new TestAppsExceptionRenderer($exception);
306
$result = ob_get_clean();
307
$this->assertContains('<b>peeled</b>', $result);
311
* test that unknown exception types with valid status codes are treated correctly.
315
public function testUnknownExceptionTypeWithExceptionThatHasA400Code() {
316
$exception = new MissingWidgetThingException('coding fail.');
317
$ExceptionRenderer = new ExceptionRenderer($exception);
318
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
319
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(404);
322
$ExceptionRenderer->render();
323
$result = ob_get_clean();
325
$this->assertFalse(method_exists($ExceptionRenderer, 'missingWidgetThing'), 'no method should exist.');
326
$this->assertEquals('error400', $ExceptionRenderer->method, 'incorrect method coercion.');
327
$this->assertContains('coding fail', $result, 'Text should show up.');
331
* test that unknown exception types with valid status codes are treated correctly.
335
public function testUnknownExceptionTypeWithNoCodeIsA500() {
336
$exception = new OutOfBoundsException('foul ball.');
337
$ExceptionRenderer = new ExceptionRenderer($exception);
338
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
339
$ExceptionRenderer->controller->response->expects($this->once())
340
->method('statusCode')
344
$ExceptionRenderer->render();
345
$result = ob_get_clean();
347
$this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
348
$this->assertContains('foul ball.', $result, 'Text should show up as its debug mode.');
352
* test that unknown exceptions have messages ignored.
356
public function testUnknownExceptionInProduction() {
357
Configure::write('debug', 0);
359
$exception = new OutOfBoundsException('foul ball.');
360
$ExceptionRenderer = new ExceptionRenderer($exception);
361
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
362
$ExceptionRenderer->controller->response->expects($this->once())
363
->method('statusCode')
367
$ExceptionRenderer->render();
368
$result = ob_get_clean();
370
$this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
371
$this->assertNotContains('foul ball.', $result, 'Text should no show up.');
372
$this->assertContains('Internal Error', $result, 'Generic message only.');
376
* test that unknown exception types with valid status codes are treated correctly.
380
public function testUnknownExceptionTypeWithCodeHigherThan500() {
381
$exception = new OutOfBoundsException('foul ball.', 501);
382
$ExceptionRenderer = new ExceptionRenderer($exception);
383
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
384
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(501);
387
$ExceptionRenderer->render();
388
$result = ob_get_clean();
390
$this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
391
$this->assertContains('foul ball.', $result, 'Text should show up as its debug mode.');
395
* testerror400 method
399
public function testError400() {
402
$request = new CakeRequest('posts/view/1000', false);
403
Router::setRequestInfo($request);
405
$exception = new NotFoundException('Custom message');
406
$ExceptionRenderer = new ExceptionRenderer($exception);
407
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
408
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(404);
411
$ExceptionRenderer->render();
412
$result = ob_get_clean();
414
$this->assertRegExp('/<h2>Custom message<\/h2>/', $result);
415
$this->assertRegExp("/<strong>'.*?\/posts\/view\/1000'<\/strong>/", $result);
419
* test that error400 only modifies the messages on CakeExceptions.
423
public function testerror400OnlyChangingCakeException() {
424
Configure::write('debug', 0);
426
$exception = new NotFoundException('Custom message');
427
$ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
430
$ExceptionRenderer->render();
431
$result = ob_get_clean();
432
$this->assertContains('Custom message', $result);
434
$exception = new MissingActionException(array('controller' => 'PostsController', 'action' => 'index'));
435
$ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
438
$ExceptionRenderer->render();
439
$result = ob_get_clean();
440
$this->assertContains('Not Found', $result);
444
* test that error400 doesn't expose XSS
448
public function testError400NoInjection() {
451
$request = new CakeRequest('pages/<span id=333>pink</span></id><script>document.body.style.background = t=document.getElementById(333).innerHTML;window.alert(t);</script>', false);
452
Router::setRequestInfo($request);
454
$exception = new NotFoundException('Custom message');
455
$ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
458
$ExceptionRenderer->render();
459
$result = ob_get_clean();
461
$this->assertNotRegExp('#<script>document#', $result);
462
$this->assertNotRegExp('#alert\(t\);</script>#', $result);
466
* testError500 method
470
public function testError500Message() {
471
$exception = new InternalErrorException('An Internal Error Has Occurred');
472
$ExceptionRenderer = new ExceptionRenderer($exception);
473
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
474
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(500);
477
$ExceptionRenderer->render();
478
$result = ob_get_clean();
480
$this->assertRegExp('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
484
* testMissingController method
488
public function testMissingController() {
489
$exception = new MissingControllerException(array('class' => 'PostsController'));
490
$ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
493
$ExceptionRenderer->render();
494
$result = ob_get_clean();
496
$this->assertRegExp('/<h2>Missing Controller<\/h2>/', $result);
497
$this->assertRegExp('/<em>PostsController<\/em>/', $result);
501
* Returns an array of tests to run for the various CakeException classes.
505
public static function testProvider() {
508
new MissingActionException(array('controller' => 'PostsController', 'action' => 'index')),
510
'/<h2>Missing Method in PostsController<\/h2>/',
511
'/<em>PostsController::<\/em><em>index\(\)<\/em>/'
516
new PrivateActionException(array('controller' => 'PostsController' , 'action' => '_secretSauce')),
518
'/<h2>Private Method in PostsController<\/h2>/',
519
'/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/'
524
new MissingTableException(array('table' => 'articles', 'class' => 'Article', 'ds' => 'test')),
526
'/<h2>Missing Database Table<\/h2>/',
527
'/Table <em>articles<\/em> for model <em>Article<\/em> was not found in datasource <em>test<\/em>/'
532
new MissingDatabaseException(array('connection' => 'default')),
534
'/<h2>Missing Database Connection<\/h2>/',
535
'/Confirm you have created the file/'
540
new MissingViewException(array('file' => '/posts/about.ctp')),
547
new MissingLayoutException(array('file' => 'layouts/my_layout.ctp')),
550
"/layouts\/my_layout.ctp/"
555
new MissingConnectionException(array('class' => 'Article')),
557
'/<h2>Missing Database Connection<\/h2>/',
558
'/Article requires a database connection/'
563
new MissingConnectionException(array('class' => 'Mysql', 'enabled' => false)),
565
'/<h2>Missing Database Connection<\/h2>/',
566
'/Mysql requires a database connection/',
567
'/Mysql driver is NOT enabled/'
572
new MissingDatasourceConfigException(array('config' => 'default')),
574
'/<h2>Missing Datasource Configuration<\/h2>/',
575
'/The datasource configuration <em>default<\/em> was not found in database.php/'
580
new MissingDatasourceException(array('class' => 'MyDatasource', 'plugin' => 'MyPlugin')),
582
'/<h2>Missing Datasource<\/h2>/',
583
'/Datasource class <em>MyPlugin.MyDatasource<\/em> could not be found/'
588
new MissingHelperException(array('class' => 'MyCustomHelper')),
590
'/<h2>Missing Helper<\/h2>/',
591
'/<em>MyCustomHelper<\/em> could not be found./',
592
'/Create the class <em>MyCustomHelper<\/em> below in file:/',
593
'/(\/|\\\)MyCustomHelper.php/'
598
new MissingBehaviorException(array('class' => 'MyCustomBehavior')),
600
'/<h2>Missing Behavior<\/h2>/',
601
'/Create the class <em>MyCustomBehavior<\/em> below in file:/',
602
'/(\/|\\\)MyCustomBehavior.php/'
607
new MissingComponentException(array('class' => 'SideboxComponent')),
609
'/<h2>Missing Component<\/h2>/',
610
'/Create the class <em>SideboxComponent<\/em> below in file:/',
611
'/(\/|\\\)SideboxComponent.php/'
616
new Exception('boom'),
623
new RuntimeException('another boom'),
630
new CakeException('base class'),
631
array('/Internal Error/'),
635
new ConfigureException('No file'),
636
array('/Internal Error/'),
643
* Test the various CakeException sub classes
645
* @dataProvider testProvider
648
public function testCakeExceptionHandling($exception, $patterns, $code) {
649
$ExceptionRenderer = new ExceptionRenderer($exception);
650
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
651
$ExceptionRenderer->controller->response->expects($this->once())
652
->method('statusCode')
656
$ExceptionRenderer->render();
657
$result = ob_get_clean();
659
foreach ($patterns as $pattern) {
660
$this->assertRegExp($pattern, $result);
665
* Test exceptions being raised when helpers are missing.
669
public function testMissingRenderSafe() {
670
$exception = new MissingHelperException(array('class' => 'Fail'));
671
$ExceptionRenderer = new ExceptionRenderer($exception);
673
$ExceptionRenderer->controller = $this->getMock('Controller');
674
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
675
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
676
$ExceptionRenderer->controller->expects($this->at(2))
678
->with('missingHelper')
679
->will($this->throwException($exception));
681
$ExceptionRenderer->controller->expects($this->at(4))
684
->will($this->returnValue(true));
686
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse');
687
$ExceptionRenderer->render();
688
sort($ExceptionRenderer->controller->helpers);
689
$this->assertEquals(array('Form', 'Html', 'Session'), $ExceptionRenderer->controller->helpers);
693
* Test that missing subDir/layoutPath don't cause other fatal errors.
697
public function testMissingSubdirRenderSafe() {
698
$exception = new NotFoundException();
699
$ExceptionRenderer = new ExceptionRenderer($exception);
701
$ExceptionRenderer->controller = $this->getMock('Controller');
702
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
703
$ExceptionRenderer->controller->layoutPath = 'json';
704
$ExceptionRenderer->controller->subDir = 'json';
705
$ExceptionRenderer->controller->viewClass = 'Json';
706
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
708
$ExceptionRenderer->controller->expects($this->at(1))
711
->will($this->throwException($exception));
713
$ExceptionRenderer->controller->expects($this->at(3))
716
->will($this->returnValue(true));
718
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse');
719
$ExceptionRenderer->controller->response->expects($this->once())
723
$ExceptionRenderer->render();
724
$this->assertEquals('', $ExceptionRenderer->controller->layoutPath);
725
$this->assertEquals('', $ExceptionRenderer->controller->subDir);
726
$this->assertEquals('View', $ExceptionRenderer->controller->viewClass);
727
$this->assertEquals('Errors/', $ExceptionRenderer->controller->viewPath);
731
* Test that exceptions can be rendered when an request hasn't been registered
736
public function testRenderWithNoRequest() {
738
$this->assertNull(Router::getRequest(false));
740
$exception = new Exception('Terrible');
741
$ExceptionRenderer = new ExceptionRenderer($exception);
742
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
743
$ExceptionRenderer->controller->response->expects($this->once())
744
->method('statusCode')
748
$ExceptionRenderer->render();
749
$result = ob_get_clean();
751
$this->assertContains('Internal Error', $result);
755
* Tests the output of rendering a PDOException
759
public function testPDOException() {
760
$exception = new PDOException('There was an error in the SQL query');
761
$exception->queryString = 'SELECT * from poo_query < 5 and :seven';
762
$exception->params = array('seven' => 7);
763
$ExceptionRenderer = new ExceptionRenderer($exception);
764
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
765
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(500);
768
$ExceptionRenderer->render();
769
$result = ob_get_clean();
771
$this->assertContains('<h2>Database Error</h2>', $result);
772
$this->assertContains('There was an error in the SQL query', $result);
773
$this->assertContains('SELECT * from poo_query < 5 and :seven', $result);
774
$this->assertContains("'seven' => (int) 7", $result);