PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

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