ErrorHandlerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. * @since 1.2.0
  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', true);
  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. restore_exception_handler();
  61. }
  62. }
  63. /**
  64. * test error handling when debug is on, an error should be printed from Debugger.
  65. *
  66. * @return void
  67. */
  68. public function testHandleErrorDebugOn() {
  69. $errorHandler = new ErrorHandler();
  70. $errorHandler->register();
  71. $this->_restoreError = true;
  72. ob_start();
  73. $wrong .= '';
  74. $result = ob_get_clean();
  75. $this->assertRegExp('/<pre class="cake-error">/', $result);
  76. $this->assertRegExp('/<b>Notice<\/b>/', $result);
  77. $this->assertRegExp('/variable:\s+wrong/', $result);
  78. }
  79. /**
  80. * provides errors for mapping tests.
  81. *
  82. * @return void
  83. */
  84. public static function errorProvider() {
  85. return array(
  86. array(E_USER_NOTICE, 'Notice'),
  87. array(E_USER_WARNING, 'Warning'),
  88. );
  89. }
  90. /**
  91. * test error mappings
  92. *
  93. * @dataProvider errorProvider
  94. * @return void
  95. */
  96. public function testErrorMapping($error, $expected) {
  97. $errorHandler = new ErrorHandler();
  98. $errorHandler->register();
  99. $this->_restoreError = true;
  100. ob_start();
  101. trigger_error('Test error', $error);
  102. $result = ob_get_clean();
  103. $this->assertRegExp('/<b>' . $expected . '<\/b>/', $result);
  104. }
  105. /**
  106. * test error prepended by @
  107. *
  108. * @return void
  109. */
  110. public function testErrorSuppressed() {
  111. $errorHandler = new ErrorHandler();
  112. $errorHandler->register();
  113. $this->_restoreError = true;
  114. ob_start();
  115. //@codingStandardsIgnoreStart
  116. @include 'invalid.file';
  117. //@codingStandardsIgnoreEnd
  118. $result = ob_get_clean();
  119. $this->assertTrue(empty($result));
  120. }
  121. /**
  122. * Test that errors go into Cake Log when debug = 0.
  123. *
  124. * @return void
  125. */
  126. public function testHandleErrorDebugOff() {
  127. Configure::write('debug', false);
  128. $errorHandler = new ErrorHandler();
  129. $errorHandler->register();
  130. $this->_restoreError = true;
  131. $this->_logger->expects($this->once())
  132. ->method('write')
  133. ->with('notice', 'Notice (8): Undefined variable: out in [' . __FILE__ . ', line ' . (__LINE__ + 2) . ']');
  134. $out .= '';
  135. }
  136. /**
  137. * Test that errors going into Cake Log include traces.
  138. *
  139. * @return void
  140. */
  141. public function testHandleErrorLoggingTrace() {
  142. Configure::write('debug', false);
  143. $errorHandler = new ErrorHandler(['trace' => true]);
  144. $errorHandler->register();
  145. $this->_restoreError = true;
  146. $this->_logger->expects($this->once())
  147. ->method('write')
  148. ->with('notice', $this->logicalAnd(
  149. $this->stringContains('Notice (8): Undefined variable: out in '),
  150. $this->stringContains('Trace:'),
  151. $this->stringContains(__NAMESPACE__ . '\ErrorHandlerTest::testHandleErrorLoggingTrace()')
  152. ));
  153. $out .= '';
  154. }
  155. /**
  156. * test handleException generating a page.
  157. *
  158. * @return void
  159. */
  160. public function testHandleException() {
  161. $error = new Error\NotFoundException('Kaboom!');
  162. $errorHandler = new ErrorHandler();
  163. ob_start();
  164. $errorHandler->handleException($error);
  165. $result = ob_get_clean();
  166. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  167. }
  168. /**
  169. * test handleException generating log.
  170. *
  171. * @return void
  172. */
  173. public function testHandleExceptionLog() {
  174. $errorHandler = new ErrorHandler(['log' => true]);
  175. $error = new Error\NotFoundException('Kaboom!');
  176. $this->_logger->expects($this->once())
  177. ->method('write')
  178. ->with('error', $this->logicalAnd(
  179. $this->stringContains('[Cake\Error\NotFoundException] Kaboom!'),
  180. $this->stringContains('ErrorHandlerTest->testHandleExceptionLog')
  181. ));
  182. ob_start();
  183. $errorHandler->handleException($error);
  184. $result = ob_get_clean();
  185. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  186. }
  187. /**
  188. * test handleException generating log.
  189. *
  190. * @return void
  191. */
  192. public function testHandleExceptionLogSkipping() {
  193. $notFound = new Error\NotFoundException('Kaboom!');
  194. $forbidden = new Error\ForbiddenException('Fooled you!');
  195. $this->_logger->expects($this->once())
  196. ->method('write')
  197. ->with(
  198. 'error',
  199. $this->stringContains('[Cake\Error\ForbiddenException] Fooled you!')
  200. );
  201. $errorHandler = new ErrorHandler([
  202. 'log' => true,
  203. 'skipLog' => ['Cake\Error\NotFoundException']
  204. ]);
  205. ob_start();
  206. $errorHandler->handleException($notFound);
  207. $result = ob_get_clean();
  208. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  209. ob_start();
  210. $errorHandler->handleException($forbidden);
  211. $result = ob_get_clean();
  212. $this->assertRegExp('/Fooled you!/', $result, 'message missing.');
  213. }
  214. /**
  215. * tests it is possible to load a plugin exception renderer
  216. *
  217. * @return void
  218. */
  219. public function testLoadPluginHandler() {
  220. Plugin::load('TestPlugin');
  221. $errorHandler = new ErrorHandler([
  222. 'exceptionRenderer' => 'TestPlugin.TestPluginExceptionRenderer',
  223. ]);
  224. $error = new Error\NotFoundException('Kaboom!');
  225. ob_start();
  226. $errorHandler->handleException($error);
  227. $result = ob_get_clean();
  228. $this->assertEquals('Rendered by test plugin', $result);
  229. Plugin::unload();
  230. }
  231. /**
  232. * test handleFatalError generating a page.
  233. *
  234. * These tests start two buffers as handleFatalError blows the outer one up.
  235. *
  236. * @return void
  237. */
  238. public function testHandleFatalErrorPage() {
  239. $line = __LINE__;
  240. $errorHandler = new ErrorHandler();
  241. Configure::write('debug', true);
  242. ob_start();
  243. ob_start();
  244. $errorHandler->handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  245. $result = ob_get_clean();
  246. $this->assertContains('Something wrong', $result, 'message missing.');
  247. $this->assertContains(__FILE__, $result, 'filename missing.');
  248. $this->assertContains((string)$line, $result, 'line missing.');
  249. ob_start();
  250. ob_start();
  251. Configure::write('debug', false);
  252. $errorHandler->handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  253. $result = ob_get_clean();
  254. $this->assertNotContains('Something wrong', $result, 'message must not appear.');
  255. $this->assertNotContains(__FILE__, $result, 'filename must not appear.');
  256. $this->assertContains('An Internal Error Has Occurred', $result);
  257. }
  258. /**
  259. * test handleFatalError generating log.
  260. *
  261. * @return void
  262. */
  263. public function testHandleFatalErrorLog() {
  264. $this->_logger->expects($this->at(0))
  265. ->method('write')
  266. ->with('error', $this->logicalAnd(
  267. $this->stringContains(__FILE__ . ', line ' . (__LINE__ + 10)),
  268. $this->stringContains('Fatal Error (1)'),
  269. $this->stringContains('Something wrong')
  270. ));
  271. $this->_logger->expects($this->at(1))
  272. ->method('write')
  273. ->with('error', $this->stringContains('[Cake\Error\FatalErrorException] Something wrong'));
  274. $errorHandler = new ErrorHandler(['log' => true]);
  275. ob_start();
  276. $errorHandler->handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
  277. ob_clean();
  278. }
  279. }