ErrorHandlerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Error;
  17. use Cake\Core\Configure;
  18. use Cake\Datasource\Exception\RecordNotFoundException;
  19. use Cake\Error\ErrorHandler;
  20. use Cake\Http\Exception\ForbiddenException;
  21. use Cake\Http\Exception\MissingControllerException;
  22. use Cake\Http\Exception\NotFoundException;
  23. use Cake\Http\ServerRequest;
  24. use Cake\Log\Log;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use Psr\Log\LoggerInterface;
  28. use RuntimeException;
  29. use TestApp\Error\TestErrorHandler;
  30. /**
  31. * ErrorHandlerTest class
  32. */
  33. class ErrorHandlerTest extends TestCase
  34. {
  35. protected $_restoreError = false;
  36. /**
  37. * @var \Psr\Log\LoggerInterface|\PHPUnit\Framework\MockObject\MockObject
  38. */
  39. protected $_logger;
  40. /**
  41. * error level property
  42. *
  43. */
  44. private static $errorLevel;
  45. /**
  46. * setup create a request object to get out of router later.
  47. *
  48. * @return void
  49. */
  50. public function setUp(): void
  51. {
  52. parent::setUp();
  53. Router::reload();
  54. $request = new ServerRequest([
  55. 'base' => '',
  56. 'environment' => [
  57. 'HTTP_REFERER' => '/referer',
  58. ],
  59. ]);
  60. Router::setRequest($request);
  61. Configure::write('debug', true);
  62. $this->_logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  63. Log::reset();
  64. Log::setConfig('error_test', [
  65. 'engine' => $this->_logger,
  66. ]);
  67. }
  68. /**
  69. * tearDown
  70. *
  71. * @return void
  72. */
  73. public function tearDown(): void
  74. {
  75. parent::tearDown();
  76. Log::reset();
  77. $this->clearPlugins();
  78. if ($this->_restoreError) {
  79. restore_error_handler();
  80. restore_exception_handler();
  81. }
  82. error_reporting(self::$errorLevel);
  83. }
  84. /**
  85. * setUpBeforeClass
  86. *
  87. * @return void
  88. */
  89. public static function setUpBeforeClass(): void
  90. {
  91. parent::setUpBeforeClass();
  92. self::$errorLevel = error_reporting();
  93. }
  94. /**
  95. * Test an invalid rendering class.
  96. *
  97. * @return void
  98. */
  99. public function testInvalidRenderer()
  100. {
  101. $this->expectException(RuntimeException::class);
  102. $this->expectExceptionMessage('The \'TotallyInvalid\' renderer class could not be found');
  103. $errorHandler = new ErrorHandler(['exceptionRenderer' => 'TotallyInvalid']);
  104. $errorHandler->getRenderer(new \Exception('Something bad'));
  105. }
  106. /**
  107. * test error handling when debug is on, an error should be printed from Debugger.
  108. *
  109. * @return void
  110. */
  111. public function testHandleErrorDebugOn()
  112. {
  113. $errorHandler = new ErrorHandler();
  114. $errorHandler->register();
  115. $this->_restoreError = true;
  116. ob_start();
  117. $wrong = $wrong + 1;
  118. $result = ob_get_clean();
  119. $this->assertRegExp('/<pre class="cake-error">/', $result);
  120. $this->assertRegExp('/<b>Notice<\/b>/', $result);
  121. $this->assertRegExp('/variable:\s+wrong/', $result);
  122. }
  123. /**
  124. * provides errors for mapping tests.
  125. *
  126. * @return array
  127. */
  128. public static function errorProvider()
  129. {
  130. return [
  131. [E_USER_NOTICE, 'Notice'],
  132. [E_USER_WARNING, 'Warning'],
  133. ];
  134. }
  135. /**
  136. * test error mappings
  137. *
  138. * @dataProvider errorProvider
  139. * @return void
  140. */
  141. public function testErrorMapping($error, $expected)
  142. {
  143. $errorHandler = new ErrorHandler();
  144. $errorHandler->register();
  145. $this->_restoreError = true;
  146. ob_start();
  147. trigger_error('Test error', $error);
  148. $result = ob_get_clean();
  149. $this->assertStringContainsString('<b>' . $expected . '</b>', $result);
  150. }
  151. /**
  152. * test error prepended by @
  153. *
  154. * @return void
  155. */
  156. public function testErrorSuppressed()
  157. {
  158. $errorHandler = new ErrorHandler();
  159. $errorHandler->register();
  160. $this->_restoreError = true;
  161. ob_start();
  162. // phpcs:disable
  163. @include 'invalid.file';
  164. // phpcs:enable
  165. $result = ob_get_clean();
  166. $this->assertEmpty($result);
  167. }
  168. /**
  169. * Test that errors go into Cake Log when debug = 0.
  170. *
  171. * @return void
  172. */
  173. public function testHandleErrorDebugOff()
  174. {
  175. Configure::write('debug', false);
  176. $errorHandler = new ErrorHandler();
  177. $errorHandler->register();
  178. $this->_restoreError = true;
  179. $this->_logger->expects($this->once())
  180. ->method('log')
  181. ->with(
  182. $this->matchesRegularExpression('(notice|debug)'),
  183. 'Notice (8): Undefined variable: out in [' . __FILE__ . ', line ' . (__LINE__ + 3) . ']' . "\n\n"
  184. );
  185. $out = $out + 1;
  186. }
  187. /**
  188. * Test that errors going into Cake Log include traces.
  189. *
  190. * @return void
  191. */
  192. public function testHandleErrorLoggingTrace()
  193. {
  194. Configure::write('debug', false);
  195. $errorHandler = new ErrorHandler(['trace' => true]);
  196. $errorHandler->register();
  197. $this->_restoreError = true;
  198. $this->_logger->expects($this->once())
  199. ->method('log')
  200. ->with(
  201. $this->matchesRegularExpression('(notice|debug)'),
  202. $this->logicalAnd(
  203. $this->stringContains('Notice (8): Undefined variable: out in '),
  204. $this->stringContains('Trace:'),
  205. $this->stringContains(__NAMESPACE__ . '\ErrorHandlerTest::testHandleErrorLoggingTrace()'),
  206. $this->stringContains('Request URL:'),
  207. $this->stringContains('Referer URL:')
  208. )
  209. );
  210. $out = $out + 1;
  211. }
  212. /**
  213. * test handleException generating a page.
  214. *
  215. * @return void
  216. */
  217. public function testHandleException()
  218. {
  219. $error = new NotFoundException('Kaboom!');
  220. $errorHandler = new TestErrorHandler();
  221. $errorHandler->handleException($error);
  222. $this->assertStringContainsString('Kaboom!', (string)$errorHandler->response->getBody(), 'message missing.');
  223. }
  224. /**
  225. * test handleException generating log.
  226. *
  227. * @return void
  228. */
  229. public function testHandleExceptionLog()
  230. {
  231. $errorHandler = new TestErrorHandler([
  232. 'log' => true,
  233. 'trace' => true,
  234. ]);
  235. $error = new NotFoundException('Kaboom!');
  236. $this->_logger->expects($this->at(0))
  237. ->method('log')
  238. ->with('error', $this->logicalAnd(
  239. $this->stringContains('[Cake\Http\Exception\NotFoundException] Kaboom!'),
  240. $this->stringContains('vendor/phpunit/phpunit/src/Framework/TestCase.php')
  241. ));
  242. $this->_logger->expects($this->at(1))
  243. ->method('log')
  244. ->with('error', $this->logicalAnd(
  245. $this->stringContains('[Cake\Http\Exception\NotFoundException] Kaboom!'),
  246. $this->logicalNot($this->stringContains('vendor/phpunit/phpunit/src/Framework/TestCase.php'))
  247. ));
  248. $errorHandler->handleException($error);
  249. $this->assertStringContainsString('Kaboom!', (string)$errorHandler->response->getBody(), 'message missing.');
  250. $errorHandler = new TestErrorHandler([
  251. 'log' => true,
  252. 'trace' => false,
  253. ]);
  254. $errorHandler->handleException($error);
  255. }
  256. /**
  257. * test logging attributes with/without debug
  258. *
  259. * @return void
  260. */
  261. public function testHandleExceptionLogAttributes()
  262. {
  263. $errorHandler = new TestErrorHandler([
  264. 'log' => true,
  265. 'trace' => true,
  266. ]);
  267. $error = new MissingControllerException(['class' => 'Derp']);
  268. $this->_logger->expects($this->at(0))
  269. ->method('log')
  270. ->with('error', $this->logicalAnd(
  271. $this->stringContains(
  272. '[Cake\Http\Exception\MissingControllerException] ' .
  273. 'Controller class Derp could not be found.'
  274. ),
  275. $this->stringContains('Exception Attributes:'),
  276. $this->stringContains('Request URL:'),
  277. $this->stringContains('Referer URL:')
  278. ));
  279. $this->_logger->expects($this->at(1))
  280. ->method('log')
  281. ->with('error', $this->logicalAnd(
  282. $this->stringContains(
  283. '[Cake\Http\Exception\MissingControllerException] ' .
  284. 'Controller class Derp could not be found.'
  285. ),
  286. $this->logicalNot($this->stringContains('Exception Attributes:'))
  287. ));
  288. $errorHandler->handleException($error);
  289. Configure::write('debug', false);
  290. $errorHandler->handleException($error);
  291. }
  292. /**
  293. * test logging attributes with previous exception
  294. *
  295. * @return void
  296. */
  297. public function testHandleExceptionLogPrevious()
  298. {
  299. $errorHandler = new TestErrorHandler([
  300. 'log' => true,
  301. 'trace' => true,
  302. ]);
  303. $previous = new RecordNotFoundException('Previous logged');
  304. $error = new NotFoundException('Kaboom!', null, $previous);
  305. $this->_logger->expects($this->at(0))
  306. ->method('log')
  307. ->with('error', $this->logicalAnd(
  308. $this->stringContains('[Cake\Http\Exception\NotFoundException] Kaboom!'),
  309. $this->stringContains('Caused by: [Cake\Datasource\Exception\RecordNotFoundException] Previous logged'),
  310. $this->stringContains('vendor/phpunit/phpunit/src/Framework/TestCase.php')
  311. ));
  312. $errorHandler->handleException($error);
  313. }
  314. /**
  315. * test handleException generating log.
  316. *
  317. * @return void
  318. */
  319. public function testHandleExceptionLogSkipping()
  320. {
  321. $notFound = new NotFoundException('Kaboom!');
  322. $forbidden = new ForbiddenException('Fooled you!');
  323. $this->_logger->expects($this->once())
  324. ->method('log')
  325. ->with(
  326. 'error',
  327. $this->stringContains('[Cake\Http\Exception\ForbiddenException] Fooled you!')
  328. );
  329. $errorHandler = new TestErrorHandler([
  330. 'log' => true,
  331. 'skipLog' => ['Cake\Http\Exception\NotFoundException'],
  332. ]);
  333. $errorHandler->handleException($notFound);
  334. $this->assertStringContainsString('Kaboom!', (string)$errorHandler->response->getBody(), 'message missing.');
  335. $errorHandler->handleException($forbidden);
  336. $this->assertStringContainsString('Fooled you!', (string)$errorHandler->response->getBody(), 'message missing.');
  337. }
  338. /**
  339. * tests it is possible to load a plugin exception renderer
  340. *
  341. * @return void
  342. */
  343. public function testLoadPluginHandler()
  344. {
  345. $this->loadPlugins(['TestPlugin']);
  346. $errorHandler = new TestErrorHandler([
  347. 'exceptionRenderer' => 'TestPlugin.TestPluginExceptionRenderer',
  348. ]);
  349. $error = new NotFoundException('Kaboom!');
  350. $errorHandler->handleException($error);
  351. $result = $errorHandler->response;
  352. $this->assertSame('Rendered by test plugin', (string)$result);
  353. }
  354. /**
  355. * test handleFatalError generating a page.
  356. *
  357. * These tests start two buffers as handleFatalError blows the outer one up.
  358. *
  359. * @return void
  360. */
  361. public function testHandleFatalErrorPage()
  362. {
  363. $line = __LINE__;
  364. $errorHandler = new TestErrorHandler();
  365. Configure::write('debug', true);
  366. $errorHandler->handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  367. $result = (string)$errorHandler->response->getBody();
  368. $this->assertStringContainsString('Something wrong', $result, 'message missing.');
  369. $this->assertStringContainsString(__FILE__, $result, 'filename missing.');
  370. $this->assertStringContainsString((string)$line, $result, 'line missing.');
  371. Configure::write('debug', false);
  372. $errorHandler->handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  373. $result = (string)$errorHandler->response->getBody();
  374. $this->assertStringNotContainsString('Something wrong', $result, 'message must not appear.');
  375. $this->assertStringNotContainsString(__FILE__, $result, 'filename must not appear.');
  376. $this->assertStringContainsString('An Internal Error Has Occurred.', $result);
  377. }
  378. /**
  379. * test handleFatalError generating log.
  380. *
  381. * @return void
  382. */
  383. public function testHandleFatalErrorLog()
  384. {
  385. $this->_logger->expects($this->at(0))
  386. ->method('log')
  387. ->with('error', $this->logicalAnd(
  388. $this->stringContains(__FILE__ . ', line ' . (__LINE__ + 9)),
  389. $this->stringContains('Fatal Error (1)'),
  390. $this->stringContains('Something wrong')
  391. ));
  392. $this->_logger->expects($this->at(1))
  393. ->method('log')
  394. ->with('error', $this->stringContains('[Cake\Error\FatalErrorException] Something wrong'));
  395. $errorHandler = new TestErrorHandler(['log' => true]);
  396. $errorHandler->handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
  397. }
  398. /**
  399. * Data provider for memory limit changing.
  400. *
  401. * @return array
  402. */
  403. public function memoryLimitProvider()
  404. {
  405. return [
  406. // start, adjust, expected
  407. ['256M', 4, '262148K'],
  408. ['262144K', 4, '262148K'],
  409. ['1G', 128, '1048704K'],
  410. ];
  411. }
  412. /**
  413. * Test increasing the memory limit.
  414. *
  415. * @dataProvider memoryLimitProvider
  416. * @return void
  417. */
  418. public function testIncreaseMemoryLimit($start, $adjust, $expected)
  419. {
  420. $initial = ini_get('memory_limit');
  421. $this->skipIf(strlen($initial) === 0, 'Cannot read memory limit, and cannot test increasing it.');
  422. // phpunit.xml often has -1 as memory limit
  423. ini_set('memory_limit', $start);
  424. $errorHandler = new TestErrorHandler();
  425. $this->assertNull($errorHandler->increaseMemoryLimit($adjust));
  426. $new = ini_get('memory_limit');
  427. $this->assertEquals($expected, $new, 'memory limit did not get increased.');
  428. ini_set('memory_limit', $initial);
  429. }
  430. }