ControllerTest.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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 $model;
  312. public function __construct(ComponentCollection $collection, $settings) {
  313. $this->controller = $collection->getController();
  314. $this->model = $this->controller->modelClass;
  315. }
  316. public function beforeRender(Controller $controller) {
  317. return false;
  318. }
  319. }
  320. /**
  321. * AnotherTestController class
  322. *
  323. * @package Cake.Test.Case.Controller
  324. */
  325. class AnotherTestController extends ControllerTestAppController {
  326. /**
  327. * name property
  328. * @var string 'Name'
  329. */
  330. public $name = 'AnotherTest';
  331. /**
  332. * uses property
  333. *
  334. * @var array
  335. */
  336. public $uses = false;
  337. /**
  338. * merge parent
  339. *
  340. * @var string
  341. */
  342. protected $_mergeParent = 'ControllerTestAppController';
  343. }
  344. /**
  345. * ControllerTest class
  346. *
  347. * @package Cake.Test.Case.Controller
  348. */
  349. class ControllerTest extends CakeTestCase {
  350. /**
  351. * fixtures property
  352. *
  353. * @var array
  354. */
  355. public $fixtures = array(
  356. 'core.post',
  357. 'core.comment'
  358. );
  359. /**
  360. * reset environment.
  361. *
  362. * @return void
  363. */
  364. public function setUp() {
  365. parent::setUp();
  366. App::objects('plugin', null, false);
  367. App::build();
  368. Router::reload();
  369. }
  370. /**
  371. * tearDown
  372. *
  373. * @return void
  374. */
  375. public function tearDown() {
  376. parent::tearDown();
  377. CakePlugin::unload();
  378. }
  379. /**
  380. * testLoadModel method
  381. *
  382. * @return void
  383. */
  384. public function testLoadModel() {
  385. $request = new CakeRequest('controller_posts/index');
  386. $response = $this->getMock('CakeResponse');
  387. $Controller = new Controller($request, $response);
  388. $this->assertFalse(isset($Controller->ControllerPost));
  389. $result = $Controller->loadModel('ControllerPost');
  390. $this->assertTrue($result);
  391. $this->assertInstanceOf('ControllerPost', $Controller->ControllerPost);
  392. $this->assertContains('ControllerPost', $Controller->uses);
  393. }
  394. /**
  395. * Test loadModel() when uses = true.
  396. *
  397. * @return void
  398. */
  399. public function testLoadModelUsesTrue() {
  400. $request = new CakeRequest('controller_posts/index');
  401. $response = $this->getMock('CakeResponse');
  402. $Controller = new Controller($request, $response);
  403. $Controller->uses = true;
  404. $Controller->loadModel('ControllerPost');
  405. $this->assertInstanceOf('ControllerPost', $Controller->ControllerPost);
  406. $this->assertContains('ControllerPost', $Controller->uses);
  407. }
  408. /**
  409. * testLoadModel method from a plugin controller
  410. *
  411. * @return void
  412. */
  413. public function testLoadModelInPlugins() {
  414. App::build(array(
  415. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  416. 'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
  417. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS)
  418. ));
  419. CakePlugin::load('TestPlugin');
  420. App::uses('TestPluginAppController', 'TestPlugin.Controller');
  421. App::uses('TestPluginController', 'TestPlugin.Controller');
  422. $Controller = new TestPluginController();
  423. $Controller->plugin = 'TestPlugin';
  424. $Controller->uses = false;
  425. $this->assertFalse(isset($Controller->Comment));
  426. $result = $Controller->loadModel('Comment');
  427. $this->assertTrue($result);
  428. $this->assertInstanceOf('Comment', $Controller->Comment);
  429. $this->assertTrue(in_array('Comment', $Controller->uses));
  430. ClassRegistry::flush();
  431. unset($Controller);
  432. }
  433. /**
  434. * testConstructClasses method
  435. *
  436. * @return void
  437. */
  438. public function testConstructClasses() {
  439. $request = new CakeRequest('controller_posts/index');
  440. $Controller = new Controller($request);
  441. $Controller->uses = array('ControllerPost', 'ControllerComment');
  442. $Controller->constructClasses();
  443. $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
  444. $this->assertTrue(is_a($Controller->ControllerComment, 'ControllerComment'));
  445. $this->assertEquals('Comment', $Controller->ControllerComment->name);
  446. unset($Controller);
  447. App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)));
  448. CakePlugin::load('TestPlugin');
  449. $Controller = new Controller($request);
  450. $Controller->uses = array('TestPlugin.TestPluginPost');
  451. $Controller->constructClasses();
  452. $this->assertTrue(isset($Controller->TestPluginPost));
  453. $this->assertTrue(is_a($Controller->TestPluginPost, 'TestPluginPost'));
  454. }
  455. /**
  456. * testConstructClassesWithComponents method
  457. *
  458. * @return void
  459. */
  460. public function testConstructClassesWithComponents() {
  461. $Controller = new TestPluginController(new CakeRequest(), new CakeResponse());
  462. $Controller->uses = array('NameTest');
  463. $Controller->components[] = 'Test2';
  464. $Controller->constructClasses();
  465. $this->assertEquals('NameTest', $Controller->Test2->model);
  466. $this->assertEquals('Name', $Controller->NameTest->name);
  467. $this->assertEquals('Name', $Controller->NameTest->alias);
  468. }
  469. /**
  470. * testAliasName method
  471. *
  472. * @return void
  473. */
  474. public function testAliasName() {
  475. $request = new CakeRequest('controller_posts/index');
  476. $Controller = new Controller($request);
  477. $Controller->uses = array('NameTest');
  478. $Controller->constructClasses();
  479. $this->assertEquals('Name', $Controller->NameTest->name);
  480. $this->assertEquals('Name', $Controller->NameTest->alias);
  481. unset($Controller);
  482. }
  483. /**
  484. * testFlash method
  485. *
  486. * @return void
  487. */
  488. public function testFlash() {
  489. $request = new CakeRequest('controller_posts/index');
  490. $request->webroot = '/';
  491. $request->base = '/';
  492. $Controller = new Controller($request, $this->getMock('CakeResponse', array('_sendHeader')));
  493. $Controller->flash('this should work', '/flash');
  494. $result = $Controller->response->body();
  495. $expected = '<!DOCTYPE html>
  496. <html>
  497. <head>
  498. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  499. <title>this should work</title>
  500. <style><!--
  501. P { text-align:center; font:bold 1.1em sans-serif }
  502. A { color:#444; text-decoration:none }
  503. A:HOVER { text-decoration: underline; color:#44E }
  504. --></style>
  505. </head>
  506. <body>
  507. <p><a href="/flash">this should work</a></p>
  508. </body>
  509. </html>';
  510. $result = str_replace(array("\t", "\r\n", "\n"), "", $result);
  511. $expected = str_replace(array("\t", "\r\n", "\n"), "", $expected);
  512. $this->assertEquals($expected, $result);
  513. App::build(array(
  514. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  515. ));
  516. $Controller = new Controller($request);
  517. $Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  518. $Controller->flash('this should work', '/flash', 1, 'ajax2');
  519. $result = $Controller->response->body();
  520. $this->assertRegExp('/Ajax!/', $result);
  521. App::build();
  522. }
  523. /**
  524. * testControllerSet method
  525. *
  526. * @return void
  527. */
  528. public function testControllerSet() {
  529. $request = new CakeRequest('controller_posts/index');
  530. $Controller = new Controller($request);
  531. $Controller->set('variable_with_underscores', null);
  532. $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
  533. $Controller->viewVars = array();
  534. $viewVars = array('ModelName' => array('id' => 1, 'name' => 'value'));
  535. $Controller->set($viewVars);
  536. $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
  537. $Controller->viewVars = array();
  538. $Controller->set('variable_with_underscores', 'value');
  539. $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
  540. $Controller->viewVars = array();
  541. $viewVars = array('ModelName' => 'name');
  542. $Controller->set($viewVars);
  543. $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
  544. $Controller->set('title', 'someTitle');
  545. $this->assertSame($Controller->viewVars['title'], 'someTitle');
  546. $this->assertTrue(empty($Controller->pageTitle));
  547. $Controller->viewVars = array();
  548. $expected = array('ModelName' => 'name', 'ModelName2' => 'name2');
  549. $Controller->set(array('ModelName', 'ModelName2'), array('name', 'name2'));
  550. $this->assertSame($expected, $Controller->viewVars);
  551. $Controller->viewVars = array();
  552. $Controller->set(array(3 => 'three', 4 => 'four'));
  553. $Controller->set(array(1 => 'one', 2 => 'two'));
  554. $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
  555. $this->assertEquals($expected, $Controller->viewVars);
  556. }
  557. /**
  558. * testRender method
  559. *
  560. * @return void
  561. */
  562. public function testRender() {
  563. App::build(array(
  564. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  565. ), App::RESET);
  566. ClassRegistry::flush();
  567. $request = new CakeRequest('controller_posts/index');
  568. $request->params['action'] = 'index';
  569. $Controller = new Controller($request, new CakeResponse());
  570. $Controller->viewPath = 'Posts';
  571. $result = $Controller->render('index');
  572. $this->assertRegExp('/posts index/', (string)$result);
  573. $Controller->view = 'index';
  574. $result = $Controller->render();
  575. $this->assertRegExp('/posts index/', (string)$result);
  576. $result = $Controller->render('/Elements/test_element');
  577. $this->assertRegExp('/this is the test element/', (string)$result);
  578. $Controller->view = null;
  579. $Controller = new TestController($request, new CakeResponse());
  580. $Controller->uses = array('ControllerAlias', 'TestPlugin.ControllerComment', 'ControllerPost');
  581. $Controller->helpers = array('Html');
  582. $Controller->constructClasses();
  583. $Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
  584. $expected = $Controller->ControllerComment->validationErrors;
  585. $Controller->viewPath = 'Posts';
  586. $result = $Controller->render('index');
  587. $View = $Controller->View;
  588. $this->assertTrue(isset($View->validationErrors['ControllerComment']));
  589. $this->assertEquals($expected, $View->validationErrors['ControllerComment']);
  590. $expectedModels = array(
  591. 'ControllerAlias' => array('plugin' => null, 'className' => 'ControllerAlias'),
  592. 'ControllerComment' => array('plugin' => 'TestPlugin', 'className' => 'ControllerComment'),
  593. 'ControllerPost' => array('plugin' => null, 'className' => 'ControllerPost')
  594. );
  595. $this->assertEquals($expectedModels, $Controller->request->params['models']);
  596. ClassRegistry::flush();
  597. App::build();
  598. }
  599. /**
  600. * test that a component beforeRender can change the controller view class.
  601. *
  602. * @return void
  603. */
  604. public function testComponentBeforeRenderChangingViewClass() {
  605. App::build(array(
  606. 'View' => array(
  607. CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS
  608. )
  609. ), true);
  610. $Controller = new Controller($this->getMock('CakeRequest'), new CakeResponse());
  611. $Controller->uses = array();
  612. $Controller->components = array('Test');
  613. $Controller->constructClasses();
  614. $Controller->Test->viewclass = 'Theme';
  615. $Controller->viewPath = 'Posts';
  616. $Controller->theme = 'TestTheme';
  617. $result = $Controller->render('index');
  618. $this->assertRegExp('/default test_theme layout/', (string)$result);
  619. App::build();
  620. }
  621. /**
  622. * test that a component beforeRender can change the controller view class.
  623. *
  624. * @return void
  625. */
  626. public function testComponentCancelRender() {
  627. $Controller = new Controller($this->getMock('CakeRequest'), new CakeResponse());
  628. $Controller->uses = array();
  629. $Controller->components = array('Test2');
  630. $Controller->constructClasses();
  631. $result = $Controller->render('index');
  632. $this->assertInstanceOf('CakeResponse', $result);
  633. }
  634. /**
  635. * testToBeInheritedGuardmethods method
  636. *
  637. * @return void
  638. */
  639. public function testToBeInheritedGuardmethods() {
  640. $request = new CakeRequest('controller_posts/index');
  641. $Controller = new Controller($request, $this->getMock('CakeResponse'));
  642. $this->assertTrue($Controller->beforeScaffold(''));
  643. $this->assertTrue($Controller->afterScaffoldSave(''));
  644. $this->assertTrue($Controller->afterScaffoldSaveError(''));
  645. $this->assertFalse($Controller->scaffoldError(''));
  646. }
  647. /**
  648. * Generates status codes for redirect test.
  649. *
  650. * @return void
  651. */
  652. public static function statusCodeProvider() {
  653. return array(
  654. array(300, "Multiple Choices"),
  655. array(301, "Moved Permanently"),
  656. array(302, "Found"),
  657. array(303, "See Other"),
  658. array(304, "Not Modified"),
  659. array(305, "Use Proxy"),
  660. array(307, "Temporary Redirect"),
  661. array(403, "Forbidden"),
  662. );
  663. }
  664. /**
  665. * testRedirect method
  666. *
  667. * @dataProvider statusCodeProvider
  668. * @return void
  669. */
  670. public function testRedirectByCode($code, $msg) {
  671. $Controller = new Controller(null);
  672. $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
  673. $Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
  674. $Controller->response->expects($this->once())->method('statusCode')
  675. ->with($code);
  676. $Controller->response->expects($this->once())->method('header')
  677. ->with('Location', 'http://cakephp.org');
  678. $Controller->redirect('http://cakephp.org', (int)$code, false);
  679. $this->assertFalse($Controller->autoRender);
  680. }
  681. /**
  682. * test redirecting by message
  683. *
  684. * @dataProvider statusCodeProvider
  685. * @return void
  686. */
  687. public function testRedirectByMessage($code, $msg) {
  688. $Controller = new Controller(null);
  689. $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
  690. $Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
  691. $Controller->response->expects($this->once())->method('statusCode')
  692. ->with($code);
  693. $Controller->response->expects($this->once())->method('header')
  694. ->with('Location', 'http://cakephp.org');
  695. $Controller->redirect('http://cakephp.org', $msg, false);
  696. $this->assertFalse($Controller->autoRender);
  697. }
  698. /**
  699. * test that redirect triggers methods on the components.
  700. *
  701. * @return void
  702. */
  703. public function testRedirectTriggeringComponentsReturnNull() {
  704. $Controller = new Controller(null);
  705. $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
  706. $Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
  707. $Controller->Components->expects($this->once())->method('trigger')
  708. ->will($this->returnValue(null));
  709. $Controller->response->expects($this->once())->method('statusCode')
  710. ->with(301);
  711. $Controller->response->expects($this->once())->method('header')
  712. ->with('Location', 'http://cakephp.org');
  713. $Controller->redirect('http://cakephp.org', 301, false);
  714. }
  715. /**
  716. * test that beforeRedirect callback returning null doesn't affect things.
  717. *
  718. * @return void
  719. */
  720. public function testRedirectBeforeRedirectModifyingParams() {
  721. $Controller = new Controller(null);
  722. $Controller->response = $this->getMock('CakeResponse', array('header', 'statusCode'));
  723. $Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
  724. $Controller->Components->expects($this->once())->method('trigger')
  725. ->will($this->returnValue(array('http://book.cakephp.org')));
  726. $Controller->response->expects($this->once())->method('statusCode')
  727. ->with(301);
  728. $Controller->response->expects($this->once())->method('header')
  729. ->with('Location', 'http://book.cakephp.org');
  730. $Controller->redirect('http://cakephp.org', 301, false);
  731. }
  732. /**
  733. * test that beforeRedirect callback returning null doesn't affect things.
  734. *
  735. * @return void
  736. */
  737. public function testRedirectBeforeRedirectModifyingParamsArrayReturn() {
  738. $Controller = $this->getMock('Controller', array('header', '_stop'));
  739. $Controller->response = $this->getMock('CakeResponse');
  740. $Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
  741. $return = array(
  742. array(
  743. 'url' => 'http://example.com/test/1',
  744. 'exit' => false,
  745. 'status' => 302
  746. ),
  747. array(
  748. 'url' => 'http://example.com/test/2',
  749. ),
  750. );
  751. $Controller->Components->expects($this->once())->method('trigger')
  752. ->will($this->returnValue($return));
  753. $Controller->response->expects($this->once())->method('header')
  754. ->with('Location', 'http://example.com/test/2');
  755. $Controller->response->expects($this->at(1))->method('statusCode')
  756. ->with(302);
  757. $Controller->expects($this->never())->method('_stop');
  758. $Controller->redirect('http://cakephp.org', 301);
  759. }
  760. /**
  761. * test that beforeRedirect callback returning false in controller
  762. *
  763. * @return void
  764. */
  765. public function testRedirectBeforeRedirectInController() {
  766. $Controller = $this->getMock('Controller', array('_stop', 'beforeRedirect'));
  767. $Controller->response = $this->getMock('CakeResponse', array('header'));
  768. $Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
  769. $Controller->expects($this->once())->method('beforeRedirect')
  770. ->with('http://cakephp.org')
  771. ->will($this->returnValue(false));
  772. $Controller->response->expects($this->never())->method('header');
  773. $Controller->expects($this->never())->method('_stop');
  774. $Controller->redirect('http://cakephp.org');
  775. }
  776. /**
  777. * Test that beforeRedirect works with returning an array from the controller method.
  778. *
  779. * @return void
  780. */
  781. public function testRedirectBeforeRedirectInControllerWithArray() {
  782. $Controller = $this->getMock('Controller', array('_stop', 'beforeRedirect'));
  783. $Controller->response = $this->getMock('CakeResponse', array('header'));
  784. $Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
  785. $Controller->expects($this->once())
  786. ->method('beforeRedirect')
  787. ->with('http://cakephp.org', null, true)
  788. ->will($this->returnValue(array(
  789. 'url' => 'http://example.org',
  790. 'status' => 302,
  791. 'exit' => true
  792. )));
  793. $Controller->response->expects($this->at(0))
  794. ->method('header')
  795. ->with('Location', 'http://example.org');
  796. $Controller->expects($this->once())->method('_stop');
  797. $Controller->redirect('http://cakephp.org');
  798. }
  799. /**
  800. * testMergeVars method
  801. *
  802. * @return void
  803. */
  804. public function testMergeVars() {
  805. $request = new CakeRequest('controller_posts/index');
  806. $TestController = new TestController($request);
  807. $TestController->constructClasses();
  808. $testVars = get_class_vars('TestController');
  809. $appVars = get_class_vars('ControllerTestAppController');
  810. $components = is_array($appVars['components'])
  811. ? array_merge($appVars['components'], $testVars['components'])
  812. : $testVars['components'];
  813. if (!in_array('Session', $components)) {
  814. $components[] = 'Session';
  815. }
  816. $helpers = is_array($appVars['helpers'])
  817. ? array_merge($appVars['helpers'], $testVars['helpers'])
  818. : $testVars['helpers'];
  819. $uses = is_array($appVars['uses'])
  820. ? array_merge($appVars['uses'], $testVars['uses'])
  821. : $testVars['uses'];
  822. $this->assertEquals(0, count(array_diff_key($TestController->helpers, array_flip($helpers))));
  823. $this->assertEquals(0, count(array_diff($TestController->uses, $uses)));
  824. $this->assertEquals(count(array_diff_assoc(Hash::normalize($TestController->components), Hash::normalize($components))), 0);
  825. $expected = array('ControllerComment', 'ControllerAlias', 'ControllerPost');
  826. $this->assertEquals($expected, $TestController->uses, '$uses was merged incorrectly, ControllerTestAppController models should be last.');
  827. $TestController = new AnotherTestController($request);
  828. $TestController->constructClasses();
  829. $appVars = get_class_vars('ControllerTestAppController');
  830. $testVars = get_class_vars('AnotherTestController');
  831. $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
  832. $this->assertFalse($testVars['uses']);
  833. $this->assertFalse(property_exists($TestController, 'ControllerPost'));
  834. $TestController = new ControllerCommentsController($request);
  835. $TestController->constructClasses();
  836. $appVars = get_class_vars('ControllerTestAppController');
  837. $testVars = get_class_vars('ControllerCommentsController');
  838. $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
  839. $this->assertEquals(array('ControllerPost'), $testVars['uses']);
  840. $this->assertTrue(isset($TestController->ControllerPost));
  841. $this->assertTrue(isset($TestController->ControllerComment));
  842. }
  843. /**
  844. * test that options from child classes replace those in the parent classes.
  845. *
  846. * @return void
  847. */
  848. public function testChildComponentOptionsSupercedeParents() {
  849. $request = new CakeRequest('controller_posts/index');
  850. $TestController = new TestController($request);
  851. $expected = array('foo');
  852. $TestController->components = array('Cookie' => $expected);
  853. $TestController->constructClasses();
  854. $this->assertEquals($expected, $TestController->components['Cookie']);
  855. }
  856. /**
  857. * Ensure that _mergeControllerVars is not being greedy and merging with
  858. * ControllerTestAppController when you make an instance of Controller
  859. *
  860. * @return void
  861. */
  862. public function testMergeVarsNotGreedy() {
  863. $request = new CakeRequest('controller_posts/index');
  864. $Controller = new Controller($request);
  865. $Controller->components = array();
  866. $Controller->uses = array();
  867. $Controller->constructClasses();
  868. $this->assertFalse(isset($Controller->Session));
  869. }
  870. /**
  871. * testReferer method
  872. *
  873. * @return void
  874. */
  875. public function testReferer() {
  876. $request = $this->getMock('CakeRequest');
  877. $request->expects($this->any())->method('referer')
  878. ->with(true)
  879. ->will($this->returnValue('/posts/index'));
  880. $Controller = new Controller($request);
  881. $result = $Controller->referer(null, true);
  882. $this->assertEquals('/posts/index', $result);
  883. $Controller = new Controller($request);
  884. $request->setReturnValue('referer', '/', array(true));
  885. $result = $Controller->referer(array('controller' => 'posts', 'action' => 'index'), true);
  886. $this->assertEquals('/posts/index', $result);
  887. $request = $this->getMock('CakeRequest');
  888. $request->expects($this->any())->method('referer')
  889. ->with(false)
  890. ->will($this->returnValue('http://localhost/posts/index'));
  891. $Controller = new Controller($request);
  892. $result = $Controller->referer();
  893. $this->assertEquals('http://localhost/posts/index', $result);
  894. $Controller = new Controller(null);
  895. $result = $Controller->referer();
  896. $this->assertEquals('/', $result);
  897. }
  898. /**
  899. * testSetAction method
  900. *
  901. * @return void
  902. */
  903. public function testSetAction() {
  904. $request = new CakeRequest('controller_posts/index');
  905. $TestController = new TestController($request);
  906. $TestController->setAction('view', 1, 2);
  907. $expected = array('testId' => 1, 'test2Id' => 2);
  908. $this->assertSame($expected, $TestController->request->data);
  909. $this->assertSame('view', $TestController->request->params['action']);
  910. $this->assertSame('view', $TestController->view);
  911. }
  912. /**
  913. * testValidateErrors method
  914. *
  915. * @return void
  916. */
  917. public function testValidateErrors() {
  918. ClassRegistry::flush();
  919. $request = new CakeRequest('controller_posts/index');
  920. $TestController = new TestController($request);
  921. $TestController->constructClasses();
  922. $this->assertFalse($TestController->validateErrors());
  923. $this->assertEquals(0, $TestController->validate());
  924. $TestController->ControllerComment->invalidate('some_field', 'error_message');
  925. $TestController->ControllerComment->invalidate('some_field2', 'error_message2');
  926. $comment = new ControllerComment($request);
  927. $comment->set('someVar', 'data');
  928. $result = $TestController->validateErrors($comment);
  929. $expected = array('some_field' => array('error_message'), 'some_field2' => array('error_message2'));
  930. $this->assertSame($expected, $result);
  931. $this->assertEquals(2, $TestController->validate($comment));
  932. }
  933. /**
  934. * test that validateErrors works with any old model.
  935. *
  936. * @return void
  937. */
  938. public function testValidateErrorsOnArbitraryModels() {
  939. $TestController = new TestController();
  940. $Post = new ControllerPost();
  941. $Post->validate = array('title' => 'notEmpty');
  942. $Post->set('title', '');
  943. $result = $TestController->validateErrors($Post);
  944. $expected = array('title' => array('This field cannot be left blank'));
  945. $this->assertEquals($expected, $result);
  946. }
  947. /**
  948. * testPostConditions method
  949. *
  950. * @return void
  951. */
  952. public function testPostConditions() {
  953. $request = new CakeRequest('controller_posts/index');
  954. $Controller = new Controller($request);
  955. $data = array(
  956. 'Model1' => array('field1' => '23'),
  957. 'Model2' => array('field2' => 'string'),
  958. 'Model3' => array('field3' => '23'),
  959. );
  960. $expected = array(
  961. 'Model1.field1' => '23',
  962. 'Model2.field2' => 'string',
  963. 'Model3.field3' => '23',
  964. );
  965. $result = $Controller->postConditions($data);
  966. $this->assertSame($expected, $result);
  967. $data = array();
  968. $Controller->data = array(
  969. 'Model1' => array('field1' => '23'),
  970. 'Model2' => array('field2' => 'string'),
  971. 'Model3' => array('field3' => '23'),
  972. );
  973. $expected = array(
  974. 'Model1.field1' => '23',
  975. 'Model2.field2' => 'string',
  976. 'Model3.field3' => '23',
  977. );
  978. $result = $Controller->postConditions($data);
  979. $this->assertSame($expected, $result);
  980. $data = array();
  981. $Controller->data = array();
  982. $result = $Controller->postConditions($data);
  983. $this->assertNull($result);
  984. $data = array();
  985. $Controller->data = array(
  986. 'Model1' => array('field1' => '23'),
  987. 'Model2' => array('field2' => 'string'),
  988. 'Model3' => array('field3' => '23'),
  989. );
  990. $ops = array(
  991. 'Model1.field1' => '>',
  992. 'Model2.field2' => 'LIKE',
  993. 'Model3.field3' => '<=',
  994. );
  995. $expected = array(
  996. 'Model1.field1 >' => '23',
  997. 'Model2.field2 LIKE' => "%string%",
  998. 'Model3.field3 <=' => '23',
  999. );
  1000. $result = $Controller->postConditions($data, $ops);
  1001. $this->assertSame($expected, $result);
  1002. }
  1003. /**
  1004. * testControllerHttpCodes method
  1005. *
  1006. * @return void
  1007. */
  1008. public function testControllerHttpCodes() {
  1009. $response = $this->getMock('CakeResponse', array('httpCodes'));
  1010. $Controller = new Controller(null, $response);
  1011. $Controller->response->expects($this->at(0))->method('httpCodes')->with(null);
  1012. $Controller->response->expects($this->at(1))->method('httpCodes')->with(100);
  1013. $Controller->httpCodes();
  1014. $Controller->httpCodes(100);
  1015. }
  1016. /**
  1017. * Tests that the startup process calls the correct functions
  1018. *
  1019. * @return void
  1020. */
  1021. public function testStartupProcess() {
  1022. $Controller = $this->getMock('Controller', array('getEventManager'));
  1023. $eventManager = $this->getMock('CakeEventManager');
  1024. $eventManager->expects($this->at(0))->method('dispatch')
  1025. ->with(
  1026. $this->logicalAnd(
  1027. $this->isInstanceOf('CakeEvent'),
  1028. $this->attributeEqualTo('_name', 'Controller.initialize'),
  1029. $this->attributeEqualTo('_subject', $Controller)
  1030. )
  1031. );
  1032. $eventManager->expects($this->at(1))->method('dispatch')
  1033. ->with(
  1034. $this->logicalAnd(
  1035. $this->isInstanceOf('CakeEvent'),
  1036. $this->attributeEqualTo('_name', 'Controller.startup'),
  1037. $this->attributeEqualTo('_subject', $Controller)
  1038. )
  1039. );
  1040. $Controller->expects($this->exactly(2))->method('getEventManager')
  1041. ->will($this->returnValue($eventManager));
  1042. $Controller->startupProcess();
  1043. }
  1044. /**
  1045. * Tests that the shutdown process calls the correct functions
  1046. *
  1047. * @return void
  1048. */
  1049. public function testStartupProcessIndirect() {
  1050. $Controller = $this->getMock('Controller', array('beforeFilter'));
  1051. $Controller->components = array('MockShutdown');
  1052. $Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
  1053. $Controller->expects($this->once())->method('beforeFilter');
  1054. $Controller->Components->expects($this->exactly(2))->method('trigger')->with($this->isInstanceOf('CakeEvent'));
  1055. $Controller->startupProcess();
  1056. }
  1057. /**
  1058. * Tests that the shutdown process calls the correct functions
  1059. *
  1060. * @return void
  1061. */
  1062. public function testShutdownProcess() {
  1063. $Controller = $this->getMock('Controller', array('getEventManager'));
  1064. $eventManager = $this->getMock('CakeEventManager');
  1065. $eventManager->expects($this->once())->method('dispatch')
  1066. ->with(
  1067. $this->logicalAnd(
  1068. $this->isInstanceOf('CakeEvent'),
  1069. $this->attributeEqualTo('_name', 'Controller.shutdown'),
  1070. $this->attributeEqualTo('_subject', $Controller)
  1071. )
  1072. );
  1073. $Controller->expects($this->once())->method('getEventManager')
  1074. ->will($this->returnValue($eventManager));
  1075. $Controller->shutdownProcess();
  1076. }
  1077. /**
  1078. * Tests that the shutdown process calls the correct functions
  1079. *
  1080. * @return void
  1081. */
  1082. public function testShutdownProcessIndirect() {
  1083. $Controller = $this->getMock('Controller', array('afterFilter'));
  1084. $Controller->components = array('MockShutdown');
  1085. $Controller->Components = $this->getMock('ComponentCollection', array('trigger'));
  1086. $Controller->expects($this->once())->method('afterFilter');
  1087. $Controller->Components->expects($this->exactly(1))->method('trigger')->with($this->isInstanceOf('CakeEvent'));
  1088. $Controller->shutdownProcess();
  1089. }
  1090. /**
  1091. * test that BC works for attributes on the request object.
  1092. *
  1093. * @return void
  1094. */
  1095. public function testPropertyBackwardsCompatibility() {
  1096. $request = new CakeRequest('posts/index', false);
  1097. $request->addParams(array('controller' => 'posts', 'action' => 'index'));
  1098. $request->data = array('Post' => array('id' => 1));
  1099. $request->here = '/posts/index';
  1100. $request->webroot = '/';
  1101. $Controller = new TestController($request);
  1102. $this->assertEquals($request->data, $Controller->data);
  1103. $this->assertEquals($request->webroot, $Controller->webroot);
  1104. $this->assertEquals($request->here, $Controller->here);
  1105. $this->assertEquals($request->action, $Controller->action);
  1106. $this->assertFalse(empty($Controller->data));
  1107. $this->assertTrue(isset($Controller->data));
  1108. $this->assertTrue(empty($Controller->something));
  1109. $this->assertFalse(isset($Controller->something));
  1110. $this->assertEquals($request, $Controller->params);
  1111. $this->assertEquals($request->params['controller'], $Controller->params['controller']);
  1112. }
  1113. /**
  1114. * test that the BC wrapper doesn't interfere with models and components.
  1115. *
  1116. * @return void
  1117. */
  1118. public function testPropertyCompatibilityAndModelsComponents() {
  1119. $request = new CakeRequest('controller_posts/index');
  1120. $Controller = new TestController($request);
  1121. $Controller->constructClasses();
  1122. $this->assertInstanceOf('SecurityComponent', $Controller->Security);
  1123. $this->assertInstanceOf('ControllerComment', $Controller->ControllerComment);
  1124. }
  1125. /**
  1126. * test that using Controller::paginate() falls back to PaginatorComponent
  1127. *
  1128. * @return void
  1129. */
  1130. public function testPaginateBackwardsCompatibility() {
  1131. $request = new CakeRequest('controller_posts/index');
  1132. $request->params['pass'] = $request->params['named'] = array();
  1133. $response = $this->getMock('CakeResponse', array('httpCodes'));
  1134. $Controller = new Controller($request, $response);
  1135. $Controller->uses = array('ControllerPost', 'ControllerComment');
  1136. $Controller->passedArgs[] = '1';
  1137. $Controller->params['url'] = array();
  1138. $Controller->constructClasses();
  1139. $expected = array('page' => 1, 'limit' => 20, 'maxLimit' => 100, 'paramType' => 'named');
  1140. $this->assertEquals($expected, $Controller->paginate);
  1141. $results = Hash::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
  1142. $this->assertEquals(array(1, 2, 3), $results);
  1143. $Controller->passedArgs = array();
  1144. $Controller->paginate = array('limit' => '1');
  1145. $this->assertEquals(array('limit' => '1'), $Controller->paginate);
  1146. $Controller->paginate('ControllerPost');
  1147. $this->assertSame($Controller->params['paging']['ControllerPost']['page'], 1);
  1148. $this->assertSame($Controller->params['paging']['ControllerPost']['pageCount'], 3);
  1149. $this->assertSame($Controller->params['paging']['ControllerPost']['prevPage'], false);
  1150. $this->assertSame($Controller->params['paging']['ControllerPost']['nextPage'], true);
  1151. }
  1152. /**
  1153. * testMissingAction method
  1154. *
  1155. * @expectedException MissingActionException
  1156. * @expectedExceptionMessage Action TestController::missing() could not be found.
  1157. * @return void
  1158. */
  1159. public function testInvokeActionMissingAction() {
  1160. $url = new CakeRequest('test/missing');
  1161. $url->addParams(array('controller' => 'test_controller', 'action' => 'missing'));
  1162. $response = $this->getMock('CakeResponse');
  1163. $Controller = new TestController($url, $response);
  1164. $Controller->invokeAction($url);
  1165. }
  1166. /**
  1167. * test invoking private methods.
  1168. *
  1169. * @expectedException PrivateActionException
  1170. * @expectedExceptionMessage Private Action TestController::private_m() is not directly accessible.
  1171. * @return void
  1172. */
  1173. public function testInvokeActionPrivate() {
  1174. $url = new CakeRequest('test/private_m/');
  1175. $url->addParams(array('controller' => 'test_controller', 'action' => 'private_m'));
  1176. $response = $this->getMock('CakeResponse');
  1177. $Controller = new TestController($url, $response);
  1178. $Controller->invokeAction($url);
  1179. }
  1180. /**
  1181. * test invoking protected methods.
  1182. *
  1183. * @expectedException PrivateActionException
  1184. * @expectedExceptionMessage Private Action TestController::protected_m() is not directly accessible.
  1185. * @return void
  1186. */
  1187. public function testInvokeActionProtected() {
  1188. $url = new CakeRequest('test/protected_m/');
  1189. $url->addParams(array('controller' => 'test_controller', 'action' => 'protected_m'));
  1190. $response = $this->getMock('CakeResponse');
  1191. $Controller = new TestController($url, $response);
  1192. $Controller->invokeAction($url);
  1193. }
  1194. /**
  1195. * test invoking hidden methods.
  1196. *
  1197. * @expectedException PrivateActionException
  1198. * @expectedExceptionMessage Private Action TestController::_hidden() is not directly accessible.
  1199. * @return void
  1200. */
  1201. public function testInvokeActionHidden() {
  1202. $url = new CakeRequest('test/_hidden/');
  1203. $url->addParams(array('controller' => 'test_controller', 'action' => '_hidden'));
  1204. $response = $this->getMock('CakeResponse');
  1205. $Controller = new TestController($url, $response);
  1206. $Controller->invokeAction($url);
  1207. }
  1208. /**
  1209. * test invoking controller methods.
  1210. *
  1211. * @expectedException PrivateActionException
  1212. * @expectedExceptionMessage Private Action TestController::redirect() is not directly accessible.
  1213. * @return void
  1214. */
  1215. public function testInvokeActionBaseMethods() {
  1216. $url = new CakeRequest('test/redirect/');
  1217. $url->addParams(array('controller' => 'test_controller', 'action' => 'redirect'));
  1218. $response = $this->getMock('CakeResponse');
  1219. $Controller = new TestController($url, $response);
  1220. $Controller->invokeAction($url);
  1221. }
  1222. /**
  1223. * test invoking controller methods.
  1224. *
  1225. * @expectedException PrivateActionException
  1226. * @expectedExceptionMessage Private Action TestController::admin_add() is not directly accessible.
  1227. * @return void
  1228. */
  1229. public function testInvokeActionPrefixProtection() {
  1230. Router::reload();
  1231. Router::connect('/admin/:controller/:action/*', array('prefix' => 'admin'));
  1232. $url = new CakeRequest('test/admin_add/');
  1233. $url->addParams(array('controller' => 'test_controller', 'action' => 'admin_add'));
  1234. $response = $this->getMock('CakeResponse');
  1235. $Controller = new TestController($url, $response);
  1236. $Controller->invokeAction($url);
  1237. }
  1238. /**
  1239. * test invoking controller methods.
  1240. *
  1241. * @return void
  1242. */
  1243. public function testInvokeActionReturnValue() {
  1244. $url = new CakeRequest('test/returner/');
  1245. $url->addParams(array(
  1246. 'controller' => 'test_controller',
  1247. 'action' => 'returner',
  1248. 'pass' => array()
  1249. ));
  1250. $response = $this->getMock('CakeResponse');
  1251. $Controller = new TestController($url, $response);
  1252. $result = $Controller->invokeAction($url);
  1253. $this->assertEquals('I am from the controller.', $result);
  1254. }
  1255. }