ErrorHandlerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * ErrorHandlerTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.Error
  15. * @since CakePHP(tm) v 1.2.0.5432
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ErrorHandler', 'Error');
  19. App::uses('Controller', 'Controller');
  20. App::uses('Router', 'Routing');
  21. /**
  22. * ErrorHandlerTest class
  23. *
  24. * @package Cake.Test.Case.Error
  25. */
  26. class ErrorHandlerTest extends CakeTestCase {
  27. protected $_restoreError = false;
  28. /**
  29. * setup create a request object to get out of router later.
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. App::build(array(
  36. 'View' => array(
  37. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
  38. )
  39. ), App::RESET);
  40. Router::reload();
  41. $request = new CakeRequest(null, false);
  42. $request->base = '';
  43. Router::setRequestInfo($request);
  44. Configure::write('debug', 2);
  45. CakeLog::disable('stdout');
  46. CakeLog::disable('stderr');
  47. }
  48. /**
  49. * tearDown
  50. *
  51. * @return void
  52. */
  53. public function tearDown() {
  54. parent::tearDown();
  55. if ($this->_restoreError) {
  56. restore_error_handler();
  57. }
  58. CakeLog::enable('stdout');
  59. CakeLog::enable('stderr');
  60. }
  61. /**
  62. * test error handling when debug is on, an error should be printed from Debugger.
  63. *
  64. * @return void
  65. */
  66. public function testHandleErrorDebugOn() {
  67. set_error_handler('ErrorHandler::handleError');
  68. $this->_restoreError = true;
  69. ob_start();
  70. $wrong .= '';
  71. $result = ob_get_clean();
  72. $this->assertRegExp('/<pre class="cake-error">/', $result);
  73. $this->assertRegExp('/<b>Notice<\/b>/', $result);
  74. $this->assertRegExp('/variable:\s+wrong/', $result);
  75. }
  76. /**
  77. * provides errors for mapping tests.
  78. *
  79. * @return void
  80. */
  81. public static function errorProvider() {
  82. return array(
  83. array(E_USER_NOTICE, 'Notice'),
  84. array(E_USER_WARNING, 'Warning'),
  85. );
  86. }
  87. /**
  88. * test error mappings
  89. *
  90. * @dataProvider errorProvider
  91. * @return void
  92. */
  93. public function testErrorMapping($error, $expected) {
  94. set_error_handler('ErrorHandler::handleError');
  95. $this->_restoreError = true;
  96. ob_start();
  97. trigger_error('Test error', $error);
  98. $result = ob_get_clean();
  99. $this->assertRegExp('/<b>' . $expected . '<\/b>/', $result);
  100. }
  101. /**
  102. * test error prepended by @
  103. *
  104. * @return void
  105. */
  106. public function testErrorSuppressed() {
  107. set_error_handler('ErrorHandler::handleError');
  108. $this->_restoreError = true;
  109. ob_start();
  110. //@codingStandardsIgnoreStart
  111. @include 'invalid.file';
  112. //@codingStandardsIgnoreEnd
  113. $result = ob_get_clean();
  114. $this->assertTrue(empty($result));
  115. }
  116. /**
  117. * Test that errors go into CakeLog when debug = 0.
  118. *
  119. * @return void
  120. */
  121. public function testHandleErrorDebugOff() {
  122. Configure::write('debug', 0);
  123. Configure::write('Error.trace', false);
  124. if (file_exists(LOGS . 'debug.log')) {
  125. unlink(LOGS . 'debug.log');
  126. }
  127. set_error_handler('ErrorHandler::handleError');
  128. $this->_restoreError = true;
  129. $out .= '';
  130. $result = file(LOGS . 'debug.log');
  131. $this->assertEquals(1, count($result));
  132. $this->assertRegExp(
  133. '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
  134. $result[0]
  135. );
  136. if (file_exists(LOGS . 'debug.log')) {
  137. unlink(LOGS . 'debug.log');
  138. }
  139. }
  140. /**
  141. * Test that errors going into CakeLog include traces.
  142. *
  143. * @return void
  144. */
  145. public function testHandleErrorLoggingTrace() {
  146. Configure::write('debug', 0);
  147. Configure::write('Error.trace', true);
  148. if (file_exists(LOGS . 'debug.log')) {
  149. unlink(LOGS . 'debug.log');
  150. }
  151. set_error_handler('ErrorHandler::handleError');
  152. $this->_restoreError = true;
  153. $out .= '';
  154. $result = file(LOGS . 'debug.log');
  155. $this->assertRegExp(
  156. '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
  157. $result[0]
  158. );
  159. $this->assertRegExp('/^Trace:/', $result[1]);
  160. $this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
  161. if (file_exists(LOGS . 'debug.log')) {
  162. unlink(LOGS . 'debug.log');
  163. }
  164. }
  165. /**
  166. * test handleException generating a page.
  167. *
  168. * @return void
  169. */
  170. public function testHandleException() {
  171. $error = new NotFoundException('Kaboom!');
  172. ob_start();
  173. ErrorHandler::handleException($error);
  174. $result = ob_get_clean();
  175. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  176. }
  177. /**
  178. * test handleException generating log.
  179. *
  180. * @return void
  181. */
  182. public function testHandleExceptionLog() {
  183. if (file_exists(LOGS . 'error.log')) {
  184. unlink(LOGS . 'error.log');
  185. }
  186. Configure::write('Exception.log', true);
  187. $error = new NotFoundException('Kaboom!');
  188. ob_start();
  189. ErrorHandler::handleException($error);
  190. $result = ob_get_clean();
  191. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  192. $log = file(LOGS . 'error.log');
  193. $this->assertContains('[NotFoundException] Kaboom!', $log[0], 'message missing.');
  194. $this->assertContains('ErrorHandlerTest->testHandleExceptionLog', $log[2], 'Stack trace missing.');
  195. }
  196. /**
  197. * test handleException generating log.
  198. *
  199. * @return void
  200. */
  201. public function testHandleExceptionLogSkipping() {
  202. if (file_exists(LOGS . 'error.log')) {
  203. unlink(LOGS . 'error.log');
  204. }
  205. Configure::write('Exception.log', true);
  206. Configure::write('Exception.skipLog', array('NotFoundException'));
  207. $notFound = new NotFoundException('Kaboom!');
  208. $forbidden = new ForbiddenException('Fooled you!');
  209. ob_start();
  210. ErrorHandler::handleException($notFound);
  211. $result = ob_get_clean();
  212. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  213. ob_start();
  214. ErrorHandler::handleException($forbidden);
  215. $result = ob_get_clean();
  216. $this->assertRegExp('/Fooled you!/', $result, 'message missing.');
  217. $log = file(LOGS . 'error.log');
  218. $this->assertNotContains('[NotFoundException] Kaboom!', $log[0], 'message should not be logged.');
  219. $this->assertContains('[ForbiddenException] Fooled you!', $log[0], 'message missing.');
  220. }
  221. /**
  222. * tests it is possible to load a plugin exception renderer
  223. *
  224. * @return void
  225. */
  226. public function testLoadPluginHandler() {
  227. App::build(array(
  228. 'Plugin' => array(
  229. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  230. )
  231. ), App::RESET);
  232. CakePlugin::load('TestPlugin');
  233. Configure::write('Exception.renderer', 'TestPlugin.TestPluginExceptionRenderer');
  234. $error = new NotFoundException('Kaboom!');
  235. ob_start();
  236. ErrorHandler::handleException($error);
  237. $result = ob_get_clean();
  238. $this->assertEquals('Rendered by test plugin', $result);
  239. CakePlugin::unload();
  240. }
  241. /**
  242. * test handleFatalError generating a page.
  243. *
  244. * These tests start two buffers as handleFatalError blows the outer one up.
  245. *
  246. * @return void
  247. */
  248. public function testHandleFatalErrorPage() {
  249. $line = __LINE__;
  250. ob_start();
  251. ob_start();
  252. Configure::write('debug', 1);
  253. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  254. $result = ob_get_clean();
  255. $this->assertContains('Something wrong', $result, 'message missing.');
  256. $this->assertContains(__FILE__, $result, 'filename missing.');
  257. $this->assertContains((string)$line, $result, 'line missing.');
  258. ob_start();
  259. ob_start();
  260. Configure::write('debug', 0);
  261. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  262. $result = ob_get_clean();
  263. $this->assertNotContains('Something wrong', $result, 'message must not appear.');
  264. $this->assertNotContains(__FILE__, $result, 'filename must not appear.');
  265. $this->assertContains('An Internal Error Has Occurred', $result);
  266. }
  267. /**
  268. * test handleException generating log.
  269. *
  270. * @return void
  271. */
  272. public function testHandleFatalErrorLog() {
  273. if (file_exists(LOGS . 'error.log')) {
  274. unlink(LOGS . 'error.log');
  275. }
  276. ob_start();
  277. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
  278. ob_clean();
  279. $log = file(LOGS . 'error.log');
  280. $this->assertContains(__FILE__, $log[0], 'missing filename');
  281. $this->assertContains('[FatalErrorException] Something wrong', $log[1], 'message missing.');
  282. }
  283. }