PageRenderTime 94ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

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