ExceptionRendererTest.php 23 KB

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