ControllerTest.php 39 KB

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