PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/Forbin/cakephp2x
PHP | 1311 lines | 709 code | 165 blank | 437 comment | 6 complexity | c7a02b869c4aa8139da84d8c91eb692f MD5 | raw file
  1. <?php
  2. /**
  3. * ControllerTest file
  4. *
  5. * PHP Version 5.x
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs.controller
  17. * @since CakePHP(tm) v 1.2.0.5436
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  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('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. /**
  335. * AnotherTestController class
  336. *
  337. * @package cake
  338. * @subpackage cake.tests.cases.libs.controller
  339. */
  340. class AnotherTestController extends AppController {
  341. /**
  342. * name property
  343. * @var string 'Name'
  344. * @access public
  345. */
  346. var $name = 'AnotherTest';
  347. /**
  348. * uses property
  349. *
  350. * @var array
  351. * @access public
  352. */
  353. var $uses = null;
  354. }
  355. /**
  356. * ControllerTest class
  357. *
  358. * @package cake
  359. * @subpackage cake.tests.cases.libs.controller
  360. */
  361. class ControllerTest extends CakeTestCase {
  362. /**
  363. * fixtures property
  364. *
  365. * @var array
  366. * @access public
  367. */
  368. var $fixtures = array('core.post', 'core.comment', 'core.name');
  369. /**
  370. * endTest
  371. *
  372. * @access public
  373. * @return void
  374. */
  375. function endTest() {
  376. App::build();
  377. }
  378. /**
  379. * testConstructClasses method
  380. *
  381. * @access public
  382. * @return void
  383. */
  384. function testConstructClasses() {
  385. $Controller = new Controller();
  386. $Controller->modelClass = 'ControllerPost';
  387. $Controller->passedArgs[] = '1';
  388. $Controller->constructClasses();
  389. $this->assertEqual($Controller->ControllerPost->id, 1);
  390. unset($Controller);
  391. $Controller = new Controller();
  392. $Controller->uses = array('ControllerPost', 'ControllerComment');
  393. $Controller->passedArgs[] = '1';
  394. $Controller->constructClasses();
  395. $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
  396. $this->assertTrue(is_a($Controller->ControllerComment, 'ControllerComment'));
  397. $this->assertEqual($Controller->ControllerComment->name, 'Comment');
  398. unset($Controller);
  399. App::build(array('plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)));
  400. $Controller = new Controller();
  401. $Controller->uses = array('TestPlugin.TestPluginPost');
  402. $Controller->constructClasses();
  403. $this->assertEqual($Controller->modelClass, 'TestPluginPost');
  404. $this->assertTrue(isset($Controller->TestPluginPost));
  405. $this->assertTrue(is_a($Controller->TestPluginPost, 'TestPluginPost'));
  406. unset($Controller);
  407. }
  408. /**
  409. * testAliasName method
  410. *
  411. * @access public
  412. * @return void
  413. */
  414. function testAliasName() {
  415. $Controller = new Controller();
  416. $Controller->uses = array('NameTest');
  417. $Controller->constructClasses();
  418. $this->assertEqual($Controller->NameTest->name, 'Name');
  419. $this->assertEqual($Controller->NameTest->alias, 'Name');
  420. unset($Controller);
  421. }
  422. /**
  423. * testPersistent method
  424. *
  425. * @access public
  426. * @return void
  427. */
  428. function testPersistent() {
  429. Configure::write('Cache.disable', false);
  430. $Controller = new Controller();
  431. $Controller->modelClass = 'ControllerPost';
  432. $Controller->persistModel = true;
  433. $Controller->constructClasses();
  434. $this->assertTrue(file_exists(CACHE . 'persistent' . DS .'controllerpost.php'));
  435. $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
  436. @unlink(CACHE . 'persistent' . DS . 'controllerpost.php');
  437. @unlink(CACHE . 'persistent' . DS . 'controllerpostregistry.php');
  438. unset($Controller);
  439. Configure::write('Cache.disable', true);
  440. }
  441. /**
  442. * testPaginate method
  443. *
  444. * @access public
  445. * @return void
  446. */
  447. function testPaginate() {
  448. $Controller = new Controller();
  449. $Controller->uses = array('ControllerPost', 'ControllerComment');
  450. $Controller->passedArgs[] = '1';
  451. $Controller->params['url'] = array();
  452. $Controller->constructClasses();
  453. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  454. $this->assertEqual($results, array(1, 2, 3));
  455. $results = Set::extract($Controller->paginate('ControllerComment'), '{n}.ControllerComment.id');
  456. $this->assertEqual($results, array(1, 2, 3, 4, 5, 6));
  457. $Controller->modelClass = null;
  458. $Controller->uses[0] = 'Plugin.ControllerPost';
  459. $results = Set::extract($Controller->paginate(), '{n}.ControllerPost.id');
  460. $this->assertEqual($results, array(1, 2, 3));
  461. $Controller->passedArgs = array('page' => '-1');
  462. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  463. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  464. $this->assertEqual($results, array(1, 2, 3));
  465. $Controller->passedArgs = array('sort' => 'ControllerPost.id', 'direction' => 'asc');
  466. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  467. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  468. $this->assertEqual($results, array(1, 2, 3));
  469. $Controller->passedArgs = array('sort' => 'ControllerPost.id', 'direction' => 'desc');
  470. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  471. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  472. $this->assertEqual($results, array(3, 2, 1));
  473. $Controller->passedArgs = array('sort' => 'id', 'direction' => 'desc');
  474. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  475. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  476. $this->assertEqual($results, array(3, 2, 1));
  477. $Controller->passedArgs = array('sort' => 'NotExisting.field', 'direction' => 'desc');
  478. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  479. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1, 'Invalid field in query %s');
  480. $this->assertEqual($results, array(1, 2, 3));
  481. $Controller->passedArgs = array('sort' => 'ControllerPost.author_id', 'direction' => 'allYourBase');
  482. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  483. $this->assertEqual($Controller->ControllerPost->lastQuery['order'][0], array('ControllerPost.author_id' => 'asc'));
  484. $this->assertEqual($results, array(1, 3, 2));
  485. $Controller->passedArgs = array('page' => '1 " onclick="alert(\'xss\');">');
  486. $Controller->paginate = array('limit' => 1);
  487. $Controller->paginate('ControllerPost');
  488. $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1, 'XSS exploit opened %s');
  489. $this->assertIdentical($Controller->params['paging']['ControllerPost']['options']['page'], 1, 'XSS exploit opened %s');
  490. $Controller->passedArgs = array();
  491. $Controller->paginate = array('limit' => 0);
  492. $Controller->paginate('ControllerPost');
  493. $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1);
  494. $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
  495. $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
  496. $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
  497. $Controller->passedArgs = array();
  498. $Controller->paginate = array('limit' => 'garbage!');
  499. $Controller->paginate('ControllerPost');
  500. $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1);
  501. $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
  502. $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
  503. $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
  504. }
  505. /**
  506. * testPaginateExtraParams method
  507. *
  508. * @access public
  509. * @return void
  510. */
  511. function testPaginateExtraParams() {
  512. $Controller = new Controller();
  513. $Controller->uses = array('ControllerPost', 'ControllerComment');
  514. $Controller->passedArgs[] = '1';
  515. $Controller->params['url'] = array();
  516. $Controller->constructClasses();
  517. $Controller->passedArgs = array('page' => '-1', 'contain' => array('ControllerComment'));
  518. $result = $Controller->paginate('ControllerPost');
  519. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  520. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3));
  521. $this->assertTrue(!isset($Controller->ControllerPost->lastQuery['contain']));
  522. $Controller->passedArgs = array('page' => '-1');
  523. $Controller->paginate = array('ControllerPost' => array('contain' => array('ControllerComment')));
  524. $result = $Controller->paginate('ControllerPost');
  525. $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
  526. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3));
  527. $this->assertTrue(isset($Controller->ControllerPost->lastQuery['contain']));
  528. $Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title')));
  529. $result = $Controller->paginate('ControllerPost');
  530. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3));
  531. $this->assertEqual($Controller->ControllerPost->lastQuery['conditions'], array('ControllerPost.id > ' => '1'));
  532. $Controller->passedArgs = array('limit' => 12);
  533. $Controller->paginate = array('limit' => 30);
  534. $result = $Controller->paginate('ControllerPost');
  535. $paging = $Controller->params['paging']['ControllerPost'];
  536. $this->assertEqual($Controller->ControllerPost->lastQuery['limit'], 12);
  537. $this->assertEqual($paging['options']['limit'], 12);
  538. $Controller = new Controller();
  539. $Controller->uses = array('ControllerPaginateModel');
  540. $Controller->params['url'] = array();
  541. $Controller->constructClasses();
  542. $Controller->paginate = array(
  543. 'ControllerPaginateModel' => array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id')
  544. );
  545. $result = $Controller->paginate('ControllerPaginateModel');
  546. $expected = array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id');
  547. $this->assertEqual($Controller->ControllerPaginateModel->extra, $expected);
  548. $this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected);
  549. $Controller->paginate = array(
  550. 'ControllerPaginateModel' => array('foo', 'contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id')
  551. );
  552. $Controller->paginate('ControllerPaginateModel');
  553. $expected = array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id', 'type' => 'foo');
  554. $this->assertEqual($Controller->ControllerPaginateModel->extra, $expected);
  555. $this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected);
  556. }
  557. /**
  558. * testPaginatePassedArgs method
  559. *
  560. * @return void
  561. * @access public
  562. */
  563. function testPaginatePassedArgs() {
  564. $Controller = new Controller();
  565. $Controller->uses = array('ControllerPost');
  566. $Controller->passedArgs[] = array('1', '2', '3');
  567. $Controller->params['url'] = array();
  568. $Controller->constructClasses();
  569. $Controller->paginate = array(
  570. 'fields' => array(),
  571. 'order' => '',
  572. 'limit' => 5,
  573. 'page' => 1,
  574. 'recursive' => -1
  575. );
  576. $conditions = array();
  577. $Controller->paginate('ControllerPost',$conditions);
  578. $expected = array(
  579. 'fields' => array(),
  580. 'order' => '',
  581. 'limit' => 5,
  582. 'page' => 1,
  583. 'recursive' => -1,
  584. 'conditions' => array()
  585. );
  586. $this->assertEqual($Controller->params['paging']['ControllerPost']['options'],$expected);
  587. }
  588. /**
  589. * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
  590. *
  591. * @return void
  592. */
  593. function testPaginateSpecialType() {
  594. $Controller =& new Controller();
  595. $Controller->uses = array('ControllerPost', 'ControllerComment');
  596. $Controller->passedArgs[] = '1';
  597. $Controller->params['url'] = array();
  598. $Controller->constructClasses();
  599. $Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title')));
  600. $result = $Controller->paginate('ControllerPost');
  601. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3));
  602. $this->assertEqual($Controller->ControllerPost->lastQuery['conditions'], array('ControllerPost.id > ' => '1'));
  603. $this->assertFalse(isset($Controller->params['paging']['ControllerPost']['defaults'][0]));
  604. $this->assertFalse(isset($Controller->params['paging']['ControllerPost']['options'][0]));
  605. }
  606. /**
  607. * testDefaultPaginateParams method
  608. *
  609. * @access public
  610. * @return void
  611. */
  612. function testDefaultPaginateParams() {
  613. $Controller = new Controller();
  614. $Controller->modelClass = 'ControllerPost';
  615. $Controller->params['url'] = array();
  616. $Controller->paginate = array('order' => 'ControllerPost.id DESC');
  617. $Controller->constructClasses();
  618. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  619. $this->assertEqual($Controller->params['paging']['ControllerPost']['defaults']['order'], 'ControllerPost.id DESC');
  620. $this->assertEqual($Controller->params['paging']['ControllerPost']['options']['order'], 'ControllerPost.id DESC');
  621. $this->assertEqual($results, array(3, 2, 1));
  622. }
  623. /**
  624. * test paginate() and virtualField interactions
  625. *
  626. * @return void
  627. */
  628. function testPaginateOrderVirtualField() {
  629. $Controller = new Controller();
  630. $Controller->uses = array('ControllerPost', 'ControllerComment');
  631. $Controller->params['url'] = array();
  632. $Controller->constructClasses();
  633. $Controller->ControllerPost->virtualFields = array(
  634. 'offset_test' => 'ControllerPost.id + 1'
  635. );
  636. $Controller->paginate = array(
  637. 'fields' => array('id', 'title', 'offset_test'),
  638. 'order' => array('offset_test' => 'DESC')
  639. );
  640. $result = $Controller->paginate('ControllerPost');
  641. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(4, 3, 2));
  642. $Controller->passedArgs = array('sort' => 'offset_test', 'direction' => 'asc');
  643. $result = $Controller->paginate('ControllerPost');
  644. $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(2, 3, 4));
  645. }
  646. /**
  647. * testFlash method
  648. *
  649. * @access public
  650. * @return void
  651. */
  652. function testFlash() {
  653. $Controller = new Controller();
  654. $Controller->flash('this should work', '/flash');
  655. $result = $Controller->output;
  656. $expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  657. <html xmlns="http://www.w3.org/1999/xhtml">
  658. <head>
  659. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  660. <title>this should work</title>
  661. <style><!--
  662. P { text-align:center; font:bold 1.1em sans-serif }
  663. A { color:#444; text-decoration:none }
  664. A:HOVER { text-decoration: underline; color:#44E }
  665. --></style>
  666. </head>
  667. <body>
  668. <p><a href="/flash">this should work</a></p>
  669. </body>
  670. </html>';
  671. $result = str_replace(array("\t", "\r\n", "\n"), "", $result);
  672. $expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
  673. $this->assertEqual($result, $expected);
  674. App::build(array('views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)));
  675. $Controller =& new Controller();
  676. $Controller->flash('this should work', '/flash', 1, 'ajax2');
  677. $result = $Controller->output;
  678. $this->assertPattern('/Ajax!/', $result);
  679. App::build();
  680. }
  681. /**
  682. * testControllerSet method
  683. *
  684. * @access public
  685. * @return void
  686. */
  687. function testControllerSet() {
  688. $Controller = new Controller();
  689. $Controller->set('variable_with_underscores', null);
  690. $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
  691. $Controller->viewVars = array();
  692. $viewVars = array('ModelName' => array('id' => 1, 'name' => 'value'));
  693. $Controller->set($viewVars);
  694. $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
  695. $Controller->viewVars = array();
  696. $Controller->set('variable_with_underscores', 'value');
  697. $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
  698. $Controller->viewVars = array();
  699. $viewVars = array('ModelName' => 'name');
  700. $Controller->set($viewVars);
  701. $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
  702. $Controller->set('title', 'someTitle');
  703. $this->assertIdentical($Controller->viewVars['title'], 'someTitle');
  704. $this->assertNotEqual($Controller->pageTitle, 'someTitle');
  705. $Controller->viewVars = array();
  706. $expected = array('ModelName' => 'name', 'ModelName2' => 'name2');
  707. $Controller->set(array('ModelName', 'ModelName2'), array('name', 'name2'));
  708. $this->assertIdentical($Controller->viewVars, $expected);
  709. }
  710. /**
  711. * testRender method
  712. *
  713. * @access public
  714. * @return void
  715. */
  716. function testRender() {
  717. App::build(array(
  718. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  719. ), true);
  720. $Controller = new Controller();
  721. $Controller->viewPath = 'posts';
  722. $result = $Controller->render('index');
  723. $this->assertPattern('/posts index/', $result);
  724. $result = $Controller->render('/elements/test_element');
  725. $this->assertPattern('/this is the test element/', $result);
  726. $Controller = new TestController();
  727. $Controller->constructClasses();
  728. $Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
  729. $expected = $Controller->ControllerComment->validationErrors;
  730. ClassRegistry::flush();
  731. $Controller->viewPath = 'posts';
  732. $result = $Controller->render('index');
  733. $View = ClassRegistry::getObject('view');
  734. $this->assertTrue(isset($View->validationErrors['ControllerComment']));
  735. $this->assertEqual($expected, $View->validationErrors['ControllerComment']);
  736. $Controller->ControllerComment->validationErrors = array();
  737. ClassRegistry::flush();
  738. App::build();
  739. }
  740. /**
  741. * testToBeInheritedGuardmethods method
  742. *
  743. * @access public
  744. * @return void
  745. */
  746. function testToBeInheritedGuardmethods() {
  747. $Controller = new Controller();
  748. $this->assertTrue($Controller->_beforeScaffold(''));
  749. $this->assertTrue($Controller->_afterScaffoldSave(''));
  750. $this->assertTrue($Controller->_afterScaffoldSaveError(''));
  751. $this->assertFalse($Controller->_scaffoldError(''));
  752. }
  753. /**
  754. * testRedirect method
  755. *
  756. * @access public
  757. * @return void
  758. */
  759. function testRedirect() {
  760. $codes = array(
  761. 100 => "Continue",
  762. 101 => "Switching Protocols",
  763. 200 => "OK",
  764. 201 => "Created",
  765. 202 => "Accepted",
  766. 203 => "Non-Authoritative Information",
  767. 204 => "No Content",
  768. 205 => "Reset Content",
  769. 206 => "Partial Content",
  770. 300 => "Multiple Choices",
  771. 301 => "Moved Permanently",
  772. 302 => "Found",
  773. 303 => "See Other",
  774. 304 => "Not Modified",
  775. 305 => "Use Proxy",
  776. 307 => "Temporary Redirect",
  777. 400 => "Bad Request",
  778. 401 => "Unauthorized",
  779. 402 => "Payment Required",
  780. 403 => "Forbidden",
  781. 404 => "Not Found",
  782. 405 => "Method Not Allowed",
  783. 406 => "Not Acceptable",
  784. 407 => "Proxy Authentication Required",
  785. 408 => "Request Time-out",
  786. 409 => "Conflict",
  787. 410 => "Gone",
  788. 411 => "Length Required",
  789. 412 => "Precondition Failed",
  790. 413 => "Request Entity Too Large",
  791. 414 => "Request-URI Too Large",
  792. 415 => "Unsupported Media Type",
  793. 416 => "Requested range not satisfiable",
  794. 417 => "Expectation Failed",
  795. 500 => "Internal Server Error",
  796. 501 => "Not Implemented",
  797. 502 => "Bad Gateway",
  798. 503 => "Service Unavailable",
  799. 504 => "Gateway Time-out"
  800. );
  801. Mock::generatePartial('Controller', 'MockController', array('header'));
  802. Mock::generate('TestComponent', 'MockTestComponent');
  803. Mock::generate('TestComponent', 'MockTestBComponent');
  804. App::import('Helper', 'Cache');
  805. foreach ($codes as $code => $msg) {
  806. $MockController = new MockController();
  807. $MockController->Component = new Component();
  808. $MockController->Component->init($MockController);
  809. $MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}"));
  810. $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
  811. $MockController->expectCallCount('header', 2);
  812. $MockController->redirect('http://cakephp.org', (int)$code, false);
  813. $this->assertFalse($MockController->autoRender);
  814. }
  815. foreach ($codes as $code => $msg) {
  816. $MockController = new MockController();
  817. $MockController->Component = new Component();
  818. $MockController->Component->init($MockController);
  819. $MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}"));
  820. $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
  821. $MockController->expectCallCount('header', 2);
  822. $MockController->redirect('http://cakephp.org', $msg, false);
  823. $this->assertFalse($MockController->autoRender);
  824. }
  825. $MockController = new MockController();
  826. $MockController->Component = new Component();
  827. $MockController->Component->init($MockController);
  828. $MockController->expectAt(0, 'header', array('Location: http://www.example.org/users/login'));
  829. $MockController->expectCallCount('header', 1);
  830. $MockController->redirect('http://www.example.org/users/login', null, false);
  831. $MockController = new MockController();
  832. $MockController->Component = new Component();
  833. $MockController->Component->init($MockController);
  834. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  835. $MockController->expectAt(1, 'header', array('Location: http://www.example.org/users/login'));
  836. $MockController->expectCallCount('header', 2);
  837. $MockController->redirect('http://www.example.org/users/login', 301, false);
  838. $MockController = new MockController();
  839. $MockController->components = array('MockTest');
  840. $MockController->Component = new Component();
  841. $MockController->Component->init($MockController);
  842. $MockController->MockTest->setReturnValue('beforeRedirect', null);
  843. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  844. $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
  845. $MockController->expectCallCount('header', 2);
  846. $MockController->redirect('http://cakephp.org', 301, false);
  847. $MockController = new MockController();
  848. $MockController->components = array('MockTest');
  849. $MockController->Component = new Component();
  850. $MockController->Component->init($MockController);
  851. $MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org');
  852. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  853. $MockController->expectAt(1, 'header', array('Location: http://book.cakephp.org'));
  854. $MockController->expectCallCount('header', 2);
  855. $MockController->redirect('http://cakephp.org', 301, false);
  856. $MockController = new MockController();
  857. $MockController->components = array('MockTest');
  858. $MockController->Component = new Component();
  859. $MockController->Component->init($MockController);
  860. $MockController->MockTest->setReturnValue('beforeRedirect', false);
  861. $MockController->expectNever('header');
  862. $MockController->redirect('http://cakephp.org', 301, false);
  863. $MockController = new MockController();
  864. $MockController->components = array('MockTest', 'MockTestB');
  865. $MockController->Component = new Component();
  866. $MockController->Component->init($MockController);
  867. $MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org');
  868. $MockController->MockTestB->setReturnValue('beforeRedirect', 'http://bakery.cakephp.org');
  869. $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
  870. $MockController->expectAt(1, 'header', array('Location: http://bakery.cakephp.org'));
  871. $MockController->expectCallCount('header', 2);
  872. $MockController->redirect('http://cakephp.org', 301, false);
  873. }
  874. /**
  875. * testMergeVars method
  876. *
  877. * @access public
  878. * @return void
  879. */
  880. function testMergeVars() {
  881. if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) {
  882. return;
  883. }
  884. $TestController = new TestController();
  885. $TestController->constructClasses();
  886. $testVars = get_class_vars('TestController');
  887. $appVars = get_class_vars('AppController');
  888. $components = is_array($appVars['components'])
  889. ? array_merge($appVars['components'], $testVars['components'])
  890. : $testVars['components'];
  891. if (!in_array('Session', $components)) {
  892. $components[] = 'Session';
  893. }
  894. $helpers = is_array($appVars['helpers'])
  895. ? array_merge($appVars['helpers'], $testVars['helpers'])
  896. : $testVars['helpers'];
  897. $uses = is_array($appVars['uses'])
  898. ? array_merge($appVars['uses'], $testVars['uses'])
  899. : $testVars['uses'];
  900. $this->assertEqual(count(array_diff($TestController->helpers, $helpers)), 0);
  901. $this->assertEqual(count(array_diff($TestController->uses, $uses)), 0);
  902. $this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->components), Set::normalize($components))), 0);
  903. $TestController = new AnotherTestController();
  904. $TestController->constructClasses();
  905. $appVars = get_class_vars('AppController');
  906. $testVars = get_class_vars('AnotherTestController');
  907. $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
  908. $this->assertNull($testVars['uses']);
  909. $this->assertFalse(isset($TestController->ControllerPost));
  910. $TestController = new ControllerCommentsController();
  911. $TestController->constructClasses();
  912. $appVars = get_class_vars('AppController');
  913. $testVars = get_class_vars('ControllerCommentsController');
  914. $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
  915. $this->assertEqual(array('ControllerPost'), $testVars['uses']);
  916. $this->assertTrue(isset($TestController->ControllerPost));
  917. $this->assertTrue(isset($TestController->ControllerComment));
  918. }
  919. /**
  920. * test that options from child classes replace those in the parent classes.
  921. *
  922. * @access public
  923. * @return void
  924. */
  925. function testChildComponentOptionsSupercedeParents() {
  926. if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) {
  927. return;
  928. }
  929. $TestController =& new TestController();
  930. $expected = array('foo');
  931. $TestController->components = array('Cookie' => $expected);
  932. $TestController->constructClasses();
  933. $this->assertEqual($TestController->components['Cookie'], $expected);
  934. }
  935. /**
  936. * Ensure that __mergeVars is not being greedy and merging with
  937. * AppController when you make an instance of Controller
  938. *
  939. * @return void
  940. */
  941. function testMergeVarsNotGreedy() {
  942. $Controller = new Controller();
  943. $Controller->components = array();
  944. $Controller->uses = array();
  945. $Controller->constructClasses();
  946. $this->assertTrue(isset($Controller->Session));
  947. }
  948. /**
  949. * testReferer method
  950. *
  951. * @access public
  952. * @return void
  953. */
  954. function testReferer() {
  955. $Controller = new Controller();
  956. $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
  957. $result = $Controller->referer(null, false);
  958. $expected = 'http://cakephp.org';
  959. $this->assertIdentical($result, $expected);
  960. $_SERVER['HTTP_REFERER'] = '';
  961. $result = $Controller->referer('http://cakephp.org', false);
  962. $expected = 'http://cakephp.org';
  963. $this->assertIdentical($result, $expected);
  964. $_SERVER['HTTP_REFERER'] = '';
  965. $referer = array(
  966. 'controller' => 'pages',
  967. 'action' => 'display',
  968. 'home'
  969. );
  970. $result = $Controller->referer($referer, false);
  971. $expected = 'http://' . env('HTTP_HOST') . '/pages/display/home';
  972. $this->assertIdentical($result, $expected);
  973. $_SERVER['HTTP_REFERER'] = '';
  974. $result = $Controller->referer(null, false);
  975. $expected = '/';
  976. $this->assertIdentical($result, $expected);
  977. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path';
  978. $result = $Controller->referer(null, false);
  979. $expected = '/some/path';
  980. $this->assertIdentical($result, $expected);
  981. $Controller->webroot .= '/';
  982. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path';
  983. $result = $Controller->referer(null, false);
  984. $expected = '/some/path';
  985. $this->assertIdentical($result, $expected);
  986. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'some/path';
  987. $result = $Controller->referer(null, false);
  988. $expected = '/some/path';
  989. $this->assertIdentical($result, $expected);
  990. $Controller->webroot = '/recipe/';
  991. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'recipes/add';
  992. $result = $Controller->referer();
  993. $expected = '/recipes/add';
  994. $this->assertIdentical($result, $expected);
  995. }
  996. /**
  997. * testSetAction method
  998. *
  999. * @access public
  1000. * @return void
  1001. */
  1002. function testSetAction() {
  1003. $TestController = new TestController();
  1004. $TestController->setAction('index', 1, 2);
  1005. $expected = array('testId' => 1, 'test2Id' => 2);
  1006. $this->assertidentical($TestController->data, $expected);
  1007. }
  1008. /**
  1009. * testUnimplementedIsAuthorized method
  1010. *
  1011. * @access public
  1012. * @return void
  1013. */
  1014. function testUnimplementedIsAuthorized() {
  1015. $TestController = new TestController();
  1016. $TestController->isAuthorized();
  1017. $this->assertError();
  1018. }
  1019. /**
  1020. * testValidateErrors method
  1021. *
  1022. * @access public
  1023. * @return void
  1024. */
  1025. function testValidateErrors() {
  1026. $TestController = new TestController();
  1027. $TestController->constructClasses();
  1028. $this->assertFalse($TestController->validateErrors());
  1029. $this->assertEqual($TestController->validate(), 0);
  1030. $TestController->ControllerComment->invalidate('some_field', 'error_message');
  1031. $TestController->ControllerComment->invalidate('some_field2', 'error_message2');
  1032. $comment = new ControllerComment;
  1033. $comment->set('someVar', 'data');
  1034. $result = $TestController->validateErrors($comment);
  1035. $expected = array('some_field' => 'error_message', 'some_field2' => 'error_message2');
  1036. $this->assertIdentical($result, $expected);
  1037. $this->assertEqual($TestController->validate($comment), 2);
  1038. }
  1039. /**
  1040. * testPostConditions method
  1041. *
  1042. * @access public
  1043. * @return void
  1044. */
  1045. function testPostConditions() {
  1046. $Controller = new Controller();
  1047. $data = array(
  1048. 'Model1' => array('field1' => '23'),
  1049. 'Model2' => array('field2' => 'string'),
  1050. 'Model3' => array('field3' => '23'),
  1051. );
  1052. $expected = array(
  1053. 'Model1.field1' => '23',
  1054. 'Model2.field2' => 'string',
  1055. 'Model3.field3' => '23',
  1056. );
  1057. $result = $Controller->postConditions($data);
  1058. $this->assertIdentical($result, $expected);
  1059. $data = array();
  1060. $Controller->data = array(
  1061. 'Model1' => array('field1' => '23'),
  1062. 'Model2' => array('field2' => 'string'),
  1063. 'Model3' => array('field3' => '23'),
  1064. );
  1065. $expected = array(
  1066. 'Model1.field1' => '23',
  1067. 'Model2.field2' => 'string',
  1068. 'Model3.field3' => '23',
  1069. );
  1070. $result = $Controller->postConditions($data);
  1071. $this->assertIdentical($result, $expected);
  1072. $data = array();
  1073. $Controller->data = array();
  1074. $result = $Controller->postConditions($data);
  1075. $this->assertNull($result);
  1076. $data = array();
  1077. $Controller->data = array(
  1078. 'Model1' => array('field1' => '23'),
  1079. 'Model2' => array('field2' => 'string'),
  1080. 'Model3' => array('field3' => '23'),
  1081. );
  1082. $ops = array(
  1083. 'Model1.field1' => '>',
  1084. 'Model2.field2' => 'LIKE',
  1085. 'Model3.field3' => '<=',
  1086. );
  1087. $expected = array(
  1088. 'Model1.field1 >' => '23',
  1089. 'Model2.field2 LIKE' => "%string%",
  1090. 'Model3.field3 <=' => '23',
  1091. );
  1092. $result = $Controller->postConditions($data, $ops);
  1093. $this->assertIdentical($result, $expected);
  1094. }
  1095. /**
  1096. * testRequestHandlerPrefers method
  1097. *
  1098. * @access public
  1099. * @return void
  1100. */
  1101. function testRequestHandlerPrefers(){
  1102. Configure::write('debug', 2);
  1103. $Controller = new Controller();
  1104. $Controller->components = array("RequestHandler");
  1105. $Controller->modelClass='ControllerPost';
  1106. $Controller->params['url']['ext'] = 'rss';
  1107. $Controller->constructClasses();
  1108. $Controller->Component->initialize($Controller);
  1109. $Controller->beforeFilter();
  1110. $Controller->Component->startup($Controller);
  1111. $this->assertEqual($Controller->RequestHandler->prefers(), 'rss');
  1112. unset($Controller);
  1113. }
  1114. /**
  1115. * testControllerHttpCodes method
  1116. *
  1117. * @access public
  1118. * @return void
  1119. */
  1120. function testControllerHttpCodes() {
  1121. $Controller = new Controller();
  1122. $result = $Controller->httpCodes();
  1123. $this->assertEqual(count($result), 39);
  1124. $result = $Controller->httpCodes(100);
  1125. $expected = array(100 => 'Continue');
  1126. $this->assertEqual($result, $expected);
  1127. $codes = array(
  1128. 1337 => 'Undefined Unicorn',
  1129. 1729 => 'Hardy-Ramanujan Located'
  1130. );
  1131. $result = $Controller->httpCodes($codes);
  1132. $this->assertTrue($result);
  1133. $this->assertEqual(count($Controller->httpCodes()), 41);
  1134. $result = $Controller->httpCodes(1337);
  1135. $expected = array(1337 => 'Undefined Unicorn');
  1136. $this->assertEqual($result, $expected);
  1137. $codes = array(404 => 'Sorry Bro');
  1138. $result = $Controller->httpCodes($codes);
  1139. $this->assertTrue($result);
  1140. $this->assertEqual(count($Controller->httpCodes()), 41);
  1141. $result = $Controller->httpCodes(404);
  1142. $expected = array(404 => 'Sorry Bro');
  1143. $this->assertEqual($result, $expected);
  1144. }
  1145. }
  1146. ?>