PageRenderTime 68ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/Datawalke/Coordino
PHP | 1546 lines | 856 code | 186 blank | 504 comment | 7 complexity | 5daab3d5fef5f505a6c5ee36f47804da MD5 | raw file
  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-2012, 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-2012, 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. * test paginate() and virtualField overlapping with real fields.
  740. *
  741. * @return void
  742. */
  743. function testPaginateOrderVirtualFieldSharedWithRealField() {
  744. $Controller =& new Controller();
  745. $Controller->uses = array('ControllerPost', 'ControllerComment');
  746. $Controller->params['url'] = array();
  747. $Controller->constructClasses();
  748. $Controller->ControllerComment->virtualFields = array(
  749. 'title' => 'ControllerComment.comment'
  750. );
  751. $Controller->ControllerComment->bindModel(array(
  752. 'belongsTo' => array(
  753. 'ControllerPost' => array(
  754. 'className' => 'ControllerPost',
  755. 'foreignKey' => 'article_id'
  756. )
  757. )
  758. ), false);
  759. $Controller->paginate = array(
  760. 'fields' => array('ControllerComment.id', 'title', 'ControllerPost.title'),
  761. );
  762. $Controller->passedArgs = array('sort' => 'ControllerPost.title', 'dir' => 'asc');
  763. $result = $Controller->paginate('ControllerComment');
  764. $this->assertEqual(Set::extract($result, '{n}.ControllerComment.id'), array(1, 2, 3, 4, 5, 6));
  765. }
  766. /**
  767. * testFlash method
  768. *
  769. * @access public
  770. * @return void
  771. */
  772. function testFlash() {
  773. $Controller =& new Controller();
  774. $Controller->flash('this should work', '/flash');
  775. $result = $Controller->output;
  776. $expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  777. <html xmlns="http://www.w3.org/1999/xhtml">
  778. <head>
  779. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  780. <title>this should work</title>
  781. <style><!--
  782. P { text-align:center; font:bold 1.1em sans-serif }
  783. A { color:#444; text-decoration:none }
  784. A:HOVER { text-decoration: underline; color:#44E }
  785. --></style>
  786. </head>
  787. <body>
  788. <p><a href="/flash">this should work</a></p>
  789. </body>
  790. </html>';
  791. $result = str_replace(array("\t", "\r\n", "\n"), "", $result);
  792. $expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
  793. $this->assertEqual($result, $expected);
  794. App::build(array('views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)));
  795. $Controller =& new Controller();
  796. $Controller->flash('this should work', '/flash', 1, 'ajax2');
  797. $result = $Controller->output;
  798. $this->assertPattern('/Ajax!/', $result);
  799. App::build();
  800. }
  801. /**
  802. * testControllerSet method
  803. *
  804. * @access public
  805. * @return void
  806. */
  807. function testControllerSet() {
  808. $Controller =& new Controller();
  809. $Controller->set('variable_with_underscores', null);
  810. $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
  811. $Controller->viewVars = array();
  812. $viewVars = array('ModelName' => array('id' => 1, 'name' => 'value'));
  813. $Controller->set($viewVars);
  814. $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
  815. $Controller->viewVars = array();
  816. $Controller->set('variable_with_underscores', 'value');
  817. $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
  818. $Controller->viewVars = array();
  819. $viewVars = array('ModelName' => 'name');
  820. $Controller->set($viewVars);
  821. $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
  822. $Controller->set('title', 'someTitle');
  823. $this->assertIdentical($Controller->viewVars['title'], 'someTitle');
  824. $this->assertTrue(empty($Controller->pageTitle));
  825. $Controller->viewVars = array();
  826. $expected = array('ModelName' => 'name', 'ModelName2' => 'name2');
  827. $Controller->set(array('ModelName', 'ModelName2'), array('name', 'name2'));
  828. $this->assertIdentical($Controller->viewVars, $expected);
  829. $Controller->viewVars = array();
  830. $Controller->set(array(3 => 'three', 4 => 'four'));
  831. $Controller->set(array(1 => 'one', 2 => 'two'));
  832. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  833. $this->assertEqual($Controller->viewVars, $expected);
  834. }
  835. /**
  836. * testRender method
  837. *
  838. * @access public
  839. * @return void
  840. */
  841. function testRender() {
  842. App::build(array(
  843. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  844. ), true);
  845. $Controller =& new Controller();
  846. $Controller->viewPath = 'posts';
  847. $result = $Controller->render('index');
  848. $this->assertPattern('/posts index/', $result);
  849. $result = $Controller->render('/elements/test_element');
  850. $this->assertPattern('/this is the test element/', $result);
  851. $Controller = new TestController();
  852. $Controller->constructClasses();
  853. $Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
  854. $expected = $Controller->ControllerComment->validationErrors;
  855. ClassRegistry::flush();
  856. $Controller->viewPath = 'posts';
  857. $result = $Controller->render('index');
  858. $View = ClassRegistry::getObject('view');
  859. $this->assertTrue(isset($View->validationErrors['ControllerComment']));
  860. $this->assertEqual($expected, $View->validationErrors['ControllerComment']);
  861. $Controller->ControllerComment->validationErrors = array();
  862. ClassRegistry::flush();
  863. App::build();
  864. }
  865. /**
  866. * test that a component beforeRender can change the controller view class.
  867. *
  868. * @return void
  869. */
  870. function testComponentBeforeRenderChangingViewClass() {
  871. $core = App::core('views');
  872. App::build(array(
  873. 'views' => array(
  874. TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS,
  875. $core[0]
  876. )
  877. ), true);
  878. $Controller =& new Controller();
  879. $Controller->uses = array();
  880. $Controller->components = array('Test');
  881. $Controller->constructClasses();
  882. $Controller->Test->viewclass = 'Theme';
  883. $Controller->viewPath = 'posts';
  884. $Controller->theme = 'test_theme';
  885. $result = $Controller->render('index');
  886. $this->assertPattern('/default test_theme layout/', $result);
  887. App::build();
  888. }
  889. /**
  890. * testToBeInheritedGuardmethods method
  891. *
  892. * @access public
  893. * @return void
  894. */
  895. function testToBeInheritedGuardmethods() {
  896. $Controller =& new Controller();
  897. $this->assertTrue($Controller->_beforeScaffold(''));
  898. $this->assertTrue($Controller->_afterScaffoldSave(''));
  899. $this->assertTrue($Controller->_afterScaffoldSaveError(''));
  900. $this->assertFalse($Controller->_scaffoldError(''));
  901. }
  902. /**
  903. * testRedirect method
  904. *
  905. * @access public
  906. * @return void
  907. */
  908. function testRedirect() {
  909. $codes = array(
  910. 100 => "Continue",
  911. 101 => "Switching Protocols",
  912. 200 => "OK",
  913. 201 => "Created",
  914. 202 => "Accepted",
  915. 203 => "Non-Authoritative Information",
  916. 204 => "No Content",
  917. 205 => "Reset Content",
  918. 206 => "Partial Content",
  919. 300 => "Multiple Choices",
  920. 301 => "Moved Permanently",
  921. 302 => "Found",
  922. 303 => "See Other",
  923. 304 => "Not Modified",
  924. 305 => "Use Proxy",
  925. 307 => "Temporary Redirect",
  926. 400 => "Bad Request",
  927. 401 => "Unauthorized",
  928. 402 => "Payment Required",
  929. 403 => "Forbidden",
  930. 404 => "Not Found",
  931. 405 => "Method Not Allowed",
  932. 406 => "Not Acceptable",
  933. 407 => "Proxy Authentication Required",
  934. 408 => "Request Time-out",
  935. 409 => "Conflict",
  936. 410 => "Gone",
  937. 411 => "Length Required",
  938. 412 => "Precondition Failed",
  939. 413 => "Request Entity Too Large",
  940. 414 => "Request-URI Too Large",
  941. 415 => "Unsupported Media Type",
  942. 416 => "Requested range not satisfiable",
  943. 417 => "Expectation Failed",
  944. 500 => "Internal Server Error",
  945. 501 => "Not Implemented",
  946. 502 => "Bad Gateway",
  947. 503 => "Service Unavailable",
  948. 504 => "Gateway Time-out"
  949. );
  950. Mock::generatePartial('Controller', 'MockController', array('header'));
  951. Mock::generate('TestComponent', 'MockTestComponent');
  952. Mock::generate('TestComponent', 'MockTestBComponent');
  953. App::import('Helper', 'Cache');
  954. foreach ($codes as $code => $msg) {
  955. $MockController =& new MockController();
  956. $MockController->Component =& new Component();
  957. $MockController->Component->init($MockController);
  958. $MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}"));
  959. $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
  960. $MockController->expectCallCount('header', 2);
  961. $MockController->redirect('http://cakephp.org', (int)$code, false);
  962. $this->assertFalse($MockController->autoRender);
  963. }
  964. foreach ($codes as $code => $msg) {
  965. $MockController =& new MockController();
  966. $MockController->Component =& new Component();
  967. $MockController->Component->init($MockController);
  968. $MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}"));
  969. $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
  970. $MockController->expectCallCount('header', 2);
  971. $MockController->redirect('http://cakephp.org', $msg, false);
  972. $this->assertFalse($MockController->autoRender);
  973. }
  974. $MockController =& new MockController();
  975. $MockController->Component =& new Component();
  976. $MockController->Component->init($MockController);
  977. $MockController->expectAt(0, 'header', array('Location: http://www.example.org/users/login'));
  978. $MockController->expectCallCount('header', 1);
  979. $MockController->redirect('http://www.example.org/users/login', null, false);
  980. $MockController =& new MockController();
  981. $MockController->Component =& new Component();
  982. $MockController->Component->init($MockController);
  983. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  984. $MockController->expectAt(1, 'header', array('Location: http://www.example.org/users/login'));
  985. $MockController->expectCallCount('header', 2);
  986. $MockController->redirect('http://www.example.org/users/login', 301, false);
  987. $MockController =& new MockController();
  988. $MockController->components = array('MockTest');
  989. $MockController->Component =& new Component();
  990. $MockController->Component->init($MockController);
  991. $MockController->MockTest->setReturnValue('beforeRedirect', null);
  992. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  993. $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
  994. $MockController->expectCallCount('header', 2);
  995. $MockController->redirect('http://cakephp.org', 301, false);
  996. $MockController =& new MockController();
  997. $MockController->components = array('MockTest');
  998. $MockController->Component =& new Component();
  999. $MockController->Component->init($MockController);
  1000. $MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org');
  1001. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  1002. $MockController->expectAt(1, 'header', array('Location: http://book.cakephp.org'));
  1003. $MockController->expectCallCount('header', 2);
  1004. $MockController->redirect('http://cakephp.org', 301, false);
  1005. $MockController =& new MockController();
  1006. $MockController->components = array('MockTest');
  1007. $MockController->Component =& new Component();
  1008. $MockController->Component->init($MockController);
  1009. $MockController->MockTest->setReturnValue('beforeRedirect', false);
  1010. $MockController->expectNever('header');
  1011. $MockController->redirect('http://cakephp.org', 301, false);
  1012. $MockController =& new MockController();
  1013. $MockController->components = array('MockTest', 'MockTestB');
  1014. $MockController->Component =& new Component();
  1015. $MockController->Component->init($MockController);
  1016. $MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org');
  1017. $MockController->MockTestB->setReturnValue('beforeRedirect', 'http://bakery.cakephp.org');
  1018. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  1019. $MockController->expectAt(1, 'header', array('Location: http://bakery.cakephp.org'));
  1020. $MockController->expectCallCount('header', 2);
  1021. $MockController->redirect('http://cakephp.org', 301, false);
  1022. }
  1023. /**
  1024. * testMergeVars method
  1025. *
  1026. * @access public
  1027. * @return void
  1028. */
  1029. function testMergeVars() {
  1030. if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) {
  1031. return;
  1032. }
  1033. $TestController =& new TestController();
  1034. $TestController->constructClasses();
  1035. $testVars = get_class_vars('TestController');
  1036. $appVars = get_class_vars('AppController');
  1037. $components = is_array($appVars['components'])
  1038. ? array_merge($appVars['components'], $testVars['components'])
  1039. : $testVars['components'];
  1040. if (!in_array('Session', $components)) {
  1041. $components[] = 'Session';
  1042. }
  1043. $helpers = is_array($appVars['helpers'])
  1044. ? array_merge($appVars['helpers'], $testVars['helpers'])
  1045. : $testVars['helpers'];
  1046. $uses = is_array($appVars['uses'])
  1047. ? array_merge($appVars['uses'], $testVars['uses'])
  1048. : $testVars['uses'];
  1049. $this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->helpers), Set::normalize($helpers))), 0);
  1050. $this->assertEqual(count(array_diff($TestController->uses, $uses)), 0);
  1051. $this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->components), Set::normalize($components))), 0);
  1052. $expected = array('ControllerComment', 'ControllerAlias', 'ControllerPost');
  1053. $this->assertEqual($expected, $TestController->uses, '$uses was merged incorrectly, AppController models should be last.');
  1054. $TestController =& new AnotherTestController();
  1055. $TestController->constructClasses();
  1056. $appVars = get_class_vars('AppController');
  1057. $testVars = get_class_vars('AnotherTestController');
  1058. $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
  1059. $this->assertNull($testVars['uses']);
  1060. $this->assertFalse(isset($TestController->ControllerPost));
  1061. $TestController =& new ControllerCommentsController();
  1062. $TestController->constructClasses();
  1063. $appVars = get_class_vars('AppController');
  1064. $testVars = get_class_vars('ControllerCommentsController');
  1065. $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
  1066. $this->assertEqual(array('ControllerPost'), $testVars['uses']);
  1067. $this->assertTrue(isset($TestController->ControllerPost));
  1068. $this->assertTrue(isset($TestController->ControllerComment));
  1069. }
  1070. /**
  1071. * test that options from child classes replace those in the parent classes.
  1072. *
  1073. * @access public
  1074. * @return void
  1075. */
  1076. function testChildComponentOptionsSupercedeParents() {
  1077. if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) {
  1078. return;
  1079. }
  1080. $TestController =& new TestController();
  1081. $expected = array('foo');
  1082. $TestController->components = array('Cookie' => $expected);
  1083. $TestController->constructClasses();
  1084. $this->assertEqual($TestController->components['Cookie'], $expected);
  1085. }
  1086. /**
  1087. * Ensure that __mergeVars is not being greedy and merging with
  1088. * AppController when you make an instance of Controller
  1089. *
  1090. * @return void
  1091. */
  1092. function testMergeVarsNotGreedy() {
  1093. $Controller =& new Controller();
  1094. $Controller->components = array();
  1095. $Controller->uses = array();
  1096. $Controller->constructClasses();
  1097. $this->assertFalse(isset($Controller->Session));
  1098. }
  1099. /**
  1100. * testReferer method
  1101. *
  1102. * @access public
  1103. * @return void
  1104. */
  1105. function testReferer() {
  1106. $Controller =& new Controller();
  1107. $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
  1108. $result = $Controller->referer(null, false);
  1109. $expected = 'http://cakephp.org';
  1110. $this->assertIdentical($result, $expected);
  1111. $_SERVER['HTTP_REFERER'] = '';
  1112. $result = $Controller->referer('http://cakephp.org', false);
  1113. $expected = 'http://cakephp.org';
  1114. $this->assertIdentical($result, $expected);
  1115. $_SERVER['HTTP_REFERER'] = '';
  1116. $referer = array(
  1117. 'controller' => 'pages',
  1118. 'action' => 'display',
  1119. 'home'
  1120. );
  1121. $result = $Controller->referer($referer, false);
  1122. $expected = 'http://' . env('HTTP_HOST') . '/pages/display/home';
  1123. $this->assertIdentical($result, $expected);
  1124. $_SERVER['HTTP_REFERER'] = '';
  1125. $result = $Controller->referer(null, false);
  1126. $expected = '/';
  1127. $this->assertIdentical($result, $expected);
  1128. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path';
  1129. $result = $Controller->referer(null, false);
  1130. $expected = '/some/path';
  1131. $this->assertIdentical($result, $expected);
  1132. $Controller->webroot .= '/';
  1133. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path';
  1134. $result = $Controller->referer(null, false);
  1135. $expected = '/some/path';
  1136. $this->assertIdentical($result, $expected);
  1137. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'some/path';
  1138. $result = $Controller->referer(null, false);
  1139. $expected = '/some/path';
  1140. $this->assertIdentical($result, $expected);
  1141. $Controller->webroot = '/recipe/';
  1142. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'recipes/add';
  1143. $result = $Controller->referer();
  1144. $expected = '/recipes/add';
  1145. $this->assertIdentical($result, $expected);
  1146. }
  1147. /**
  1148. * testSetAction method
  1149. *
  1150. * @access public
  1151. * @return void
  1152. */
  1153. function testSetAction() {
  1154. $TestController =& new TestController();
  1155. $TestController->setAction('index', 1, 2);
  1156. $expected = array('testId' => 1, 'test2Id' => 2);
  1157. $this->assertidentical($TestController->data, $expected);
  1158. }
  1159. /**
  1160. * testUnimplementedIsAuthorized method
  1161. *
  1162. * @access public
  1163. * @return void
  1164. */
  1165. function testUnimplementedIsAuthorized() {
  1166. $TestController =& new TestController();
  1167. $TestController->isAuthorized();
  1168. $this->assertError();
  1169. }
  1170. /**
  1171. * testValidateErrors method
  1172. *
  1173. * @access public
  1174. * @return void
  1175. */
  1176. function testValidateErrors() {
  1177. $TestController =& new TestController();
  1178. $TestController->constructClasses();
  1179. $this->assertFalse($TestController->validateErrors());
  1180. $this->assertEqual($TestController->validate(), 0);
  1181. $TestController->ControllerComment->invalidate('some_field', 'error_message');
  1182. $TestController->ControllerComment->invalidate('some_field2', 'error_message2');
  1183. $comment =& new ControllerComment();
  1184. $comment->set('someVar', 'data');
  1185. $result = $TestController->validateErrors($comment);
  1186. $expected = array('some_field' => 'error_message', 'some_field2' => 'error_message2');
  1187. $this->assertIdentical($result, $expected);
  1188. $this->assertEqual($TestController->validate($comment), 2);
  1189. }
  1190. /**
  1191. * test that validateErrors works with any old model.
  1192. *
  1193. * @return void
  1194. */
  1195. function testValidateErrorsOnArbitraryModels() {
  1196. $TestController =& new TestController();
  1197. $Post = new ControllerPost();
  1198. $Post->validate = array('title' => 'notEmpty');
  1199. $Post->set('title', '');
  1200. $result = $TestController->validateErrors($Post);
  1201. $expected = array('title' => 'This field cannot be left blank');
  1202. $this->assertEqual($result, $expected);
  1203. }
  1204. /**
  1205. * testPostConditions method
  1206. *
  1207. * @access public
  1208. * @return void
  1209. */
  1210. function testPostConditions() {
  1211. $Controller =& new Controller();
  1212. $data = array(
  1213. 'Model1' => array('field1' => '23'),
  1214. 'Model2' => array('field2' => 'string'),
  1215. 'Model3' => array('field3' => '23'),
  1216. );
  1217. $expected = array(
  1218. 'Model1.field1' => '23',
  1219. 'Model2.field2' => 'string',
  1220. 'Model3.field3' => '23',
  1221. );
  1222. $result = $Controller->postConditions($data);
  1223. $this->assertIdentical($result, $expected);
  1224. $data = array();
  1225. $Controller->data = array(
  1226. 'Model1' => array('field1' => '23'),
  1227. 'Model2' => array('field2' => 'string'),
  1228. 'Model3' => array('field3' => '23'),
  1229. );
  1230. $expected = array(
  1231. 'Model1.field1' => '23',
  1232. 'Model2.field2' => 'string',
  1233. 'Model3.field3' => '23',
  1234. );
  1235. $result = $Controller->postConditions($data);
  1236. $this->assertIdentical($result, $expected);
  1237. $data = array();
  1238. $Controller->data = array();
  1239. $result = $Controller->postConditions($data);
  1240. $this->assertNull($result);
  1241. $data = array();
  1242. $Controller->data = array(
  1243. 'Model1' => array('field1' => '23'),
  1244. 'Model2' => array('field2' => 'string'),
  1245. 'Model3' => array('field3' => '23'),
  1246. );
  1247. $ops = array(
  1248. 'Model1.field1' => '>',
  1249. 'Model2.field2' => 'LIKE',
  1250. 'Model3.field3' => '<=',
  1251. );
  1252. $expected = array(
  1253. 'Model1.field1 >' => '23',
  1254. 'Model2.field2 LIKE' => "%string%",
  1255. 'Model3.field3 <=' => '23',
  1256. );
  1257. $result = $Controller->postConditions($data, $ops);
  1258. $this->assertIdentical($result, $expected);
  1259. }
  1260. /**
  1261. * testRequestHandlerPrefers method
  1262. *
  1263. * @access public
  1264. * @return void
  1265. */
  1266. function testRequestHandlerPrefers(){
  1267. Configure::write('debug', 2);
  1268. $Controller =& new Controller();
  1269. $Controller->components = array("RequestHandler");
  1270. $Controller->modelClass='ControllerPost';
  1271. $Controller->params['url']['ext'] = 'rss';
  1272. $Controller->constructClasses();
  1273. $Controller->Component->initialize($Controller);
  1274. $Controller->beforeFilter();
  1275. $Controller->Component->startup($Controller);
  1276. $this->assertEqual($Controller->RequestHandler->prefers(), 'rss');
  1277. unset($Controller);
  1278. }
  1279. /**
  1280. * testControllerHttpCodes method
  1281. *
  1282. * @access public
  1283. * @return void
  1284. */
  1285. function testControllerHttpCodes() {
  1286. $Controller =& new Controller();
  1287. $result = $Controller->httpCodes();
  1288. $this->assertEqual(count($result), 39);
  1289. $result = $Controller->httpCodes(100);
  1290. $expected = array(100 => 'Continue');
  1291. $this->assertEqual($result, $expected);
  1292. $codes = array(
  1293. 1337 => 'Undefined Unicorn',
  1294. 1729 => 'Hardy-Ramanujan Located'
  1295. );
  1296. $result = $Controller->httpCodes($codes);
  1297. $this->assertTrue($result);
  1298. $this->assertEqual(count($Controller->httpCodes()), 41);
  1299. $result = $Controller->httpCodes(1337);
  1300. $expected = array(1337 => 'Undefined Unicorn');
  1301. $this->assertEqual($result, $expected);
  1302. $codes = array(404 => 'Sorry Bro');
  1303. $result = $Controller->httpCodes($codes);
  1304. $this->assertTrue($result);
  1305. $this->assertEqual(count($Controller->httpCodes()), 41);
  1306. $result = $Controller->httpCodes(404);
  1307. $expected = array(404 => 'Sorry Bro');
  1308. $this->assertEqual($result, $expected);
  1309. }
  1310. /**
  1311. * Tests that the startup process calls the correct functions
  1312. *
  1313. * @access public
  1314. * @return void
  1315. */
  1316. function testStartupProcess() {
  1317. Mock::generatePartial('AnotherTestController','MockedController', array('beforeFilter', 'afterFilter'));
  1318. Mock::generate('TestComponent', 'MockTestComponent', array('startup', 'initialize'));
  1319. $MockedController =& new MockedController();
  1320. $MockedController->components = array('MockTest');
  1321. $MockedController->Component =& new Component();
  1322. $MockedController->Component->init($MockedController);
  1323. $MockedController->expectCallCount('beforeFilter', 1);
  1324. $MockedController->MockTest->expectCallCount('initialize', 1);
  1325. $MockedController->MockTest->expectCallCount('startup', 1);
  1326. $MockedController->startupProcess();
  1327. }
  1328. /**
  1329. * Tests that the shutdown process calls the correct functions
  1330. *
  1331. * @access public
  1332. * @return void
  1333. */
  1334. function testShutdownProcess() {
  1335. Mock::generate('TestComponent', 'MockTestComponent', array('shutdown'));
  1336. $MockedController =& new MockedController();
  1337. $MockedController->components = array('MockTest');
  1338. $MockedController->Component =& new Component();
  1339. $MockedController->Component->init($MockedController);
  1340. $MockedController->expectCallCount('afterFilter', 1);
  1341. $MockedController->MockTest->expectCallCount('shutdown', 1);
  1342. $MockedController->shutdownProcess();
  1343. }
  1344. /**
  1345. * Tests that the correct alias is selected
  1346. *
  1347. * @return void
  1348. */
  1349. function testValidateSortAlias() {
  1350. $Controller =& new Controller();
  1351. $Controller->uses = array('ControllerPost', 'ControllerComment');
  1352. $Controller->passedArgs[] = '1';
  1353. $Controller->params['url'] = array();
  1354. $Controller->constructClasses();
  1355. $Controller->passedArgs = array('sort' => 'Derp.id', 'direction' => 'asc');
  1356. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  1357. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  1358. $this->assertEqual($results, array(1, 2, 3));
  1359. }
  1360. }