PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/tests/cases/libs/error.test.php

https://bitbucket.org/mattraykowski/ryzomcore_test
PHP | 630 lines | 280 code | 51 blank | 299 comment | 3 complexity | b1ec53de64a1f8706d313d99d885915a MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * ErrorHandlerTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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(
  270. 'className' => 'Missing', 'message' => 'Page not found'
  271. ));
  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. App::build(array(
  304. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS)
  305. ), true);
  306. ob_start();
  307. $TestErrorHandler = new TestErrorHandler('error404', array('message' => 'Page not found', 'url' => '/test_error'));
  308. $result = ob_get_clean();
  309. $this->assertPattern('/<h2>Not Found<\/h2>/', $result);
  310. $this->assertPattern("/<strong>'\/test_error'<\/strong>/", $result);
  311. ob_start();
  312. $TestErrorHandler =& new TestErrorHandler('error404', array('message' => 'Page not found'));
  313. ob_get_clean();
  314. ob_start();
  315. $TestErrorHandler->error404(array(
  316. 'url' => 'pages/<span id=333>pink</span></id><script>document.body.style.background = t=document.getElementById(333).innerHTML;window.alert(t);</script>',
  317. 'message' => 'Page not found'
  318. ));
  319. $result = ob_get_clean();
  320. $this->assertNoPattern('#<script>#', $result);
  321. $this->assertNoPattern('#</script>#', $result);
  322. App::build();
  323. }
  324. /**
  325. * testError500 method
  326. *
  327. * @access public
  328. * @return void
  329. */
  330. function testError500() {
  331. ob_start();
  332. $TestErrorHandler = new TestErrorHandler('error500', array(
  333. 'message' => 'An Internal Error Has Occurred'
  334. ));
  335. $result = ob_get_clean();
  336. $this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
  337. ob_start();
  338. $TestErrorHandler = new TestErrorHandler('error500', array(
  339. 'message' => 'An Internal Error Has Occurred',
  340. 'code' => '500'
  341. ));
  342. $result = ob_get_clean();
  343. $this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
  344. }
  345. /**
  346. * testMissingController method
  347. *
  348. * @access public
  349. * @return void
  350. */
  351. function testMissingController() {
  352. $this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController');
  353. ob_start();
  354. $TestErrorHandler = new TestErrorHandler('missingController', array('className' => 'PostsController'));
  355. $result = ob_get_clean();
  356. $this->assertPattern('/<h2>Missing Controller<\/h2>/', $result);
  357. $this->assertPattern('/<em>PostsController<\/em>/', $result);
  358. $this->assertPattern('/BlueberryComponent/', $result);
  359. }
  360. /**
  361. * testMissingAction method
  362. *
  363. * @access public
  364. * @return void
  365. */
  366. function testMissingAction() {
  367. ob_start();
  368. $TestErrorHandler = new TestErrorHandler('missingAction', array('className' => 'PostsController', 'action' => 'index'));
  369. $result = ob_get_clean();
  370. $this->assertPattern('/<h2>Missing Method in PostsController<\/h2>/', $result);
  371. $this->assertPattern('/<em>PostsController::<\/em><em>index\(\)<\/em>/', $result);
  372. ob_start();
  373. $dispatcher = new BlueberryDispatcher('/blueberry/inexistent');
  374. $result = ob_get_clean();
  375. $this->assertPattern('/<h2>Missing Method in BlueberryController<\/h2>/', $result);
  376. $this->assertPattern('/<em>BlueberryController::<\/em><em>inexistent\(\)<\/em>/', $result);
  377. $this->assertNoPattern('/Location: (.*)\/users\/login/', $result);
  378. $this->assertNoPattern('/Stopped with status: 0/', $result);
  379. }
  380. /**
  381. * testPrivateAction method
  382. *
  383. * @access public
  384. * @return void
  385. */
  386. function testPrivateAction() {
  387. ob_start();
  388. $TestErrorHandler = new TestErrorHandler('privateAction', array('className' => 'PostsController', 'action' => '_secretSauce'));
  389. $result = ob_get_clean();
  390. $this->assertPattern('/<h2>Private Method in PostsController<\/h2>/', $result);
  391. $this->assertPattern('/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/', $result);
  392. }
  393. /**
  394. * testMissingTable method
  395. *
  396. * @access public
  397. * @return void
  398. */
  399. function testMissingTable() {
  400. ob_start();
  401. $TestErrorHandler = new TestErrorHandler('missingTable', array('className' => 'Article', 'table' => 'articles'));
  402. $result = ob_get_clean();
  403. $this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result);
  404. $this->assertPattern('/<h2>Missing Database Table<\/h2>/', $result);
  405. $this->assertPattern('/table <em>articles<\/em> for model <em>Article<\/em>/', $result);
  406. }
  407. /**
  408. * testMissingDatabase method
  409. *
  410. * @access public
  411. * @return void
  412. */
  413. function testMissingDatabase() {
  414. ob_start();
  415. $TestErrorHandler = new TestErrorHandler('missingDatabase', array());
  416. $result = ob_get_clean();
  417. $this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result);
  418. $this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
  419. $this->assertPattern('/Confirm you have created the file/', $result);
  420. }
  421. /**
  422. * testMissingView method
  423. *
  424. * @access public
  425. * @return void
  426. */
  427. function testMissingView() {
  428. restore_error_handler();
  429. ob_start();
  430. $TestErrorHandler = new TestErrorHandler('missingView', array('className' => 'Pages', 'action' => 'display', 'file' => 'pages/about.ctp', 'base' => ''));
  431. $expected = ob_get_clean();
  432. set_error_handler('simpleTestErrorHandler');
  433. $this->assertPattern("/PagesController::/", $expected);
  434. $this->assertPattern("/pages\/about.ctp/", $expected);
  435. }
  436. /**
  437. * testMissingLayout method
  438. *
  439. * @access public
  440. * @return void
  441. */
  442. function testMissingLayout() {
  443. restore_error_handler();
  444. ob_start();
  445. $TestErrorHandler = new TestErrorHandler('missingLayout', array( 'layout' => 'my_layout', 'file' => 'layouts/my_layout.ctp', 'base' => ''));
  446. $expected = ob_get_clean();
  447. set_error_handler('simpleTestErrorHandler');
  448. $this->assertPattern("/Missing Layout/", $expected);
  449. $this->assertPattern("/layouts\/my_layout.ctp/", $expected);
  450. }
  451. /**
  452. * testMissingConnection method
  453. *
  454. * @access public
  455. * @return void
  456. */
  457. function testMissingConnection() {
  458. ob_start();
  459. $TestErrorHandler = new TestErrorHandler('missingConnection', array('className' => 'Article'));
  460. $result = ob_get_clean();
  461. $this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
  462. $this->assertPattern('/Article requires a database connection/', $result);
  463. }
  464. /**
  465. * testMissingHelperFile method
  466. *
  467. * @access public
  468. * @return void
  469. */
  470. function testMissingHelperFile() {
  471. ob_start();
  472. $TestErrorHandler = new TestErrorHandler('missingHelperFile', array('helper' => 'MyCustom', 'file' => 'my_custom.php'));
  473. $result = ob_get_clean();
  474. $this->assertPattern('/<h2>Missing Helper File<\/h2>/', $result);
  475. $this->assertPattern('/Create the class below in file:/', $result);
  476. $this->assertPattern('/(\/|\\\)my_custom.php/', $result);
  477. }
  478. /**
  479. * testMissingHelperClass method
  480. *
  481. * @access public
  482. * @return void
  483. */
  484. function testMissingHelperClass() {
  485. ob_start();
  486. $TestErrorHandler = new TestErrorHandler('missingHelperClass', array('helper' => 'MyCustom', 'file' => 'my_custom.php'));
  487. $result = ob_get_clean();
  488. $this->assertPattern('/<h2>Missing Helper Class<\/h2>/', $result);
  489. $this->assertPattern('/The helper class <em>MyCustomHelper<\/em> can not be found or does not exist./', $result);
  490. $this->assertPattern('/(\/|\\\)my_custom.php/', $result);
  491. }
  492. /**
  493. * test missingBehaviorFile method
  494. *
  495. * @access public
  496. * @return void
  497. */
  498. function testMissingBehaviorFile() {
  499. ob_start();
  500. $TestErrorHandler = new TestErrorHandler('missingBehaviorFile', array('behavior' => 'MyCustom', 'file' => 'my_custom.php'));
  501. $result = ob_get_clean();
  502. $this->assertPattern('/<h2>Missing Behavior File<\/h2>/', $result);
  503. $this->assertPattern('/Create the class below in file:/', $result);
  504. $this->assertPattern('/(\/|\\\)my_custom.php/', $result);
  505. }
  506. /**
  507. * test MissingBehaviorClass method
  508. *
  509. * @access public
  510. * @return void
  511. */
  512. function testMissingBehaviorClass() {
  513. ob_start();
  514. $TestErrorHandler = new TestErrorHandler('missingBehaviorClass', array('behavior' => 'MyCustom', 'file' => 'my_custom.php'));
  515. $result = ob_get_clean();
  516. $this->assertPattern('/<h2>Missing Behavior Class<\/h2>/', $result);
  517. $this->assertPattern('/The behavior class <em>MyCustomBehavior<\/em> can not be found or does not exist./', $result);
  518. $this->assertPattern('/(\/|\\\)my_custom.php/', $result);
  519. }
  520. /**
  521. * testMissingComponentFile method
  522. *
  523. * @access public
  524. * @return void
  525. */
  526. function testMissingComponentFile() {
  527. ob_start();
  528. $TestErrorHandler = new TestErrorHandler('missingComponentFile', array('className' => 'PostsController', 'component' => 'Sidebox', 'file' => 'sidebox.php'));
  529. $result = ob_get_clean();
  530. $this->assertPattern('/<h2>Missing Component File<\/h2>/', $result);
  531. $this->assertPattern('/Create the class <em>SideboxComponent<\/em> in file:/', $result);
  532. $this->assertPattern('/(\/|\\\)sidebox.php/', $result);
  533. }
  534. /**
  535. * testMissingComponentClass method
  536. *
  537. * @access public
  538. * @return void
  539. */
  540. function testMissingComponentClass() {
  541. ob_start();
  542. $TestErrorHandler = new TestErrorHandler('missingComponentClass', array('className' => 'PostsController', 'component' => 'Sidebox', 'file' => 'sidebox.php'));
  543. $result = ob_get_clean();
  544. $this->assertPattern('/<h2>Missing Component Class<\/h2>/', $result);
  545. $this->assertPattern('/Create the class <em>SideboxComponent<\/em> in file:/', $result);
  546. $this->assertPattern('/(\/|\\\)sidebox.php/', $result);
  547. }
  548. /**
  549. * testMissingModel method
  550. *
  551. * @access public
  552. * @return void
  553. */
  554. function testMissingModel() {
  555. ob_start();
  556. $TestErrorHandler = new TestErrorHandler('missingModel', array('className' => 'Article', 'file' => 'article.php'));
  557. $result = ob_get_clean();
  558. $this->assertPattern('/<h2>Missing Model<\/h2>/', $result);
  559. $this->assertPattern('/<em>Article<\/em> could not be found./', $result);
  560. $this->assertPattern('/(\/|\\\)article.php/', $result);
  561. }
  562. /**
  563. * testing that having a code => 500 in the cakeError call makes an
  564. * internal server error.
  565. *
  566. * @return void
  567. */
  568. function testThatCode500Works() {
  569. Configure::write('debug', 0);
  570. ob_start();
  571. $TestErrorHandler = new TestErrorHandler('missingTable', array(
  572. 'className' => 'Article',
  573. 'table' => 'articles',
  574. 'code' => 500
  575. ));
  576. $result = ob_get_clean();
  577. $this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
  578. }
  579. }