Skip to content

Commit 07d9a75

Browse files
committed
Helpers in custom CakeErrorController are lost
Since many exceptions do not have its own 'template' file, customized APP/Controller/CakeErrorController with its own list of helpers could be ignored. This happens becase ExceptionRenderer is forced to to use _outputMessageSafe when a template is missing. This causes Controller::$helpers to be reset with default values.
1 parent 483d712 commit 07d9a75

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

lib/Cake/Error/ExceptionRenderer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ protected function _outputMessage($template) {
263263
$this->controller->render($template);
264264
$this->controller->afterFilter();
265265
$this->controller->response->send();
266+
} catch (MissingViewException $e) {
267+
try {
268+
$this->_outputMessage('error500');
269+
} catch (Exception $e) {
270+
$this->_outputMessageSafe('error500');
271+
}
266272
} catch (Exception $e) {
267273
$this->_outputMessageSafe('error500');
268274
}

lib/Cake/Test/Case/Error/ExceptionRendererTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,49 @@ public function testErrorMethodCoercion() {
277277
$this->assertEquals($exception, $ExceptionRenderer->error);
278278
}
279279

280+
/**
281+
* test that helpers in custom CakeErrorController are not lost
282+
*/
283+
public function testCakeErrorHelpersNotLost() {
284+
$testApp = CAKE . 'Test' . DS . 'test_app' . DS;
285+
App::build(array(
286+
'Controller' => array(
287+
$testApp . 'Controller' . DS
288+
),
289+
'View/Helper' => array(
290+
$testApp . 'View' . DS . 'Helper' . DS
291+
),
292+
'View/Layouts' => array(
293+
$testApp . 'View' . DS . 'Layouts' . DS
294+
),
295+
'Error' => array(
296+
$testApp . 'Error' . DS
297+
),
298+
), App::RESET);
299+
Configure::write('Error', array(
300+
'handler' => 'TestAppsErrorHandler::handleError',
301+
'level' => E_ALL & ~E_DEPRECATED,
302+
'trace' => true
303+
));
304+
305+
Configure::write('Exception', array(
306+
'handler' => 'TestAppsErrorHandler::handleException',
307+
'renderer' => 'TestAppsExceptionRenderer',
308+
'log' => true
309+
));
310+
311+
App::uses('TestAppsErrorController', 'Controller');
312+
App::uses('TestAppsExceptionRenderer', 'Error');
313+
314+
$exception = new SocketException('socket exception');
315+
$renderer = new TestAppsExceptionRenderer($exception);
316+
317+
ob_start();
318+
$renderer->render();
319+
$result = ob_get_clean();
320+
$this->assertContains('<b>peeled</b>', $result);
321+
}
322+
280323
/**
281324
* test that unknown exception types with valid status codes are treated correctly.
282325
*
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
App::uses('CakeErrorController', 'Controller');
4+
5+
class TestAppsErrorController extends CakeErrorController {
6+
7+
public $helpers = array(
8+
'Html',
9+
'Session',
10+
'Form',
11+
'Banana',
12+
);
13+
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
class TestAppsExceptionRenderer extends ExceptionRenderer {
4+
5+
protected function _getController($exception) {
6+
App::uses('TestAppsErrorController', 'Controller');
7+
if (!$request = Router::getRequest(true)) {
8+
$request = new CakeRequest();
9+
}
10+
$response = new CakeResponse(array('charset' => Configure::read('App.encoding')));
11+
try {
12+
$controller = new TestAppsErrorController($request, $response);
13+
$controller->layout = 'banana';
14+
} catch (Exception $e) {
15+
$controller = new Controller($request, $response);
16+
$controller->viewPath = 'Errors';
17+
}
18+
return $controller;
19+
}
20+
21+
}

lib/Cake/Test/test_app/View/Helper/BananaHelper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@
1919
App::uses('Helper', 'View');
2020

2121
class BananaHelper extends Helper {
22+
23+
public function peel() {
24+
return '<b>peeled</b>';
25+
}
26+
2227
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<body>
2+
<?php
3+
echo $this->Banana->peel();
4+
?>
5+
</body>

0 commit comments

Comments
 (0)