ErrorHandlerTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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(
  135. $this->matchesRegularExpression('(notice|debug)'),
  136. 'Notice (8): Undefined variable: out in [' . __FILE__ . ', line ' . (__LINE__ + 3) . ']'
  137. );
  138. $out .= '';
  139. }
  140. /**
  141. * Test that errors going into Cake Log include traces.
  142. *
  143. * @return void
  144. */
  145. public function testHandleErrorLoggingTrace() {
  146. Configure::write('debug', false);
  147. $errorHandler = new ErrorHandler(['trace' => true]);
  148. $errorHandler->register();
  149. $this->_restoreError = true;
  150. $this->_logger->expects($this->once())
  151. ->method('write')
  152. ->with(
  153. $this->matchesRegularExpression('(notice|debug)'),
  154. $this->logicalAnd(
  155. $this->stringContains('Notice (8): Undefined variable: out in '),
  156. $this->stringContains('Trace:'),
  157. $this->stringContains(__NAMESPACE__ . '\ErrorHandlerTest::testHandleErrorLoggingTrace()')
  158. )
  159. );
  160. $out .= '';
  161. }
  162. /**
  163. * test handleException generating a page.
  164. *
  165. * @return void
  166. */
  167. public function testHandleException() {
  168. $error = new Error\NotFoundException('Kaboom!');
  169. $errorHandler = new ErrorHandler();
  170. ob_start();
  171. $errorHandler->handleException($error);
  172. $result = ob_get_clean();
  173. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  174. }
  175. /**
  176. * test handleException generating log.
  177. *
  178. * @return void
  179. */
  180. public function testHandleExceptionLog() {
  181. $errorHandler = new ErrorHandler(['log' => true]);
  182. $error = new Error\NotFoundException('Kaboom!');
  183. $this->_logger->expects($this->once())
  184. ->method('write')
  185. ->with('error', $this->logicalAnd(
  186. $this->stringContains('[Cake\Error\NotFoundException] Kaboom!'),
  187. $this->stringContains('ErrorHandlerTest->testHandleExceptionLog')
  188. ));
  189. ob_start();
  190. $errorHandler->handleException($error);
  191. $result = ob_get_clean();
  192. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  193. }
  194. /**
  195. * test handleException generating log.
  196. *
  197. * @return void
  198. */
  199. public function testHandleExceptionLogSkipping() {
  200. $notFound = new Error\NotFoundException('Kaboom!');
  201. $forbidden = new Error\ForbiddenException('Fooled you!');
  202. $this->_logger->expects($this->once())
  203. ->method('write')
  204. ->with(
  205. 'error',
  206. $this->stringContains('[Cake\Error\ForbiddenException] Fooled you!')
  207. );
  208. $errorHandler = new ErrorHandler([
  209. 'log' => true,
  210. 'skipLog' => ['Cake\Error\NotFoundException']
  211. ]);
  212. ob_start();
  213. $errorHandler->handleException($notFound);
  214. $result = ob_get_clean();
  215. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  216. ob_start();
  217. $errorHandler->handleException($forbidden);
  218. $result = ob_get_clean();
  219. $this->assertRegExp('/Fooled you!/', $result, '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. Plugin::load('TestPlugin');
  228. $errorHandler = new ErrorHandler([
  229. 'exceptionRenderer' => 'TestPlugin.TestPluginExceptionRenderer',
  230. ]);
  231. $error = new Error\NotFoundException('Kaboom!');
  232. ob_start();
  233. $errorHandler->handleException($error);
  234. $result = ob_get_clean();
  235. $this->assertEquals('Rendered by test plugin', $result);
  236. Plugin::unload();
  237. }
  238. /**
  239. * test handleFatalError generating a page.
  240. *
  241. * These tests start two buffers as handleFatalError blows the outer one up.
  242. *
  243. * @return void
  244. */
  245. public function testHandleFatalErrorPage() {
  246. $line = __LINE__;
  247. $errorHandler = new ErrorHandler();
  248. Configure::write('debug', true);
  249. ob_start();
  250. ob_start();
  251. $errorHandler->handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  252. $result = ob_get_clean();
  253. $this->assertContains('Something wrong', $result, 'message missing.');
  254. $this->assertContains(__FILE__, $result, 'filename missing.');
  255. $this->assertContains((string)$line, $result, 'line missing.');
  256. ob_start();
  257. ob_start();
  258. Configure::write('debug', false);
  259. $errorHandler->handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  260. $result = ob_get_clean();
  261. $this->assertNotContains('Something wrong', $result, 'message must not appear.');
  262. $this->assertNotContains(__FILE__, $result, 'filename must not appear.');
  263. $this->assertContains('An Internal Error Has Occurred', $result);
  264. }
  265. /**
  266. * test handleFatalError generating log.
  267. *
  268. * @return void
  269. */
  270. public function testHandleFatalErrorLog() {
  271. $this->_logger->expects($this->at(0))
  272. ->method('write')
  273. ->with('error', $this->logicalAnd(
  274. $this->stringContains(__FILE__ . ', line ' . (__LINE__ + 10)),
  275. $this->stringContains('Fatal Error (1)'),
  276. $this->stringContains('Something wrong')
  277. ));
  278. $this->_logger->expects($this->at(1))
  279. ->method('write')
  280. ->with('error', $this->stringContains('[Cake\Error\FatalErrorException] Something wrong'));
  281. $errorHandler = new ErrorHandler(['log' => true]);
  282. ob_start();
  283. $errorHandler->handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
  284. ob_clean();
  285. }
  286. }