PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/error.test.php

https://github.com/Forbin/cakephp2x
PHP | 605 lines | 264 code | 48 blank | 293 comment | 3 complexity | 468ab35ad048f897dd425366aa24b0af MD5 | raw file
  1. <?php
  2. /**
  3. * ErrorHandlerTest file
  4. *
  5. * PHP Version 5.x
  6. *
  7. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  8. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs
  17. * @since CakePHP(tm) v 1.2.0.5432
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. if (class_exists('TestErrorHandler')) {
  21. return;
  22. }
  23. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  24. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  25. }
  26. /**
  27. * BlueberryComponent class
  28. *
  29. * @package cake
  30. * @subpackage cake.tests.cases.libs
  31. */
  32. class BlueberryComponent extends Object {
  33. /**
  34. * testName property
  35. *
  36. * @access public
  37. * @return void
  38. */
  39. var $testName = null;
  40. /**
  41. * initialize method
  42. *
  43. * @access public
  44. * @return void
  45. */
  46. function initialize(&$controller) {
  47. $this->testName = 'BlueberryComponent';
  48. }
  49. }
  50. /**
  51. * BlueberryDispatcher class
  52. *
  53. * @package cake
  54. * @subpackage cake.tests.cases.libs
  55. */
  56. class BlueberryDispatcher extends Dispatcher {
  57. /**
  58. * cakeError method
  59. *
  60. * @access public
  61. * @return void
  62. */
  63. function cakeError($method, $messages = array()) {
  64. $error = new TestErrorHandler($method, $messages);
  65. return $error;
  66. }
  67. }
  68. /**
  69. * Short description for class.
  70. *
  71. * @package cake
  72. * @subpackage cake.tests.cases.libs
  73. */
  74. class AuthBlueberryUser extends CakeTestModel {
  75. /**
  76. * name property
  77. *
  78. * @var string 'AuthBlueberryUser'
  79. * @access public
  80. */
  81. var $name = 'AuthBlueberryUser';
  82. /**
  83. * useTable property
  84. *
  85. * @var string
  86. * @access public
  87. */
  88. var $useTable = false;
  89. }
  90. if (!class_exists('AppController')) {
  91. /**
  92. * AppController class
  93. *
  94. * @package cake
  95. * @subpackage cake.tests.cases.libs
  96. */
  97. class AppController extends Controller {
  98. /**
  99. * components property
  100. *
  101. * @access public
  102. * @return void
  103. */
  104. var $components = array('Blueberry');
  105. /**
  106. * beforeRender method
  107. *
  108. * @access public
  109. * @return void
  110. */
  111. function beforeRender() {
  112. echo $this->Blueberry->testName;
  113. }
  114. /**
  115. * header method
  116. *
  117. * @access public
  118. * @return void
  119. */
  120. function header($header) {
  121. echo $header;
  122. }
  123. /**
  124. * _stop method
  125. *
  126. * @access public
  127. * @return void
  128. */
  129. function _stop($status = 0) {
  130. echo 'Stopped with status: ' . $status;
  131. }
  132. }
  133. } elseif (!defined('APP_CONTROLLER_EXISTS')){
  134. define('APP_CONTROLLER_EXISTS', true);
  135. }
  136. App::import('Core', array('Error', 'Controller'));
  137. /**
  138. * TestErrorController class
  139. *
  140. * @package cake
  141. * @subpackage cake.tests.cases.libs
  142. */
  143. class TestErrorController extends AppController {
  144. /**
  145. * uses property
  146. *
  147. * @var array
  148. * @access public
  149. */
  150. var $uses = array();
  151. /**
  152. * index method
  153. *
  154. * @access public
  155. * @return void
  156. */
  157. function index() {
  158. $this->autoRender = false;
  159. return 'what up';
  160. }
  161. }
  162. /**
  163. * BlueberryController class
  164. *
  165. * @package cake
  166. * @subpackage cake.tests.cases.libs
  167. */
  168. class BlueberryController extends AppController {
  169. /**
  170. * name property
  171. *
  172. * @access public
  173. * @return void
  174. */
  175. var $name = 'BlueberryController';
  176. /**
  177. * uses property
  178. *
  179. * @access public
  180. * @return void
  181. */
  182. var $uses = array('AuthBlueberryUser');
  183. /**
  184. * components property
  185. *
  186. * @access public
  187. * @return void
  188. */
  189. var $components = array('Auth');
  190. }
  191. /**
  192. * MyCustomErrorHandler class
  193. *
  194. * @package cake
  195. * @subpackage cake.tests.cases.libs
  196. */
  197. class MyCustomErrorHandler extends ErrorHandler {
  198. /**
  199. * custom error message type.
  200. *
  201. * @return void
  202. */
  203. function missingWidgetThing() {
  204. echo 'widget thing is missing';
  205. }
  206. /**
  207. * stop method
  208. *
  209. * @access public
  210. * @return void
  211. */
  212. function _stop() {
  213. return;
  214. }
  215. }
  216. /**
  217. * TestErrorHandler class
  218. *
  219. * @package cake
  220. * @subpackage cake.tests.cases.libs
  221. */
  222. class TestErrorHandler extends ErrorHandler {
  223. /**
  224. * stop method
  225. *
  226. * @access public
  227. * @return void
  228. */
  229. function _stop() {
  230. return;
  231. }
  232. }
  233. /**
  234. * ErrorHandlerTest class
  235. *
  236. * @package cake
  237. * @subpackage cake.tests.cases.libs
  238. */
  239. class ErrorHandlerTest extends CakeTestCase {
  240. /**
  241. * skip method
  242. *
  243. * @access public
  244. * @return void
  245. */
  246. function skip() {
  247. $this->skipIf(PHP_SAPI === 'cli', '%s Cannot be run from console');
  248. }
  249. /**
  250. * test that methods declared in an ErrorHandler subclass are not converted
  251. * into error404 when debug == 0
  252. *
  253. * @return void
  254. */
  255. function testSubclassMethodsNotBeingConvertedToError() {
  256. $back = Configure::read('debug');
  257. Configure::write('debug', 2);
  258. ob_start();
  259. $ErrorHandler = new MyCustomErrorHandler('missingWidgetThing', array('message' => 'doh!'));
  260. $result = ob_get_clean();
  261. $this->assertEqual($result, 'widget thing is missing');
  262. Configure::write('debug', 0);
  263. ob_start();
  264. $ErrorHandler = new MyCustomErrorHandler('missingWidgetThing', array('message' => 'doh!'));
  265. $result = ob_get_clean();
  266. $this->assertEqual($result, 'widget thing is missing', 'Method declared in subclass converted to error404. %s');
  267. Configure::write('debug', 0);
  268. ob_start();
  269. $ErrorHandler = new MyCustomErrorHandler('missingController', array('message' => 'Page not found'));
  270. $result = ob_get_clean();
  271. $this->assertPattern('/Not Found/', $result, 'Method declared in error handler not converted to error404. %s');
  272. Configure::write('debug', $back);
  273. }
  274. /**
  275. * testError method
  276. *
  277. * @access public
  278. * @return void
  279. */
  280. function testError() {
  281. ob_start();
  282. $TestErrorHandler = new TestErrorHandler('error404', array('message' => 'Page not found'));
  283. ob_clean();
  284. ob_start();
  285. $TestErrorHandler->error(array(
  286. 'code' => 404,
  287. 'message' => 'Page not Found',
  288. 'name' => "Couldn't find what you were looking for"
  289. ));
  290. $result = ob_get_clean();
  291. $this->assertPattern("/<h2>Couldn't find what you were looking for<\/h2>/", $result);
  292. $this->assertPattern('/Page not Found/', $result);
  293. }
  294. /**
  295. * testError404 method
  296. *
  297. * @access public
  298. * @return void
  299. */
  300. function testError404() {
  301. ob_start();
  302. $TestErrorHandler = new TestErrorHandler('error404', array('message' => 'Page not found', 'url' => '/test_error'));
  303. $result = ob_get_clean();
  304. $this->assertPattern('/<h2>Not Found<\/h2>/', $result);
  305. $this->assertPattern("/<strong>'\/test_error'<\/strong>/", $result);
  306. ob_start();
  307. $TestErrorHandler = new TestErrorHandler('error404', array('message' => 'Page not found'));
  308. ob_get_clean();
  309. ob_start();
  310. $TestErrorHandler->error404(array(
  311. 'url' => 'pages/<span id=333>pink</span></id><script>document.body.style.background = t=document.getElementById(333).innerHTML;window.alert(t);</script>',
  312. 'message' => 'Page not found'
  313. ));
  314. $result = ob_get_clean();
  315. $this->assertNoPattern('#<script>#', $result);
  316. $this->assertNoPattern('#</script>#', $result);
  317. }
  318. /**
  319. * testError500 method
  320. *
  321. * @access public
  322. * @return void
  323. */
  324. function testError500() {
  325. ob_start();
  326. $TestErrorHandler = new TestErrorHandler('error500', array(
  327. 'message' => 'An Internal Error Has Occurred'
  328. ));
  329. $result = ob_get_clean();
  330. $this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
  331. ob_start();
  332. $TestErrorHandler = new TestErrorHandler('error500', array(
  333. 'message' => 'An Internal Error Has Occurred',
  334. 'code' => '500'
  335. ));
  336. $result = ob_get_clean();
  337. $this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
  338. }
  339. /**
  340. * testMissingController method
  341. *
  342. * @access public
  343. * @return void
  344. */
  345. function testMissingController() {
  346. $this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController');
  347. ob_start();
  348. $TestErrorHandler = new TestErrorHandler('missingController', array('className' => 'PostsController'));
  349. $result = ob_get_clean();
  350. $this->assertPattern('/<h2>Missing Controller<\/h2>/', $result);
  351. $this->assertPattern('/<em>PostsController<\/em>/', $result);
  352. $this->assertPattern('/BlueberryComponent/', $result);
  353. }
  354. /**
  355. * testMissingAction method
  356. *
  357. * @access public
  358. * @return void
  359. */
  360. function testMissingAction() {
  361. ob_start();
  362. $TestErrorHandler = new TestErrorHandler('missingAction', array('className' => 'PostsController', 'action' => 'index'));
  363. $result = ob_get_clean();
  364. $this->assertPattern('/<h2>Missing Method in PostsController<\/h2>/', $result);
  365. $this->assertPattern('/<em>PostsController::<\/em><em>index\(\)<\/em>/', $result);
  366. ob_start();
  367. $dispatcher = new BlueberryDispatcher('/blueberry/inexistent');
  368. $result = ob_get_clean();
  369. $this->assertPattern('/<h2>Missing Method in BlueberryController<\/h2>/', $result);
  370. $this->assertPattern('/<em>BlueberryController::<\/em><em>inexistent\(\)<\/em>/', $result);
  371. $this->assertNoPattern('/Location: (.*)\/users\/login/', $result);
  372. $this->assertNoPattern('/Stopped with status: 0/', $result);
  373. }
  374. /**
  375. * testPrivateAction method
  376. *
  377. * @access public
  378. * @return void
  379. */
  380. function testPrivateAction() {
  381. ob_start();
  382. $TestErrorHandler = new TestErrorHandler('privateAction', array('className' => 'PostsController', 'action' => '_secretSauce'));
  383. $result = ob_get_clean();
  384. $this->assertPattern('/<h2>Private Method in PostsController<\/h2>/', $result);
  385. $this->assertPattern('/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/', $result);
  386. }
  387. /**
  388. * testMissingTable method
  389. *
  390. * @access public
  391. * @return void
  392. */
  393. function testMissingTable() {
  394. ob_start();
  395. $TestErrorHandler = new TestErrorHandler('missingTable', array('className' => 'Article', 'table' => 'articles'));
  396. $result = ob_get_clean();
  397. $this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result);
  398. $this->assertPattern('/<h2>Missing Database Table<\/h2>/', $result);
  399. $this->assertPattern('/table <em>articles<\/em> for model <em>Article<\/em>/', $result);
  400. }
  401. /**
  402. * testMissingDatabase method
  403. *
  404. * @access public
  405. * @return void
  406. */
  407. function testMissingDatabase() {
  408. ob_start();
  409. $TestErrorHandler = new TestErrorHandler('missingDatabase', array());
  410. $result = ob_get_clean();
  411. $this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result);
  412. $this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
  413. $this->assertPattern('/Confirm you have created the file/', $result);
  414. }
  415. /**
  416. * testMissingView method
  417. *
  418. * @access public
  419. * @return void
  420. */
  421. function testMissingView() {
  422. restore_error_handler();
  423. ob_start();
  424. $TestErrorHandler = new TestErrorHandler('missingView', array('className' => 'Pages', 'action' => 'display', 'file' => 'pages/about.ctp', 'base' => ''));
  425. $expected = ob_get_clean();
  426. set_error_handler('simpleTestErrorHandler');
  427. $this->assertPattern("/PagesController::/", $expected);
  428. $this->assertPattern("/pages\/about.ctp/", $expected);
  429. }
  430. /**
  431. * testMissingLayout method
  432. *
  433. * @access public
  434. * @return void
  435. */
  436. function testMissingLayout() {
  437. restore_error_handler();
  438. ob_start();
  439. $TestErrorHandler = new TestErrorHandler('missingLayout', array( 'layout' => 'my_layout', 'file' => 'layouts/my_layout.ctp', 'base' => ''));
  440. $expected = ob_get_clean();
  441. set_error_handler('simpleTestErrorHandler');
  442. $this->assertPattern("/Missing Layout/", $expected);
  443. $this->assertPattern("/layouts\/my_layout.ctp/", $expected);
  444. }
  445. /**
  446. * testMissingConnection method
  447. *
  448. * @access public
  449. * @return void
  450. */
  451. function testMissingConnection() {
  452. ob_start();
  453. $TestErrorHandler = new TestErrorHandler('missingConnection', array('className' => 'Article'));
  454. $result = ob_get_clean();
  455. $this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
  456. $this->assertPattern('/Article requires a database connection/', $result);
  457. }
  458. /**
  459. * testMissingHelperFile method
  460. *
  461. * @access public
  462. * @return void
  463. */
  464. function testMissingHelperFile() {
  465. ob_start();
  466. $TestErrorHandler = new TestErrorHandler('missingHelperFile', array('helper' => 'MyCustom', 'file' => 'my_custom.php'));
  467. $result = ob_get_clean();
  468. $this->assertPattern('/<h2>Missing Helper File<\/h2>/', $result);
  469. $this->assertPattern('/Create the class below in file:/', $result);
  470. $this->assertPattern('/(\/|\\\)my_custom.php/', $result);
  471. }
  472. /**
  473. * testMissingHelperClass method
  474. *
  475. * @access public
  476. * @return void
  477. */
  478. function testMissingHelperClass() {
  479. ob_start();
  480. $TestErrorHandler = new TestErrorHandler('missingHelperClass', array('helper' => 'MyCustom', 'file' => 'my_custom.php'));
  481. $result = ob_get_clean();
  482. $this->assertPattern('/<h2>Missing Helper Class<\/h2>/', $result);
  483. $this->assertPattern('/The helper class <em>MyCustomHelper<\/em> can not be found or does not exist./', $result);
  484. $this->assertPattern('/(\/|\\\)my_custom.php/', $result);
  485. }
  486. /**
  487. * test missingBehaviorFile method
  488. *
  489. * @access public
  490. * @return void
  491. */
  492. function testMissingBehaviorFile() {
  493. ob_start();
  494. $TestErrorHandler = new TestErrorHandler('missingBehaviorFile', array('behavior' => 'MyCustom', 'file' => 'my_custom.php'));
  495. $result = ob_get_clean();
  496. $this->assertPattern('/<h2>Missing Behavior File<\/h2>/', $result);
  497. $this->assertPattern('/Create the class below in file:/', $result);
  498. $this->assertPattern('/(\/|\\\)my_custom.php/', $result);
  499. }
  500. /**
  501. * test MissingBehaviorClass method
  502. *
  503. * @access public
  504. * @return void
  505. */
  506. function testMissingBehaviorClass() {
  507. ob_start();
  508. $TestErrorHandler = new TestErrorHandler('missingBehaviorClass', array('behavior' => 'MyCustom', 'file' => 'my_custom.php'));
  509. $result = ob_get_clean();
  510. $this->assertPattern('/<h2>Missing Behavior Class<\/h2>/', $result);
  511. $this->assertPattern('/The behavior class <em>MyCustomBehavior<\/em> can not be found or does not exist./', $result);
  512. $this->assertPattern('/(\/|\\\)my_custom.php/', $result);
  513. }
  514. /**
  515. * testMissingComponentFile method
  516. *
  517. * @access public
  518. * @return void
  519. */
  520. function testMissingComponentFile() {
  521. ob_start();
  522. $TestErrorHandler = new TestErrorHandler('missingComponentFile', array('className' => 'PostsController', 'component' => 'Sidebox', 'file' => 'sidebox.php'));
  523. $result = ob_get_clean();
  524. $this->assertPattern('/<h2>Missing Component File<\/h2>/', $result);
  525. $this->assertPattern('/Create the class <em>SideboxComponent<\/em> in file:/', $result);
  526. $this->assertPattern('/(\/|\\\)sidebox.php/', $result);
  527. }
  528. /**
  529. * testMissingComponentClass method
  530. *
  531. * @access public
  532. * @return void
  533. */
  534. function testMissingComponentClass() {
  535. ob_start();
  536. $TestErrorHandler = new TestErrorHandler('missingComponentClass', array('className' => 'PostsController', 'component' => 'Sidebox', 'file' => 'sidebox.php'));
  537. $result = ob_get_clean();
  538. $this->assertPattern('/<h2>Missing Component Class<\/h2>/', $result);
  539. $this->assertPattern('/Create the class <em>SideboxComponent<\/em> in file:/', $result);
  540. $this->assertPattern('/(\/|\\\)sidebox.php/', $result);
  541. }
  542. /**
  543. * testMissingModel method
  544. *
  545. * @access public
  546. * @return void
  547. */
  548. function testMissingModel() {
  549. ob_start();
  550. $TestErrorHandler = new TestErrorHandler('missingModel', array('className' => 'Article', 'file' => 'article.php'));
  551. $result = ob_get_clean();
  552. $this->assertPattern('/<h2>Missing Model<\/h2>/', $result);
  553. $this->assertPattern('/<em>Article<\/em> could not be found./', $result);
  554. $this->assertPattern('/(\/|\\\)article.php/', $result);
  555. }
  556. }
  557. ?>