ExceptionRendererTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. /**
  3. * ExceptionRendererTest file
  4. *
  5. * PHP 5
  6. *
  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)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  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)
  18. */
  19. App::uses('ExceptionRenderer', 'Error');
  20. App::uses('Controller', 'Controller');
  21. App::uses('AppController', 'Controller');
  22. App::uses('Component', 'Controller');
  23. App::uses('Router', 'Routing');
  24. /**
  25. * Short description for class.
  26. *
  27. * @package Cake.Test.Case.Error
  28. */
  29. class AuthBlueberryUser extends CakeTestModel {
  30. /**
  31. * name property
  32. *
  33. * @var string 'AuthBlueberryUser'
  34. */
  35. public $name = 'AuthBlueberryUser';
  36. /**
  37. * useTable property
  38. *
  39. * @var string
  40. */
  41. public $useTable = false;
  42. }
  43. /**
  44. * BlueberryComponent class
  45. *
  46. * @package Cake.Test.Case.Error
  47. */
  48. class BlueberryComponent extends Component {
  49. /**
  50. * testName property
  51. *
  52. * @return void
  53. */
  54. public $testName = null;
  55. /**
  56. * initialize method
  57. *
  58. * @return void
  59. */
  60. public function initialize(Controller $controller) {
  61. $this->testName = 'BlueberryComponent';
  62. }
  63. }
  64. /**
  65. * TestErrorController class
  66. *
  67. * @package Cake.Test.Case.Error
  68. */
  69. class TestErrorController extends Controller {
  70. /**
  71. * uses property
  72. *
  73. * @var array
  74. */
  75. public $uses = array();
  76. /**
  77. * components property
  78. *
  79. * @return void
  80. */
  81. public $components = array('Blueberry');
  82. /**
  83. * beforeRender method
  84. *
  85. * @return void
  86. */
  87. public function beforeRender() {
  88. echo $this->Blueberry->testName;
  89. }
  90. /**
  91. * index method
  92. *
  93. * @return void
  94. */
  95. public function index() {
  96. $this->autoRender = false;
  97. return 'what up';
  98. }
  99. }
  100. /**
  101. * MyCustomExceptionRenderer class
  102. *
  103. * @package Cake.Test.Case.Error
  104. */
  105. class MyCustomExceptionRenderer extends ExceptionRenderer {
  106. /**
  107. * custom error message type.
  108. *
  109. * @return void
  110. */
  111. public function missingWidgetThing() {
  112. echo 'widget thing is missing';
  113. }
  114. }
  115. /**
  116. * Exception class for testing app error handlers and custom errors.
  117. *
  118. * @package Cake.Test.Case.Error
  119. */
  120. class MissingWidgetThingException extends NotFoundException {
  121. }
  122. /**
  123. * ExceptionRendererTest class
  124. *
  125. * @package Cake.Test.Case.Error
  126. */
  127. class ExceptionRendererTest extends CakeTestCase {
  128. protected $_restoreError = false;
  129. /**
  130. * setup create a request object to get out of router later.
  131. *
  132. * @return void
  133. */
  134. public function setUp() {
  135. parent::setUp();
  136. App::build(array(
  137. 'View' => array(
  138. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
  139. )
  140. ), App::RESET);
  141. Router::reload();
  142. $request = new CakeRequest(null, false);
  143. $request->base = '';
  144. Router::setRequestInfo($request);
  145. Configure::write('debug', 2);
  146. }
  147. /**
  148. * tearDown
  149. *
  150. * @return void
  151. */
  152. public function tearDown() {
  153. parent::tearDown();
  154. if ($this->_restoreError) {
  155. restore_error_handler();
  156. }
  157. }
  158. /**
  159. * Mocks out the response on the ExceptionRenderer object so headers aren't modified.
  160. *
  161. * @return void
  162. */
  163. protected function _mockResponse($error) {
  164. $error->controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  165. return $error;
  166. }
  167. /**
  168. * test that methods declared in an ExceptionRenderer subclass are not converted
  169. * into error400 when debug > 0
  170. *
  171. * @return void
  172. */
  173. public function testSubclassMethodsNotBeingConvertedToError() {
  174. Configure::write('debug', 2);
  175. $exception = new MissingWidgetThingException('Widget not found');
  176. $ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
  177. ob_start();
  178. $ExceptionRenderer->render();
  179. $result = ob_get_clean();
  180. $this->assertEquals('widget thing is missing', $result);
  181. }
  182. /**
  183. * test that subclass methods are not converted when debug = 0
  184. *
  185. * @return void
  186. */
  187. public function testSubclassMethodsNotBeingConvertedDebug0() {
  188. Configure::write('debug', 0);
  189. $exception = new MissingWidgetThingException('Widget not found');
  190. $ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
  191. $this->assertEquals('missingWidgetThing', $ExceptionRenderer->method);
  192. ob_start();
  193. $ExceptionRenderer->render();
  194. $result = ob_get_clean();
  195. $this->assertEquals('widget thing is missing', $result, 'Method declared in subclass converted to error400');
  196. }
  197. /**
  198. * test that ExceptionRenderer subclasses properly convert framework errors.
  199. *
  200. * @return void
  201. */
  202. public function testSubclassConvertingFrameworkErrors() {
  203. Configure::write('debug', 0);
  204. $exception = new MissingControllerException('PostsController');
  205. $ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
  206. $this->assertEquals('error400', $ExceptionRenderer->method);
  207. ob_start();
  208. $ExceptionRenderer->render();
  209. $result = ob_get_clean();
  210. $this->assertRegExp('/Not Found/', $result, 'Method declared in error handler not converted to error400. %s');
  211. }
  212. /**
  213. * test things in the constructor.
  214. *
  215. * @return void
  216. */
  217. public function testConstruction() {
  218. $exception = new NotFoundException('Page not found');
  219. $ExceptionRenderer = new ExceptionRenderer($exception);
  220. $this->assertInstanceOf('CakeErrorController', $ExceptionRenderer->controller);
  221. $this->assertEquals('error400', $ExceptionRenderer->method);
  222. $this->assertEquals($exception, $ExceptionRenderer->error);
  223. }
  224. /**
  225. * test that method gets coerced when debug = 0
  226. *
  227. * @return void
  228. */
  229. public function testErrorMethodCoercion() {
  230. Configure::write('debug', 0);
  231. $exception = new MissingActionException('Page not found');
  232. $ExceptionRenderer = new ExceptionRenderer($exception);
  233. $this->assertInstanceOf('CakeErrorController', $ExceptionRenderer->controller);
  234. $this->assertEquals('error400', $ExceptionRenderer->method);
  235. $this->assertEquals($exception, $ExceptionRenderer->error);
  236. }
  237. /**
  238. * test that helpers in custom CakeErrorController are not lost
  239. */
  240. public function testCakeErrorHelpersNotLost() {
  241. $testApp = CAKE . 'Test' . DS . 'test_app' . DS;
  242. App::build(array(
  243. 'Controller' => array(
  244. $testApp . 'Controller' . DS
  245. ),
  246. 'View/Helper' => array(
  247. $testApp . 'View' . DS . 'Helper' . DS
  248. ),
  249. 'View/Layouts' => array(
  250. $testApp . 'View' . DS . 'Layouts' . DS
  251. ),
  252. 'Error' => array(
  253. $testApp . 'Error' . DS
  254. ),
  255. ), App::RESET);
  256. App::uses('TestAppsExceptionRenderer', 'Error');
  257. $exception = new SocketException('socket exception');
  258. $renderer = new TestAppsExceptionRenderer($exception);
  259. ob_start();
  260. $renderer->render();
  261. $result = ob_get_clean();
  262. $this->assertContains('<b>peeled</b>', $result);
  263. }
  264. /**
  265. * test that unknown exception types with valid status codes are treated correctly.
  266. *
  267. * @return void
  268. */
  269. public function testUnknownExceptionTypeWithExceptionThatHasA400Code() {
  270. $exception = new MissingWidgetThingException('coding fail.');
  271. $ExceptionRenderer = new ExceptionRenderer($exception);
  272. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  273. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(404);
  274. ob_start();
  275. $ExceptionRenderer->render();
  276. $result = ob_get_clean();
  277. $this->assertFalse(method_exists($ExceptionRenderer, 'missingWidgetThing'), 'no method should exist.');
  278. $this->assertEquals('error400', $ExceptionRenderer->method, 'incorrect method coercion.');
  279. $this->assertContains('coding fail', $result, 'Text should show up.');
  280. }
  281. /**
  282. * test that unknown exception types with valid status codes are treated correctly.
  283. *
  284. * @return void
  285. */
  286. public function testUnknownExceptionTypeWithNoCodeIsA500() {
  287. $exception = new OutOfBoundsException('foul ball.');
  288. $ExceptionRenderer = new ExceptionRenderer($exception);
  289. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  290. $ExceptionRenderer->controller->response->expects($this->once())
  291. ->method('statusCode')
  292. ->with(500);
  293. ob_start();
  294. $ExceptionRenderer->render();
  295. $result = ob_get_clean();
  296. $this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
  297. $this->assertContains('foul ball.', $result, 'Text should show up as its debug mode.');
  298. }
  299. /**
  300. * test that unknown exceptions have messages ignored.
  301. *
  302. * @return void
  303. */
  304. public function testUnknownExceptionInProduction() {
  305. Configure::write('debug', 0);
  306. $exception = new OutOfBoundsException('foul ball.');
  307. $ExceptionRenderer = new ExceptionRenderer($exception);
  308. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  309. $ExceptionRenderer->controller->response->expects($this->once())
  310. ->method('statusCode')
  311. ->with(500);
  312. ob_start();
  313. $ExceptionRenderer->render();
  314. $result = ob_get_clean();
  315. $this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
  316. $this->assertNotContains('foul ball.', $result, 'Text should no show up.');
  317. $this->assertContains('Internal Error', $result, 'Generic message only.');
  318. }
  319. /**
  320. * test that unknown exception types with valid status codes are treated correctly.
  321. *
  322. * @return void
  323. */
  324. public function testUnknownExceptionTypeWithCodeHigherThan500() {
  325. $exception = new OutOfBoundsException('foul ball.', 501);
  326. $ExceptionRenderer = new ExceptionRenderer($exception);
  327. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  328. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(501);
  329. ob_start();
  330. $ExceptionRenderer->render();
  331. $result = ob_get_clean();
  332. $this->assertEquals('error500', $ExceptionRenderer->method, 'incorrect method coercion.');
  333. $this->assertContains('foul ball.', $result, 'Text should show up as its debug mode.');
  334. }
  335. /**
  336. * testerror400 method
  337. *
  338. * @return void
  339. */
  340. public function testError400() {
  341. Router::reload();
  342. $request = new CakeRequest('posts/view/1000', false);
  343. Router::setRequestInfo($request);
  344. $exception = new NotFoundException('Custom message');
  345. $ExceptionRenderer = new ExceptionRenderer($exception);
  346. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  347. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(404);
  348. ob_start();
  349. $ExceptionRenderer->render();
  350. $result = ob_get_clean();
  351. $this->assertRegExp('/<h2>Custom message<\/h2>/', $result);
  352. $this->assertRegExp("/<strong>'.*?\/posts\/view\/1000'<\/strong>/", $result);
  353. }
  354. /**
  355. * test that error400 only modifies the messages on CakeExceptions.
  356. *
  357. * @return void
  358. */
  359. public function testerror400OnlyChangingCakeException() {
  360. Configure::write('debug', 0);
  361. $exception = new NotFoundException('Custom message');
  362. $ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
  363. ob_start();
  364. $ExceptionRenderer->render();
  365. $result = ob_get_clean();
  366. $this->assertContains('Custom message', $result);
  367. $exception = new MissingActionException(array('controller' => 'PostsController', 'action' => 'index'));
  368. $ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
  369. ob_start();
  370. $ExceptionRenderer->render();
  371. $result = ob_get_clean();
  372. $this->assertContains('Not Found', $result);
  373. }
  374. /**
  375. * test that error400 doesn't expose XSS
  376. *
  377. * @return void
  378. */
  379. public function testError400NoInjection() {
  380. Router::reload();
  381. $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);
  382. Router::setRequestInfo($request);
  383. $exception = new NotFoundException('Custom message');
  384. $ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
  385. ob_start();
  386. $ExceptionRenderer->render();
  387. $result = ob_get_clean();
  388. $this->assertNotRegExp('#<script>document#', $result);
  389. $this->assertNotRegExp('#alert\(t\);</script>#', $result);
  390. }
  391. /**
  392. * testError500 method
  393. *
  394. * @return void
  395. */
  396. public function testError500Message() {
  397. $exception = new InternalErrorException('An Internal Error Has Occurred');
  398. $ExceptionRenderer = new ExceptionRenderer($exception);
  399. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  400. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(500);
  401. ob_start();
  402. $ExceptionRenderer->render();
  403. $result = ob_get_clean();
  404. $this->assertRegExp('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
  405. }
  406. /**
  407. * testMissingController method
  408. *
  409. * @return void
  410. */
  411. public function testMissingController() {
  412. $exception = new MissingControllerException(array('class' => 'PostsController'));
  413. $ExceptionRenderer = $this->_mockResponse(new ExceptionRenderer($exception));
  414. ob_start();
  415. $ExceptionRenderer->render();
  416. $result = ob_get_clean();
  417. $this->assertRegExp('/<h2>Missing Controller<\/h2>/', $result);
  418. $this->assertRegExp('/<em>PostsController<\/em>/', $result);
  419. }
  420. /**
  421. * Returns an array of tests to run for the various CakeException classes.
  422. *
  423. * @return void
  424. */
  425. public static function testProvider() {
  426. return array(
  427. array(
  428. new MissingActionException(array('controller' => 'PostsController', 'action' => 'index')),
  429. array(
  430. '/<h2>Missing Method in PostsController<\/h2>/',
  431. '/<em>PostsController::<\/em><em>index\(\)<\/em>/'
  432. ),
  433. 404
  434. ),
  435. array(
  436. new PrivateActionException(array('controller' => 'PostsController' , 'action' => '_secretSauce')),
  437. array(
  438. '/<h2>Private Method in PostsController<\/h2>/',
  439. '/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/'
  440. ),
  441. 404
  442. ),
  443. array(
  444. new MissingTableException(array('table' => 'articles', 'class' => 'Article', 'ds' => 'test')),
  445. array(
  446. '/<h2>Missing Database Table<\/h2>/',
  447. '/Table <em>articles<\/em> for model <em>Article<\/em> was not found in datasource <em>test<\/em>/'
  448. ),
  449. 500
  450. ),
  451. array(
  452. new MissingDatabaseException(array('connection' => 'default')),
  453. array(
  454. '/<h2>Missing Database Connection<\/h2>/',
  455. '/Confirm you have created the file/'
  456. ),
  457. 500
  458. ),
  459. array(
  460. new MissingViewException(array('file' => '/posts/about.ctp')),
  461. array(
  462. "/posts\/about.ctp/"
  463. ),
  464. 500
  465. ),
  466. array(
  467. new MissingLayoutException(array('file' => 'layouts/my_layout.ctp')),
  468. array(
  469. "/Missing Layout/",
  470. "/layouts\/my_layout.ctp/"
  471. ),
  472. 500
  473. ),
  474. array(
  475. new MissingConnectionException(array('class' => 'Article')),
  476. array(
  477. '/<h2>Missing Database Connection<\/h2>/',
  478. '/Article requires a database connection/'
  479. ),
  480. 500
  481. ),
  482. array(
  483. new MissingDatasourceConfigException(array('config' => 'default')),
  484. array(
  485. '/<h2>Missing Datasource Configuration<\/h2>/',
  486. '/The datasource configuration <em>default<\/em> was not found in database.php/'
  487. ),
  488. 500
  489. ),
  490. array(
  491. new MissingDatasourceException(array('class' => 'MyDatasource', 'plugin' => 'MyPlugin')),
  492. array(
  493. '/<h2>Missing Datasource<\/h2>/',
  494. '/Datasource class <em>MyPlugin.MyDatasource<\/em> could not be found/'
  495. ),
  496. 500
  497. ),
  498. array(
  499. new MissingHelperException(array('class' => 'MyCustomHelper')),
  500. array(
  501. '/<h2>Missing Helper<\/h2>/',
  502. '/<em>MyCustomHelper<\/em> could not be found./',
  503. '/Create the class <em>MyCustomHelper<\/em> below in file:/',
  504. '/(\/|\\\)MyCustomHelper.php/'
  505. ),
  506. 500
  507. ),
  508. array(
  509. new MissingBehaviorException(array('class' => 'MyCustomBehavior')),
  510. array(
  511. '/<h2>Missing Behavior<\/h2>/',
  512. '/Create the class <em>MyCustomBehavior<\/em> below in file:/',
  513. '/(\/|\\\)MyCustomBehavior.php/'
  514. ),
  515. 500
  516. ),
  517. array(
  518. new MissingComponentException(array('class' => 'SideboxComponent')),
  519. array(
  520. '/<h2>Missing Component<\/h2>/',
  521. '/Create the class <em>SideboxComponent<\/em> below in file:/',
  522. '/(\/|\\\)SideboxComponent.php/'
  523. ),
  524. 500
  525. ),
  526. array(
  527. new Exception('boom'),
  528. array(
  529. '/Internal Error/'
  530. ),
  531. 500
  532. ),
  533. array(
  534. new RuntimeException('another boom'),
  535. array(
  536. '/Internal Error/'
  537. ),
  538. 500
  539. ),
  540. array(
  541. new CakeException('base class'),
  542. array('/Internal Error/'),
  543. 500
  544. ),
  545. array(
  546. new ConfigureException('No file'),
  547. array('/Internal Error/'),
  548. 500
  549. )
  550. );
  551. }
  552. /**
  553. * Test the various CakeException sub classes
  554. *
  555. * @dataProvider testProvider
  556. * @return void
  557. */
  558. public function testCakeExceptionHandling($exception, $patterns, $code) {
  559. $ExceptionRenderer = new ExceptionRenderer($exception);
  560. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  561. $ExceptionRenderer->controller->response->expects($this->once())
  562. ->method('statusCode')
  563. ->with($code);
  564. ob_start();
  565. $ExceptionRenderer->render();
  566. $result = ob_get_clean();
  567. foreach ($patterns as $pattern) {
  568. $this->assertRegExp($pattern, $result);
  569. }
  570. }
  571. /**
  572. * Test exceptions being raised when helpers are missing.
  573. *
  574. * @return void
  575. */
  576. public function testMissingRenderSafe() {
  577. $exception = new MissingHelperException(array('class' => 'Fail'));
  578. $ExceptionRenderer = new ExceptionRenderer($exception);
  579. $ExceptionRenderer->controller = $this->getMock('Controller');
  580. $ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
  581. $ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
  582. $ExceptionRenderer->controller->expects($this->at(2))
  583. ->method('render')
  584. ->with('missingHelper')
  585. ->will($this->throwException($exception));
  586. $ExceptionRenderer->controller->expects($this->at(4))
  587. ->method('render')
  588. ->with('error500')
  589. ->will($this->returnValue(true));
  590. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse');
  591. $ExceptionRenderer->render();
  592. sort($ExceptionRenderer->controller->helpers);
  593. $this->assertEquals(array('Form', 'Html', 'Session'), $ExceptionRenderer->controller->helpers);
  594. }
  595. /**
  596. * Test that missing subDir/layoutPath don't cause other fatal errors.
  597. *
  598. * @return void
  599. */
  600. public function testMissingSubdirRenderSafe() {
  601. $exception = new NotFoundException();
  602. $ExceptionRenderer = new ExceptionRenderer($exception);
  603. $ExceptionRenderer->controller = $this->getMock('Controller');
  604. $ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
  605. $ExceptionRenderer->controller->layoutPath = 'json';
  606. $ExceptionRenderer->controller->subDir = 'json';
  607. $ExceptionRenderer->controller->viewClass = 'Json';
  608. $ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
  609. $ExceptionRenderer->controller->expects($this->at(1))
  610. ->method('render')
  611. ->with('error400')
  612. ->will($this->throwException($exception));
  613. $ExceptionRenderer->controller->expects($this->at(3))
  614. ->method('render')
  615. ->with('error500')
  616. ->will($this->returnValue(true));
  617. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse');
  618. $ExceptionRenderer->controller->response->expects($this->once())
  619. ->method('type')
  620. ->with('html');
  621. $ExceptionRenderer->render();
  622. $this->assertEquals('', $ExceptionRenderer->controller->layoutPath);
  623. $this->assertEquals('', $ExceptionRenderer->controller->subDir);
  624. $this->assertEquals('View', $ExceptionRenderer->controller->viewClass);
  625. $this->assertEquals('Errors/', $ExceptionRenderer->controller->viewPath);
  626. }
  627. /**
  628. * Test that exceptions can be rendered when an request hasn't been registered
  629. * with Router
  630. *
  631. * @return void
  632. */
  633. public function testRenderWithNoRequest() {
  634. Router::reload();
  635. $this->assertNull(Router::getRequest(false));
  636. $exception = new Exception('Terrible');
  637. $ExceptionRenderer = new ExceptionRenderer($exception);
  638. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  639. $ExceptionRenderer->controller->response->expects($this->once())
  640. ->method('statusCode')
  641. ->with(500);
  642. ob_start();
  643. $ExceptionRenderer->render();
  644. $result = ob_get_clean();
  645. $this->assertContains('Internal Error', $result);
  646. }
  647. /**
  648. * Tests the output of rendering a PDOException
  649. *
  650. * @return void
  651. */
  652. public function testPDOException() {
  653. $exception = new PDOException('There was an error in the SQL query');
  654. $exception->queryString = 'SELECT * from poo_query < 5 and :seven';
  655. $exception->params = array('seven' => 7);
  656. $ExceptionRenderer = new ExceptionRenderer($exception);
  657. $ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
  658. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(500);
  659. ob_start();
  660. $ExceptionRenderer->render();
  661. $result = ob_get_clean();
  662. $this->assertContains('<h2>Database Error</h2>', $result);
  663. $this->assertContains('There was an error in the SQL query', $result);
  664. $this->assertContains('SELECT * from poo_query < 5 and :seven', $result);
  665. $this->assertContains("'seven' => (int) 7", $result);
  666. }
  667. }