PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Error/ErrorHandlerTest.php

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 384 lines | 220 code | 49 blank | 115 comment | 8 complexity | e424305eb5bcabfc8229760b7f9c81ec MD5 | raw file
  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. * @package Cake.Test.Case.Error
  15. * @since CakePHP(tm) v 1.2.0.5432
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ErrorHandler', 'Error');
  19. App::uses('Controller', 'Controller');
  20. App::uses('Router', 'Routing');
  21. /**
  22. * A faulty ExceptionRenderer to test nesting.
  23. */
  24. class FaultyExceptionRenderer extends ExceptionRenderer {
  25. /**
  26. * Dummy rendering implementation.
  27. *
  28. * @return void
  29. * @throws Exception
  30. */
  31. public function render() {
  32. throw new Exception('Error from renderer.');
  33. }
  34. }
  35. /**
  36. * ErrorHandlerTest class
  37. *
  38. * @package Cake.Test.Case.Error
  39. */
  40. class ErrorHandlerTest extends CakeTestCase {
  41. protected $_restoreError = false;
  42. /**
  43. * setup create a request object to get out of router later.
  44. *
  45. * @return void
  46. */
  47. public function setUp() {
  48. parent::setUp();
  49. App::build(array(
  50. 'View' => array(
  51. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
  52. )
  53. ), App::RESET);
  54. Router::reload();
  55. $request = new CakeRequest(null, false);
  56. $request->base = '';
  57. Router::setRequestInfo($request);
  58. Configure::write('debug', 2);
  59. CakeLog::disable('stdout');
  60. CakeLog::disable('stderr');
  61. }
  62. /**
  63. * tearDown
  64. *
  65. * @return void
  66. */
  67. public function tearDown() {
  68. parent::tearDown();
  69. if ($this->_restoreError) {
  70. restore_error_handler();
  71. }
  72. CakeLog::enable('stdout');
  73. CakeLog::enable('stderr');
  74. }
  75. /**
  76. * test error handling when debug is on, an error should be printed from Debugger.
  77. *
  78. * @return void
  79. */
  80. public function testHandleErrorDebugOn() {
  81. set_error_handler('ErrorHandler::handleError');
  82. $this->_restoreError = true;
  83. ob_start();
  84. $wrong .= '';
  85. $result = ob_get_clean();
  86. $this->assertRegExp('/<pre class="cake-error">/', $result);
  87. $this->assertRegExp('/<b>Notice<\/b>/', $result);
  88. $this->assertRegExp('/variable:\s+wrong/', $result);
  89. }
  90. /**
  91. * provides errors for mapping tests.
  92. *
  93. * @return void
  94. */
  95. public static function errorProvider() {
  96. return array(
  97. array(E_USER_NOTICE, 'Notice'),
  98. array(E_USER_WARNING, 'Warning'),
  99. );
  100. }
  101. /**
  102. * test error mappings
  103. *
  104. * @dataProvider errorProvider
  105. * @return void
  106. */
  107. public function testErrorMapping($error, $expected) {
  108. set_error_handler('ErrorHandler::handleError');
  109. $this->_restoreError = true;
  110. ob_start();
  111. trigger_error('Test error', $error);
  112. $result = ob_get_clean();
  113. $this->assertRegExp('/<b>' . $expected . '<\/b>/', $result);
  114. }
  115. /**
  116. * test error prepended by @
  117. *
  118. * @return void
  119. */
  120. public function testErrorSuppressed() {
  121. set_error_handler('ErrorHandler::handleError');
  122. $this->_restoreError = true;
  123. ob_start();
  124. //@codingStandardsIgnoreStart
  125. @include 'invalid.file';
  126. //@codingStandardsIgnoreEnd
  127. $result = ob_get_clean();
  128. $this->assertTrue(empty($result));
  129. }
  130. /**
  131. * Test that errors go into CakeLog when debug = 0.
  132. *
  133. * @return void
  134. */
  135. public function testHandleErrorDebugOff() {
  136. Configure::write('debug', 0);
  137. Configure::write('Error.trace', false);
  138. if (file_exists(LOGS . 'debug.log')) {
  139. unlink(LOGS . 'debug.log');
  140. }
  141. set_error_handler('ErrorHandler::handleError');
  142. $this->_restoreError = true;
  143. $out .= '';
  144. $result = file(LOGS . 'debug.log');
  145. $this->assertEquals(1, count($result));
  146. $this->assertRegExp(
  147. '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
  148. $result[0]
  149. );
  150. if (file_exists(LOGS . 'debug.log')) {
  151. unlink(LOGS . 'debug.log');
  152. }
  153. }
  154. /**
  155. * Test that errors going into CakeLog include traces.
  156. *
  157. * @return void
  158. */
  159. public function testHandleErrorLoggingTrace() {
  160. Configure::write('debug', 0);
  161. Configure::write('Error.trace', true);
  162. if (file_exists(LOGS . 'debug.log')) {
  163. unlink(LOGS . 'debug.log');
  164. }
  165. set_error_handler('ErrorHandler::handleError');
  166. $this->_restoreError = true;
  167. $out .= '';
  168. $result = file(LOGS . 'debug.log');
  169. $this->assertRegExp(
  170. '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} (Notice|Debug): Notice \(8\): Undefined variable:\s+out in \[.+ line \d+\]$/',
  171. $result[0]
  172. );
  173. $this->assertRegExp('/^Trace:/', $result[1]);
  174. $this->assertRegExp('/^ErrorHandlerTest\:\:testHandleErrorLoggingTrace\(\)/', $result[2]);
  175. if (file_exists(LOGS . 'debug.log')) {
  176. unlink(LOGS . 'debug.log');
  177. }
  178. }
  179. /**
  180. * test handleException generating a page.
  181. *
  182. * @return void
  183. */
  184. public function testHandleException() {
  185. $error = new NotFoundException('Kaboom!');
  186. ob_start();
  187. ErrorHandler::handleException($error);
  188. $result = ob_get_clean();
  189. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  190. }
  191. /**
  192. * test handleException generating log.
  193. *
  194. * @return void
  195. */
  196. public function testHandleExceptionLog() {
  197. if (file_exists(LOGS . 'error.log')) {
  198. unlink(LOGS . 'error.log');
  199. }
  200. Configure::write('Exception.log', true);
  201. $error = new NotFoundException('Kaboom!');
  202. ob_start();
  203. ErrorHandler::handleException($error);
  204. $result = ob_get_clean();
  205. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  206. $log = file(LOGS . 'error.log');
  207. $this->assertContains('[NotFoundException] Kaboom!', $log[0], 'message missing.');
  208. $this->assertContains('ErrorHandlerTest->testHandleExceptionLog', $log[2], 'Stack trace missing.');
  209. }
  210. /**
  211. * test handleException generating log.
  212. *
  213. * @return void
  214. */
  215. public function testHandleExceptionLogSkipping() {
  216. if (file_exists(LOGS . 'error.log')) {
  217. unlink(LOGS . 'error.log');
  218. }
  219. Configure::write('Exception.log', true);
  220. Configure::write('Exception.skipLog', array('NotFoundException'));
  221. $notFound = new NotFoundException('Kaboom!');
  222. $forbidden = new ForbiddenException('Fooled you!');
  223. ob_start();
  224. ErrorHandler::handleException($notFound);
  225. $result = ob_get_clean();
  226. $this->assertRegExp('/Kaboom!/', $result, 'message missing.');
  227. ob_start();
  228. ErrorHandler::handleException($forbidden);
  229. $result = ob_get_clean();
  230. $this->assertRegExp('/Fooled you!/', $result, 'message missing.');
  231. $log = file(LOGS . 'error.log');
  232. $this->assertNotContains('[NotFoundException] Kaboom!', $log[0], 'message should not be logged.');
  233. $this->assertContains('[ForbiddenException] Fooled you!', $log[0], 'message missing.');
  234. }
  235. /**
  236. * tests it is possible to load a plugin exception renderer
  237. *
  238. * @return void
  239. */
  240. public function testLoadPluginHandler() {
  241. App::build(array(
  242. 'Plugin' => array(
  243. CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
  244. )
  245. ), App::RESET);
  246. CakePlugin::load('TestPlugin');
  247. Configure::write('Exception.renderer', 'TestPlugin.TestPluginExceptionRenderer');
  248. $error = new NotFoundException('Kaboom!');
  249. ob_start();
  250. ErrorHandler::handleException($error);
  251. $result = ob_get_clean();
  252. $this->assertEquals('Rendered by test plugin', $result);
  253. CakePlugin::unload();
  254. }
  255. /**
  256. * test handleFatalError generating a page.
  257. *
  258. * These tests start two buffers as handleFatalError blows the outer one up.
  259. *
  260. * @return void
  261. */
  262. public function testHandleFatalErrorPage() {
  263. $line = __LINE__;
  264. ob_start();
  265. ob_start();
  266. Configure::write('debug', 1);
  267. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  268. $result = ob_get_clean();
  269. $this->assertContains('Something wrong', $result, 'message missing.');
  270. $this->assertContains(__FILE__, $result, 'filename missing.');
  271. $this->assertContains((string)$line, $result, 'line missing.');
  272. ob_start();
  273. ob_start();
  274. Configure::write('debug', 0);
  275. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, $line);
  276. $result = ob_get_clean();
  277. $this->assertNotContains('Something wrong', $result, 'message must not appear.');
  278. $this->assertNotContains(__FILE__, $result, 'filename must not appear.');
  279. $this->assertContains('An Internal Error Has Occurred', $result);
  280. }
  281. /**
  282. * test handleException generating log.
  283. *
  284. * @return void
  285. */
  286. public function testHandleFatalErrorLog() {
  287. if (file_exists(LOGS . 'error.log')) {
  288. unlink(LOGS . 'error.log');
  289. }
  290. ob_start();
  291. ErrorHandler::handleFatalError(E_ERROR, 'Something wrong', __FILE__, __LINE__);
  292. ob_clean();
  293. $log = file(LOGS . 'error.log');
  294. $this->assertContains(__FILE__, $log[0], 'missing filename');
  295. $this->assertContains('[FatalErrorException] Something wrong', $log[1], 'message missing.');
  296. }
  297. /**
  298. * testExceptionRendererNestingDebug method
  299. *
  300. * @return void
  301. */
  302. public function testExceptionRendererNestingDebug() {
  303. Configure::write('debug', 2);
  304. Configure::write('Exception.renderer', 'FaultyExceptionRenderer');
  305. $result = false;
  306. try {
  307. ob_start();
  308. ob_start();
  309. ErrorHandler::handleFatalError(E_USER_ERROR, 'Initial error', __FILE__, __LINE__);
  310. } catch (Exception $e) {
  311. $result = $e instanceof FatalErrorException;
  312. }
  313. restore_error_handler();
  314. $this->assertTrue($result);
  315. }
  316. /**
  317. * testExceptionRendererNestingProduction method
  318. *
  319. * @return void
  320. */
  321. public function testExceptionRendererNestingProduction() {
  322. Configure::write('debug', 0);
  323. Configure::write('Exception.renderer', 'FaultyExceptionRenderer');
  324. $result = false;
  325. try {
  326. ob_start();
  327. ob_start();
  328. ErrorHandler::handleFatalError(E_USER_ERROR, 'Initial error', __FILE__, __LINE__);
  329. } catch (Exception $e) {
  330. $result = $e instanceof InternalErrorException;
  331. }
  332. restore_error_handler();
  333. $this->assertTrue($result);
  334. }
  335. }