PageRenderTime 35ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 1498 lines | 822 code | 182 blank | 494 comment | 7 complexity | cebb1598108b4de910a0cb37f91d24bc MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * ControllerTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT 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://cakephp.org CakePHP Project
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs.controller
  17. * @since CakePHP(tm) v 1.2.0.5436
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. App::import('Controller', 'Controller', false);
  21. App::import('Component', 'Security');
  22. App::import('Component', 'Cookie');
  23. /**
  24. * AppController class
  25. *
  26. * @package cake
  27. * @subpackage cake.tests.cases.libs.controller
  28. */
  29. if (!class_exists('AppController')) {
  30. /**
  31. * AppController class
  32. *
  33. * @package cake
  34. * @subpackage cake.tests.cases.libs.controller
  35. */
  36. class AppController extends Controller {
  37. /**
  38. * helpers property
  39. *
  40. * @var array
  41. * @access public
  42. */
  43. var $helpers = array('Html', 'Javascript');
  44. /**
  45. * uses property
  46. *
  47. * @var array
  48. * @access public
  49. */
  50. var $uses = array('ControllerPost');
  51. /**
  52. * components property
  53. *
  54. * @var array
  55. * @access public
  56. */
  57. var $components = array('Cookie');
  58. }
  59. } elseif (!defined('APP_CONTROLLER_EXISTS')) {
  60. define('APP_CONTROLLER_EXISTS', true);
  61. }
  62. /**
  63. * ControllerPost class
  64. *
  65. * @package cake
  66. * @subpackage cake.tests.cases.libs.controller
  67. */
  68. class ControllerPost extends CakeTestModel {
  69. /**
  70. * name property
  71. *
  72. * @var string 'ControllerPost'
  73. * @access public
  74. */
  75. var $name = 'ControllerPost';
  76. /**
  77. * useTable property
  78. *
  79. * @var string 'posts'
  80. * @access public
  81. */
  82. var $useTable = 'posts';
  83. /**
  84. * invalidFields property
  85. *
  86. * @var array
  87. * @access public
  88. */
  89. var $invalidFields = array('name' => 'error_msg');
  90. /**
  91. * lastQuery property
  92. *
  93. * @var mixed null
  94. * @access public
  95. */
  96. var $lastQuery = null;
  97. /**
  98. * beforeFind method
  99. *
  100. * @param mixed $query
  101. * @access public
  102. * @return void
  103. */
  104. function beforeFind($query) {
  105. $this->lastQuery = $query;
  106. }
  107. /**
  108. * find method
  109. *
  110. * @param mixed $type
  111. * @param array $options
  112. * @access public
  113. * @return void
  114. */
  115. function find($type, $options = array()) {
  116. if ($type == 'popular') {
  117. $conditions = array($this->name . '.' . $this->primaryKey .' > ' => '1');
  118. $options = Set::merge($options, compact('conditions'));
  119. return parent::find('all', $options);
  120. }
  121. return parent::find($type, $options);
  122. }
  123. }
  124. /**
  125. * ControllerPostsController class
  126. *
  127. * @package cake
  128. * @subpackage cake.tests.cases.libs.controller
  129. */
  130. class ControllerCommentsController extends AppController {
  131. /**
  132. * name property
  133. *
  134. * @var string 'ControllerPost'
  135. * @access public
  136. */
  137. var $name = 'ControllerComments';
  138. }
  139. /**
  140. * ControllerComment class
  141. *
  142. * @package cake
  143. * @subpackage cake.tests.cases.libs.controller
  144. */
  145. class ControllerComment extends CakeTestModel {
  146. /**
  147. * name property
  148. *
  149. * @var string 'ControllerComment'
  150. * @access public
  151. */
  152. var $name = 'Comment';
  153. /**
  154. * useTable property
  155. *
  156. * @var string 'comments'
  157. * @access public
  158. */
  159. var $useTable = 'comments';
  160. /**
  161. * data property
  162. *
  163. * @var array
  164. * @access public
  165. */
  166. var $data = array('name' => 'Some Name');
  167. /**
  168. * alias property
  169. *
  170. * @var string 'ControllerComment'
  171. * @access public
  172. */
  173. var $alias = 'ControllerComment';
  174. }
  175. /**
  176. * ControllerAlias class
  177. *
  178. * @package cake
  179. * @subpackage cake.tests.cases.libs.controller
  180. */
  181. class ControllerAlias extends CakeTestModel {
  182. /**
  183. * name property
  184. *
  185. * @var string 'ControllerAlias'
  186. * @access public
  187. */
  188. var $name = 'ControllerAlias';
  189. /**
  190. * alias property
  191. *
  192. * @var string 'ControllerSomeAlias'
  193. * @access public
  194. */
  195. var $alias = 'ControllerSomeAlias';
  196. /**
  197. * useTable property
  198. *
  199. * @var string 'posts'
  200. * @access public
  201. */
  202. var $useTable = 'posts';
  203. }
  204. /**
  205. * ControllerPaginateModel class
  206. *
  207. * @package cake
  208. * @subpackage cake.tests.cases.libs.controller
  209. */
  210. class ControllerPaginateModel extends CakeTestModel {
  211. /**
  212. * name property
  213. *
  214. * @var string
  215. * @access public
  216. */
  217. var $name = 'ControllerPaginateModel';
  218. /**
  219. * useTable property
  220. *
  221. * @var string'
  222. * @access public
  223. */
  224. var $useTable = 'comments';
  225. /**
  226. * paginate method
  227. *
  228. * @return void
  229. * @access public
  230. */
  231. function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra) {
  232. $this->extra = $extra;
  233. }
  234. /**
  235. * paginateCount
  236. *
  237. * @access public
  238. * @return void
  239. */
  240. function paginateCount($conditions, $recursive, $extra) {
  241. $this->extraCount = $extra;
  242. }
  243. }
  244. /**
  245. * NameTest class
  246. *
  247. * @package cake
  248. * @subpackage cake.tests.cases.libs.controller
  249. */
  250. class NameTest extends CakeTestModel {
  251. /**
  252. * name property
  253. * @var string 'Name'
  254. * @access public
  255. */
  256. var $name = 'Name';
  257. /**
  258. * useTable property
  259. * @var string 'names'
  260. * @access public
  261. */
  262. var $useTable = 'comments';
  263. /**
  264. * alias property
  265. *
  266. * @var string 'ControllerComment'
  267. * @access public
  268. */
  269. var $alias = 'Name';
  270. }
  271. /**
  272. * TestController class
  273. *
  274. * @package cake
  275. * @subpackage cake.tests.cases.libs.controller
  276. */
  277. class TestController extends AppController {
  278. /**
  279. * name property
  280. * @var string 'Name'
  281. * @access public
  282. */
  283. var $name = 'TestController';
  284. /**
  285. * helpers property
  286. *
  287. * @var array
  288. * @access public
  289. */
  290. var $helpers = array('Session', 'Xml');
  291. /**
  292. * components property
  293. *
  294. * @var array
  295. * @access public
  296. */
  297. var $components = array('Security');
  298. /**
  299. * uses property
  300. *
  301. * @var array
  302. * @access public
  303. */
  304. var $uses = array('ControllerComment', 'ControllerAlias');
  305. /**
  306. * index method
  307. *
  308. * @param mixed $testId
  309. * @param mixed $test2Id
  310. * @access public
  311. * @return void
  312. */
  313. function index($testId, $test2Id) {
  314. $this->data['testId'] = $testId;
  315. $this->data['test2Id'] = $test2Id;
  316. }
  317. }
  318. /**
  319. * TestComponent class
  320. *
  321. * @package cake
  322. * @subpackage cake.tests.cases.libs.controller
  323. */
  324. class TestComponent extends Object {
  325. /**
  326. * beforeRedirect method
  327. *
  328. * @access public
  329. * @return void
  330. */
  331. function beforeRedirect() {
  332. }
  333. /**
  334. * initialize method
  335. *
  336. * @access public
  337. * @return void
  338. */
  339. function initialize(&$controller) {
  340. }
  341. /**
  342. * startup method
  343. *
  344. * @access public
  345. * @return void
  346. */
  347. function startup(&$controller) {
  348. }
  349. /**
  350. * shutdown method
  351. *
  352. * @access public
  353. * @return void
  354. */
  355. function shutdown(&$controller) {
  356. }
  357. /**
  358. * beforeRender callback
  359. *
  360. * @return void
  361. */
  362. function beforeRender(&$controller) {
  363. if ($this->viewclass) {
  364. $controller->view = $this->viewclass;
  365. }
  366. }
  367. }
  368. /**
  369. * AnotherTestController class
  370. *
  371. * @package cake
  372. * @subpackage cake.tests.cases.libs.controller
  373. */
  374. class AnotherTestController extends AppController {
  375. /**
  376. * name property
  377. * @var string 'Name'
  378. * @access public
  379. */
  380. var $name = 'AnotherTest';
  381. /**
  382. * uses property
  383. *
  384. * @var array
  385. * @access public
  386. */
  387. var $uses = null;
  388. }
  389. /**
  390. * ControllerTest class
  391. *
  392. * @package cake
  393. * @subpackage cake.tests.cases.libs.controller
  394. */
  395. class ControllerTest extends CakeTestCase {
  396. /**
  397. * fixtures property
  398. *
  399. * @var array
  400. * @access public
  401. */
  402. var $fixtures = array('core.post', 'core.comment', 'core.name');
  403. /**
  404. * endTest
  405. *
  406. * @access public
  407. * @return void
  408. */
  409. function endTest() {
  410. App::build();
  411. }
  412. /**
  413. * testLoadModel method
  414. *
  415. * @access public
  416. * @return void
  417. */
  418. function testLoadModel() {
  419. $Controller =& new Controller();
  420. $this->assertFalse(isset($Controller->ControllerPost));
  421. $result = $Controller->loadModel('ControllerPost');
  422. $this->assertTrue($result);
  423. $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
  424. $this->assertTrue(in_array('ControllerPost', $Controller->modelNames));
  425. ClassRegistry::flush();
  426. unset($Controller);
  427. }
  428. /**
  429. * testConstructClasses method
  430. *
  431. * @access public
  432. * @return void
  433. */
  434. function testConstructClasses() {
  435. $Controller =& new Controller();
  436. $Controller->modelClass = 'ControllerPost';
  437. $Controller->passedArgs[] = '1';
  438. $Controller->constructClasses();
  439. $this->assertEqual($Controller->ControllerPost->id, 1);
  440. unset($Controller);
  441. $Controller =& new Controller();
  442. $Controller->uses = array('ControllerPost', 'ControllerComment');
  443. $Controller->passedArgs[] = '1';
  444. $Controller->constructClasses();
  445. $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
  446. $this->assertTrue(is_a($Controller->ControllerComment, 'ControllerComment'));
  447. $this->assertEqual($Controller->ControllerComment->name, 'Comment');
  448. unset($Controller);
  449. App::build(array('plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)));
  450. $Controller =& new Controller();
  451. $Controller->uses = array('TestPlugin.TestPluginPost');
  452. $Controller->constructClasses();
  453. $this->assertEqual($Controller->modelClass, 'TestPluginPost');
  454. $this->assertTrue(isset($Controller->TestPluginPost));
  455. $this->assertTrue(is_a($Controller->TestPluginPost, 'TestPluginPost'));
  456. unset($Controller);
  457. }
  458. /**
  459. * testAliasName method
  460. *
  461. * @access public
  462. * @return void
  463. */
  464. function testAliasName() {
  465. $Controller =& new Controller();
  466. $Controller->uses = array('NameTest');
  467. $Controller->constructClasses();
  468. $this->assertEqual($Controller->NameTest->name, 'Name');
  469. $this->assertEqual($Controller->NameTest->alias, 'Name');
  470. unset($Controller);
  471. }
  472. /**
  473. * testPersistent method
  474. *
  475. * @access public
  476. * @return void
  477. */
  478. function testPersistent() {
  479. Configure::write('Cache.disable', false);
  480. $Controller =& new Controller();
  481. $Controller->modelClass = 'ControllerPost';
  482. $Controller->persistModel = true;
  483. $Controller->constructClasses();
  484. $this->assertTrue(file_exists(CACHE . 'persistent' . DS .'controllerpost.php'));
  485. $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
  486. @unlink(CACHE . 'persistent' . DS . 'controllerpost.php');
  487. @unlink(CACHE . 'persistent' . DS . 'controllerpostregistry.php');
  488. unset($Controller);
  489. Configure::write('Cache.disable', true);
  490. }
  491. /**
  492. * testPaginate method
  493. *
  494. * @access public
  495. * @return void
  496. */
  497. function testPaginate() {
  498. $Controller =& new Controller();
  499. $Controller->uses = array('ControllerPost', 'ControllerComment');
  500. $Controller->passedArgs[] = '1';
  501. $Controller->params['url'] = array();
  502. $Controller->constructClasses();
  503. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  504. $this->assertEqual($results, array(1, 2, 3));
  505. $results = Set::extract($Controller->paginate('ControllerComment'), '{n}.ControllerComment.id');
  506. $this->assertEqual($results, array(1, 2, 3, 4, 5, 6));
  507. $Controller->modelClass = null;
  508. $Controller->uses[0] = 'Plugin.ControllerPost';
  509. $results = Set::extract($Controller->paginate(), '{n}.ControllerPost.id');
  510. $this->assertEqual($results, array(1, 2, 3));
  511. $Controller->passedArgs = array('page' => '-1');
  512. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  513. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  514. $this->assertEqual($results, array(1, 2, 3));
  515. $Controller->passedArgs = array('sort' => 'ControllerPost.id', 'direction' => 'asc');
  516. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  517. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  518. $this->assertEqual($results, array(1, 2, 3));
  519. $Controller->passedArgs = array('sort' => 'ControllerPost.id', 'direction' => 'desc');
  520. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  521. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  522. $this->assertEqual($results, array(3, 2, 1));
  523. $Controller->passedArgs = array('sort' => 'id', 'direction' => 'desc');
  524. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  525. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  526. $this->assertEqual($results, array(3, 2, 1));
  527. $Controller->passedArgs = array('sort' => 'NotExisting.field', 'direction' => 'desc');
  528. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  529. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1, 'Invalid field in query %s');
  530. $this->assertEqual($results, array(1, 2, 3));
  531. $Controller->passedArgs = array('sort' => 'ControllerPost.author_id', 'direction' => 'allYourBase');
  532. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  533. $this->assertEqual($Controller->ControllerPost->lastQuery['order'][0], array('ControllerPost.author_id' => 'asc'));
  534. $this->assertEqual($results, array(1, 3, 2));
  535. $Controller->passedArgs = array('page' => '1 " onclick="alert(\'xss\');">');
  536. $Controller->paginate = array('limit' => 1);
  537. $Controller->paginate('ControllerPost');
  538. $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1, 'XSS exploit opened %s');
  539. $this->assertIdentical($Controller->params['paging']['ControllerPost']['options']['page'], 1, 'XSS exploit opened %s');
  540. $Controller->passedArgs = array();
  541. $Controller->paginate = array('limit' => 0);
  542. $Controller->paginate('ControllerPost');
  543. $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1);
  544. $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
  545. $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
  546. $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
  547. $Controller->passedArgs = array();
  548. $Controller->paginate = array('limit' => 'garbage!');
  549. $Controller->paginate('ControllerPost');
  550. $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1);
  551. $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
  552. $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
  553. $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
  554. $Controller->passedArgs = array();
  555. $Controller->paginate = array('limit' => '-1');
  556. $Controller->paginate('ControllerPost');
  557. $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1);
  558. $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
  559. $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
  560. $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
  561. }
  562. /**
  563. * testPaginateExtraParams method
  564. *
  565. * @access public
  566. * @return void
  567. */
  568. function testPaginateExtraParams() {
  569. $Controller =& new Controller();
  570. $Controller->uses = array('ControllerPost', 'ControllerComment');
  571. $Controller->passedArgs[] = '1';
  572. $Controller->params['url'] = array();
  573. $Controller->constructClasses();
  574. $Controller->passedArgs = array('page' => '-1', 'contain' => array('ControllerComment'));
  575. $result = $Controller->paginate('ControllerPost');
  576. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  577. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3));
  578. $this->assertTrue(!isset($Controller->ControllerPost->lastQuery['contain']));
  579. $Controller->passedArgs = array('page' => '-1');
  580. $Controller->paginate = array('ControllerPost' => array('contain' => array('ControllerComment')));
  581. $result = $Controller->paginate('ControllerPost');
  582. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  583. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3));
  584. $this->assertTrue(isset($Controller->ControllerPost->lastQuery['contain']));
  585. $Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title')));
  586. $result = $Controller->paginate('ControllerPost');
  587. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3));
  588. $this->assertEqual($Controller->ControllerPost->lastQuery['conditions'], array('ControllerPost.id > ' => '1'));
  589. $Controller->passedArgs = array('limit' => 12);
  590. $Controller->paginate = array('limit' => 30);
  591. $result = $Controller->paginate('ControllerPost');
  592. $paging = $Controller->params['paging']['ControllerPost'];
  593. $this->assertEqual($Controller->ControllerPost->lastQuery['limit'], 12);
  594. $this->assertEqual($paging['options']['limit'], 12);
  595. $Controller =& new Controller();
  596. $Controller->uses = array('ControllerPaginateModel');
  597. $Controller->params['url'] = array();
  598. $Controller->constructClasses();
  599. $Controller->paginate = array(
  600. 'ControllerPaginateModel' => array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id')
  601. );
  602. $result = $Controller->paginate('ControllerPaginateModel');
  603. $expected = array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id');
  604. $this->assertEqual($Controller->ControllerPaginateModel->extra, $expected);
  605. $this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected);
  606. $Controller->paginate = array(
  607. 'ControllerPaginateModel' => array('foo', 'contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id')
  608. );
  609. $Controller->paginate('ControllerPaginateModel');
  610. $expected = array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id', 'type' => 'foo');
  611. $this->assertEqual($Controller->ControllerPaginateModel->extra, $expected);
  612. $this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected);
  613. }
  614. /**
  615. * testPaginateFieldsDouble method
  616. *
  617. * @return void
  618. * @access public
  619. */
  620. function testPaginateFieldsDouble(){
  621. $Controller =& new Controller();
  622. $Controller->uses = array('ControllerPost');
  623. $Controller->params['url'] = array();
  624. $Controller->constructClasses();
  625. $Controller->paginate = array(
  626. 'fields' => array(
  627. 'ControllerPost.id',
  628. 'radians(180.0) as floatvalue'
  629. ),
  630. 'order' => array('ControllerPost.created'=>'DESC'),
  631. 'limit' => 1,
  632. 'page' => 1,
  633. 'recursive' => -1
  634. );
  635. $conditions = array();
  636. $result = $Controller->paginate('ControllerPost',$conditions);
  637. $expected = array(
  638. array(
  639. 'ControllerPost' => array(
  640. 'id' => 3,
  641. ),
  642. 0 => array(
  643. 'floatvalue' => '3.14159265358979',
  644. ),
  645. ),
  646. );
  647. $this->assertEqual($result, $expected);
  648. }
  649. /**
  650. * testPaginatePassedArgs method
  651. *
  652. * @return void
  653. * @access public
  654. */
  655. function testPaginatePassedArgs() {
  656. $Controller =& new Controller();
  657. $Controller->uses = array('ControllerPost');
  658. $Controller->passedArgs[] = array('1', '2', '3');
  659. $Controller->params['url'] = array();
  660. $Controller->constructClasses();
  661. $Controller->paginate = array(
  662. 'fields' => array(),
  663. 'order' => '',
  664. 'limit' => 5,
  665. 'page' => 1,
  666. 'recursive' => -1
  667. );
  668. $conditions = array();
  669. $Controller->paginate('ControllerPost',$conditions);
  670. $expected = array(
  671. 'fields' => array(),
  672. 'order' => '',
  673. 'limit' => 5,
  674. 'page' => 1,
  675. 'recursive' => -1,
  676. 'conditions' => array()
  677. );
  678. $this->assertEqual($Controller->params['paging']['ControllerPost']['options'],$expected);
  679. }
  680. /**
  681. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  682. *
  683. * @return void
  684. */
  685. function testPaginateSpecialType() {
  686. $Controller =& new Controller();
  687. $Controller->uses = array('ControllerPost', 'ControllerComment');
  688. $Controller->passedArgs[] = '1';
  689. $Controller->params['url'] = array();
  690. $Controller->constructClasses();
  691. $Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title')));
  692. $result = $Controller->paginate('ControllerPost');
  693. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3));
  694. $this->assertEqual($Controller->ControllerPost->lastQuery['conditions'], array('ControllerPost.id > ' => '1'));
  695. $this->assertFalse(isset($Controller->params['paging']['ControllerPost']['defaults'][0]));
  696. $this->assertFalse(isset($Controller->params['paging']['ControllerPost']['options'][0]));
  697. }
  698. /**
  699. * testDefaultPaginateParams method
  700. *
  701. * @access public
  702. * @return void
  703. */
  704. function testDefaultPaginateParams() {
  705. $Controller =& new Controller();
  706. $Controller->modelClass = 'ControllerPost';
  707. $Controller->params['url'] = array();
  708. $Controller->paginate = array('order' => 'ControllerPost.id DESC');
  709. $Controller->constructClasses();
  710. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  711. $this->assertEqual($Controller->params['paging']['ControllerPost']['defaults']['order'], 'ControllerPost.id DESC');
  712. $this->assertEqual($Controller->params['paging']['ControllerPost']['options']['order'], 'ControllerPost.id DESC');
  713. $this->assertEqual($results, array(3, 2, 1));
  714. }
  715. /**
  716. * test paginate() and virtualField interactions
  717. *
  718. * @return void
  719. */
  720. function testPaginateOrderVirtualField() {
  721. $Controller =& new Controller();
  722. $Controller->uses = array('ControllerPost', 'ControllerComment');
  723. $Controller->params['url'] = array();
  724. $Controller->constructClasses();
  725. $Controller->ControllerPost->virtualFields = array(
  726. 'offset_test' => 'ControllerPost.id + 1'
  727. );
  728. $Controller->paginate = array(
  729. 'fields' => array('id', 'title', 'offset_test'),
  730. 'order' => array('offset_test' => 'DESC')
  731. );
  732. $result = $Controller->paginate('ControllerPost');
  733. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(4, 3, 2));
  734. $Controller->passedArgs = array('sort' => 'offset_test', 'direction' => 'asc');
  735. $result = $Controller->paginate('ControllerPost');
  736. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(2, 3, 4));
  737. }
  738. /**
  739. * testFlash method
  740. *
  741. * @access public
  742. * @return void
  743. */
  744. function testFlash() {
  745. $Controller =& new Controller();
  746. $Controller->flash('this should work', '/flash');
  747. $result = $Controller->output;
  748. $expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  749. <html xmlns="http://www.w3.org/1999/xhtml">
  750. <head>
  751. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  752. <title>this should work</title>
  753. <style><!--
  754. P { text-align:center; font:bold 1.1em sans-serif }
  755. A { color:#444; text-decoration:none }
  756. A:HOVER { text-decoration: underline; color:#44E }
  757. --></style>
  758. </head>
  759. <body>
  760. <p><a href="/flash">this should work</a></p>
  761. </body>
  762. </html>';
  763. $result = str_replace(array("\t", "\r\n", "\n"), "", $result);
  764. $expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
  765. $this->assertEqual($result, $expected);
  766. App::build(array('views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)));
  767. $Controller =& new Controller();
  768. $Controller->flash('this should work', '/flash', 1, 'ajax2');
  769. $result = $Controller->output;
  770. $this->assertPattern('/Ajax!/', $result);
  771. App::build();
  772. }
  773. /**
  774. * testControllerSet method
  775. *
  776. * @access public
  777. * @return void
  778. */
  779. function testControllerSet() {
  780. $Controller =& new Controller();
  781. $Controller->set('variable_with_underscores', null);
  782. $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
  783. $Controller->viewVars = array();
  784. $viewVars = array('ModelName' => array('id' => 1, 'name' => 'value'));
  785. $Controller->set($viewVars);
  786. $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
  787. $Controller->viewVars = array();
  788. $Controller->set('variable_with_underscores', 'value');
  789. $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
  790. $Controller->viewVars = array();
  791. $viewVars = array('ModelName' => 'name');
  792. $Controller->set($viewVars);
  793. $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
  794. $Controller->set('title', 'someTitle');
  795. $this->assertIdentical($Controller->viewVars['title'], 'someTitle');
  796. $this->assertTrue(empty($Controller->pageTitle));
  797. $Controller->viewVars = array();
  798. $expected = array('ModelName' => 'name', 'ModelName2' => 'name2');
  799. $Controller->set(array('ModelName', 'ModelName2'), array('name', 'name2'));
  800. $this->assertIdentical($Controller->viewVars, $expected);
  801. $Controller->viewVars = array();
  802. $Controller->set(array(3 => 'three', 4 => 'four'));
  803. $Controller->set(array(1 => 'one', 2 => 'two'));
  804. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  805. $this->assertEqual($Controller->viewVars, $expected);
  806. }
  807. /**
  808. * testRender method
  809. *
  810. * @access public
  811. * @return void
  812. */
  813. function testRender() {
  814. App::build(array(
  815. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  816. ), true);
  817. $Controller =& new Controller();
  818. $Controller->viewPath = 'posts';
  819. $result = $Controller->render('index');
  820. $this->assertPattern('/posts index/', $result);
  821. $result = $Controller->render('/elements/test_element');
  822. $this->assertPattern('/this is the test element/', $result);
  823. $Controller = new TestController();
  824. $Controller->constructClasses();
  825. $Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
  826. $expected = $Controller->ControllerComment->validationErrors;
  827. ClassRegistry::flush();
  828. $Controller->viewPath = 'posts';
  829. $result = $Controller->render('index');
  830. $View = ClassRegistry::getObject('view');
  831. $this->assertTrue(isset($View->validationErrors['ControllerComment']));
  832. $this->assertEqual($expected, $View->validationErrors['ControllerComment']);
  833. $Controller->ControllerComment->validationErrors = array();
  834. ClassRegistry::flush();
  835. App::build();
  836. }
  837. /**
  838. * test that a component beforeRender can change the controller view class.
  839. *
  840. * @return void
  841. */
  842. function testComponentBeforeRenderChangingViewClass() {
  843. $core = App::core('views');
  844. App::build(array(
  845. 'views' => array(
  846. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS,
  847. $core[0]
  848. )
  849. ), true);
  850. $Controller =& new Controller();
  851. $Controller->uses = array();
  852. $Controller->components = array('Test');
  853. $Controller->constructClasses();
  854. $Controller->Test->viewclass = 'Theme';
  855. $Controller->viewPath = 'posts';
  856. $Controller->theme = 'test_theme';
  857. $result = $Controller->render('index');
  858. $this->assertPattern('/default test_theme layout/', $result);
  859. App::build();
  860. }
  861. /**
  862. * testToBeInheritedGuardmethods method
  863. *
  864. * @access public
  865. * @return void
  866. */
  867. function testToBeInheritedGuardmethods() {
  868. $Controller =& new Controller();
  869. $this->assertTrue($Controller->_beforeScaffold(''));
  870. $this->assertTrue($Controller->_afterScaffoldSave(''));
  871. $this->assertTrue($Controller->_afterScaffoldSaveError(''));
  872. $this->assertFalse($Controller->_scaffoldError(''));
  873. }
  874. /**
  875. * testRedirect method
  876. *
  877. * @access public
  878. * @return void
  879. */
  880. function testRedirect() {
  881. $codes = array(
  882. 100 => "Continue",
  883. 101 => "Switching Protocols",
  884. 200 => "OK",
  885. 201 => "Created",
  886. 202 => "Accepted",
  887. 203 => "Non-Authoritative Information",
  888. 204 => "No Content",
  889. 205 => "Reset Content",
  890. 206 => "Partial Content",
  891. 300 => "Multiple Choices",
  892. 301 => "Moved Permanently",
  893. 302 => "Found",
  894. 303 => "See Other",
  895. 304 => "Not Modified",
  896. 305 => "Use Proxy",
  897. 307 => "Temporary Redirect",
  898. 400 => "Bad Request",
  899. 401 => "Unauthorized",
  900. 402 => "Payment Required",
  901. 403 => "Forbidden",
  902. 404 => "Not Found",
  903. 405 => "Method Not Allowed",
  904. 406 => "Not Acceptable",
  905. 407 => "Proxy Authentication Required",
  906. 408 => "Request Time-out",
  907. 409 => "Conflict",
  908. 410 => "Gone",
  909. 411 => "Length Required",
  910. 412 => "Precondition Failed",
  911. 413 => "Request Entity Too Large",
  912. 414 => "Request-URI Too Large",
  913. 415 => "Unsupported Media Type",
  914. 416 => "Requested range not satisfiable",
  915. 417 => "Expectation Failed",
  916. 500 => "Internal Server Error",
  917. 501 => "Not Implemented",
  918. 502 => "Bad Gateway",
  919. 503 => "Service Unavailable",
  920. 504 => "Gateway Time-out"
  921. );
  922. Mock::generatePartial('Controller', 'MockController', array('header'));
  923. Mock::generate('TestComponent', 'MockTestComponent');
  924. Mock::generate('TestComponent', 'MockTestBComponent');
  925. App::import('Helper', 'Cache');
  926. foreach ($codes as $code => $msg) {
  927. $MockController =& new MockController();
  928. $MockController->Component =& new Component();
  929. $MockController->Component->init($MockController);
  930. $MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}"));
  931. $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
  932. $MockController->expectCallCount('header', 2);
  933. $MockController->redirect('http://cakephp.org', (int)$code, false);
  934. $this->assertFalse($MockController->autoRender);
  935. }
  936. foreach ($codes as $code => $msg) {
  937. $MockController =& new MockController();
  938. $MockController->Component =& new Component();
  939. $MockController->Component->init($MockController);
  940. $MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}"));
  941. $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
  942. $MockController->expectCallCount('header', 2);
  943. $MockController->redirect('http://cakephp.org', $msg, false);
  944. $this->assertFalse($MockController->autoRender);
  945. }
  946. $MockController =& new MockController();
  947. $MockController->Component =& new Component();
  948. $MockController->Component->init($MockController);
  949. $MockController->expectAt(0, 'header', array('Location: http://www.example.org/users/login'));
  950. $MockController->expectCallCount('header', 1);
  951. $MockController->redirect('http://www.example.org/users/login', null, false);
  952. $MockController =& new MockController();
  953. $MockController->Component =& new Component();
  954. $MockController->Component->init($MockController);
  955. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  956. $MockController->expectAt(1, 'header', array('Location: http://www.example.org/users/login'));
  957. $MockController->expectCallCount('header', 2);
  958. $MockController->redirect('http://www.example.org/users/login', 301, false);
  959. $MockController =& new MockController();
  960. $MockController->components = array('MockTest');
  961. $MockController->Component =& new Component();
  962. $MockController->Component->init($MockController);
  963. $MockController->MockTest->setReturnValue('beforeRedirect', null);
  964. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  965. $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
  966. $MockController->expectCallCount('header', 2);
  967. $MockController->redirect('http://cakephp.org', 301, false);
  968. $MockController =& new MockController();
  969. $MockController->components = array('MockTest');
  970. $MockController->Component =& new Component();
  971. $MockController->Component->init($MockController);
  972. $MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org');
  973. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  974. $MockController->expectAt(1, 'header', array('Location: http://book.cakephp.org'));
  975. $MockController->expectCallCount('header', 2);
  976. $MockController->redirect('http://cakephp.org', 301, false);
  977. $MockController =& new MockController();
  978. $MockController->components = array('MockTest');
  979. $MockController->Component =& new Component();
  980. $MockController->Component->init($MockController);
  981. $MockController->MockTest->setReturnValue('beforeRedirect', false);
  982. $MockController->expectNever('header');
  983. $MockController->redirect('http://cakephp.org', 301, false);
  984. $MockController =& new MockController();
  985. $MockController->components = array('MockTest', 'MockTestB');
  986. $MockController->Component =& new Component();
  987. $MockController->Component->init($MockController);
  988. $MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org');
  989. $MockController->MockTestB->setReturnValue('beforeRedirect', 'http://bakery.cakephp.org');
  990. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  991. $MockController->expectAt(1, 'header', array('Location: http://bakery.cakephp.org'));
  992. $MockController->expectCallCount('header', 2);
  993. $MockController->redirect('http://cakephp.org', 301, false);
  994. }
  995. /**
  996. * testMergeVars method
  997. *
  998. * @access public
  999. * @return void
  1000. */
  1001. function testMergeVars() {
  1002. if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) {
  1003. return;
  1004. }
  1005. $TestController =& new TestController();
  1006. $TestController->constructClasses();
  1007. $testVars = get_class_vars('TestController');
  1008. $appVars = get_class_vars('AppController');
  1009. $components = is_array($appVars['components'])
  1010. ? array_merge($appVars['components'], $testVars['components'])
  1011. : $testVars['components'];
  1012. if (!in_array('Session', $components)) {
  1013. $components[] = 'Session';
  1014. }
  1015. $helpers = is_array($appVars['helpers'])
  1016. ? array_merge($appVars['helpers'], $testVars['helpers'])
  1017. : $testVars['helpers'];
  1018. $uses = is_array($appVars['uses'])
  1019. ? array_merge($appVars['uses'], $testVars['uses'])
  1020. : $testVars['uses'];
  1021. $this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->helpers), Set::normalize($helpers))), 0);
  1022. $this->assertEqual(count(array_diff($TestController->uses, $uses)), 0);
  1023. $this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->components), Set::normalize($components))), 0);
  1024. $expected = array('ControllerComment', 'ControllerAlias', 'ControllerPost');
  1025. $this->assertEqual($expected, $TestController->uses, '$uses was merged incorrectly, AppController models should be last.');
  1026. $TestController =& new AnotherTestController();
  1027. $TestController->constructClasses();
  1028. $appVars = get_class_vars('AppController');
  1029. $testVars = get_class_vars('AnotherTestController');
  1030. $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
  1031. $this->assertNull($testVars['uses']);
  1032. $this->assertFalse(isset($TestController->ControllerPost));
  1033. $TestController =& new ControllerCommentsController();
  1034. $TestController->constructClasses();
  1035. $appVars = get_class_vars('AppController');
  1036. $testVars = get_class_vars('ControllerCommentsController');
  1037. $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
  1038. $this->assertEqual(array('ControllerPost'), $testVars['uses']);
  1039. $this->assertTrue(isset($TestController->ControllerPost));
  1040. $this->assertTrue(isset($TestController->ControllerComment));
  1041. }
  1042. /**
  1043. * test that options from child classes replace those in the parent classes.
  1044. *
  1045. * @access public
  1046. * @return void
  1047. */
  1048. function testChildComponentOptionsSupercedeParents() {
  1049. if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) {
  1050. return;
  1051. }
  1052. $TestController =& new TestController();
  1053. $expected = array('foo');
  1054. $TestController->components = array('Cookie' => $expected);
  1055. $TestController->constructClasses();
  1056. $this->assertEqual($TestController->components['Cookie'], $expected);
  1057. }
  1058. /**
  1059. * Ensure that __mergeVars is not being greedy and merging with
  1060. * AppController when you make an instance of Controller
  1061. *
  1062. * @return void
  1063. */
  1064. function testMergeVarsNotGreedy() {
  1065. $Controller =& new Controller();
  1066. $Controller->components = array();
  1067. $Controller->uses = array();
  1068. $Controller->constructClasses();
  1069. $this->assertFalse(isset($Controller->Session));
  1070. }
  1071. /**
  1072. * testReferer method
  1073. *
  1074. * @access public
  1075. * @return void
  1076. */
  1077. function testReferer() {
  1078. $Controller =& new Controller();
  1079. $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
  1080. $result = $Controller->referer(null, false);
  1081. $expected = 'http://cakephp.org';
  1082. $this->assertIdentical($result, $expected);
  1083. $_SERVER['HTTP_REFERER'] = '';
  1084. $result = $Controller->referer('http://cakephp.org', false);
  1085. $expected = 'http://cakephp.org';
  1086. $this->assertIdentical($result, $expected);
  1087. $_SERVER['HTTP_REFERER'] = '';
  1088. $referer = array(
  1089. 'controller' => 'pages',
  1090. 'action' => 'display',
  1091. 'home'
  1092. );
  1093. $result = $Controller->referer($referer, false);
  1094. $expected = 'http://' . env('HTTP_HOST') . '/pages/display/home';
  1095. $this->assertIdentical($result, $expected);
  1096. $_SERVER['HTTP_REFERER'] = '';
  1097. $result = $Controller->referer(null, false);
  1098. $expected = '/';
  1099. $this->assertIdentical($result, $expected);
  1100. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path';
  1101. $result = $Controller->referer(null, false);
  1102. $expected = '/some/path';
  1103. $this->assertIdentical($result, $expected);
  1104. $Controller->webroot .= '/';
  1105. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path';
  1106. $result = $Controller->referer(null, false);
  1107. $expected = '/some/path';
  1108. $this->assertIdentical($result, $expected);
  1109. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'some/path';
  1110. $result = $Controller->referer(null, false);
  1111. $expected = '/some/path';
  1112. $this->assertIdentical($result, $expected);
  1113. $Controller->webroot = '/recipe/';
  1114. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'recipes/add';
  1115. $result = $Controller->referer();
  1116. $expected = '/recipes/add';
  1117. $this->assertIdentical($result, $expected);
  1118. }
  1119. /**
  1120. * testSetAction method
  1121. *
  1122. * @access public
  1123. * @return void
  1124. */
  1125. function testSetAction() {
  1126. $TestController =& new TestController();
  1127. $TestController->setAction('index', 1, 2);
  1128. $expected = array('testId' => 1, 'test2Id' => 2);
  1129. $this->assertidentical($TestController->data, $expected);
  1130. }
  1131. /**
  1132. * testUnimplementedIsAuthorized method
  1133. *
  1134. * @access public
  1135. * @return void
  1136. */
  1137. function testUnimplementedIsAuthorized() {
  1138. $TestController =& new TestController();
  1139. $TestController->isAuthorized();
  1140. $this->assertError();
  1141. }
  1142. /**
  1143. * testValidateErrors method
  1144. *
  1145. * @access public
  1146. * @return void
  1147. */
  1148. function testValidateErrors() {
  1149. $TestController =& new TestController();
  1150. $TestController->constructClasses();
  1151. $this->assertFalse($TestController->validateErrors());
  1152. $this->assertEqual($TestController->validate(), 0);
  1153. $TestController->ControllerComment->invalidate('some_field', 'error_message');
  1154. $TestController->ControllerComment->invalidate('some_field2', 'error_message2');
  1155. $comment =& new ControllerComment();
  1156. $comment->set('someVar', 'data');
  1157. $result = $TestController->validateErrors($comment);
  1158. $expected = array('some_field' => 'error_message', 'some_field2' => 'error_message2');
  1159. $this->assertIdentical($result, $expected);
  1160. $this->assertEqual($TestController->validate($comment), 2);
  1161. }
  1162. /**
  1163. * test that validateErrors works with any old model.
  1164. *
  1165. * @return void
  1166. */
  1167. function testValidateErrorsOnArbitraryModels() {
  1168. $TestController =& new TestController();
  1169. $Post = new ControllerPost();
  1170. $Post->validate = array('title' => 'notEmpty');
  1171. $Post->set('title', '');
  1172. $result = $TestController->validateErrors($Post);
  1173. $expected = array('title' => 'This field cannot be left blank');
  1174. $this->assertEqual($result, $expected);
  1175. }
  1176. /**
  1177. * testPostConditions method
  1178. *
  1179. * @access public
  1180. * @return void
  1181. */
  1182. function testPostConditions() {
  1183. $Controller =& new Controller();
  1184. $data = array(
  1185. 'Model1' => array('field1' => '23'),
  1186. 'Model2' => array('field2' => 'string'),
  1187. 'Model3' => array('field3' => '23'),
  1188. );
  1189. $expected = array(
  1190. 'Model1.field1' => '23',
  1191. 'Model2.field2' => 'string',
  1192. 'Model3.field3' => '23',
  1193. );
  1194. $result = $Controller->postConditions($data);
  1195. $this->assertIdentical($result, $expected);
  1196. $data = array();
  1197. $Controller->data = array(
  1198. 'Model1' => array('field1' => '23'),
  1199. 'Model2' => array('field2' => 'string'),
  1200. 'Model3' => array('field3' => '23'),
  1201. );
  1202. $expected = array(
  1203. 'Model1.field1' => '23',
  1204. 'Model2.field2' => 'string',
  1205. 'Model3.field3' => '23',
  1206. );
  1207. $result = $Controller->postConditions($data);
  1208. $this->assertIdentical($result, $expected);
  1209. $data = array();
  1210. $Controller->data = array();
  1211. $result = $Controller->postConditions($data);
  1212. $this->assertNull($result);
  1213. $data = array();
  1214. $Controller->data = array(
  1215. 'Model1' => array('field1' => '23'),
  1216. 'Model2' => array('field2' => 'string'),
  1217. 'Model3' => array('field3' => '23'),
  1218. );
  1219. $ops = array(
  1220. 'Model1.field1' => '>',
  1221. 'Model2.field2' => 'LIKE',
  1222. 'Model3.field3' => '<=',
  1223. );
  1224. $expected = array(
  1225. 'Model1.field1 >' => '23',
  1226. 'Model2.field2 LIKE' => "%string%",
  1227. 'Model3.field3 <=' => '23',
  1228. );
  1229. $result = $Controller->postConditions($data, $ops);
  1230. $this->assertIdentical($result, $expected);
  1231. }
  1232. /**
  1233. * testRequestHandlerPrefers method
  1234. *
  1235. * @access public
  1236. * @return void
  1237. */
  1238. function testRequestHandlerPrefers(){
  1239. Configure::write('debug', 2);
  1240. $Controller =& new Controller();
  1241. $Controller->components = array("RequestHandler");
  1242. $Controller->modelClass='ControllerPost';
  1243. $Controller->params['url']['ext'] = 'rss';
  1244. $Controller->constructClasses();
  1245. $Controller->Component->initialize($Controller);
  1246. $Controller->beforeFilter();
  1247. $Controller->Component->startup($Controller);
  1248. $this->assertEqual($Controller->RequestHandler->prefers(), 'rss');
  1249. unset($Controller);
  1250. }
  1251. /**
  1252. * testControllerHttpCodes method
  1253. *
  1254. * @access public
  1255. * @return void
  1256. */
  1257. function testControllerHttpCodes() {
  1258. $Controller =& new Controller();
  1259. $result = $Controller->httpCodes();
  1260. $this->assertEqual(count($result), 39);
  1261. $result = $Controller->httpCodes(100);
  1262. $expected = array(100 => 'Continue');
  1263. $this->assertEqual($result, $expected);
  1264. $codes = array(
  1265. 1337 => 'Undefined Unicorn',
  1266. 1729 => 'Hardy-Ramanujan Located'
  1267. );
  1268. $result = $Controller->httpCodes($codes);
  1269. $this->assertTrue($result);
  1270. $this->assertEqual(count($Controller->httpCodes()), 41);
  1271. $result = $Controller->httpCodes(1337);
  1272. $expected = array(1337 => 'Undefined Unicorn');
  1273. $this->assertEqual($result, $expected);
  1274. $codes = array(404 => 'Sorry Bro');
  1275. $result = $Controller->httpCodes($codes);
  1276. $this->assertTrue($result);
  1277. $this->assertEqual(count($Controller->httpCodes()), 41);
  1278. $result = $Controller->httpCodes(404);
  1279. $expected = array(404 => 'Sorry Bro');
  1280. $this->assertEqual($result, $expected);
  1281. }
  1282. /**
  1283. * Tests that the startup process calls the correct functions
  1284. *
  1285. * @access public
  1286. * @return void
  1287. */
  1288. function testStartupProcess() {
  1289. Mock::generatePartial('AnotherTestController','MockedController', array('beforeFilter', 'afterFilter'));
  1290. Mock::generate('TestComponent', 'MockTestComponent', array('startup', 'initialize'));
  1291. $MockedController =& new MockedController();
  1292. $MockedController->components = array('MockTest');
  1293. $MockedController->Component =& new Component();
  1294. $MockedController->Component->init($MockedController);
  1295. $MockedController->expectCallCount('beforeFilter', 1);
  1296. $MockedController->MockTest->expectCallCount('initialize', 1);
  1297. $MockedController->MockTest->expectCallCount('startup', 1);
  1298. $MockedController->startupProcess();
  1299. }
  1300. /**
  1301. * Tests that the shutdown process calls the correct functions
  1302. *
  1303. * @access public
  1304. * @return void
  1305. */
  1306. function testShutdownProcess() {
  1307. Mock::generate('TestComponent', 'MockTestComponent', array('shutdown'));
  1308. $MockedController =& new MockedController();
  1309. $MockedController->components = array('MockTest');
  1310. $MockedController->Component =& new Component();
  1311. $MockedController->Component->init($MockedController);
  1312. $MockedController->expectCallCount('afterFilter', 1);
  1313. $MockedController->MockTest->expectCallCount('shutdown', 1);
  1314. $MockedController->shutdownProcess();
  1315. }
  1316. }