PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

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