PageRenderTime 105ms CodeModel.GetById 31ms RepoModel.GetById 2ms app.codeStats 0ms

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

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