ControllerTest.php 39 KB

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