ErrorHandlerTest.php 8.0 KB

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