ErrorHandlerTest.php 8.0 KB

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