ErrorHandlerTest.php 9.0 KB

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