PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Routing/DispatcherTest.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 1578 lines | 839 code | 230 blank | 509 comment | 12 complexity | f1781736745eb56d2f14246bc60a86ed MD5 | raw file
  1. <?php
  2. /**
  3. * DispatcherTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Routing
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Dispatcher', 'Routing');
  20. if (!class_exists('AppController', false)) {
  21. require_once CAKE . 'Controller' . DS . 'AppController.php';
  22. } elseif (!defined('APP_CONTROLLER_EXISTS')) {
  23. define('APP_CONTROLLER_EXISTS', true);
  24. }
  25. /**
  26. * A testing stub that doesn't send headers.
  27. *
  28. * @package Cake.Test.Case.Routing
  29. */
  30. class DispatcherMockCakeResponse extends CakeResponse {
  31. protected function _sendHeader($name, $value = null) {
  32. return $name . ' ' . $value;
  33. }
  34. }
  35. /**
  36. * TestDispatcher class
  37. *
  38. * @package Cake.Test.Case.Routing
  39. */
  40. class TestDispatcher extends Dispatcher {
  41. /**
  42. * invoke method
  43. *
  44. * @param mixed $controller
  45. * @param mixed $request
  46. * @return void
  47. */
  48. protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) {
  49. $result = parent::_invoke($controller, $request, $response);
  50. return $controller;
  51. }
  52. }
  53. /**
  54. * MyPluginAppController class
  55. *
  56. * @package Cake.Test.Case.Routing
  57. */
  58. class MyPluginAppController extends AppController {
  59. }
  60. abstract class DispatcherTestAbstractController extends Controller {
  61. abstract public function index();
  62. }
  63. interface DispatcherTestInterfaceController {
  64. public function index();
  65. }
  66. /**
  67. * MyPluginController class
  68. *
  69. * @package Cake.Test.Case.Routing
  70. */
  71. class MyPluginController extends MyPluginAppController {
  72. /**
  73. * name property
  74. *
  75. * @var string 'MyPlugin'
  76. */
  77. public $name = 'MyPlugin';
  78. /**
  79. * uses property
  80. *
  81. * @var array
  82. */
  83. public $uses = array();
  84. /**
  85. * index method
  86. *
  87. * @return void
  88. */
  89. public function index() {
  90. return true;
  91. }
  92. /**
  93. * add method
  94. *
  95. * @return void
  96. */
  97. public function add() {
  98. return true;
  99. }
  100. /**
  101. * admin_add method
  102. *
  103. * @param mixed $id
  104. * @return void
  105. */
  106. public function admin_add($id = null) {
  107. return $id;
  108. }
  109. }
  110. /**
  111. * SomePagesController class
  112. *
  113. * @package Cake.Test.Case.Routing
  114. */
  115. class SomePagesController extends AppController {
  116. /**
  117. * name property
  118. *
  119. * @var string 'SomePages'
  120. */
  121. public $name = 'SomePages';
  122. /**
  123. * uses property
  124. *
  125. * @var array
  126. */
  127. public $uses = array();
  128. /**
  129. * display method
  130. *
  131. * @param mixed $page
  132. * @return void
  133. */
  134. public function display($page = null) {
  135. return $page;
  136. }
  137. /**
  138. * index method
  139. *
  140. * @return void
  141. */
  142. public function index() {
  143. return true;
  144. }
  145. /**
  146. * Test method for returning responses.
  147. *
  148. * @return CakeResponse
  149. */
  150. public function responseGenerator() {
  151. return new CakeResponse(array('body' => 'new response'));
  152. }
  153. }
  154. /**
  155. * OtherPagesController class
  156. *
  157. * @package Cake.Test.Case.Routing
  158. */
  159. class OtherPagesController extends MyPluginAppController {
  160. /**
  161. * name property
  162. *
  163. * @var string 'OtherPages'
  164. */
  165. public $name = 'OtherPages';
  166. /**
  167. * uses property
  168. *
  169. * @var array
  170. */
  171. public $uses = array();
  172. /**
  173. * display method
  174. *
  175. * @param mixed $page
  176. * @return void
  177. */
  178. public function display($page = null) {
  179. return $page;
  180. }
  181. /**
  182. * index method
  183. *
  184. * @return void
  185. */
  186. public function index() {
  187. return true;
  188. }
  189. }
  190. /**
  191. * TestDispatchPagesController class
  192. *
  193. * @package Cake.Test.Case.Routing
  194. */
  195. class TestDispatchPagesController extends AppController {
  196. /**
  197. * name property
  198. *
  199. * @var string 'TestDispatchPages'
  200. */
  201. public $name = 'TestDispatchPages';
  202. /**
  203. * uses property
  204. *
  205. * @var array
  206. */
  207. public $uses = array();
  208. /**
  209. * admin_index method
  210. *
  211. * @return void
  212. */
  213. public function admin_index() {
  214. return true;
  215. }
  216. /**
  217. * camelCased method
  218. *
  219. * @return void
  220. */
  221. public function camelCased() {
  222. return true;
  223. }
  224. }
  225. /**
  226. * ArticlesTestAppController class
  227. *
  228. * @package Cake.Test.Case.Routing
  229. */
  230. class ArticlesTestAppController extends AppController {
  231. }
  232. /**
  233. * ArticlesTestController class
  234. *
  235. * @package Cake.Test.Case.Routing
  236. */
  237. class ArticlesTestController extends ArticlesTestAppController {
  238. /**
  239. * name property
  240. *
  241. * @var string 'ArticlesTest'
  242. */
  243. public $name = 'ArticlesTest';
  244. /**
  245. * uses property
  246. *
  247. * @var array
  248. */
  249. public $uses = array();
  250. /**
  251. * admin_index method
  252. *
  253. * @return void
  254. */
  255. public function admin_index() {
  256. return true;
  257. }
  258. /**
  259. * fake index method.
  260. *
  261. * @return void
  262. */
  263. public function index() {
  264. return true;
  265. }
  266. }
  267. /**
  268. * SomePostsController class
  269. *
  270. * @package Cake.Test.Case.Routing
  271. */
  272. class SomePostsController extends AppController {
  273. /**
  274. * name property
  275. *
  276. * @var string 'SomePosts'
  277. */
  278. public $name = 'SomePosts';
  279. /**
  280. * uses property
  281. *
  282. * @var array
  283. */
  284. public $uses = array();
  285. /**
  286. * autoRender property
  287. *
  288. * @var bool false
  289. */
  290. public $autoRender = false;
  291. /**
  292. * beforeFilter method
  293. *
  294. * @return void
  295. */
  296. public function beforeFilter() {
  297. if ($this->params['action'] == 'index') {
  298. $this->params['action'] = 'view';
  299. } else {
  300. $this->params['action'] = 'change';
  301. }
  302. $this->params['pass'] = array('changed');
  303. }
  304. /**
  305. * index method
  306. *
  307. * @return void
  308. */
  309. public function index() {
  310. return true;
  311. }
  312. /**
  313. * change method
  314. *
  315. * @return void
  316. */
  317. public function change() {
  318. return true;
  319. }
  320. }
  321. /**
  322. * TestCachedPagesController class
  323. *
  324. * @package Cake.Test.Case.Routing
  325. */
  326. class TestCachedPagesController extends Controller {
  327. /**
  328. * name property
  329. *
  330. * @var string 'TestCachedPages'
  331. */
  332. public $name = 'TestCachedPages';
  333. /**
  334. * uses property
  335. *
  336. * @var array
  337. */
  338. public $uses = array();
  339. /**
  340. * helpers property
  341. *
  342. * @var array
  343. */
  344. public $helpers = array('Cache', 'Html');
  345. /**
  346. * cacheAction property
  347. *
  348. * @var array
  349. */
  350. public $cacheAction = array(
  351. 'index' => '+2 sec',
  352. 'test_nocache_tags' => '+2 sec',
  353. 'view' => '+2 sec'
  354. );
  355. /**
  356. * Mock out the reponse object so it doesn't send headers.
  357. *
  358. * @var string
  359. */
  360. protected $_responseClass = 'DispatcherMockCakeResponse';
  361. /**
  362. * viewPath property
  363. *
  364. * @var string 'posts'
  365. */
  366. public $viewPath = 'Posts';
  367. /**
  368. * index method
  369. *
  370. * @return void
  371. */
  372. public function index() {
  373. $this->render();
  374. }
  375. /**
  376. * test_nocache_tags method
  377. *
  378. * @return void
  379. */
  380. public function test_nocache_tags() {
  381. $this->render();
  382. }
  383. /**
  384. * view method
  385. *
  386. * @return void
  387. */
  388. public function view($id = null) {
  389. $this->render('index');
  390. }
  391. /**
  392. * test cached forms / tests view object being registered
  393. *
  394. * @return void
  395. */
  396. public function cache_form() {
  397. $this->cacheAction = 10;
  398. $this->helpers[] = 'Form';
  399. }
  400. /**
  401. * Test cached views with themes.
  402. */
  403. public function themed() {
  404. $this->cacheAction = 10;
  405. $this->viewClass = 'Theme';
  406. $this->theme = 'TestTheme';
  407. }
  408. }
  409. /**
  410. * TimesheetsController class
  411. *
  412. * @package Cake.Test.Case.Routing
  413. */
  414. class TimesheetsController extends Controller {
  415. /**
  416. * name property
  417. *
  418. * @var string 'Timesheets'
  419. */
  420. public $name = 'Timesheets';
  421. /**
  422. * uses property
  423. *
  424. * @var array
  425. */
  426. public $uses = array();
  427. /**
  428. * index method
  429. *
  430. * @return void
  431. */
  432. public function index() {
  433. return true;
  434. }
  435. }
  436. /**
  437. * DispatcherTest class
  438. *
  439. * @package Cake.Test.Case.Routing
  440. */
  441. class DispatcherTest extends CakeTestCase {
  442. /**
  443. * setUp method
  444. *
  445. * @return void
  446. */
  447. public function setUp() {
  448. $this->_get = $_GET;
  449. $_GET = array();
  450. $this->_post = $_POST;
  451. $this->_files = $_FILES;
  452. $this->_server = $_SERVER;
  453. $this->_app = Configure::read('App');
  454. Configure::write('App.base', false);
  455. Configure::write('App.baseUrl', false);
  456. Configure::write('App.dir', 'app');
  457. Configure::write('App.webroot', 'webroot');
  458. $this->_cache = Configure::read('Cache');
  459. Configure::write('Cache.disable', true);
  460. $this->_debug = Configure::read('debug');
  461. App::build();
  462. App::objects('plugin', null, false);
  463. }
  464. /**
  465. * tearDown method
  466. *
  467. * @return void
  468. */
  469. public function tearDown() {
  470. $_GET = $this->_get;
  471. $_POST = $this->_post;
  472. $_FILES = $this->_files;
  473. $_SERVER = $this->_server;
  474. App::build();
  475. CakePlugin::unload();
  476. Configure::write('App', $this->_app);
  477. Configure::write('Cache', $this->_cache);
  478. Configure::write('debug', $this->_debug);
  479. }
  480. /**
  481. * testParseParamsWithoutZerosAndEmptyPost method
  482. *
  483. * @return void
  484. */
  485. public function testParseParamsWithoutZerosAndEmptyPost() {
  486. $Dispatcher = new Dispatcher();
  487. $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/params1/params2/params3"));
  488. $this->assertSame($test['controller'], 'testcontroller');
  489. $this->assertSame($test['action'], 'testaction');
  490. $this->assertSame($test['pass'][0], 'params1');
  491. $this->assertSame($test['pass'][1], 'params2');
  492. $this->assertSame($test['pass'][2], 'params3');
  493. $this->assertFalse(!empty($test['form']));
  494. }
  495. /**
  496. * testParseParamsReturnsPostedData method
  497. *
  498. * @return void
  499. */
  500. public function testParseParamsReturnsPostedData() {
  501. $_POST['testdata'] = "My Posted Content";
  502. $Dispatcher = new Dispatcher();
  503. $test = $Dispatcher->parseParams(new CakeRequest("/"));
  504. $this->assertEquals($test['data']['testdata'], "My Posted Content");
  505. }
  506. /**
  507. * testParseParamsWithSingleZero method
  508. *
  509. * @return void
  510. */
  511. public function testParseParamsWithSingleZero() {
  512. $Dispatcher = new Dispatcher();
  513. $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/1/0/23"));
  514. $this->assertSame($test['controller'], 'testcontroller');
  515. $this->assertSame($test['action'], 'testaction');
  516. $this->assertSame($test['pass'][0], '1');
  517. $this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][1]);
  518. $this->assertSame($test['pass'][2], '23');
  519. }
  520. /**
  521. * testParseParamsWithManySingleZeros method
  522. *
  523. * @return void
  524. */
  525. public function testParseParamsWithManySingleZeros() {
  526. $Dispatcher = new Dispatcher();
  527. $test = $Dispatcher->parseParams(new CakeRequest("/testcontroller/testaction/0/0/0/0/0/0"));
  528. $this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][0]);
  529. $this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][1]);
  530. $this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][2]);
  531. $this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][3]);
  532. $this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][4]);
  533. $this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][5]);
  534. }
  535. /**
  536. * testParseParamsWithManyZerosInEachSectionOfUrl method
  537. *
  538. * @return void
  539. */
  540. public function testParseParamsWithManyZerosInEachSectionOfUrl() {
  541. $Dispatcher = new Dispatcher();
  542. $request = new CakeRequest("/testcontroller/testaction/000/0000/00000/000000/000000/0000000");
  543. $test = $Dispatcher->parseParams($request);
  544. $this->assertRegExp('/\\A(?:000)\\z/', $test['pass'][0]);
  545. $this->assertRegExp('/\\A(?:0000)\\z/', $test['pass'][1]);
  546. $this->assertRegExp('/\\A(?:00000)\\z/', $test['pass'][2]);
  547. $this->assertRegExp('/\\A(?:000000)\\z/', $test['pass'][3]);
  548. $this->assertRegExp('/\\A(?:000000)\\z/', $test['pass'][4]);
  549. $this->assertRegExp('/\\A(?:0000000)\\z/', $test['pass'][5]);
  550. }
  551. /**
  552. * testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl method
  553. *
  554. * @return void
  555. */
  556. public function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() {
  557. $Dispatcher = new Dispatcher();
  558. $request = new CakeRequest("/testcontroller/testaction/01/0403/04010/000002/000030/0000400");
  559. $test = $Dispatcher->parseParams($request);
  560. $this->assertRegExp('/\\A(?:01)\\z/', $test['pass'][0]);
  561. $this->assertRegExp('/\\A(?:0403)\\z/', $test['pass'][1]);
  562. $this->assertRegExp('/\\A(?:04010)\\z/', $test['pass'][2]);
  563. $this->assertRegExp('/\\A(?:000002)\\z/', $test['pass'][3]);
  564. $this->assertRegExp('/\\A(?:000030)\\z/', $test['pass'][4]);
  565. $this->assertRegExp('/\\A(?:0000400)\\z/', $test['pass'][5]);
  566. }
  567. /**
  568. * testQueryStringOnRoot method
  569. *
  570. * @return void
  571. */
  572. public function testQueryStringOnRoot() {
  573. Router::reload();
  574. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  575. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  576. Router::connect('/:controller/:action/*');
  577. $_GET = array('coffee' => 'life', 'sleep' => 'sissies');
  578. $Dispatcher = new Dispatcher();
  579. $uri = new CakeRequest('posts/home/?coffee=life&sleep=sissies');
  580. $result = $Dispatcher->parseParams($uri);
  581. $this->assertRegExp('/posts/', $result['controller']);
  582. $this->assertRegExp('/home/', $result['action']);
  583. $this->assertTrue(isset($result['url']['sleep']));
  584. $this->assertTrue(isset($result['url']['coffee']));
  585. $Dispatcher = new Dispatcher();
  586. $uri = new CakeRequest('/?coffee=life&sleep=sissy');
  587. $result = $Dispatcher->parseParams($uri);
  588. $this->assertRegExp('/pages/', $result['controller']);
  589. $this->assertRegExp('/display/', $result['action']);
  590. $this->assertTrue(isset($result['url']['sleep']));
  591. $this->assertTrue(isset($result['url']['coffee']));
  592. $this->assertEquals($result['url']['coffee'], 'life');
  593. }
  594. /**
  595. * testMissingController method
  596. *
  597. * @expectedException MissingControllerException
  598. * @expectedExceptionMessage Controller class SomeControllerController could not be found.
  599. * @return void
  600. */
  601. public function testMissingController() {
  602. Router::connect('/:controller/:action/*');
  603. $Dispatcher = new TestDispatcher();
  604. Configure::write('App.baseUrl', '/index.php');
  605. $url = new CakeRequest('some_controller/home/param:value/param2:value2');
  606. $response = $this->getMock('CakeResponse');
  607. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  608. }
  609. /**
  610. * testMissingControllerInterface method
  611. *
  612. * @expectedException MissingControllerException
  613. * @expectedExceptionMessage Controller class DispatcherTestInterfaceController could not be found.
  614. * @return void
  615. */
  616. public function testMissingControllerInterface() {
  617. Router::connect('/:controller/:action/*');
  618. $Dispatcher = new TestDispatcher();
  619. Configure::write('App.baseUrl', '/index.php');
  620. $url = new CakeRequest('dispatcher_test_interface/index');
  621. $response = $this->getMock('CakeResponse');
  622. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  623. }
  624. /**
  625. * testMissingControllerInterface method
  626. *
  627. * @expectedException MissingControllerException
  628. * @expectedExceptionMessage Controller class DispatcherTestAbstractController could not be found.
  629. * @return void
  630. */
  631. public function testMissingControllerAbstract() {
  632. Router::connect('/:controller/:action/*');
  633. $Dispatcher = new TestDispatcher();
  634. Configure::write('App.baseUrl', '/index.php');
  635. $url = new CakeRequest('dispatcher_test_abstract/index');
  636. $response = $this->getMock('CakeResponse');
  637. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  638. }
  639. /**
  640. * testDispatch method
  641. *
  642. * @return void
  643. */
  644. public function testDispatchBasic() {
  645. App::build(array(
  646. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  647. ));
  648. $Dispatcher = new TestDispatcher();
  649. Configure::write('App.baseUrl', '/index.php');
  650. $url = new CakeRequest('pages/home/param:value/param2:value2');
  651. $response = $this->getMock('CakeResponse');
  652. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  653. $expected = 'Pages';
  654. $this->assertEquals($expected, $controller->name);
  655. $expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2');
  656. $this->assertSame($expected, $controller->passedArgs);
  657. Configure::write('App.baseUrl','/pages/index.php');
  658. $url = new CakeRequest('pages/home');
  659. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  660. $expected = 'Pages';
  661. $this->assertEquals($expected, $controller->name);
  662. $url = new CakeRequest('pages/home/');
  663. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  664. $this->assertNull($controller->plugin);
  665. $expected = 'Pages';
  666. $this->assertEquals($expected, $controller->name);
  667. unset($Dispatcher);
  668. require CAKE . 'Config' . DS . 'routes.php';
  669. $Dispatcher = new TestDispatcher();
  670. Configure::write('App.baseUrl', '/timesheets/index.php');
  671. $url = new CakeRequest('timesheets');
  672. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  673. $expected = 'Timesheets';
  674. $this->assertEquals($expected, $controller->name);
  675. $url = new CakeRequest('timesheets/');
  676. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  677. $this->assertEquals('Timesheets', $controller->name);
  678. $this->assertEquals('/timesheets/index.php', $url->base);
  679. $url = new CakeRequest('test_dispatch_pages/camelCased');
  680. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  681. $this->assertEquals('TestDispatchPages', $controller->name);
  682. $url = new CakeRequest('test_dispatch_pages/camelCased/something. .');
  683. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  684. $this->assertEquals($controller->params['pass'][0], 'something. .', 'Period was chopped off. %s');
  685. }
  686. /**
  687. * Test that Dispatcher handles actions that return response objects.
  688. *
  689. * @return void
  690. */
  691. public function testDispatchActionReturnsResponse() {
  692. Router::connect('/:controller/:action');
  693. $Dispatcher = new Dispatcher();
  694. $request = new CakeRequest('some_pages/responseGenerator');
  695. $response = $this->getMock('CakeResponse', array('_sendHeader'));
  696. ob_start();
  697. $Dispatcher->dispatch($request, $response);
  698. $result = ob_get_clean();
  699. $this->assertEquals('new response', $result);
  700. }
  701. /**
  702. * testAdminDispatch method
  703. *
  704. * @return void
  705. */
  706. public function testAdminDispatch() {
  707. $_POST = array();
  708. $Dispatcher = new TestDispatcher();
  709. Configure::write('Routing.prefixes', array('admin'));
  710. Configure::write('App.baseUrl','/cake/repo/branches/1.2.x.x/index.php');
  711. $url = new CakeRequest('admin/test_dispatch_pages/index/param:value/param2:value2');
  712. $response = $this->getMock('CakeResponse');
  713. Router::reload();
  714. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  715. $this->assertEquals($controller->name, 'TestDispatchPages');
  716. $this->assertSame($controller->passedArgs, array('param' => 'value', 'param2' => 'value2'));
  717. $this->assertTrue($controller->params['admin']);
  718. $expected = '/cake/repo/branches/1.2.x.x/index.php/admin/test_dispatch_pages/index/param:value/param2:value2';
  719. $this->assertSame($expected, $controller->here);
  720. $expected = '/cake/repo/branches/1.2.x.x/index.php';
  721. $this->assertSame($expected, $controller->base);
  722. }
  723. /**
  724. * testPluginDispatch method
  725. *
  726. * @return void
  727. */
  728. public function testPluginDispatch() {
  729. $_POST = array();
  730. Router::reload();
  731. $Dispatcher = new TestDispatcher();
  732. Router::connect(
  733. '/my_plugin/:controller/*',
  734. array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'display')
  735. );
  736. $url = new CakeRequest('my_plugin/some_pages/home/param:value/param2:value2');
  737. $response = $this->getMock('CakeResponse');
  738. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  739. $result = $Dispatcher->parseParams($url);
  740. $expected = array(
  741. 'pass' => array('home'),
  742. 'named' => array('param' => 'value', 'param2' => 'value2'), 'plugin' => 'my_plugin',
  743. 'controller' => 'some_pages', 'action' => 'display'
  744. );
  745. foreach ($expected as $key => $value) {
  746. $this->assertEquals($result[$key], $value, 'Value mismatch ' . $key . ' %');
  747. }
  748. $this->assertSame($controller->plugin, 'MyPlugin');
  749. $this->assertSame($controller->name, 'SomePages');
  750. $this->assertSame($controller->params['controller'], 'some_pages');
  751. $this->assertSame($controller->passedArgs, array('0' => 'home', 'param' => 'value', 'param2' => 'value2'));
  752. }
  753. /**
  754. * testAutomaticPluginDispatch method
  755. *
  756. * @return void
  757. */
  758. public function testAutomaticPluginDispatch() {
  759. $_POST = array();
  760. $_SERVER['SCRIPT_NAME'] = '/cake/repo/branches/1.2.x.x/index.php';
  761. Router::reload();
  762. $Dispatcher = new TestDispatcher();
  763. Router::connect(
  764. '/my_plugin/:controller/:action/*',
  765. array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'display')
  766. );
  767. $Dispatcher->base = false;
  768. $url = new CakeRequest('my_plugin/other_pages/index/param:value/param2:value2');
  769. $response = $this->getMock('CakeResponse');
  770. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  771. $this->assertSame($controller->plugin, 'MyPlugin');
  772. $this->assertSame($controller->name, 'OtherPages');
  773. $this->assertSame($controller->action, 'index');
  774. $this->assertSame($controller->passedArgs, array('param' => 'value', 'param2' => 'value2'));
  775. $expected = '/cake/repo/branches/1.2.x.x/my_plugin/other_pages/index/param:value/param2:value2';
  776. $this->assertSame($expected, $url->here);
  777. $expected = '/cake/repo/branches/1.2.x.x';
  778. $this->assertSame($expected, $url->base);
  779. }
  780. /**
  781. * testAutomaticPluginControllerDispatch method
  782. *
  783. * @return void
  784. */
  785. public function testAutomaticPluginControllerDispatch() {
  786. $plugins = App::objects('plugin');
  787. $plugins[] = 'MyPlugin';
  788. $plugins[] = 'ArticlesTest';
  789. CakePlugin::load('MyPlugin', array('path' => '/fake/path'));
  790. Router::reload();
  791. $Dispatcher = new TestDispatcher();
  792. $Dispatcher->base = false;
  793. $url = new CakeRequest('my_plugin/my_plugin/add/param:value/param2:value2');
  794. $response = $this->getMock('CakeResponse');
  795. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  796. $this->assertSame($controller->plugin, 'MyPlugin');
  797. $this->assertSame($controller->name, 'MyPlugin');
  798. $this->assertSame($controller->action, 'add');
  799. $this->assertEquals($controller->params['named'], array('param' => 'value', 'param2' => 'value2'));
  800. Router::reload();
  801. require CAKE . 'Config' . DS . 'routes.php';
  802. $Dispatcher = new TestDispatcher();
  803. $Dispatcher->base = false;
  804. // Simulates the Route for a real plugin, installed in APP/plugins
  805. Router::connect('/my_plugin/:controller/:action/*', array('plugin' => 'my_plugin'));
  806. $plugin = 'MyPlugin';
  807. $pluginUrl = Inflector::underscore($plugin);
  808. $url = new CakeRequest($pluginUrl);
  809. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  810. $this->assertSame($controller->plugin, 'MyPlugin');
  811. $this->assertSame($controller->name, 'MyPlugin');
  812. $this->assertSame($controller->action, 'index');
  813. $expected = $pluginUrl;
  814. $this->assertEquals($controller->params['controller'], $expected);
  815. Configure::write('Routing.prefixes', array('admin'));
  816. Router::reload();
  817. require CAKE . 'Config' . DS . 'routes.php';
  818. $Dispatcher = new TestDispatcher();
  819. $url = new CakeRequest('admin/my_plugin/my_plugin/add/5/param:value/param2:value2');
  820. $response = $this->getMock('CakeResponse');
  821. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  822. $this->assertEquals($controller->params['plugin'], 'my_plugin');
  823. $this->assertEquals($controller->params['controller'], 'my_plugin');
  824. $this->assertEquals($controller->params['action'], 'admin_add');
  825. $this->assertEquals($controller->params['pass'], array(5));
  826. $this->assertEquals($controller->params['named'], array('param' => 'value', 'param2' => 'value2'));
  827. $this->assertSame($controller->plugin, 'MyPlugin');
  828. $this->assertSame($controller->name, 'MyPlugin');
  829. $this->assertSame($controller->action, 'admin_add');
  830. $expected = array(0 => 5, 'param' => 'value', 'param2' => 'value2');
  831. $this->assertEquals($controller->passedArgs, $expected);
  832. Configure::write('Routing.prefixes', array('admin'));
  833. CakePlugin::load('ArticlesTest', array('path' => '/fake/path'));
  834. Router::reload();
  835. require CAKE . 'Config' . DS . 'routes.php';
  836. $Dispatcher = new TestDispatcher();
  837. $controller = $Dispatcher->dispatch(new CakeRequest('admin/articles_test'), $response, array('return' => 1));
  838. $this->assertSame($controller->plugin, 'ArticlesTest');
  839. $this->assertSame($controller->name, 'ArticlesTest');
  840. $this->assertSame($controller->action, 'admin_index');
  841. $expected = array(
  842. 'pass' => array(),
  843. 'named' => array(),
  844. 'controller' => 'articles_test',
  845. 'plugin' => 'articles_test',
  846. 'action' => 'admin_index',
  847. 'prefix' => 'admin',
  848. 'admin' => true,
  849. 'return' => 1
  850. );
  851. foreach ($expected as $key => $value) {
  852. $this->assertEquals($controller->request[$key], $expected[$key], 'Value mismatch ' . $key);
  853. }
  854. }
  855. /**
  856. * test Plugin dispatching without controller name and using
  857. * plugin short form instead.
  858. *
  859. * @return void
  860. */
  861. public function testAutomaticPluginDispatchWithShortAccess() {
  862. CakePlugin::load('MyPlugin', array('path' => '/fake/path'));
  863. Router::reload();
  864. $Dispatcher = new TestDispatcher();
  865. $Dispatcher->base = false;
  866. $url = new CakeRequest('my_plugin/');
  867. $response = $this->getMock('CakeResponse');
  868. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  869. $this->assertEquals($controller->params['controller'], 'my_plugin');
  870. $this->assertEquals($controller->params['plugin'], 'my_plugin');
  871. $this->assertEquals($controller->params['action'], 'index');
  872. $this->assertFalse(isset($controller->params['pass'][0]));
  873. }
  874. /**
  875. * test plugin shortcut urls with controllers that need to be loaded,
  876. * the above test uses a controller that has already been included.
  877. *
  878. * @return void
  879. */
  880. public function testPluginShortCutUrlsWithControllerThatNeedsToBeLoaded() {
  881. $loaded = class_exists('TestPluginController', false);
  882. $this->skipIf($loaded, 'TestPluginController already loaded.');
  883. Router::reload();
  884. App::build(array(
  885. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  886. ), true);
  887. CakePlugin::loadAll();
  888. $Dispatcher = new TestDispatcher();
  889. $Dispatcher->base = false;
  890. $url = new CakeRequest('test_plugin/');
  891. $response = $this->getMock('CakeResponse');
  892. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  893. $this->assertEquals($controller->params['controller'], 'test_plugin');
  894. $this->assertEquals($controller->params['plugin'], 'test_plugin');
  895. $this->assertEquals($controller->params['action'], 'index');
  896. $this->assertFalse(isset($controller->params['pass'][0]));
  897. $url = new CakeRequest('/test_plugin/tests/index');
  898. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  899. $this->assertEquals($controller->params['controller'], 'tests');
  900. $this->assertEquals($controller->params['plugin'], 'test_plugin');
  901. $this->assertEquals($controller->params['action'], 'index');
  902. $this->assertFalse(isset($controller->params['pass'][0]));
  903. $url = new CakeRequest('/test_plugin/tests/index/some_param');
  904. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  905. $this->assertEquals($controller->params['controller'], 'tests');
  906. $this->assertEquals($controller->params['plugin'], 'test_plugin');
  907. $this->assertEquals($controller->params['action'], 'index');
  908. $this->assertEquals($controller->params['pass'][0], 'some_param');
  909. App::build();
  910. }
  911. /**
  912. * testAutomaticPluginControllerMissingActionDispatch method
  913. *
  914. * @expectedException MissingActionException
  915. * @expectedExceptionMessage Action MyPluginController::not_here() could not be found.
  916. * @return void
  917. */
  918. public function testAutomaticPluginControllerMissingActionDispatch() {
  919. Router::reload();
  920. $Dispatcher = new TestDispatcher();
  921. $url = new CakeRequest('my_plugin/not_here/param:value/param2:value2');
  922. $response = $this->getMock('CakeResponse');
  923. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  924. }
  925. /**
  926. * testAutomaticPluginControllerMissingActionDispatch method
  927. *
  928. * @expectedException MissingActionException
  929. * @expectedExceptionMessage Action MyPluginController::param:value() could not be found.
  930. * @return void
  931. */
  932. public function testAutomaticPluginControllerIndexMissingAction() {
  933. Router::reload();
  934. $Dispatcher = new TestDispatcher();
  935. $url = new CakeRequest('my_plugin/param:value/param2:value2');
  936. $response = $this->getMock('CakeResponse');
  937. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  938. }
  939. /**
  940. * Test dispatching into the TestPlugin in the test_app
  941. *
  942. * @return void
  943. */
  944. public function testTestPluginDispatch() {
  945. $Dispatcher = new TestDispatcher();
  946. App::build(array(
  947. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  948. ), APP::RESET);
  949. CakePlugin::loadAll();
  950. Router::reload();
  951. Router::parse('/');
  952. $url = new CakeRequest('/test_plugin/tests/index');
  953. $response = $this->getMock('CakeResponse');
  954. $result = $Dispatcher->dispatch($url, $response, array('return' => 1));
  955. $this->assertTrue(class_exists('TestsController'));
  956. $this->assertTrue(class_exists('TestPluginAppController'));
  957. $this->assertTrue(class_exists('PluginsComponentComponent'));
  958. $this->assertEquals($result->params['controller'], 'tests');
  959. $this->assertEquals($result->params['plugin'], 'test_plugin');
  960. $this->assertEquals($result->params['action'], 'index');
  961. App::build();
  962. }
  963. /**
  964. * testChangingParamsFromBeforeFilter method
  965. *
  966. * @return void
  967. */
  968. public function testChangingParamsFromBeforeFilter() {
  969. $Dispatcher = new TestDispatcher();
  970. $response = $this->getMock('CakeResponse');
  971. $url = new CakeRequest('some_posts/index/param:value/param2:value2');
  972. try {
  973. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  974. $this->fail('No exception.');
  975. } catch (MissingActionException $e) {
  976. $this->assertEquals('Action SomePostsController::view() could not be found.', $e->getMessage());
  977. }
  978. $url = new CakeRequest('some_posts/something_else/param:value/param2:value2');
  979. $controller = $Dispatcher->dispatch($url, $response, array('return' => 1));
  980. $expected = 'SomePosts';
  981. $this->assertEquals($expected, $controller->name);
  982. $expected = 'change';
  983. $this->assertEquals($expected, $controller->action);
  984. $expected = array('changed');
  985. $this->assertSame($expected, $controller->params['pass']);
  986. }
  987. /**
  988. * testStaticAssets method
  989. *
  990. * @return void
  991. */
  992. public function testAssets() {
  993. Router::reload();
  994. App::build(array(
  995. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  996. 'vendors' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor'. DS),
  997. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  998. ));
  999. CakePlugin::loadAll();
  1000. $Dispatcher = new TestDispatcher();
  1001. $response = $this->getMock('CakeResponse', array('_sendHeader'));
  1002. try {
  1003. $Dispatcher->dispatch(new CakeRequest('theme/test_theme/../webroot/css/test_asset.css'), $response);
  1004. $this->fail('No exception');
  1005. } catch (MissingControllerException $e) {
  1006. $this->assertEquals('Controller class ThemeController could not be found.', $e->getMessage());
  1007. }
  1008. try {
  1009. $Dispatcher->dispatch(new CakeRequest('theme/test_theme/pdfs'), $response);
  1010. $this->fail('No exception');
  1011. } catch (MissingControllerException $e) {
  1012. $this->assertEquals('Controller class ThemeController could not be found.', $e->getMessage());
  1013. }
  1014. }
  1015. /**
  1016. * Data provider for asset()
  1017. *
  1018. * - theme assets.
  1019. * - plugin assets.
  1020. * - plugin assets in sub directories.
  1021. * - unknown plugin assets.
  1022. *
  1023. * @return array
  1024. */
  1025. public static function assetProvider() {
  1026. return array(
  1027. array(
  1028. 'theme/test_theme/flash/theme_test.swf',
  1029. 'View/Themed/TestTheme/webroot/flash/theme_test.swf'
  1030. ),
  1031. array(
  1032. 'theme/test_theme/pdfs/theme_test.pdf',
  1033. 'View/Themed/TestTheme/webroot/pdfs/theme_test.pdf'
  1034. ),
  1035. array(
  1036. 'theme/test_theme/img/test.jpg',
  1037. 'View/Themed/TestTheme/webroot/img/test.jpg'
  1038. ),
  1039. array(
  1040. 'theme/test_theme/css/test_asset.css',
  1041. 'View/Themed/TestTheme/webroot/css/test_asset.css'
  1042. ),
  1043. array(
  1044. 'theme/test_theme/js/theme.js',
  1045. 'View/Themed/TestTheme/webroot/js/theme.js'
  1046. ),
  1047. array(
  1048. 'theme/test_theme/js/one/theme_one.js',
  1049. 'View/Themed/TestTheme/webroot/js/one/theme_one.js'
  1050. ),
  1051. array(
  1052. 'theme/test_theme/space%20image.text',
  1053. 'View/Themed/TestTheme/webroot/space image.text'
  1054. ),
  1055. array(
  1056. 'test_plugin/root.js',
  1057. 'Plugin/TestPlugin/webroot/root.js'
  1058. ),
  1059. array(
  1060. 'test_plugin/flash/plugin_test.swf',
  1061. 'Plugin/TestPlugin/webroot/flash/plugin_test.swf'
  1062. ),
  1063. array(
  1064. 'test_plugin/pdfs/plugin_test.pdf',
  1065. 'Plugin/TestPlugin/webroot/pdfs/plugin_test.pdf'
  1066. ),
  1067. array(
  1068. 'test_plugin/js/test_plugin/test.js',
  1069. 'Plugin/TestPlugin/webroot/js/test_plugin/test.js'
  1070. ),
  1071. array(
  1072. 'test_plugin/css/test_plugin_asset.css',
  1073. 'Plugin/TestPlugin/webroot/css/test_plugin_asset.css'
  1074. ),
  1075. array(
  1076. 'test_plugin/img/cake.icon.gif',
  1077. 'Plugin/TestPlugin/webroot/img/cake.icon.gif'
  1078. ),
  1079. array(
  1080. 'plugin_js/js/plugin_js.js',
  1081. 'Plugin/PluginJs/webroot/js/plugin_js.js'
  1082. ),
  1083. array(
  1084. 'plugin_js/js/one/plugin_one.js',
  1085. 'Plugin/PluginJs/webroot/js/one/plugin_one.js'
  1086. ),
  1087. array(
  1088. 'test_plugin/css/unknown.extension',
  1089. 'Plugin/TestPlugin/webroot/css/unknown.extension'
  1090. ),
  1091. array(
  1092. 'test_plugin/css/theme_one.htc',
  1093. 'Plugin/TestPlugin/webroot/css/theme_one.htc'
  1094. ),
  1095. );
  1096. }
  1097. /**
  1098. * Test assets
  1099. *
  1100. * @dataProvider assetProvider
  1101. * @outputBuffering enabled
  1102. * @return void
  1103. */
  1104. public function testAsset($url, $file) {
  1105. Router::reload();
  1106. App::build(array(
  1107. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  1108. 'Vendor' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor'. DS),
  1109. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
  1110. ));
  1111. CakePlugin::loadAll();
  1112. $Dispatcher = new TestDispatcher();
  1113. $response = $this->getMock('CakeResponse', array('_sendHeader'));
  1114. $Dispatcher->dispatch(new CakeRequest($url), $response);
  1115. $result = ob_get_clean();
  1116. $path = CAKE. 'Test' . DS . 'test_app' . DS . str_replace('/', DS, $file);
  1117. $file = file_get_contents($path);
  1118. $this->assertEquals($file, $result);
  1119. $expected = filesize($path);
  1120. $headers = $response->header();
  1121. $this->assertEquals($expected, $headers['Content-Length']);
  1122. }
  1123. /**
  1124. * test that missing asset processors trigger a 404 with no response body.
  1125. *
  1126. * @return void
  1127. */
  1128. public function testMissingAssetProcessor404() {
  1129. $response = $this->getMock('CakeResponse', array('_sendHeader'));
  1130. $Dispatcher = new TestDispatcher();
  1131. Configure::write('Asset.filter', array(
  1132. 'js' => '',
  1133. 'css' => null
  1134. ));
  1135. ob_start();
  1136. $this->assertTrue($Dispatcher->asset('ccss/cake.generic.css', $response));
  1137. $result = ob_get_clean();
  1138. }
  1139. /**
  1140. * test that asset filters work for theme and plugin assets
  1141. *
  1142. * @return void
  1143. */
  1144. public function testAssetFilterForThemeAndPlugins() {
  1145. $Dispatcher = new TestDispatcher();
  1146. $response = $this->getMock('CakeResponse', array('_sendHeader'));
  1147. Configure::write('Asset.filter', array(
  1148. 'js' => '',
  1149. 'css' => ''
  1150. ));
  1151. $this->assertTrue($Dispatcher->asset('theme/test_theme/ccss/cake.generic.css', $response));
  1152. $this->assertTrue($Dispatcher->asset('theme/test_theme/cjs/debug_kit.js', $response));
  1153. $this->assertTrue($Dispatcher->asset('test_plugin/ccss/cake.generic.css', $response));
  1154. $this->assertTrue($Dispatcher->asset('test_plugin/cjs/debug_kit.js', $response));
  1155. $this->assertFalse($Dispatcher->asset('css/ccss/debug_kit.css', $response));
  1156. $this->assertFalse($Dispatcher->asset('js/cjs/debug_kit.js', $response));
  1157. }
  1158. /**
  1159. * Data provider for cached actions.
  1160. *
  1161. * - Test simple views
  1162. * - Test views with nocache tags
  1163. * - Test requests with named + passed params.
  1164. * - Test requests with query string params
  1165. * - Test themed views.
  1166. *
  1167. * @return array
  1168. */
  1169. public static function cacheActionProvider() {
  1170. return array(
  1171. array('/'),
  1172. array('test_cached_pages/index'),
  1173. array('TestCachedPages/index'),
  1174. array('test_cached_pages/test_nocache_tags'),
  1175. array('TestCachedPages/test_nocache_tags'),
  1176. array('test_cached_pages/view/param/param'),
  1177. array('test_cached_pages/view/foo:bar/value:goo'),
  1178. array('test_cached_pages/view?q=cakephp'),
  1179. array('test_cached_pages/themed'),
  1180. );
  1181. }
  1182. /**
  1183. * testFullPageCachingDispatch method
  1184. *
  1185. * @dataProvider cacheActionProvider
  1186. * @return void
  1187. */
  1188. public function testFullPageCachingDispatch($url) {
  1189. Configure::write('Cache.disable', false);
  1190. Configure::write('Cache.check', true);
  1191. Configure::write('debug', 2);
  1192. Router::reload();
  1193. Router::connect('/', array('controller' => 'test_cached_pages', 'action' => 'index'));
  1194. Router::connect('/:controller/:action/*');
  1195. App::build(array(
  1196. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
  1197. ), true);
  1198. $dispatcher = new TestDispatcher();
  1199. $request = new CakeRequest($url);
  1200. $response = new CakeResponse();
  1201. ob_start();
  1202. $dispatcher->dispatch($request, $response);
  1203. $out = ob_get_clean();
  1204. ob_start();
  1205. $dispatcher->cached($request->here());
  1206. $cached = ob_get_clean();
  1207. $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
  1208. $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
  1209. $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
  1210. $this->assertEquals($expected, $result);
  1211. $filename = $this->__cachePath($request->here());
  1212. unlink($filename);
  1213. }
  1214. /**
  1215. * testHttpMethodOverrides method
  1216. *
  1217. * @return void
  1218. */
  1219. public function testHttpMethodOverrides() {
  1220. Router::reload();
  1221. Router::mapResources('Posts');
  1222. $_SERVER['REQUEST_METHOD'] = 'POST';
  1223. $dispatcher = new Dispatcher();
  1224. $result = $dispatcher->parseParams(new CakeRequest('/posts'));
  1225. $expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST');
  1226. foreach ($expected as $key => $value) {
  1227. $this->assertEquals($result[$key], $value, 'Value mismatch for ' . $key . ' %s');
  1228. }
  1229. $_SERVER['REQUEST_METHOD'] = 'GET';
  1230. $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
  1231. $result = $dispatcher->parseParams(new CakeRequest('/posts/5'));
  1232. $expected = array(
  1233. 'pass' => array('5'),
  1234. 'named' => array(),
  1235. 'id' => '5',
  1236. 'plugin' => null,
  1237. 'controller' => 'posts',
  1238. 'action' => 'edit',
  1239. '[method]' => 'PUT'
  1240. );
  1241. foreach ($expected as $key => $value) {
  1242. $this->assertEquals($result[$key], $value, 'Value mismatch for ' . $key . ' %s');
  1243. }
  1244. unset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
  1245. $_SERVER['REQUEST_METHOD'] = 'GET';
  1246. $result = $dispatcher->parseParams(new CakeRequest('/posts/5'));
  1247. $expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '[method]' => 'GET');
  1248. foreach ($expected as $key => $value) {
  1249. $this->assertEquals($result[$key], $value, 'Value mismatch for ' . $key . ' %s');
  1250. }
  1251. $_POST['_method'] = 'PUT';
  1252. $result = $dispatcher->parseParams(new CakeRequest('/posts/5'));
  1253. $expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT');
  1254. foreach ($expected as $key => $value) {
  1255. $this->assertEquals($result[$key], $value, 'Value mismatch for ' . $key . ' %s');
  1256. }
  1257. $_POST['_method'] = 'POST';
  1258. $_POST['data'] = array('Post' => array('title' => 'New Post'));
  1259. $_POST['extra'] = 'data';
  1260. $_SERVER = array();
  1261. $result = $dispatcher->parseParams(new CakeRequest('/posts'));
  1262. $expected = array(
  1263. 'pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add',
  1264. '[method]' => 'POST', 'data' => array('extra' => 'data', 'Post' => array('title' => 'New Post')),
  1265. );
  1266. foreach ($expected as $key => $value) {
  1267. $this->assertEquals($result[$key], $value, 'Value mismatch for ' . $key . ' %s');
  1268. }
  1269. unset($_POST['_method']);
  1270. }
  1271. /**
  1272. * backupEnvironment method
  1273. *
  1274. * @return void
  1275. */
  1276. function __backupEnvironment() {
  1277. return array(
  1278. 'App' => Configure::read('App'),
  1279. 'GET' => $_GET,
  1280. 'POST' => $_POST,
  1281. 'SERVER' => $_SERVER
  1282. );
  1283. }
  1284. /**
  1285. * reloadEnvironment method
  1286. *
  1287. * @return void
  1288. */
  1289. function __reloadEnvironment() {
  1290. foreach ($_GET as $key => $val) {
  1291. unset($_GET[$key]);
  1292. }
  1293. foreach ($_POST as $key => $val) {
  1294. unset($_POST[$key]);
  1295. }
  1296. foreach ($_SERVER as $key => $val) {
  1297. unset($_SERVER[$key]);
  1298. }
  1299. Configure::write('App', array());
  1300. }
  1301. /**
  1302. * loadEnvironment method
  1303. *
  1304. * @param mixed $env
  1305. * @return void
  1306. */
  1307. function __loadEnvironment($env) {
  1308. if ($env['reload']) {
  1309. $this->__reloadEnvironment();
  1310. }
  1311. if (isset($env['App'])) {
  1312. Configure::write('App', $env['App']);
  1313. }
  1314. if (isset($env['GET'])) {
  1315. foreach ($env['GET'] as $key => $val) {
  1316. $_GET[$key] = $val;
  1317. }
  1318. }
  1319. if (isset($env['POST'])) {
  1320. foreach ($env['POST'] as $key => $val) {
  1321. $_POST[$key] = $val;
  1322. }
  1323. }
  1324. if (isset($env['SERVER'])) {
  1325. foreach ($env['SERVER'] as $key => $val) {
  1326. $_SERVER[$key] = $val;
  1327. }
  1328. }
  1329. }
  1330. /**
  1331. * cachePath method
  1332. *
  1333. * @param mixed $her
  1334. * @return string
  1335. */
  1336. function __cachePath($here) {
  1337. $path = $here;
  1338. if ($here == '/') {
  1339. $path = 'home';
  1340. }
  1341. $path = strtolower(Inflector::slug($path));
  1342. $filename = CACHE . 'views' . DS . $path . '.php';
  1343. if (!file_exists($filename)) {
  1344. $filename = CACHE . 'views' . DS . $path . '_index.php';
  1345. }
  1346. return $filename;
  1347. }
  1348. }