ExceptionRendererTest.php 23 KB

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