PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/dispatcher.test.php

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