ExceptionRendererTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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\Exception\MissingActionException;
  19. use Cake\Controller\Exception\MissingComponentException;
  20. use Cake\Core\App;
  21. use Cake\Core\Configure;
  22. use Cake\Core\Exception\MissingPluginException;
  23. use Cake\Core\Plugin;
  24. use Cake\Datasource\Exception\MissingDatasourceConfigException;
  25. use Cake\Datasource\Exception\MissingDatasourceException;
  26. use Cake\Error;
  27. use Cake\Error\ExceptionRenderer;
  28. use Cake\Event\Event;
  29. use Cake\Network\Exception\InternalErrorException;
  30. use Cake\Network\Exception\MethodNotAllowedException;
  31. use Cake\Network\Exception\NotFoundException;
  32. use Cake\Network\Exception\SocketException;
  33. use Cake\Network\Request;
  34. use Cake\ORM\Exception\MissingBehaviorException;
  35. use Cake\Routing\Exception\MissingControllerException;
  36. use Cake\Routing\Router;
  37. use Cake\TestSuite\TestCase;
  38. use Cake\View\Exception\MissingHelperException;
  39. use Cake\View\Exception\MissingLayoutException;
  40. use Cake\View\Exception\MissingTemplateException;
  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 array $config
  56. * @return void
  57. */
  58. public function initialize(array $config) {
  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. $this->assertContains('An Internal Error Has Occurred.</p>', $result->body());
  364. }
  365. /**
  366. * testExceptionResponseHeader method
  367. *
  368. * @return void
  369. */
  370. public function testExceptionResponseHeader() {
  371. $exception = new MethodNotAllowedException('Only allowing POST and DELETE');
  372. $exception->responseHeader(array('Allow: POST, DELETE'));
  373. $ExceptionRenderer = new ExceptionRenderer($exception);
  374. $result = $ExceptionRenderer->render();
  375. $headers = $result->header();
  376. $this->assertArrayHasKey('Allow', $headers);
  377. $this->assertEquals('POST, DELETE', $headers['Allow']);
  378. }
  379. /**
  380. * testMissingController method
  381. *
  382. * @return void
  383. */
  384. public function testMissingController() {
  385. $exception = new MissingControllerException(array(
  386. 'class' => 'Posts',
  387. 'prefix' => '',
  388. 'plugin' => '',
  389. ));
  390. $ExceptionRenderer = $this->_mockResponse(new MyCustomExceptionRenderer($exception));
  391. $result = $ExceptionRenderer->render()->body();
  392. $this->assertEquals('missingController', $ExceptionRenderer->template);
  393. $this->assertContains('<h2>Missing Controller</h2>', $result);
  394. $this->assertContains('<em>PostsController</em>', $result);
  395. }
  396. /**
  397. * Returns an array of tests to run for the various Cake Exception classes.
  398. *
  399. * @return array
  400. */
  401. public static function exceptionProvider() {
  402. return array(
  403. array(
  404. new MissingActionException(array(
  405. 'controller' => 'PostsController',
  406. 'action' => 'index',
  407. 'prefix' => '',
  408. 'plugin' => '',
  409. )),
  410. array(
  411. '/<h2>Missing Method in PostsController<\/h2>/',
  412. '/<em>PostsController::index\(\)<\/em>/'
  413. ),
  414. 404
  415. ),
  416. array(
  417. new MissingTemplateException(array('file' => '/posts/about.ctp')),
  418. array(
  419. "/posts\/about.ctp/"
  420. ),
  421. 500
  422. ),
  423. array(
  424. new MissingLayoutException(array('file' => 'layouts/my_layout.ctp')),
  425. array(
  426. "/Missing Layout/",
  427. "/layouts\/my_layout.ctp/"
  428. ),
  429. 500
  430. ),
  431. array(
  432. new MissingHelperException(array('class' => 'MyCustomHelper')),
  433. array(
  434. '/<h2>Missing Helper<\/h2>/',
  435. '/<em>MyCustomHelper<\/em> could not be found./',
  436. '/Create the class <em>MyCustomHelper<\/em> below in file:/',
  437. '/(\/|\\\)MyCustomHelper.php/'
  438. ),
  439. 500
  440. ),
  441. array(
  442. new MissingBehaviorException(array('class' => 'MyCustomBehavior')),
  443. array(
  444. '/<h2>Missing Behavior<\/h2>/',
  445. '/Create the class <em>MyCustomBehavior<\/em> below in file:/',
  446. '/(\/|\\\)MyCustomBehavior.php/'
  447. ),
  448. 500
  449. ),
  450. array(
  451. new MissingComponentException(array('class' => 'SideboxComponent')),
  452. array(
  453. '/<h2>Missing Component<\/h2>/',
  454. '/Create the class <em>SideboxComponent<\/em> below in file:/',
  455. '/(\/|\\\)SideboxComponent.php/'
  456. ),
  457. 500
  458. ),
  459. array(
  460. new MissingDatasourceConfigException(array('name' => 'MyDatasourceConfig')),
  461. array(
  462. '/<h2>Missing Datasource Configuration<\/h2>/',
  463. '/<em>MyDatasourceConfig<\/em> was not found/'
  464. ),
  465. 500
  466. ),
  467. array(
  468. new MissingDatasourceException(array('class' => 'MyDatasource', 'plugin' => 'MyPlugin')),
  469. array(
  470. '/<h2>Missing Datasource<\/h2>/',
  471. '/<em>MyPlugin.MyDatasource<\/em> could not be found./'
  472. ),
  473. 500
  474. ),
  475. array(
  476. new \Exception('boom'),
  477. array(
  478. '/Internal Error/'
  479. ),
  480. 500
  481. ),
  482. array(
  483. new \RuntimeException('another boom'),
  484. array(
  485. '/Internal Error/'
  486. ),
  487. 500
  488. ),
  489. array(
  490. new \Cake\Core\Exception\Exception('base class'),
  491. array('/Internal Error/'),
  492. 500
  493. )
  494. );
  495. }
  496. /**
  497. * Test the various Cake Exception sub classes
  498. *
  499. * @dataProvider exceptionProvider
  500. * @return void
  501. */
  502. public function testCakeExceptionHandling($exception, $patterns, $code) {
  503. $ExceptionRenderer = new ExceptionRenderer($exception);
  504. $ExceptionRenderer->controller->response = $this->getMock('Cake\Network\Response', array('statusCode', '_sendHeader'));
  505. $ExceptionRenderer->controller->response->expects($this->once())
  506. ->method('statusCode')
  507. ->with($code);
  508. $result = $ExceptionRenderer->render()->body();
  509. foreach ($patterns as $pattern) {
  510. $this->assertRegExp($pattern, $result);
  511. }
  512. }
  513. /**
  514. * Test exceptions being raised when helpers are missing.
  515. *
  516. * @return void
  517. */
  518. public function testMissingRenderSafe() {
  519. $exception = new MissingHelperException(array('class' => 'Fail'));
  520. $ExceptionRenderer = new ExceptionRenderer($exception);
  521. $ExceptionRenderer->controller = $this->getMock('Cake\Controller\Controller', array('render'));
  522. $ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
  523. $ExceptionRenderer->controller->request = new Request;
  524. $ExceptionRenderer->controller->expects($this->at(0))
  525. ->method('render')
  526. ->with('missingHelper')
  527. ->will($this->throwException($exception));
  528. $response = $this->getMock('Cake\Network\Response');
  529. $response->expects($this->once())
  530. ->method('body')
  531. ->with($this->stringContains('Helper class Fail'));
  532. $ExceptionRenderer->controller->response = $response;
  533. $ExceptionRenderer->render();
  534. sort($ExceptionRenderer->controller->helpers);
  535. $this->assertEquals(array('Form', 'Html', 'Session'), $ExceptionRenderer->controller->helpers);
  536. }
  537. /**
  538. * Test that exceptions in beforeRender() are handled by outputMessageSafe
  539. *
  540. * @return void
  541. */
  542. public function testRenderExceptionInBeforeRender() {
  543. $exception = new NotFoundException('Not there, sorry');
  544. $ExceptionRenderer = new ExceptionRenderer($exception);
  545. $ExceptionRenderer->controller = $this->getMock('Cake\Controller\Controller', array('beforeRender'));
  546. $ExceptionRenderer->controller->request = new Request;
  547. $ExceptionRenderer->controller->expects($this->any())
  548. ->method('beforeRender')
  549. ->will($this->throwException($exception));
  550. $response = $this->getMock('Cake\Network\Response');
  551. $response->expects($this->once())
  552. ->method('body')
  553. ->with($this->stringContains('Not there, sorry'));
  554. $ExceptionRenderer->controller->response = $response;
  555. $ExceptionRenderer->render();
  556. }
  557. /**
  558. * Test that missing subDir/layoutPath don't cause other fatal errors.
  559. *
  560. * @return void
  561. */
  562. public function testMissingSubdirRenderSafe() {
  563. $exception = new NotFoundException();
  564. $ExceptionRenderer = new ExceptionRenderer($exception);
  565. $ExceptionRenderer->controller = $this->getMock('Cake\Controller\Controller', array('render'));
  566. $ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
  567. $ExceptionRenderer->controller->layoutPath = 'boom';
  568. $ExceptionRenderer->controller->subDir = 'boom';
  569. $ExceptionRenderer->controller->request = new Request;
  570. $ExceptionRenderer->controller->expects($this->once())
  571. ->method('render')
  572. ->with('error400')
  573. ->will($this->throwException($exception));
  574. $response = $this->getMock('Cake\Network\Response');
  575. $response->expects($this->once())
  576. ->method('body')
  577. ->with($this->stringContains('Not Found'));
  578. $response->expects($this->once())
  579. ->method('type')
  580. ->with('html');
  581. $ExceptionRenderer->controller->response = $response;
  582. $ExceptionRenderer->render();
  583. $this->assertEquals('', $ExceptionRenderer->controller->layoutPath);
  584. $this->assertEquals('', $ExceptionRenderer->controller->subDir);
  585. $this->assertEquals('Error', $ExceptionRenderer->controller->viewPath);
  586. }
  587. /**
  588. * Test that missing plugin disables Controller::$plugin if the two are the same plugin.
  589. *
  590. * @return void
  591. */
  592. public function testMissingPluginRenderSafe() {
  593. $exception = new NotFoundException();
  594. $ExceptionRenderer = new ExceptionRenderer($exception);
  595. $ExceptionRenderer->controller = $this->getMock('Cake\Controller\Controller', array('render'));
  596. $ExceptionRenderer->controller->plugin = 'TestPlugin';
  597. $ExceptionRenderer->controller->request = $this->getMock('Cake\Network\Request');
  598. $exception = new MissingPluginException(array('plugin' => 'TestPlugin'));
  599. $ExceptionRenderer->controller->expects($this->once())
  600. ->method('render')
  601. ->with('error400')
  602. ->will($this->throwException($exception));
  603. $response = $this->getMock('Cake\Network\Response');
  604. $response->expects($this->once())
  605. ->method('body')
  606. ->with($this->logicalAnd(
  607. $this->logicalNot($this->stringContains('test plugin error500')),
  608. $this->stringContains('Not Found')
  609. ));
  610. $ExceptionRenderer->controller->response = $response;
  611. $ExceptionRenderer->render();
  612. }
  613. /**
  614. * Test that missing plugin doesn't disable Controller::$plugin if the two aren't the same plugin.
  615. *
  616. * @return void
  617. */
  618. public function testMissingPluginRenderSafeWithPlugin() {
  619. Plugin::load('TestPlugin');
  620. $exception = new NotFoundException();
  621. $ExceptionRenderer = new ExceptionRenderer($exception);
  622. $ExceptionRenderer->controller = $this->getMock('Cake\Controller\Controller', array('render'));
  623. $ExceptionRenderer->controller->plugin = 'TestPlugin';
  624. $ExceptionRenderer->controller->request = $this->getMock('Cake\Network\Request');
  625. $exception = new MissingPluginException(array('plugin' => 'TestPluginTwo'));
  626. $ExceptionRenderer->controller->expects($this->once())
  627. ->method('render')
  628. ->with('error400')
  629. ->will($this->throwException($exception));
  630. $response = $this->getMock('Cake\Network\Response');
  631. $response->expects($this->once())
  632. ->method('body')
  633. ->with($this->logicalAnd(
  634. $this->stringContains('test plugin error500'),
  635. $this->stringContains('Not Found')
  636. ));
  637. $ExceptionRenderer->controller->response = $response;
  638. $ExceptionRenderer->render();
  639. Plugin::unload();
  640. }
  641. /**
  642. * Test that exceptions can be rendered when a request hasn't been registered
  643. * with Router
  644. *
  645. * @return void
  646. */
  647. public function testRenderWithNoRequest() {
  648. Router::reload();
  649. $this->assertNull(Router::getRequest(false));
  650. $exception = new \Exception('Terrible');
  651. $ExceptionRenderer = new ExceptionRenderer($exception);
  652. $result = $ExceptionRenderer->render();
  653. $this->assertContains('Internal Error', $result->body());
  654. $this->assertEquals(500, $result->statusCode());
  655. }
  656. /**
  657. * Tests the output of rendering a PDOException
  658. *
  659. * @return void
  660. */
  661. public function testPDOException() {
  662. $exception = new \PDOException('There was an error in the SQL query');
  663. $exception->queryString = 'SELECT * from poo_query < 5 and :seven';
  664. $exception->params = array('seven' => 7);
  665. $ExceptionRenderer = new ExceptionRenderer($exception);
  666. $ExceptionRenderer->controller->response = $this->getMock('Cake\Network\Response', array('statusCode', '_sendHeader'));
  667. $ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(500);
  668. $result = $ExceptionRenderer->render()->body();
  669. $this->assertContains('<h2>Database Error</h2>', $result);
  670. $this->assertContains('There was an error in the SQL query', $result);
  671. $this->assertContains(h('SELECT * from poo_query < 5 and :seven'), $result);
  672. $this->assertContains("'seven' => (int) 7", $result);
  673. }
  674. }