ControllerTest.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. <?php
  2. /**
  3. * ControllerTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP Project
  15. * @package cake.tests.cases.libs.controller
  16. * @since CakePHP(tm) v 1.2.0.5436
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Controller', 'Controller');
  20. App::uses('Router', 'Routing');
  21. App::uses('CakeRequest', 'Network');
  22. App::uses('CakeResponse', 'Network');
  23. App::uses('SecurityComponent', 'Controller/Component');
  24. App::uses('CookieComponent', 'Controller/Component');
  25. /**
  26. * AppController class
  27. *
  28. * @package cake.tests.cases.libs.controller
  29. */
  30. class ControllerTestAppController extends Controller {
  31. /**
  32. * helpers property
  33. *
  34. * @var array
  35. * @access public
  36. */
  37. public $helpers = array('Html');
  38. /**
  39. * uses property
  40. *
  41. * @var array
  42. * @access public
  43. */
  44. public $uses = array('ControllerPost');
  45. /**
  46. * components property
  47. *
  48. * @var array
  49. * @access public
  50. */
  51. public $components = array('Cookie');
  52. }
  53. /**
  54. * ControllerPost class
  55. *
  56. * @package cake.tests.cases.libs.controller
  57. */
  58. class ControllerPost extends CakeTestModel {
  59. /**
  60. * name property
  61. *
  62. * @var string 'ControllerPost'
  63. * @access public
  64. */
  65. public $name = 'ControllerPost';
  66. /**
  67. * useTable property
  68. *
  69. * @var string 'posts'
  70. * @access public
  71. */
  72. public $useTable = 'posts';
  73. /**
  74. * invalidFields property
  75. *
  76. * @var array
  77. * @access public
  78. */
  79. public $invalidFields = array('name' => 'error_msg');
  80. /**
  81. * lastQuery property
  82. *
  83. * @var mixed null
  84. * @access public
  85. */
  86. public $lastQuery = null;
  87. /**
  88. * beforeFind method
  89. *
  90. * @param mixed $query
  91. * @access public
  92. * @return void
  93. */
  94. public function beforeFind($query) {
  95. $this->lastQuery = $query;
  96. }
  97. /**
  98. * find method
  99. *
  100. * @param mixed $type
  101. * @param array $options
  102. * @access public
  103. * @return void
  104. */
  105. public function find($type, $options = array()) {
  106. if ($type == 'popular') {
  107. $conditions = array($this->name . '.' . $this->primaryKey .' > ' => '1');
  108. $options = Set::merge($options, compact('conditions'));
  109. return parent::find('all', $options);
  110. }
  111. return parent::find($type, $options);
  112. }
  113. }
  114. /**
  115. * ControllerPostsController class
  116. *
  117. * @package cake.tests.cases.libs.controller
  118. */
  119. class ControllerCommentsController extends ControllerTestAppController {
  120. /**
  121. * name property
  122. *
  123. * @var string 'ControllerPost'
  124. * @access public
  125. */
  126. public $name = 'ControllerComments';
  127. protected $_mergeParent = 'ControllerTestAppController';
  128. }
  129. /**
  130. * ControllerComment class
  131. *
  132. * @package cake.tests.cases.libs.controller
  133. */
  134. class ControllerComment extends CakeTestModel {
  135. /**
  136. * name property
  137. *
  138. * @var string 'ControllerComment'
  139. * @access public
  140. */
  141. public $name = 'Comment';
  142. /**
  143. * useTable property
  144. *
  145. * @var string 'comments'
  146. * @access public
  147. */
  148. public $useTable = 'comments';
  149. /**
  150. * data property
  151. *
  152. * @var array
  153. * @access public
  154. */
  155. public $data = array('name' => 'Some Name');
  156. /**
  157. * alias property
  158. *
  159. * @var string 'ControllerComment'
  160. * @access public
  161. */
  162. public $alias = 'ControllerComment';
  163. }
  164. /**
  165. * ControllerAlias class
  166. *
  167. * @package cake.tests.cases.libs.controller
  168. */
  169. class ControllerAlias extends CakeTestModel {
  170. /**
  171. * name property
  172. *
  173. * @var string 'ControllerAlias'
  174. * @access public
  175. */
  176. public $name = 'ControllerAlias';
  177. /**
  178. * alias property
  179. *
  180. * @var string 'ControllerSomeAlias'
  181. * @access public
  182. */
  183. public $alias = 'ControllerSomeAlias';
  184. /**
  185. * useTable property
  186. *
  187. * @var string 'posts'
  188. * @access public
  189. */
  190. public $useTable = 'posts';
  191. }
  192. /**
  193. * NameTest class
  194. *
  195. * @package cake.tests.cases.libs.controller
  196. */
  197. class NameTest extends CakeTestModel {
  198. /**
  199. * name property
  200. * @var string 'Name'
  201. * @access public
  202. */
  203. public $name = 'Name';
  204. /**
  205. * useTable property
  206. * @var string 'names'
  207. * @access public
  208. */
  209. public $useTable = 'comments';
  210. /**
  211. * alias property
  212. *
  213. * @var string 'ControllerComment'
  214. * @access public
  215. */
  216. public $alias = 'Name';
  217. }
  218. /**
  219. * TestController class
  220. *
  221. * @package cake.tests.cases.libs.controller
  222. */
  223. class TestController extends ControllerTestAppController {
  224. /**
  225. * name property
  226. * @var string 'Name'
  227. * @access public
  228. */
  229. public $name = 'TestController';
  230. /**
  231. * helpers property
  232. *
  233. * @var array
  234. * @access public
  235. */
  236. public $helpers = array('Session', 'Xml');
  237. /**
  238. * components property
  239. *
  240. * @var array
  241. * @access public
  242. */
  243. public $components = array('Security');
  244. /**
  245. * uses property
  246. *
  247. * @var array
  248. * @access public
  249. */
  250. public $uses = array('ControllerComment', 'ControllerAlias');
  251. protected $_mergeParent = 'ControllerTestAppController';
  252. /**
  253. * index method
  254. *
  255. * @param mixed $testId
  256. * @param mixed $test2Id
  257. * @access public
  258. * @return void
  259. */
  260. public function index($testId, $test2Id) {
  261. $this->data = array(
  262. 'testId' => $testId,
  263. 'test2Id' => $test2Id
  264. );
  265. }
  266. }
  267. /**
  268. * TestComponent class
  269. *
  270. * @package cake.tests.cases.libs.controller
  271. */
  272. class TestComponent extends Object {
  273. /**
  274. * beforeRedirect method
  275. *
  276. * @access public
  277. * @return void
  278. */
  279. public function beforeRedirect() {
  280. }
  281. /**
  282. * initialize method
  283. *
  284. * @access public
  285. * @return void
  286. */
  287. public function initialize(&$controller) {
  288. }
  289. /**
  290. * startup method
  291. *
  292. * @access public
  293. * @return void
  294. */
  295. public function startup(&$controller) {
  296. }
  297. /**
  298. * shutdown method
  299. *
  300. * @access public
  301. * @return void
  302. */
  303. public function shutdown(&$controller) {
  304. }
  305. /**
  306. * beforeRender callback
  307. *
  308. * @return void
  309. */
  310. public function beforeRender(&$controller) {
  311. if ($this->viewclass) {
  312. $controller->viewClass = $this->viewclass;
  313. }
  314. }
  315. }
  316. /**
  317. * AnotherTestController class
  318. *
  319. * @package cake.tests.cases.libs.controller
  320. */
  321. class AnotherTestController extends ControllerTestAppController {
  322. /**
  323. * name property
  324. * @var string 'Name'
  325. * @access public
  326. */
  327. public $name = 'AnotherTest';
  328. /**
  329. * uses property
  330. *
  331. * @var array
  332. * @access public
  333. */
  334. public $uses = null;
  335. protected $_mergeParent = 'ControllerTestAppController';
  336. }
  337. /**
  338. * ControllerTest class
  339. *
  340. * @package cake.tests.cases.libs.controller
  341. */
  342. class ControllerTest extends CakeTestCase {
  343. /**
  344. * fixtures property
  345. *
  346. * @var array
  347. * @access public
  348. */
  349. public $fixtures = array('core.post', 'core.comment', 'core.name');
  350. /**
  351. * reset environment.
  352. *
  353. * @return void
  354. */
  355. public function setUp() {
  356. App::objects('plugin', null, false);
  357. App::build();
  358. Router::reload();
  359. }
  360. /**
  361. * teardown
  362. *
  363. * @access public
  364. * @return void
  365. */
  366. public function teardown() {
  367. CakePlugin::unload();
  368. App::build();
  369. }
  370. /**
  371. * testLoadModel method
  372. *
  373. * @access public
  374. * @return void
  375. */
  376. public function testLoadModel() {
  377. $request = new CakeRequest('controller_posts/index');
  378. $Controller = new Controller($request);
  379. $this->assertFalse(isset($Controller->ControllerPost));
  380. $result = $Controller->loadModel('ControllerPost');
  381. $this->assertTrue($result);
  382. $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
  383. $this->assertTrue(in_array('ControllerPost', $Controller->modelNames));
  384. ClassRegistry::flush();
  385. unset($Controller);
  386. }
  387. /**
  388. * testLoadModel method from a plugin controller
  389. *
  390. * @access public
  391. * @return void
  392. */
  393. public function testLoadModelInPlugins() {
  394. App::build(array(
  395. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  396. 'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
  397. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS)
  398. ));
  399. CakePlugin::load('TestPlugin');
  400. App::uses('TestPluginAppController', 'TestPlugin.Controller');
  401. App::uses('TestPluginController', 'TestPlugin.Controller');
  402. $Controller = new TestPluginController();
  403. $Controller->plugin = 'TestPlugin';
  404. $Controller->uses = false;
  405. $this->assertFalse(isset($Controller->Comment));
  406. $result = $Controller->loadModel('Comment');
  407. $this->assertTrue($result);
  408. $this->assertInstanceOf('Comment', $Controller->Comment);
  409. $this->assertTrue(in_array('Comment', $Controller->modelNames));
  410. ClassRegistry::flush();
  411. unset($Controller);
  412. }
  413. /**
  414. * testConstructClasses method
  415. *
  416. * @access public
  417. * @return void
  418. */
  419. public function testConstructClasses() {
  420. $request = new CakeRequest('controller_posts/index');
  421. $Controller = new Controller($request);
  422. $Controller = new Controller($request);
  423. $Controller->uses = array('ControllerPost', 'ControllerComment');
  424. $Controller->constructClasses();
  425. $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
  426. $this->assertTrue(is_a($Controller->ControllerComment, 'ControllerComment'));
  427. $this->assertEqual($Controller->ControllerComment->name, 'Comment');
  428. unset($Controller);
  429. App::build(array('plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)));
  430. CakePlugin::load('TestPlugin');
  431. $Controller = new Controller($request);
  432. $Controller->uses = array('TestPlugin.TestPluginPost');
  433. $Controller->constructClasses();
  434. $this->assertTrue(isset($Controller->TestPluginPost));
  435. $this->assertTrue(is_a($Controller->TestPluginPost, 'TestPluginPost'));
  436. unset($Controller);
  437. }
  438. /**
  439. * testAliasName method
  440. *
  441. * @access public
  442. * @return void
  443. */
  444. public function testAliasName() {
  445. $request = new CakeRequest('controller_posts/index');
  446. $Controller = new Controller($request);
  447. $Controller->uses = array('NameTest');
  448. $Controller->constructClasses();
  449. $this->assertEqual($Controller->NameTest->name, 'Name');
  450. $this->assertEqual($Controller->NameTest->alias, 'Name');
  451. unset($Controller);
  452. }
  453. /**
  454. * testFlash method
  455. *
  456. * @access public
  457. * @return void
  458. */
  459. public function testFlash() {
  460. $request = new CakeRequest('controller_posts/index');
  461. $request->webroot = '/';
  462. $request->base = '/';
  463. $Controller = new Controller($request);
  464. $Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  465. $Controller->flash('this should work', '/flash');
  466. $result = $Controller->response->body();
  467. $expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  468. <html xmlns="http://www.w3.org/1999/xhtml">
  469. <head>
  470. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  471. <title>this should work</title>
  472. <style><!--
  473. P { text-align:center; font:bold 1.1em sans-serif }
  474. A { color:#444; text-decoration:none }
  475. A:HOVER { text-decoration: underline; color:#44E }
  476. --></style>
  477. </head>
  478. <body>
  479. <p><a href="/flash">this should work</a></p>
  480. </body>
  481. </html>';
  482. $result = str_replace(array("\t", "\r\n", "\n"), "", $result);
  483. $expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
  484. $this->assertEqual($expected, $result);
  485. App::build(array(
  486. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  487. ));
  488. $Controller = new Controller($request);
  489. $Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  490. $Controller->flash('this should work', '/flash', 1, 'ajax2');
  491. $result = $Controller->response->body();
  492. $this->assertPattern('/Ajax!/', $result);
  493. App::build();
  494. }
  495. /**
  496. * testControllerSet method
  497. *
  498. * @access public
  499. * @return void
  500. */
  501. public function testControllerSet() {
  502. $request = new CakeRequest('controller_posts/index');
  503. $Controller = new Controller($request);
  504. $Controller->set('variable_with_underscores', null);
  505. $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
  506. $Controller->viewVars = array();
  507. $viewVars = array('ModelName' => array('id' => 1, 'name' => 'value'));
  508. $Controller->set($viewVars);
  509. $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
  510. $Controller->viewVars = array();
  511. $Controller->set('variable_with_underscores', 'value');
  512. $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
  513. $Controller->viewVars = array();
  514. $viewVars = array('ModelName' => 'name');
  515. $Controller->set($viewVars);
  516. $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
  517. $Controller->set('title', 'someTitle');
  518. $this->assertIdentical($Controller->viewVars['title'], 'someTitle');
  519. $this->assertTrue(empty($Controller->pageTitle));
  520. $Controller->viewVars = array();
  521. $expected = array('ModelName' => 'name', 'ModelName2' => 'name2');
  522. $Controller->set(array('ModelName', 'ModelName2'), array('name', 'name2'));
  523. $this->assertIdentical($Controller->viewVars, $expected);
  524. $Controller->viewVars = array();
  525. $Controller->set(array(3 => 'three', 4 => 'four'));
  526. $Controller->set(array(1 => 'one', 2 => 'two'));
  527. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  528. $this->assertEqual($Controller->viewVars, $expected);
  529. }
  530. /**
  531. * testRender method
  532. *
  533. * @access public
  534. * @return void
  535. */
  536. public function testRender() {
  537. App::build(array(
  538. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  539. ), true);
  540. $request = new CakeRequest('controller_posts/index');
  541. $request->params['action'] = 'index';
  542. $Controller = new Controller($request, $this->getMock('CakeResponse'));
  543. $Controller->viewPath = 'Posts';
  544. $result = $Controller->render('index');
  545. $this->assertPattern('/posts index/', $result);
  546. $Controller->view = 'index';
  547. $result = $Controller->render();
  548. $this->assertPattern('/posts index/', $result);
  549. $result = $Controller->render('/Elements/test_element');
  550. $this->assertPattern('/this is the test element/', $result);
  551. $Controller->view = null;
  552. $Controller = new TestController($request);
  553. $Controller->helpers = array('Html');
  554. $Controller->constructClasses();
  555. $Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
  556. $expected = $Controller->ControllerComment->validationErrors;
  557. ClassRegistry::flush();
  558. $Controller->viewPath = 'Posts';
  559. $result = $Controller->render('index');
  560. $View = $Controller->View;
  561. $this->assertTrue(isset($View->validationErrors['ControllerComment']));
  562. $this->assertEqual($expected, $View->validationErrors['ControllerComment']);
  563. $Controller->ControllerComment->validationErrors = array();
  564. ClassRegistry::flush();
  565. App::build();
  566. }
  567. /**
  568. * test that a component beforeRender can change the controller view class.
  569. *
  570. * @return void
  571. */
  572. public function testComponentBeforeRenderChangingViewClass() {
  573. App::build(array(
  574. 'View' => array(
  575. CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS
  576. )
  577. ), true);
  578. $Controller = new Controller($this->getMock('CakeRequest'));
  579. $Controller->uses = array();
  580. $Controller->components = array('Test');
  581. $Controller->constructClasses();
  582. $Controller->Test->viewclass = 'Theme';
  583. $Controller->viewPath = 'Posts';
  584. $Controller->theme = 'TestTheme';
  585. $result = $Controller->render('index');
  586. $this->assertPattern('/default test_theme layout/', $result);
  587. App::build();
  588. }
  589. /**
  590. * testToBeInheritedGuardmethods method
  591. *
  592. * @access public
  593. * @return void
  594. */
  595. public function testToBeInheritedGuardmethods() {
  596. $request = new CakeRequest('controller_posts/index');
  597. $Controller = new Controller($request);
  598. $this->assertTrue($Controller->_beforeScaffold(''));
  599. $this->assertTrue($Controller->_afterScaffoldSave(''));
  600. $this->assertTrue($Controller->_afterScaffoldSaveError(''));
  601. $this->assertFalse($Controller->_scaffoldError(''));
  602. }
  603. /**
  604. * Generates status codes for redirect test.
  605. *
  606. * @return void
  607. */
  608. public static function statusCodeProvider() {
  609. return array(
  610. array(300, "Multiple Choices"),
  611. array(301, "Moved Permanently"),
  612. array(302, "Found"),
  613. array(303, "See Other"),
  614. array(304, "Not Modified"),
  615. array(305, "Use Proxy"),
  616. array(307, "Temporary Redirect")
  617. );
  618. }
  619. /**
  620. * testRedirect method
  621. *
  622. * @dataProvider statusCodeProvider
  623. * @access public
  624. * @return void
  625. */
  626. public function testRedirectByCode($code, $msg) {
  627. $Controller = new Controller(null);
  628. $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
  629. $Controller->Components = $this->getMock('ComponentCollection');
  630. $Controller->response->expects($this->once())->method('statusCode')
  631. ->with($code);
  632. $Controller->response->expects($this->once())->method('header')
  633. ->with('Location', 'http://cakephp.org');
  634. $Controller->redirect('http://cakephp.org', (int)$code, false);
  635. $this->assertFalse($Controller->autoRender);
  636. }
  637. /**
  638. * test redirecting by message
  639. *
  640. * @dataProvider statusCodeProvider
  641. * @return void
  642. */
  643. public function testRedirectByMessage($code, $msg) {
  644. $Controller = new Controller(null);
  645. $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
  646. $Controller->Components = $this->getMock('ComponentCollection');
  647. $Controller->response->expects($this->once())->method('statusCode')
  648. ->with($code);
  649. $Controller->response->expects($this->once())->method('header')
  650. ->with('Location', 'http://cakephp.org');
  651. $Controller->redirect('http://cakephp.org', $msg, false);
  652. $this->assertFalse($Controller->autoRender);
  653. }
  654. /**
  655. * test that redirect triggers methods on the components.
  656. *
  657. * @return void
  658. */
  659. public function testRedirectTriggeringComponentsReturnNull() {
  660. $Controller = new Controller(null);
  661. $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
  662. $Controller->Components = $this->getMock('ComponentCollection');
  663. $Controller->Components->expects($this->once())->method('trigger')
  664. ->will($this->returnValue(null));
  665. $Controller->response->expects($this->once())->method('statusCode')
  666. ->with(301);
  667. $Controller->response->expects($this->once())->method('header')
  668. ->with('Location', 'http://cakephp.org');
  669. $Controller->redirect('http://cakephp.org', 301, false);
  670. }
  671. /**
  672. * test that beforeRedirect callback returnning null doesn't affect things.
  673. *
  674. * @return void
  675. */
  676. public function testRedirectBeforeRedirectModifyingParams() {
  677. $Controller = new Controller(null);
  678. $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
  679. $Controller->Components = $this->getMock('ComponentCollection');
  680. $Controller->Components->expects($this->once())->method('trigger')
  681. ->will($this->returnValue(array('http://book.cakephp.org')));
  682. $Controller->response->expects($this->once())->method('statusCode')
  683. ->with(301);
  684. $Controller->response->expects($this->once())->method('header')
  685. ->with('Location', 'http://book.cakephp.org');
  686. $Controller->redirect('http://cakephp.org', 301, false);
  687. }
  688. /**
  689. * test that beforeRedirect callback returnning null doesn't affect things.
  690. *
  691. * @return void
  692. */
  693. public function testRedirectBeforeRedirectModifyingParamsArrayReturn() {
  694. $Controller = $this->getMock('Controller', array('header', '_stop'));
  695. $Controller->response = $this->getMock('CakeResponse');
  696. $Controller->Components = $this->getMock('ComponentCollection');
  697. $return = array(
  698. array(
  699. 'url' => 'http://example.com/test/1',
  700. 'exit' => false,
  701. 'status' => 302
  702. ),
  703. array(
  704. 'url' => 'http://example.com/test/2',
  705. ),
  706. );
  707. $Controller->Components->expects($this->once())->method('trigger')
  708. ->will($this->returnValue($return));
  709. $Controller->response->expects($this->at(0))->method('header')
  710. ->with('Location', 'http://example.com/test/2');
  711. $Controller->response->expects($this->at(1))->method('statusCode')
  712. ->with(302);
  713. $Controller->expects($this->never())->method('_stop');
  714. $Controller->redirect('http://cakephp.org', 301);
  715. }
  716. /**
  717. * test that beforeRedirect callback returnning false in controller
  718. *
  719. * @return void
  720. */
  721. public function testRedirectBeforeRedirectInController() {
  722. $Controller = $this->getMock('Controller', array('_stop', 'beforeRedirect'));
  723. $Controller->response = $this->getMock('CakeResponse', array('header'));
  724. $Controller->Components = $this->getMock('ComponentCollection');
  725. $Controller->expects($this->once())->method('beforeRedirect')
  726. ->will($this->returnValue(false));
  727. $Controller->response->expects($this->never())->method('header');
  728. $Controller->expects($this->never())->method('_stop');
  729. $Controller->redirect('http://cakephp.org');
  730. }
  731. /**
  732. * testMergeVars method
  733. *
  734. * @access public
  735. * @return void
  736. */
  737. public function testMergeVars() {
  738. $request = new CakeRequest('controller_posts/index');
  739. $TestController = new TestController($request);
  740. $TestController->constructClasses();
  741. $testVars = get_class_vars('TestController');
  742. $appVars = get_class_vars('ControllerTestAppController');
  743. $components = is_array($appVars['components'])
  744. ? array_merge($appVars['components'], $testVars['components'])
  745. : $testVars['components'];
  746. if (!in_array('Session', $components)) {
  747. $components[] = 'Session';
  748. }
  749. $helpers = is_array($appVars['helpers'])
  750. ? array_merge($appVars['helpers'], $testVars['helpers'])
  751. : $testVars['helpers'];
  752. $uses = is_array($appVars['uses'])
  753. ? array_merge($appVars['uses'], $testVars['uses'])
  754. : $testVars['uses'];
  755. $this->assertEqual(count(array_diff_key($TestController->helpers, array_flip($helpers))), 0);
  756. $this->assertEqual(count(array_diff($TestController->uses, $uses)), 0);
  757. $this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->components), Set::normalize($components))), 0);
  758. $expected = array('ControllerComment', 'ControllerAlias', 'ControllerPost');
  759. $this->assertEquals($expected, $TestController->uses, '$uses was merged incorrectly, ControllerTestAppController models should be last.');
  760. $TestController = new AnotherTestController($request);
  761. $TestController->constructClasses();
  762. $appVars = get_class_vars('ControllerTestAppController');
  763. $testVars = get_class_vars('AnotherTestController');
  764. $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
  765. $this->assertNull($testVars['uses']);
  766. $this->assertFalse(property_exists($TestController, 'ControllerPost'));
  767. $TestController = new ControllerCommentsController($request);
  768. $TestController->constructClasses();
  769. $appVars = get_class_vars('ControllerTestAppController');
  770. $testVars = get_class_vars('ControllerCommentsController');
  771. $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
  772. $this->assertEqual(array('ControllerPost'), $testVars['uses']);
  773. $this->assertTrue(isset($TestController->ControllerPost));
  774. $this->assertTrue(isset($TestController->ControllerComment));
  775. }
  776. /**
  777. * test that options from child classes replace those in the parent classes.
  778. *
  779. * @access public
  780. * @return void
  781. */
  782. public function testChildComponentOptionsSupercedeParents() {
  783. $request = new CakeRequest('controller_posts/index');
  784. $TestController = new TestController($request);
  785. $expected = array('foo');
  786. $TestController->components = array('Cookie' => $expected);
  787. $TestController->constructClasses();
  788. $this->assertEqual($TestController->components['Cookie'], $expected);
  789. }
  790. /**
  791. * Ensure that __mergeVars is not being greedy and merging with
  792. * ControllerTestAppController when you make an instance of Controller
  793. *
  794. * @return void
  795. */
  796. public function testMergeVarsNotGreedy() {
  797. $request = new CakeRequest('controller_posts/index');
  798. $Controller = new Controller($request);
  799. $Controller->components = array();
  800. $Controller->uses = array();
  801. $Controller->constructClasses();
  802. $this->assertFalse(isset($Controller->Session));
  803. }
  804. /**
  805. * testReferer method
  806. *
  807. * @access public
  808. * @return void
  809. */
  810. public function testReferer() {
  811. $request = $this->getMock('CakeRequest');
  812. $request->expects($this->any())->method('referer')
  813. ->with(true)
  814. ->will($this->returnValue('/posts/index'));
  815. $Controller = new Controller($request);
  816. $result = $Controller->referer(null, true);
  817. $this->assertEqual($result, '/posts/index');
  818. $Controller = new Controller($request);
  819. $request->setReturnValue('referer', '/', array(true));
  820. $result = $Controller->referer(array('controller' => 'posts', 'action' => 'index'), true);
  821. $this->assertEqual($result, '/posts/index');
  822. $request = $this->getMock('CakeRequest');
  823. $request->expects($this->any())->method('referer')
  824. ->with(false)
  825. ->will($this->returnValue('http://localhost/posts/index'));
  826. $Controller = new Controller($request);
  827. $result = $Controller->referer();
  828. $this->assertEqual($result, 'http://localhost/posts/index');
  829. $Controller = new Controller(null);
  830. $result = $Controller->referer();
  831. $this->assertEqual($result, '/');
  832. }
  833. /**
  834. * testSetAction method
  835. *
  836. * @access public
  837. * @return void
  838. */
  839. public function testSetAction() {
  840. $request = new CakeRequest('controller_posts/index');
  841. $TestController = new TestController($request);
  842. $TestController->setAction('index', 1, 2);
  843. $expected = array('testId' => 1, 'test2Id' => 2);
  844. $this->assertidentical($TestController->data, $expected);
  845. }
  846. /**
  847. * testValidateErrors method
  848. *
  849. * @access public
  850. * @return void
  851. */
  852. public function testValidateErrors() {
  853. $request = new CakeRequest('controller_posts/index');
  854. $TestController = new TestController($request);
  855. $TestController->constructClasses();
  856. $this->assertFalse($TestController->validateErrors());
  857. $this->assertEqual($TestController->validate(), 0);
  858. $TestController->ControllerComment->invalidate('some_field', 'error_message');
  859. $TestController->ControllerComment->invalidate('some_field2', 'error_message2');
  860. $comment = new ControllerComment($request);
  861. $comment->set('someVar', 'data');
  862. $result = $TestController->validateErrors($comment);
  863. $expected = array('some_field' => array('error_message'), 'some_field2' => array('error_message2'));
  864. $this->assertIdentical($expected, $result);
  865. $this->assertEqual($TestController->validate($comment), 2);
  866. }
  867. /**
  868. * test that validateErrors works with any old model.
  869. *
  870. * @return void
  871. */
  872. public function testValidateErrorsOnArbitraryModels() {
  873. $TestController = new TestController();
  874. $Post = new ControllerPost();
  875. $Post->validate = array('title' => 'notEmpty');
  876. $Post->set('title', '');
  877. $result = $TestController->validateErrors($Post);
  878. $expected = array('title' => array('This field cannot be left blank'));
  879. $this->assertEqual($expected, $result);
  880. }
  881. /**
  882. * testPostConditions method
  883. *
  884. * @access public
  885. * @return void
  886. */
  887. public function testPostConditions() {
  888. $request = new CakeRequest('controller_posts/index');
  889. $Controller = new Controller($request);
  890. $data = array(
  891. 'Model1' => array('field1' => '23'),
  892. 'Model2' => array('field2' => 'string'),
  893. 'Model3' => array('field3' => '23'),
  894. );
  895. $expected = array(
  896. 'Model1.field1' => '23',
  897. 'Model2.field2' => 'string',
  898. 'Model3.field3' => '23',
  899. );
  900. $result = $Controller->postConditions($data);
  901. $this->assertIdentical($expected, $result);
  902. $data = array();
  903. $Controller->data = array(
  904. 'Model1' => array('field1' => '23'),
  905. 'Model2' => array('field2' => 'string'),
  906. 'Model3' => array('field3' => '23'),
  907. );
  908. $expected = array(
  909. 'Model1.field1' => '23',
  910. 'Model2.field2' => 'string',
  911. 'Model3.field3' => '23',
  912. );
  913. $result = $Controller->postConditions($data);
  914. $this->assertIdentical($expected, $result);
  915. $data = array();
  916. $Controller->data = array();
  917. $result = $Controller->postConditions($data);
  918. $this->assertNull($result);
  919. $data = array();
  920. $Controller->data = array(
  921. 'Model1' => array('field1' => '23'),
  922. 'Model2' => array('field2' => 'string'),
  923. 'Model3' => array('field3' => '23'),
  924. );
  925. $ops = array(
  926. 'Model1.field1' => '>',
  927. 'Model2.field2' => 'LIKE',
  928. 'Model3.field3' => '<=',
  929. );
  930. $expected = array(
  931. 'Model1.field1 >' => '23',
  932. 'Model2.field2 LIKE' => "%string%",
  933. 'Model3.field3 <=' => '23',
  934. );
  935. $result = $Controller->postConditions($data, $ops);
  936. $this->assertIdentical($expected, $result);
  937. }
  938. /**
  939. * testRequestHandlerPrefers method
  940. *
  941. * @access public
  942. * @return void
  943. */
  944. public function testRequestHandlerPrefers(){
  945. Configure::write('debug', 2);
  946. $request = new CakeRequest('controller_posts/index');
  947. $Controller = new Controller($request);
  948. $Controller->components = array("RequestHandler");
  949. $Controller->modelClass='ControllerPost';
  950. $Controller->params['url'] = array('ext' => 'rss');
  951. $Controller->constructClasses();
  952. $Controller->Components->trigger('initialize', array(&$Controller));
  953. $Controller->beforeFilter();
  954. $Controller->Components->trigger('startup', array(&$Controller));
  955. $this->assertEqual($Controller->RequestHandler->prefers(), 'rss');
  956. unset($Controller);
  957. }
  958. /**
  959. * testControllerHttpCodes method
  960. *
  961. * @access public
  962. * @return void
  963. */
  964. public function testControllerHttpCodes() {
  965. $Controller = new Controller(null);
  966. $Controller->response = $this->getMock('CakeResponse', array('httpCodes'));
  967. $Controller->response->expects($this->at(0))->method('httpCodes')->with(null);
  968. $Controller->response->expects($this->at(1))->method('httpCodes')->with(100);
  969. $Controller->httpCodes();
  970. $Controller->httpCodes(100);
  971. }
  972. /**
  973. * Tests that the startup process calls the correct functions
  974. *
  975. * @access public
  976. * @return void
  977. */
  978. public function testStartupProcess() {
  979. $Controller = $this->getMock('Controller', array('beforeFilter', 'afterFilter'));
  980. $Controller->components = array('MockStartup');
  981. $Controller->Components = $this->getMock('ComponentCollection');
  982. $Controller->expects($this->once())->method('beforeFilter');
  983. $Controller->Components->expects($this->at(0))->method('trigger')
  984. ->with('initialize', array(&$Controller));
  985. $Controller->Components->expects($this->at(1))->method('trigger')
  986. ->with('startup', array(&$Controller));
  987. $Controller->startupProcess();
  988. }
  989. /**
  990. * Tests that the shutdown process calls the correct functions
  991. *
  992. * @access public
  993. * @return void
  994. */
  995. public function testShutdownProcess() {
  996. $Controller = $this->getMock('Controller', array('beforeFilter', 'afterFilter'));
  997. $Controller->components = array('MockShutdown');
  998. $Controller->Components = $this->getMock('ComponentCollection');
  999. $Controller->expects($this->once())->method('afterFilter');
  1000. $Controller->Components->expects($this->once())->method('trigger')
  1001. ->with('shutdown', array(&$Controller));
  1002. $Controller->shutdownProcess();
  1003. }
  1004. /**
  1005. * test that BC works for attributes on the request object.
  1006. *
  1007. * @return void
  1008. */
  1009. public function testPropertyBackwardsCompatibility() {
  1010. $request = new CakeRequest('posts/index', null);
  1011. $request->addParams(array('controller' => 'posts', 'action' => 'index'));
  1012. $request->data = array('Post' => array('id' => 1));
  1013. $request->here = '/posts/index';
  1014. $request->webroot = '/';
  1015. $Controller = new TestController($request);
  1016. $this->assertEquals($request->data, $Controller->data);
  1017. $this->assertEquals($request->webroot, $Controller->webroot);
  1018. $this->assertEquals($request->here, $Controller->here);
  1019. $this->assertEquals($request->action, $Controller->action);
  1020. $this->assertFalse(empty($Controller->data));
  1021. $this->assertTrue(isset($Controller->data));
  1022. $this->assertTrue(empty($Controller->something));
  1023. $this->assertFalse(isset($Controller->something));
  1024. $this->assertEquals($request, $Controller->params);
  1025. $this->assertEquals($request->params['controller'], $Controller->params['controller']);
  1026. }
  1027. /**
  1028. * test that the BC wrapper doesn't interfere with models and components.
  1029. *
  1030. * @return void
  1031. */
  1032. public function testPropertyCompatibilityAndModelsComponents() {
  1033. $request = new CakeRequest('controller_posts/index');
  1034. $Controller = new TestController($request);
  1035. $Controller->constructClasses();
  1036. $this->assertInstanceOf('SecurityComponent', $Controller->Security);
  1037. $this->assertInstanceOf('ControllerComment', $Controller->ControllerComment);
  1038. }
  1039. /**
  1040. * test that using Controller::paginate() falls back to PaginatorComponent
  1041. *
  1042. * @return void
  1043. */
  1044. public function testPaginateBackwardsCompatibility() {
  1045. $request = new CakeRequest('controller_posts/index');
  1046. $request->params['pass'] = $request->params['named'] = array();
  1047. $Controller = new Controller($request);
  1048. $Controller->uses = array('ControllerPost', 'ControllerComment');
  1049. $Controller->passedArgs[] = '1';
  1050. $Controller->params['url'] = array();
  1051. $Controller->constructClasses();
  1052. $expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 100, 'paramType' => 'named');
  1053. $this->assertEqual($Controller->paginate, $expected);
  1054. $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  1055. $this->assertEqual($results, array(1, 2, 3));
  1056. $Controller->passedArgs = array();
  1057. $Controller->paginate = array('limit' => '-1');
  1058. $this->assertEqual($Controller->paginate, array('limit' => '-1'));
  1059. $Controller->paginate('ControllerPost');
  1060. $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1);
  1061. $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
  1062. $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
  1063. $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
  1064. }
  1065. }