PageRenderTime 63ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/sanjeevam/beug
PHP | 2246 lines | 1361 code | 241 blank | 644 comment | 17 complexity | 8cad4d2a27a13be01659862479702d58 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * DispatcherTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  18. * @package cake
  19. * @subpackage cake.tests.cases
  20. * @since CakePHP(tm) v 1.2.0.4206
  21. * @version $Revision$
  22. * @modifiedby $LastChangedBy$
  23. * @lastmodified $Date$
  24. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  25. */
  26. require_once CAKE . 'dispatcher.php';
  27. if (!class_exists('AppController')) {
  28. require_once LIBS . 'controller' . DS . 'app_controller.php';
  29. } elseif (!defined('APP_CONTROLLER_EXISTS')){
  30. define('APP_CONTROLLER_EXISTS', true);
  31. }
  32. /**
  33. * TestDispatcher class
  34. *
  35. * @package cake
  36. * @subpackage cake.tests.cases
  37. */
  38. class TestDispatcher extends Dispatcher {
  39. /**
  40. * invoke method
  41. *
  42. * @param mixed $controller
  43. * @param mixed $params
  44. * @param mixed $missingAction
  45. * @access protected
  46. * @return void
  47. */
  48. function _invoke(&$controller, $params) {
  49. restore_error_handler();
  50. if ($result = parent::_invoke($controller, $params)) {
  51. if ($result[0] === 'missingAction') {
  52. return $result;
  53. }
  54. }
  55. set_error_handler('simpleTestErrorHandler');
  56. return $controller;
  57. }
  58. /**
  59. * cakeError method
  60. *
  61. * @param mixed $filename
  62. * @access public
  63. * @return void
  64. */
  65. function cakeError($filename, $params) {
  66. return array($filename, $params);
  67. }
  68. /**
  69. * _stop method
  70. *
  71. * @access protected
  72. * @return void
  73. */
  74. function _stop() {
  75. return true;
  76. }
  77. }
  78. /**
  79. * MyPluginAppController class
  80. *
  81. * @package cake
  82. * @subpackage cake.tests.cases
  83. */
  84. class MyPluginAppController extends AppController {
  85. }
  86. /**
  87. * MyPluginController class
  88. *
  89. * @package cake
  90. * @subpackage cake.tests.cases
  91. */
  92. class MyPluginController extends MyPluginAppController {
  93. /**
  94. * name property
  95. *
  96. * @var string 'MyPlugin'
  97. * @access public
  98. */
  99. var $name = 'MyPlugin';
  100. /**
  101. * uses property
  102. *
  103. * @var array
  104. * @access public
  105. */
  106. var $uses = array();
  107. /**
  108. * index method
  109. *
  110. * @access public
  111. * @return void
  112. */
  113. function index() {
  114. return true;
  115. }
  116. /**
  117. * add method
  118. *
  119. * @access public
  120. * @return void
  121. */
  122. function add() {
  123. return true;
  124. }
  125. /**
  126. * admin_add method
  127. *
  128. * @param mixed $id
  129. * @access public
  130. * @return void
  131. */
  132. function admin_add($id = null) {
  133. return $id;
  134. }
  135. }
  136. /**
  137. * SomePagesController class
  138. *
  139. * @package cake
  140. * @subpackage cake.tests.cases
  141. */
  142. class SomePagesController extends AppController {
  143. /**
  144. * name property
  145. *
  146. * @var string 'SomePages'
  147. * @access public
  148. */
  149. var $name = 'SomePages';
  150. /**
  151. * uses property
  152. *
  153. * @var array
  154. * @access public
  155. */
  156. var $uses = array();
  157. /**
  158. * display method
  159. *
  160. * @param mixed $page
  161. * @access public
  162. * @return void
  163. */
  164. function display($page = null) {
  165. return $page;
  166. }
  167. /**
  168. * index method
  169. *
  170. * @access public
  171. * @return void
  172. */
  173. function index() {
  174. return true;
  175. }
  176. /**
  177. * protected method
  178. *
  179. * @access protected
  180. * @return void
  181. */
  182. function _protected() {
  183. return true;
  184. }
  185. /**
  186. * redirect method overriding
  187. *
  188. * @access public
  189. * @return void
  190. */
  191. function redirect() {
  192. echo 'this should not be accessible';
  193. }
  194. }
  195. /**
  196. * OtherPagesController class
  197. *
  198. * @package cake
  199. * @subpackage cake.tests.cases
  200. */
  201. class OtherPagesController extends MyPluginAppController {
  202. /**
  203. * name property
  204. *
  205. * @var string 'OtherPages'
  206. * @access public
  207. */
  208. var $name = 'OtherPages';
  209. /**
  210. * uses property
  211. *
  212. * @var array
  213. * @access public
  214. */
  215. var $uses = array();
  216. /**
  217. * display method
  218. *
  219. * @param mixed $page
  220. * @access public
  221. * @return void
  222. */
  223. function display($page = null) {
  224. return $page;
  225. }
  226. /**
  227. * index method
  228. *
  229. * @access public
  230. * @return void
  231. */
  232. function index() {
  233. return true;
  234. }
  235. }
  236. /**
  237. * TestDispatchPagesController class
  238. *
  239. * @package cake
  240. * @subpackage cake.tests.cases
  241. */
  242. class TestDispatchPagesController extends AppController {
  243. /**
  244. * name property
  245. *
  246. * @var string 'TestDispatchPages'
  247. * @access public
  248. */
  249. var $name = 'TestDispatchPages';
  250. /**
  251. * uses property
  252. *
  253. * @var array
  254. * @access public
  255. */
  256. var $uses = array();
  257. /**
  258. * admin_index method
  259. *
  260. * @access public
  261. * @return void
  262. */
  263. function admin_index() {
  264. return true;
  265. }
  266. /**
  267. * camelCased method
  268. *
  269. * @access public
  270. * @return void
  271. */
  272. function camelCased() {
  273. return true;
  274. }
  275. }
  276. /**
  277. * ArticlesTestAppController class
  278. *
  279. * @package cake
  280. * @subpackage cake.tests.cases
  281. */
  282. class ArticlesTestAppController extends AppController {
  283. }
  284. /**
  285. * ArticlesTestController class
  286. *
  287. * @package cake
  288. * @subpackage cake.tests.cases
  289. */
  290. class ArticlesTestController extends ArticlesTestAppController {
  291. /**
  292. * name property
  293. *
  294. * @var string 'ArticlesTest'
  295. * @access public
  296. */
  297. var $name = 'ArticlesTest';
  298. /**
  299. * uses property
  300. *
  301. * @var array
  302. * @access public
  303. */
  304. var $uses = array();
  305. /**
  306. * admin_index method
  307. *
  308. * @access public
  309. * @return void
  310. */
  311. function admin_index() {
  312. return true;
  313. }
  314. }
  315. /**
  316. * SomePostsController class
  317. *
  318. * @package cake
  319. * @subpackage cake.tests.cases
  320. */
  321. class SomePostsController extends AppController {
  322. /**
  323. * name property
  324. *
  325. * @var string 'SomePosts'
  326. * @access public
  327. */
  328. var $name = 'SomePosts';
  329. /**
  330. * uses property
  331. *
  332. * @var array
  333. * @access public
  334. */
  335. var $uses = array();
  336. /**
  337. * autoRender property
  338. *
  339. * @var bool false
  340. * @access public
  341. */
  342. var $autoRender = false;
  343. /**
  344. * beforeFilter method
  345. *
  346. * @access public
  347. * @return void
  348. */
  349. function beforeFilter() {
  350. if ($this->params['action'] == 'index') {
  351. $this->params['action'] = 'view';
  352. } else {
  353. $this->params['action'] = 'change';
  354. }
  355. $this->params['pass'] = array('changed');
  356. }
  357. /**
  358. * index method
  359. *
  360. * @access public
  361. * @return void
  362. */
  363. function index() {
  364. return true;
  365. }
  366. /**
  367. * change method
  368. *
  369. * @access public
  370. * @return void
  371. */
  372. function change() {
  373. return true;
  374. }
  375. }
  376. /**
  377. * TestCachedPagesController class
  378. *
  379. * @package cake
  380. * @subpackage cake.tests.cases
  381. */
  382. class TestCachedPagesController extends AppController {
  383. /**
  384. * name property
  385. *
  386. * @var string 'TestCachedPages'
  387. * @access public
  388. */
  389. var $name = 'TestCachedPages';
  390. /**
  391. * uses property
  392. *
  393. * @var array
  394. * @access public
  395. */
  396. var $uses = array();
  397. /**
  398. * helpers property
  399. *
  400. * @var array
  401. * @access public
  402. */
  403. var $helpers = array('Cache');
  404. /**
  405. * cacheAction property
  406. *
  407. * @var array
  408. * @access public
  409. */
  410. var $cacheAction = array(
  411. 'index'=> '+2 sec', 'test_nocache_tags'=>'+2 sec',
  412. 'view/' => '+2 sec'
  413. );
  414. /**
  415. * viewPath property
  416. *
  417. * @var string 'posts'
  418. * @access public
  419. */
  420. var $viewPath = 'posts';
  421. /**
  422. * index method
  423. *
  424. * @access public
  425. * @return void
  426. */
  427. function index() {
  428. $this->render();
  429. }
  430. /**
  431. * test_nocache_tags method
  432. *
  433. * @access public
  434. * @return void
  435. */
  436. function test_nocache_tags() {
  437. $this->render();
  438. }
  439. /**
  440. * view method
  441. *
  442. * @access public
  443. * @return void
  444. */
  445. function view($id = null) {
  446. $this->render('index');
  447. }
  448. /**
  449. * test cached forms / tests view object being registered
  450. *
  451. * @return void
  452. */
  453. function cache_form() {
  454. $this->cacheAction = 10;
  455. $this->helpers[] = 'Form';
  456. }
  457. }
  458. /**
  459. * TimesheetsController class
  460. *
  461. * @package cake
  462. * @subpackage cake.tests.cases
  463. */
  464. class TimesheetsController extends AppController {
  465. /**
  466. * name property
  467. *
  468. * @var string 'Timesheets'
  469. * @access public
  470. */
  471. var $name = 'Timesheets';
  472. /**
  473. * uses property
  474. *
  475. * @var array
  476. * @access public
  477. */
  478. var $uses = array();
  479. /**
  480. * index method
  481. *
  482. * @access public
  483. * @return void
  484. */
  485. function index() {
  486. return true;
  487. }
  488. }
  489. /**
  490. * DispatcherTest class
  491. *
  492. * @package cake
  493. * @subpackage cake.tests.cases
  494. */
  495. class DispatcherTest extends CakeTestCase {
  496. /**
  497. * setUp method
  498. *
  499. * @access public
  500. * @return void
  501. */
  502. function startTest() {
  503. $this->_get = $_GET;
  504. $_GET = array();
  505. $this->_post = $_POST;
  506. $this->_files = $_FILES;
  507. $this->_server = $_SERVER;
  508. $this->_app = Configure::read('App');
  509. Configure::write('App.base', false);
  510. Configure::write('App.baseUrl', false);
  511. Configure::write('App.dir', 'app');
  512. Configure::write('App.webroot', 'webroot');
  513. $this->_cache = Configure::read('Cache');
  514. Configure::write('Cache.disable', true);
  515. $this->_vendorPaths = Configure::read('vendorPaths');
  516. $this->_pluginPaths = Configure::read('pluginPaths');
  517. $this->_viewPaths = Configure::read('viewPaths');
  518. $this->_controllerPaths = Configure::read('controllerPaths');
  519. $this->_debug = Configure::read('debug');
  520. Configure::write('controllerPaths', Configure::corePaths('controller'));
  521. Configure::write('viewPaths', Configure::corePaths('view'));
  522. }
  523. /**
  524. * tearDown method
  525. *
  526. * @access public
  527. * @return void
  528. */
  529. function endTest() {
  530. $_GET = $this->_get;
  531. $_POST = $this->_post;
  532. $_FILES = $this->_files;
  533. $_SERVER = $this->_server;
  534. Configure::write('App', $this->_app);
  535. Configure::write('Cache', $this->_cache);
  536. Configure::write('vendorPaths', $this->_vendorPaths);
  537. Configure::write('pluginPaths', $this->_pluginPaths);
  538. Configure::write('viewPaths', $this->_viewPaths);
  539. Configure::write('controllerPaths', $this->_controllerPaths);
  540. Configure::write('debug', $this->_debug);
  541. }
  542. /**
  543. * testParseParamsWithoutZerosAndEmptyPost method
  544. *
  545. * @access public
  546. * @return void
  547. */
  548. function testParseParamsWithoutZerosAndEmptyPost() {
  549. $Dispatcher =& new Dispatcher();
  550. $test = $Dispatcher->parseParams("/testcontroller/testaction/params1/params2/params3");
  551. $this->assertIdentical($test['controller'], 'testcontroller');
  552. $this->assertIdentical($test['action'], 'testaction');
  553. $this->assertIdentical($test['pass'][0], 'params1');
  554. $this->assertIdentical($test['pass'][1], 'params2');
  555. $this->assertIdentical($test['pass'][2], 'params3');
  556. $this->assertFalse(!empty($test['form']));
  557. }
  558. /**
  559. * testParseParamsReturnsPostedData method
  560. *
  561. * @access public
  562. * @return void
  563. */
  564. function testParseParamsReturnsPostedData() {
  565. $_POST['testdata'] = "My Posted Content";
  566. $Dispatcher =& new Dispatcher();
  567. $test = $Dispatcher->parseParams("/");
  568. $this->assertTrue($test['form'], "Parsed URL not returning post data");
  569. $this->assertIdentical($test['form']['testdata'], "My Posted Content");
  570. }
  571. /**
  572. * testParseParamsWithSingleZero method
  573. *
  574. * @access public
  575. * @return void
  576. */
  577. function testParseParamsWithSingleZero() {
  578. $Dispatcher =& new Dispatcher();
  579. $test = $Dispatcher->parseParams("/testcontroller/testaction/1/0/23");
  580. $this->assertIdentical($test['controller'], 'testcontroller');
  581. $this->assertIdentical($test['action'], 'testaction');
  582. $this->assertIdentical($test['pass'][0], '1');
  583. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][1]);
  584. $this->assertIdentical($test['pass'][2], '23');
  585. }
  586. /**
  587. * testParseParamsWithManySingleZeros method
  588. *
  589. * @access public
  590. * @return void
  591. */
  592. function testParseParamsWithManySingleZeros() {
  593. $Dispatcher =& new Dispatcher();
  594. $test = $Dispatcher->parseParams("/testcontroller/testaction/0/0/0/0/0/0");
  595. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][0]);
  596. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][1]);
  597. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][2]);
  598. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][3]);
  599. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][4]);
  600. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][5]);
  601. }
  602. /**
  603. * testParseParamsWithManyZerosInEachSectionOfUrl method
  604. *
  605. * @access public
  606. * @return void
  607. */
  608. function testParseParamsWithManyZerosInEachSectionOfUrl() {
  609. $Dispatcher =& new Dispatcher();
  610. $test = $Dispatcher->parseParams("/testcontroller/testaction/000/0000/00000/000000/000000/0000000");
  611. $this->assertPattern('/\\A(?:000)\\z/', $test['pass'][0]);
  612. $this->assertPattern('/\\A(?:0000)\\z/', $test['pass'][1]);
  613. $this->assertPattern('/\\A(?:00000)\\z/', $test['pass'][2]);
  614. $this->assertPattern('/\\A(?:000000)\\z/', $test['pass'][3]);
  615. $this->assertPattern('/\\A(?:000000)\\z/', $test['pass'][4]);
  616. $this->assertPattern('/\\A(?:0000000)\\z/', $test['pass'][5]);
  617. }
  618. /**
  619. * testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl method
  620. *
  621. * @access public
  622. * @return void
  623. */
  624. function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() {
  625. $Dispatcher =& new Dispatcher();
  626. $test = $Dispatcher->parseParams("/testcontroller/testaction/01/0403/04010/000002/000030/0000400");
  627. $this->assertPattern('/\\A(?:01)\\z/', $test['pass'][0]);
  628. $this->assertPattern('/\\A(?:0403)\\z/', $test['pass'][1]);
  629. $this->assertPattern('/\\A(?:04010)\\z/', $test['pass'][2]);
  630. $this->assertPattern('/\\A(?:000002)\\z/', $test['pass'][3]);
  631. $this->assertPattern('/\\A(?:000030)\\z/', $test['pass'][4]);
  632. $this->assertPattern('/\\A(?:0000400)\\z/', $test['pass'][5]);
  633. }
  634. /**
  635. * testQueryStringOnRoot method
  636. *
  637. * @access public
  638. * @return void
  639. */
  640. function testQueryStringOnRoot() {
  641. Router::reload();
  642. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  643. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  644. $_GET = array('coffee' => 'life', 'sleep' => 'sissies');
  645. $Dispatcher =& new Dispatcher();
  646. $uri = 'posts/home/?coffee=life&sleep=sissies';
  647. $result = $Dispatcher->parseParams($uri);
  648. $this->assertPattern('/posts/', $result['controller']);
  649. $this->assertPattern('/home/', $result['action']);
  650. $this->assertTrue(isset($result['url']['sleep']));
  651. $this->assertTrue(isset($result['url']['coffee']));
  652. $Dispatcher =& new Dispatcher();
  653. $uri = '/?coffee=life&sleep=sissy';
  654. $result = $Dispatcher->parseParams($uri);
  655. $this->assertPattern('/pages/', $result['controller']);
  656. $this->assertPattern('/display/', $result['action']);
  657. $this->assertTrue(isset($result['url']['sleep']));
  658. $this->assertTrue(isset($result['url']['coffee']));
  659. $this->assertEqual($result['url']['coffee'], 'life');
  660. }
  661. /**
  662. * testFileUploadArrayStructure method
  663. *
  664. * @access public
  665. * @return void
  666. */
  667. function testFileUploadArrayStructure() {
  668. $_FILES = array('data' => array('name' => array(
  669. 'File' => array(
  670. array('data' => 'cake_mssql_patch.patch'),
  671. array('data' => 'controller.diff'),
  672. array('data' => ''),
  673. array('data' => ''),
  674. ),
  675. 'Post' => array('attachment' => 'jquery-1.2.1.js'),
  676. ),
  677. 'type' => array(
  678. 'File' => array(
  679. array('data' => ''),
  680. array('data' => ''),
  681. array('data' => ''),
  682. array('data' => ''),
  683. ),
  684. 'Post' => array('attachment' => 'application/x-javascript'),
  685. ),
  686. 'tmp_name' => array(
  687. 'File' => array(
  688. array('data' => '/private/var/tmp/phpy05Ywj'),
  689. array('data' => '/private/var/tmp/php7MBztY'),
  690. array('data' => ''),
  691. array('data' => ''),
  692. ),
  693. 'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
  694. ),
  695. 'error' => array(
  696. 'File' => array(
  697. array('data' => 0),
  698. array('data' => 0),
  699. array('data' => 4),
  700. array('data' => 4)
  701. ),
  702. 'Post' => array('attachment' => 0)
  703. ),
  704. 'size' => array(
  705. 'File' => array(
  706. array('data' => 6271),
  707. array('data' => 350),
  708. array('data' => 0),
  709. array('data' => 0),
  710. ),
  711. 'Post' => array('attachment' => 80469)
  712. ),
  713. ));
  714. $Dispatcher =& new Dispatcher();
  715. $result = $Dispatcher->parseParams('/');
  716. $expected = array(
  717. 'File' => array(
  718. array('data' => array(
  719. 'name' => 'cake_mssql_patch.patch',
  720. 'type' => '',
  721. 'tmp_name' => '/private/var/tmp/phpy05Ywj',
  722. 'error' => 0,
  723. 'size' => 6271,
  724. ),
  725. ),
  726. array('data' => array(
  727. 'name' => 'controller.diff',
  728. 'type' => '',
  729. 'tmp_name' => '/private/var/tmp/php7MBztY',
  730. 'error' => 0,
  731. 'size' => 350,
  732. )),
  733. array('data' => array(
  734. 'name' => '',
  735. 'type' => '',
  736. 'tmp_name' => '',
  737. 'error' => 4,
  738. 'size' => 0,
  739. )),
  740. array('data' => array(
  741. 'name' => '',
  742. 'type' => '',
  743. 'tmp_name' => '',
  744. 'error' => 4,
  745. 'size' => 0,
  746. )),
  747. ),
  748. 'Post' => array('attachment' => array(
  749. 'name' => 'jquery-1.2.1.js',
  750. 'type' => 'application/x-javascript',
  751. 'tmp_name' => '/private/var/tmp/phpEwlrIo',
  752. 'error' => 0,
  753. 'size' => 80469,
  754. )));
  755. $this->assertEqual($result['data'], $expected);
  756. $_FILES = array(
  757. 'data' => array(
  758. 'name' => array(
  759. 'Document' => array(
  760. 1 => array(
  761. 'birth_cert' => 'born on.txt',
  762. 'passport' => 'passport.txt',
  763. 'drivers_license' => 'ugly pic.jpg'
  764. ),
  765. 2 => array(
  766. 'birth_cert' => 'aunt betty.txt',
  767. 'passport' => 'betty-passport.txt',
  768. 'drivers_license' => 'betty-photo.jpg'
  769. ),
  770. ),
  771. ),
  772. 'type' => array(
  773. 'Document' => array(
  774. 1 => array(
  775. 'birth_cert' => 'application/octet-stream',
  776. 'passport' => 'application/octet-stream',
  777. 'drivers_license' => 'application/octet-stream',
  778. ),
  779. 2 => array(
  780. 'birth_cert' => 'application/octet-stream',
  781. 'passport' => 'application/octet-stream',
  782. 'drivers_license' => 'application/octet-stream',
  783. )
  784. )
  785. ),
  786. 'tmp_name' => array(
  787. 'Document' => array(
  788. 1 => array(
  789. 'birth_cert' => '/private/var/tmp/phpbsUWfH',
  790. 'passport' => '/private/var/tmp/php7f5zLt',
  791. 'drivers_license' => '/private/var/tmp/phpMXpZgT',
  792. ),
  793. 2 => array(
  794. 'birth_cert' => '/private/var/tmp/php5kHZt0',
  795. 'passport' => '/private/var/tmp/phpnYkOuM',
  796. 'drivers_license' => '/private/var/tmp/php9Rq0P3',
  797. )
  798. )
  799. ),
  800. 'error' => array(
  801. 'Document' => array(
  802. 1 => array(
  803. 'birth_cert' => 0,
  804. 'passport' => 0,
  805. 'drivers_license' => 0,
  806. ),
  807. 2 => array(
  808. 'birth_cert' => 0,
  809. 'passport' => 0,
  810. 'drivers_license' => 0,
  811. )
  812. )
  813. ),
  814. 'size' => array(
  815. 'Document' => array(
  816. 1 => array(
  817. 'birth_cert' => 123,
  818. 'passport' => 458,
  819. 'drivers_license' => 875,
  820. ),
  821. 2 => array(
  822. 'birth_cert' => 876,
  823. 'passport' => 976,
  824. 'drivers_license' => 9783,
  825. )
  826. )
  827. )
  828. )
  829. );
  830. $Dispatcher =& new Dispatcher();
  831. $result = $Dispatcher->parseParams('/');
  832. $expected = array(
  833. 'Document' => array(
  834. 1 => array(
  835. 'birth_cert' => array(
  836. 'name' => 'born on.txt',
  837. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  838. 'error' => 0,
  839. 'size' => 123,
  840. 'type' => 'application/octet-stream',
  841. ),
  842. 'passport' => array(
  843. 'name' => 'passport.txt',
  844. 'tmp_name' => '/private/var/tmp/php7f5zLt',
  845. 'error' => 0,
  846. 'size' => 458,
  847. 'type' => 'application/octet-stream',
  848. ),
  849. 'drivers_license' => array(
  850. 'name' => 'ugly pic.jpg',
  851. 'tmp_name' => '/private/var/tmp/phpMXpZgT',
  852. 'error' => 0,
  853. 'size' => 875,
  854. 'type' => 'application/octet-stream',
  855. ),
  856. ),
  857. 2 => array(
  858. 'birth_cert' => array(
  859. 'name' => 'aunt betty.txt',
  860. 'tmp_name' => '/private/var/tmp/php5kHZt0',
  861. 'error' => 0,
  862. 'size' => 876,
  863. 'type' => 'application/octet-stream',
  864. ),
  865. 'passport' => array(
  866. 'name' => 'betty-passport.txt',
  867. 'tmp_name' => '/private/var/tmp/phpnYkOuM',
  868. 'error' => 0,
  869. 'size' => 976,
  870. 'type' => 'application/octet-stream',
  871. ),
  872. 'drivers_license' => array(
  873. 'name' => 'betty-photo.jpg',
  874. 'tmp_name' => '/private/var/tmp/php9Rq0P3',
  875. 'error' => 0,
  876. 'size' => 9783,
  877. 'type' => 'application/octet-stream',
  878. ),
  879. ),
  880. )
  881. );
  882. $this->assertEqual($result['data'], $expected);
  883. $_FILES = array(
  884. 'data' => array(
  885. 'name' => array('birth_cert' => 'born on.txt'),
  886. 'type' => array('birth_cert' => 'application/octet-stream'),
  887. 'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
  888. 'error' => array('birth_cert' => 0),
  889. 'size' => array('birth_cert' => 123)
  890. )
  891. );
  892. $Dispatcher =& new Dispatcher();
  893. $result = $Dispatcher->parseParams('/');
  894. $expected = array(
  895. 'birth_cert' => array(
  896. 'name' => 'born on.txt',
  897. 'type' => 'application/octet-stream',
  898. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  899. 'error' => 0,
  900. 'size' => 123
  901. )
  902. );
  903. $this->assertEqual($result['data'], $expected);
  904. }
  905. /**
  906. * testGetUrl method
  907. *
  908. * @access public
  909. * @return void
  910. */
  911. function testGetUrl() {
  912. $Dispatcher =& new Dispatcher();
  913. $Dispatcher->base = '/app/webroot/index.php';
  914. $uri = '/app/webroot/index.php/posts/add';
  915. $result = $Dispatcher->getUrl($uri);
  916. $expected = 'posts/add';
  917. $this->assertEqual($expected, $result);
  918. Configure::write('App.baseUrl', '/app/webroot/index.php');
  919. $uri = '/posts/add';
  920. $result = $Dispatcher->getUrl($uri);
  921. $expected = 'posts/add';
  922. $this->assertEqual($expected, $result);
  923. $_GET['url'] = array();
  924. Configure::write('App.base', '/control');
  925. $Dispatcher =& new Dispatcher();
  926. $Dispatcher->baseUrl();
  927. $uri = '/control/students/browse';
  928. $result = $Dispatcher->getUrl($uri);
  929. $expected = 'students/browse';
  930. $this->assertEqual($expected, $result);
  931. $_GET['url'] = array();
  932. $Dispatcher =& new Dispatcher();
  933. $Dispatcher->base = '';
  934. $uri = '/?/home';
  935. $result = $Dispatcher->getUrl($uri);
  936. $expected = '?/home';
  937. $this->assertEqual($expected, $result);
  938. }
  939. /**
  940. * testBaseUrlAndWebrootWithModRewrite method
  941. *
  942. * @access public
  943. * @return void
  944. */
  945. function testBaseUrlAndWebrootWithModRewrite() {
  946. $Dispatcher =& new Dispatcher();
  947. $Dispatcher->base = false;
  948. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  949. $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/app/webroot/index.php';
  950. $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
  951. $result = $Dispatcher->baseUrl();
  952. $expected = '/1.2.x.x';
  953. $this->assertEqual($expected, $result);
  954. $expectedWebroot = '/1.2.x.x/';
  955. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  956. $Dispatcher->base = false;
  957. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
  958. $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/app/webroot/index.php';
  959. $_SERVER['PHP_SELF'] = '/index.php';
  960. $result = $Dispatcher->baseUrl();
  961. $expected = '';
  962. $this->assertEqual($expected, $result);
  963. $expectedWebroot = '/';
  964. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  965. $Dispatcher->base = false;
  966. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  967. $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/test/webroot/index.php';
  968. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  969. $result = $Dispatcher->baseUrl();
  970. $expected = '';
  971. $this->assertEqual($expected, $result);
  972. $expectedWebroot = '/';
  973. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  974. $Dispatcher->base = false;;
  975. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  976. $_SERVER['SCRIPT_FILENAME'] = '/some/apps/where/app/webroot/index.php';
  977. $_SERVER['PHP_SELF'] = '/some/apps/where/app/webroot/index.php';
  978. $result = $Dispatcher->baseUrl();
  979. $expected = '/some/apps/where';
  980. $this->assertEqual($expected, $result);
  981. $expectedWebroot = '/some/apps/where/';
  982. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  983. Configure::write('App.dir', 'auth');
  984. $Dispatcher->base = false;;
  985. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  986. $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/demos/auth/webroot/index.php';
  987. $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
  988. $result = $Dispatcher->baseUrl();
  989. $expected = '/demos/auth';
  990. $this->assertEqual($expected, $result);
  991. $expectedWebroot = '/demos/auth/';
  992. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  993. Configure::write('App.dir', 'code');
  994. $Dispatcher->base = false;;
  995. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  996. $_SERVER['SCRIPT_FILENAME'] = '/Library/WebServer/Documents/clients/PewterReport/code/webroot/index.php';
  997. $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
  998. $result = $Dispatcher->baseUrl();
  999. $expected = '/clients/PewterReport/code';
  1000. $this->assertEqual($expected, $result);
  1001. $expectedWebroot = '/clients/PewterReport/code/';
  1002. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1003. }
  1004. /**
  1005. * testBaseUrlwithModRewriteAlias method
  1006. *
  1007. * @access public
  1008. * @return void
  1009. */
  1010. function testBaseUrlwithModRewriteAlias() {
  1011. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  1012. $_SERVER['SCRIPT_FILENAME'] = '/home/aplusnur/cake2/app/webroot/index.php';
  1013. $_SERVER['PHP_SELF'] = '/control/index.php';
  1014. Configure::write('App.base', '/control');
  1015. $Dispatcher =& new Dispatcher();
  1016. $result = $Dispatcher->baseUrl();
  1017. $expected = '/control';
  1018. $this->assertEqual($expected, $result);
  1019. $expectedWebroot = '/control/';
  1020. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1021. Configure::write('App.base', false);
  1022. Configure::write('App.dir', 'affiliate');
  1023. Configure::write('App.webroot', 'newaffiliate');
  1024. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  1025. $_SERVER['SCRIPT_FILENAME'] = '/var/www/abtravaff/html/newaffiliate/index.php';
  1026. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  1027. $Dispatcher =& new Dispatcher();
  1028. $result = $Dispatcher->baseUrl();
  1029. $expected = '/newaffiliate';
  1030. $this->assertEqual($expected, $result);
  1031. $expectedWebroot = '/newaffiliate/';
  1032. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1033. }
  1034. /**
  1035. * testBaseUrlAndWebrootWithBaseUrl method
  1036. *
  1037. * @access public
  1038. * @return void
  1039. */
  1040. function testBaseUrlAndWebrootWithBaseUrl() {
  1041. $Dispatcher =& new Dispatcher();
  1042. Configure::write('App.dir', 'app');
  1043. Configure::write('App.baseUrl', '/app/webroot/index.php');
  1044. $result = $Dispatcher->baseUrl();
  1045. $expected = '/app/webroot/index.php';
  1046. $this->assertEqual($expected, $result);
  1047. $expectedWebroot = '/app/webroot/';
  1048. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1049. Configure::write('App.baseUrl', '/app/webroot/test.php');
  1050. $result = $Dispatcher->baseUrl();
  1051. $expected = '/app/webroot/test.php';
  1052. $this->assertEqual($expected, $result);
  1053. $expectedWebroot = '/app/webroot/';
  1054. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1055. Configure::write('App.baseUrl', '/app/index.php');
  1056. $result = $Dispatcher->baseUrl();
  1057. $expected = '/app/index.php';
  1058. $this->assertEqual($expected, $result);
  1059. $expectedWebroot = '/app/webroot/';
  1060. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1061. Configure::write('App.baseUrl', '/index.php');
  1062. $result = $Dispatcher->baseUrl();
  1063. $expected = '/index.php';
  1064. $this->assertEqual($expected, $result);
  1065. $expectedWebroot = '/';
  1066. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1067. Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
  1068. $result = $Dispatcher->baseUrl();
  1069. $expected = '/CakeBB/app/webroot/index.php';
  1070. $this->assertEqual($expected, $result);
  1071. $expectedWebroot = '/CakeBB/app/webroot/';
  1072. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1073. Configure::write('App.baseUrl', '/CakeBB/app/index.php');
  1074. $result = $Dispatcher->baseUrl();
  1075. $expected = '/CakeBB/app/index.php';
  1076. $this->assertEqual($expected, $result);
  1077. $expectedWebroot = '/CakeBB/app/webroot/';
  1078. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1079. Configure::write('App.baseUrl', '/CakeBB/index.php');
  1080. $result = $Dispatcher->baseUrl();
  1081. $expected = '/CakeBB/index.php';
  1082. $this->assertEqual($expected, $result);
  1083. $expectedWebroot = '/CakeBB/app/webroot/';
  1084. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1085. Configure::write('App.baseUrl', '/dbhauser/index.php');
  1086. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  1087. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  1088. $result = $Dispatcher->baseUrl();
  1089. $expected = '/dbhauser/index.php';
  1090. $this->assertEqual($expected, $result);
  1091. $expectedWebroot = '/dbhauser/app/webroot/';
  1092. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1093. }
  1094. /**
  1095. * testBaseUrlAndWebrootWithBase method
  1096. *
  1097. * @access public
  1098. * @return void
  1099. */
  1100. function testBaseUrlAndWebrootWithBase() {
  1101. $Dispatcher =& new Dispatcher();
  1102. $Dispatcher->base = '/app';
  1103. $result = $Dispatcher->baseUrl();
  1104. $expected = '/app';
  1105. $this->assertEqual($expected, $result);
  1106. $expectedWebroot = '/app/';
  1107. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1108. $Dispatcher->base = '';
  1109. $result = $Dispatcher->baseUrl();
  1110. $expected = '';
  1111. $this->assertEqual($expected, $result);
  1112. $expectedWebroot = '/';
  1113. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1114. Configure::write('App.dir', 'testbed');
  1115. $Dispatcher->base = '/cake/testbed/webroot';
  1116. $result = $Dispatcher->baseUrl();
  1117. $expected = '/cake/testbed/webroot';
  1118. $this->assertEqual($expected, $result);
  1119. $expectedWebroot = '/cake/testbed/webroot/';
  1120. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1121. }
  1122. /**
  1123. * testMissingController method
  1124. *
  1125. * @access public
  1126. * @return void
  1127. */
  1128. function testMissingController() {
  1129. $Dispatcher =& new TestDispatcher();
  1130. Configure::write('App.baseUrl', '/index.php');
  1131. $url = 'some_controller/home/param:value/param2:value2';
  1132. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1133. $expected = array('missingController', array(array(
  1134. 'className' => 'SomeControllerController',
  1135. 'webroot' => '/',
  1136. 'url' => 'some_controller/home/param:value/param2:value2',
  1137. 'base' => '/index.php'
  1138. )));
  1139. $this->assertEqual($expected, $controller);
  1140. }
  1141. /**
  1142. * testPrivate method
  1143. *
  1144. * @access public
  1145. * @return void
  1146. */
  1147. function testPrivate() {
  1148. $Dispatcher =& new TestDispatcher();
  1149. Configure::write('App.baseUrl','/index.php');
  1150. $url = 'some_pages/_protected/param:value/param2:value2';
  1151. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1152. $expected = array('privateAction', array(array(
  1153. 'className' => 'SomePagesController',
  1154. 'action' => '_protected',
  1155. 'webroot' => '/',
  1156. 'url' => 'some_pages/_protected/param:value/param2:value2',
  1157. 'base' => '/index.php'
  1158. )));
  1159. $this->assertEqual($controller, $expected);
  1160. }
  1161. /**
  1162. * testMissingAction method
  1163. *
  1164. * @access public
  1165. * @return void
  1166. */
  1167. function testMissingAction() {
  1168. $Dispatcher =& new TestDispatcher();
  1169. Configure::write('App.baseUrl', '/index.php');
  1170. $url = 'some_pages/home/param:value/param2:value2';
  1171. $controller = $Dispatcher->dispatch($url, array('return'=> 1));
  1172. $expected = array('missingAction', array(array(
  1173. 'className' => 'SomePagesController',
  1174. 'action' => 'home',
  1175. 'webroot' => '/',
  1176. 'url' => '/index.php/some_pages/home/param:value/param2:value2',
  1177. 'base' => '/index.php'
  1178. )));
  1179. $this->assertEqual($expected, $controller);
  1180. $Dispatcher =& new TestDispatcher();
  1181. Configure::write('App.baseUrl','/index.php');
  1182. $url = 'some_pages/redirect/param:value/param2:value2';
  1183. $controller = $Dispatcher->dispatch($url, array('return'=> 1));
  1184. $expected = array('missingAction', array(array(
  1185. 'className' => 'SomePagesController',
  1186. 'action' => 'redirect',
  1187. 'webroot' => '/',
  1188. 'url' => '/index.php/some_pages/redirect/param:value/param2:value2',
  1189. 'base' => '/index.php'
  1190. )));
  1191. $this->assertEqual($expected, $controller);
  1192. }
  1193. /**
  1194. * testDispatch method
  1195. *
  1196. * @access public
  1197. * @return void
  1198. */
  1199. function testDispatch() {
  1200. $Dispatcher =& new TestDispatcher();
  1201. Configure::write('App.baseUrl','/index.php');
  1202. $url = 'pages/home/param:value/param2:value2';
  1203. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1204. $expected = 'Pages';
  1205. $this->assertEqual($expected, $controller->name);
  1206. $expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2');
  1207. $this->assertIdentical($expected, $controller->passedArgs);
  1208. Configure::write('App.baseUrl','/pages/index.php');
  1209. $url = 'pages/home';
  1210. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1211. $expected = 'Pages';
  1212. $this->assertEqual($expected, $controller->name);
  1213. $url = 'pages/home/';
  1214. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1215. $expected = 'Pages';
  1216. $this->assertEqual($expected, $controller->name);
  1217. unset($Dispatcher);
  1218. $Dispatcher =& new TestDispatcher();
  1219. Configure::write('App.baseUrl','/timesheets/index.php');
  1220. $url = 'timesheets';
  1221. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1222. $expected = 'Timesheets';
  1223. $this->assertEqual($expected, $controller->name);
  1224. $url = 'timesheets/';
  1225. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1226. $this->assertEqual('Timesheets', $controller->name);
  1227. $this->assertEqual('/timesheets/index.php', $Dispatcher->base);
  1228. $url = 'test_dispatch_pages/camelCased';
  1229. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1230. $this->assertEqual('TestDispatchPages', $controller->name);
  1231. $url = 'test_dispatch_pages/camelCased/something. .';
  1232. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1233. $this->assertEqual($controller->params['pass'][0], 'something. .', 'Period was chopped off. %s');
  1234. }
  1235. /**
  1236. * testDispatchWithArray method
  1237. *
  1238. * @access public
  1239. * @return void
  1240. */
  1241. function testDispatchWithArray() {
  1242. $Dispatcher =& new TestDispatcher();
  1243. Configure::write('App.baseUrl','/index.php');
  1244. $url = 'pages/home/param:value/param2:value2';
  1245. $url = array('controller' => 'pages', 'action' => 'display');
  1246. $controller = $Dispatcher->dispatch($url, array('pass' => array('home'), 'named' => array('param' => 'value', 'param2' => 'value2'), 'return' => 1));
  1247. $expected = 'Pages';
  1248. $this->assertEqual($expected, $controller->name);
  1249. $expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2');
  1250. $this->assertIdentical($expected, $controller->passedArgs);
  1251. }
  1252. /**
  1253. * testAdminDispatch method
  1254. *
  1255. * @access public
  1256. * @return void
  1257. */
  1258. function testAdminDispatch() {
  1259. $_POST = array();
  1260. $Dispatcher =& new TestDispatcher();
  1261. Configure::write('Routing.admin', 'admin');
  1262. Configure::write('App.baseUrl','/cake/repo/branches/1.2.x.x/index.php');
  1263. $url = 'admin/test_dispatch_pages/index/param:value/param2:value2';
  1264. Router::reload();
  1265. $Router =& Router::getInstance();
  1266. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1267. $expected = 'TestDispatchPages';
  1268. $this->assertEqual($expected, $controller->name);
  1269. $expected = array('param' => 'value', 'param2' => 'value2');
  1270. $this->assertIdentical($expected, $controller->passedArgs);
  1271. $this->assertTrue($controller->params['admin']);
  1272. $expected = '/cake/repo/branches/1.2.x.x/index.php/admin/test_dispatch_pages/index/param:value/param2:value2';
  1273. $this->assertIdentical($expected, $controller->here);
  1274. $expected = '/cake/repo/branches/1.2.x.x/index.php';
  1275. $this->assertIdentical($expected, $controller->base);
  1276. }
  1277. /**
  1278. * testPluginDispatch method
  1279. *
  1280. * @access public
  1281. * @return void
  1282. */
  1283. function testPluginDispatch() {
  1284. $_POST = array();
  1285. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1286. Router::reload();
  1287. $Dispatcher =& new TestDispatcher();
  1288. Router::connect('/my_plugin/:controller/*', array('plugin'=>'my_plugin', 'controller'=>'pages', 'action'=>'display'));
  1289. $Dispatcher->base = false;
  1290. $url = 'my_plugin/some_pages/home/param:value/param2:value2';
  1291. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1292. $result = $Dispatcher->parseParams($url);
  1293. $expected = array(
  1294. 'pass' => array('home'),
  1295. 'named' => array('param'=> 'value', 'param2'=> 'value2'), 'plugin'=> 'my_plugin',
  1296. 'controller'=> 'some_pages', 'action'=> 'display', 'form'=> null,
  1297. 'url'=> array('url'=> 'my_plugin/some_pages/home/param:value/param2:value2'),
  1298. );
  1299. ksort($expected);
  1300. ksort($result);
  1301. $this->assertEqual($expected, $result);
  1302. $expected = 'my_plugin';
  1303. $this->assertIdentical($expected, $controller->plugin);
  1304. $expected = 'SomePages';
  1305. $this->assertIdentical($expected, $controller->name);
  1306. $expected = 'some_pages';
  1307. $this->assertIdentical($expected, $controller->params['controller']);
  1308. $expected = array('0' => 'home', 'param'=>'value', 'param2'=>'value2');
  1309. $this->assertIdentical($expected, $controller->passedArgs);
  1310. $expected = '/cake/repo/branches/1.2.x.x/my_plugin/some_pages/home/param:value/param2:value2';
  1311. $this->assertIdentical($expected, $controller->here);
  1312. $expected = '/cake/repo/branches/1.2.x.x';
  1313. $this->assertIdentical($expected, $controller->base);
  1314. }
  1315. /**
  1316. * testAutomaticPluginDispatch method
  1317. *
  1318. * @access public
  1319. * @return void
  1320. */
  1321. function testAutomaticPluginDispatch() {
  1322. $_POST = array();
  1323. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1324. Router::reload();
  1325. $Dispatcher =& new TestDispatcher();
  1326. Router::connect('/my_plugin/:controller/:action/*', array('plugin'=>'my_plugin', 'controller'=>'pages', 'action'=>'display'));
  1327. $Dispatcher->base = false;
  1328. $url = 'my_plugin/other_pages/index/param:value/param2:value2';
  1329. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1330. $expected = 'my_plugin';
  1331. $this->assertIdentical($expected, $controller->plugin);
  1332. $expected = 'OtherPages';
  1333. $this->assertIdentical($expected, $controller->name);
  1334. $expected = 'index';
  1335. $this->assertIdentical($expected, $controller->action);
  1336. $expected = array('param' => 'value', 'param2' => 'value2');
  1337. $this->assertIdentical($expected, $controller->passedArgs);
  1338. $expected = '/cake/repo/branches/1.2.x.x/my_plugin/other_pages/index/param:value/param2:value2';
  1339. $this->assertIdentical($expected, $controller->here);
  1340. $expected = '/cake/repo/branches/1.2.x.x';
  1341. $this->assertIdentical($expected, $controller->base);
  1342. }
  1343. /**
  1344. * testAutomaticPluginControllerDispatch method
  1345. *
  1346. * @access public
  1347. * @return void
  1348. */
  1349. function testAutomaticPluginControllerDispatch() {
  1350. $_POST = array();
  1351. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1352. Router::reload();
  1353. $Dispatcher =& new TestDispatcher();
  1354. $Dispatcher->base = false;
  1355. $url = 'my_plugin/add/param:value/param2:value2';
  1356. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1357. $expected = 'my_plugin';
  1358. $this->assertIdentical($controller->plugin, $expected);
  1359. $expected = 'MyPlugin';
  1360. $this->assertIdentical($controller->name, $expected);
  1361. $expected = 'add';
  1362. $this->assertIdentical($controller->action, $expected);
  1363. $expected = array('param' => 'value', 'param2' => 'value2');
  1364. $this->assertEqual($controller->params['named'], $expected);
  1365. Router::reload();
  1366. $Dispatcher =& new TestDispatcher();
  1367. $Dispatcher->base = false;
  1368. /* Simulates the Route for a real plugin, installed in APP/plugins */
  1369. Router::connect('/my_plugin/:controller/:action/*', array('plugin' => 'my_plugin'));
  1370. $plugin = 'MyPlugin';
  1371. $pluginUrl = Inflector::underscore($plugin);
  1372. $url = $pluginUrl;
  1373. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1374. $expected = $pluginUrl;
  1375. $this->assertIdentical($controller->plugin, $expected);
  1376. $expected = $plugin;
  1377. $this->assertIdentical($controller->name, $expected);
  1378. $expected = 'index';
  1379. $this->assertIdentical($controller->action, $expected);
  1380. $expected = $pluginUrl;
  1381. $this->assertEqual($controller->params['controller'], $expected);
  1382. Configure::write('Routing.admin', 'admin');
  1383. Router::reload();
  1384. $Dispatcher =& new TestDispatcher();
  1385. $Dispatcher->base = false;
  1386. $url = 'admin/my_plugin/add/5/param:value/param2:value2';
  1387. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1388. $expected = 'my_plugin';
  1389. $this->assertIdentical($controller->plugin, $expected);
  1390. $expected = 'MyPlugin';
  1391. $this->assertIdentical($controller->name, $expected);
  1392. $expected = 'admin_add';
  1393. $this->assertIdentical($controller->action, $expected);
  1394. $expected = array(0 => 5, 'param'=>'value', 'param2'=>'value2');
  1395. $this->assertEqual($controller->passedArgs, $expected);
  1396. Router::reload();
  1397. $Dispatcher =& new TestDispatcher();
  1398. $Dispatcher->base = false;
  1399. $controller = $Dispatcher->dispatch('admin/articles_test', array('return' => 1));
  1400. $expected = 'articles_test';
  1401. $this->assertIdentical($controller->plugin, $expected);
  1402. $expected = 'ArticlesTest';
  1403. $this->assertIdentical($controller->name, $expected);
  1404. $expected = 'admin_index';
  1405. $this->assertIdentical($controller->action, $expected);
  1406. $expected = array(
  1407. 'pass'=> array(), 'named' => array(), 'controller' => 'articles_test', 'plugin' => 'articles_test', 'action' => 'admin_index',
  1408. 'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/articles_test'), 'return' => 1
  1409. );
  1410. $this->assertEqual($controller->params, $expected);
  1411. }
  1412. /**
  1413. * test Plugin dispatching without controller name and using
  1414. * plugin short form instead.
  1415. *
  1416. * @return void
  1417. **/
  1418. function testAutomaticPluginDispatchWithShortAccess() {
  1419. $_POST = array();
  1420. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1421. Router::reload();
  1422. Router::connect('/my_plugin/:controller/:action/*', array('plugin'=>'my_plugin'));
  1423. $Dispatcher =& new TestDispatcher();
  1424. $Dispatcher->base = false;
  1425. $url = 'my_plugin/my_plugin/add';
  1426. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1427. $this->assertFalse(isset($controller->params['pass'][0]));
  1428. $Dispatcher =& new TestDispatcher();
  1429. $Dispatcher->base = false;
  1430. $url = 'my_plugin/my_plugin/add/0';
  1431. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1432. $this->assertTrue(isset($controller->params['pass'][0]));
  1433. $Dispatcher =& new TestDispatcher();
  1434. $Dispatcher->base = false;
  1435. $url = 'my_plugin/add';
  1436. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1437. $this->assertFalse(isset($controller->params['pass'][0]));
  1438. $Dispatcher =& new TestDispatcher();
  1439. $Dispatcher->base = false;
  1440. $url = 'my_plugin/add/0';
  1441. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1442. $this->assertIdentical('0',$controller->params['pass'][0]);
  1443. $Dispatcher =& new TestDispatcher();
  1444. $Dispatcher->base = false;
  1445. $url = 'my_plugin/add/1';
  1446. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1447. $this->assertIdentical('1',$controller->params['pass'][0]);
  1448. }
  1449. /**
  1450. * testAutomaticPluginControllerMissingActionDispatch method
  1451. *
  1452. * @access public
  1453. * @return void
  1454. */
  1455. function testAutomaticPluginControllerMissingActionDispatch() {
  1456. $_POST = array();
  1457. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1458. Router::reload();
  1459. $Dispatcher =& new TestDispatcher();
  1460. $Dispatcher->base = false;
  1461. $url = 'my_plugin/not_here/param:value/param2:value2';
  1462. $controller = $Dispatcher->dispatch($url, array('return'=> 1));
  1463. $expected = array('missingAction', array(array(
  1464. 'className' => 'MyPluginController',
  1465. 'action' => 'not_here',
  1466. 'webroot' => '/cake/repo/branches/1.2.x.x/',
  1467. 'url' => '/cake/repo/branches/1.2.x.x/my_plugin/not_here/param:value/param2:value2',
  1468. 'base' => '/cake/repo/branches/1.2.x.x'
  1469. )));
  1470. $this->assertIdentical($expected, $controller);
  1471. Router::reload();
  1472. $Dispatcher =& new TestDispatcher();
  1473. $Dispatcher->base = false;
  1474. $url = 'my_plugin/param:value/param2:value2';
  1475. $controller = $Dispatcher->dispatch($url, array('return'=> 1));
  1476. $expected = array('missingAction', array(array(
  1477. 'className' => 'MyPluginController',
  1478. 'action' => 'param:value',
  1479. 'webroot' => '/cake/repo/branches/1.2.x.x/',
  1480. 'url' => '/cake/repo/branches/1.2.x.x/my_plugin/param:value/param2:value2',
  1481. 'base' => '/cake/repo/branches/1.2.x.x'
  1482. )));
  1483. $this->assertIdentical($expected, $controller);
  1484. }
  1485. /**
  1486. * testPrefixProtection method
  1487. *
  1488. * @access public
  1489. * @return void
  1490. */
  1491. function testPrefixProtection() {
  1492. $_POST = array();
  1493. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1494. Router::reload();
  1495. Router::connect('/admin/:controller/:action/*', array('prefix'=>'admin'), array('controller', 'action'));
  1496. $Dispatcher =& new TestDispatcher();
  1497. $Dispatcher->base = false;
  1498. $url = 'test_dispatch_pages/admin_index/param:value/param2:value2';
  1499. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1500. $expected = array('privateAction', array(array(
  1501. 'className' => 'TestDispatchPagesController',
  1502. 'action' => 'admin_index',
  1503. 'webroot' => '/cake/repo/branches/1.2.x.x/',
  1504. 'url' => 'test_dispatch_pages/admin_index/param:value/param2:value2',
  1505. 'base' => '/cake/repo/branches/1.2.x.x'
  1506. )));
  1507. $this->assertIdentical($expected, $controller);
  1508. }
  1509. /**
  1510. * undocumented function
  1511. *
  1512. * @return void
  1513. **/
  1514. function testTestPluginDispatch() {
  1515. $Dispatcher =& new TestDispatcher();
  1516. $_back = Configure::read('pluginPaths');
  1517. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  1518. $url = '/test_plugin/tests/index';
  1519. $result = $Dispatcher->dispatch($url, array('return' => 1));
  1520. $this->assertTrue(class_exists('TestsController'));
  1521. $this->assertTrue(class_exists('TestPluginAppController'));
  1522. $this->assertTrue(class_exists('OtherComponentComponent'));
  1523. $this->assertTrue(class_exists('PluginsComponentComponent'));
  1524. Configure::write('pluginPaths', $_back);
  1525. }
  1526. /**
  1527. * testChangingParamsFromBeforeFilter method
  1528. *
  1529. * @access public
  1530. * @return void
  1531. */
  1532. function testChangingParamsFromBeforeFilter() {
  1533. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1534. $Dispatcher =& new TestDispatcher();
  1535. $url = 'some_posts/index/param:value/param2:value2';
  1536. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1537. $expected = array('missingAction', array(array(
  1538. 'className' => 'SomePostsController',
  1539. 'action' => 'view',
  1540. 'webroot' => '/cake/repo/branches/1.2.x.x/',
  1541. 'url' => '/cake/repo/branches/1.2.x.x/some_posts/index/param:value/param2:value2',
  1542. 'base' => '/cake/repo/branches/1.2.x.x'
  1543. )));
  1544. $this->assertEqual($expected, $controller);
  1545. $url = 'some_posts/something_else/param:value/param2:value2';
  1546. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1547. $expected = 'SomePosts';
  1548. $this->assertEqual($expected, $controller->name);
  1549. $expected = 'change';
  1550. $this->assertEqual($expected, $controller->action);
  1551. $expected = array('changed');
  1552. $this->assertIdentical($expected, $controller->params['pass']);
  1553. }
  1554. /**
  1555. * testStaticAssets method
  1556. *
  1557. * @access public
  1558. * @return void
  1559. */
  1560. function testStaticAssets() {
  1561. Router::reload();
  1562. $Configure = Configure::getInstance();
  1563. $Configure->__objects = null;
  1564. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  1565. Configure::write('vendorPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors'. DS));
  1566. $Dispatcher =& new TestDispatcher();
  1567. Configure::write('debug', 0);
  1568. ob_start();
  1569. $Dispatcher->dispatch('img/test.jpg');
  1570. $result = ob_get_clean();
  1571. $file = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'img' . DS . 'test.jpg');
  1572. $this->assertEqual($file, $result);
  1573. Configure::write('debug', 0);
  1574. $Dispatcher->params = $Dispatcher->parseParams('css/test_asset.css');
  1575. ob_start();
  1576. $Dispatcher->cached('css/test_asset.css');
  1577. $result = ob_get_clean();
  1578. $this->assertEqual('this is the test asset css file', $result);
  1579. ob_start();
  1580. $Dispatcher->cached('test_plugin/js/test_plugin/test.js');
  1581. $result = ob_get_clean();
  1582. $this->assertEqual('alert("Test App");', $result);
  1583. Configure::write('debug', 0);
  1584. $Dispatcher->params = $Dispatcher->parseParams('test_plugin/js/test_plugin/test.js');
  1585. ob_start();
  1586. $Dispatcher->cached('test_plugin/js/test_plugin/test.js');
  1587. $result = ob_get_clean();
  1588. $this->assertEqual('alert("Test App");', $result);
  1589. Configure::write('debug', 0);
  1590. $Dispatcher->params = $Dispatcher->parseParams('test_plugin/css/test_plugin_asset.css');
  1591. ob_start();
  1592. $Dispatcher->cached('test_plugin/css/test_plugin_asset.css');
  1593. $result = ob_get_clean();
  1594. $this->assertEqual('this is the test plugin asset css file', $result);
  1595. Configure::write('debug', 0);
  1596. $Dispatcher->params = $Dispatcher->parseParams('test_plugin/img/cake.icon.gif');
  1597. ob_start();
  1598. $Dispatcher->cached('test_plugin/img/cake.icon.gif');
  1599. $result = ob_get_clean();
  1600. $file = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' .DS . 'vendors' . DS . 'img' . DS . 'cake.icon.gif');
  1601. $this->assertEqual($file, $result);
  1602. Configure::write('debug', 2);
  1603. $Dispatcher->params = $Dispatcher->parseParams('plugin_js/js/plugin_js.js');
  1604. ob_start();
  1605. $Dispatcher->cached('plugin_js/js/plugin_js.js');
  1606. $result = ob_get_clean();
  1607. $expected = "alert('win sauce');";
  1608. $this->assertEqual($result, $expected);
  1609. header('Content-type: text/html');//reset the header content-type without page can render as plain text.
  1610. }
  1611. /**
  1612. * testFullPageCachingDispatch method
  1613. *
  1614. * @access public
  1615. * @return void
  1616. */
  1617. function testFullPageCachingDispatch() {
  1618. Configure::write('Cache.disable', false);
  1619. Configure::write('Cache.check', true);
  1620. Configure::write('debug', 2);
  1621. $_POST = array();
  1622. $_SERVER['PHP_SELF'] = '/';
  1623. Router::reload();
  1624. Router::connect('/', array('controller' => 'test_cached_pages', 'action' => 'index'));
  1625. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS));
  1626. $dispatcher =& new Dispatcher();
  1627. $dispatcher->base = false;
  1628. $url = '/';
  1629. ob_start();
  1630. $dispatcher->dispatch($url);
  1631. $out = ob_get_clean();
  1632. ob_start();
  1633. $dispatcher->cached($url);
  1634. $cached = ob_get_clean();
  1635. $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
  1636. $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
  1637. $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
  1638. $this->assertEqual($result, $expected);
  1639. $filename = $this->__cachePath($dispatcher->here);
  1640. unlink($filename);
  1641. $dispatcher->base = false;
  1642. $url = 'test_cached_pages/index';
  1643. ob_start();
  1644. $dispatcher->dispatch($url);
  1645. $out = ob_get_clean();
  1646. ob_start();
  1647. $dispatcher->cached($url);
  1648. $cached = ob_get_clean();
  1649. $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
  1650. $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
  1651. $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
  1652. $this->assertEqual($result, $expected);
  1653. $filename = $this->__cachePath($dispatcher->here);
  1654. unlink($filename);
  1655. $url = 'TestCachedPages/index';
  1656. ob_start();
  1657. $dispatcher->dispatch($url);
  1658. $out = ob_get_clean();
  1659. ob_start();
  1660. $dispatcher->cached($url);
  1661. $cached = ob_get_clean();
  1662. $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
  1663. $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
  1664. $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
  1665. $this->assertEqual($result, $expected);
  1666. $filename = $this->__cachePath($dispatcher->here);
  1667. unlink($filename);
  1668. $url = 'TestCachedPages/test_nocache_tags';
  1669. ob_start();
  1670. $dispatcher->dispatch($url);
  1671. $out = ob_get_clean();
  1672. ob_start();
  1673. $dispatcher->cached($url);
  1674. $cached = ob_get_clean();
  1675. $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
  1676. $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
  1677. $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
  1678. $this->assertEqual($result, $expected);
  1679. $filename = $this->__cachePath($dispatcher->here);
  1680. unlink($filename);
  1681. $url = 'test_cached_pages/view/param/param';
  1682. ob_start();
  1683. $dispatcher->dispatch($url);
  1684. $out = ob_get_clean();
  1685. ob_start();
  1686. $dispatcher->cached($url);
  1687. $cached = ob_get_clean();
  1688. $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
  1689. $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
  1690. $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
  1691. $this->assertEqual($result, $expected);
  1692. $filename = $this->__cachePath($dispatcher->here);
  1693. unlink($filename);
  1694. $url = 'test_cached_pages/view/foo:bar/value:goo';
  1695. ob_start();
  1696. $dispatcher->dispatch($url);
  1697. $out = ob_get_clean();
  1698. ob_start();
  1699. $dispatcher->cached($url);
  1700. $cached = ob_get_clean();
  1701. $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
  1702. $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
  1703. $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
  1704. $this->assertEqual($result, $expected);
  1705. $filename = $this->__cachePath($dispatcher->here);
  1706. $this->assertTrue(file_exists($filename));
  1707. unlink($filename);
  1708. $url = 'TestCachedPages/test_nocache_tags';
  1709. }
  1710. /**
  1711. * test that cached() registers a view and un-registers it. Tests
  1712. * that helpers using ClassRegistry::getObject('view'); don't fail
  1713. *
  1714. * @return void
  1715. */
  1716. function testCachedRegisteringViewObject() {
  1717. Configure::write('Cache.disable', false);
  1718. Configure::write('Cache.check', true);
  1719. Configure::write('debug', 2);
  1720. $_POST = array();
  1721. $_SERVER['PHP_SELF'] = '/';
  1722. Router::reload();
  1723. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS));
  1724. $dispatcher =& new Dispatcher();
  1725. $dispatcher->base = false;
  1726. $url = 'test_cached_pages/cache_form';
  1727. ob_start();
  1728. $dispatcher->dispatch($url);
  1729. $out = ob_get_clean();
  1730. ClassRegistry::flush();
  1731. ob_start();
  1732. $dispatcher->cached($url);
  1733. $cached = ob_get_clean();
  1734. $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
  1735. $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
  1736. $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
  1737. $this->assertEqual($result, $expected);
  1738. $filename = $this->__cachePath($dispatcher->here);
  1739. unlink($filename);
  1740. ClassRegistry::flush();
  1741. }
  1742. /**
  1743. * testHttpMethodOverrides method
  1744. *
  1745. * @access public
  1746. * @return void
  1747. */
  1748. function testHttpMethodOverrides() {
  1749. Router::reload();
  1750. Router::mapResources('Posts');
  1751. $_SERVER['REQUEST_METHOD'] = 'POST';
  1752. $dispatcher =& new Dispatcher();
  1753. $dispatcher->base = false;
  1754. $result = $dispatcher->parseParams('/posts');
  1755. $expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST', 'form' => array(), 'url' => array());
  1756. $this->assertEqual($result, $expected);
  1757. $_SERVER['REQUEST_METHOD'] = 'GET';
  1758. $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
  1759. $result = $dispatcher->parseParams('/posts/5');
  1760. $expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'form' => array(), 'url' => array());
  1761. $this->assertEqual($result, $expected);
  1762. unset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
  1763. $_SERVER['REQUEST_METHOD'] = 'GET';
  1764. $result = $dispatcher->parseParams('/posts/5');
  1765. $expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '[method]' => 'GET', 'form' => array(), 'url' => array());
  1766. $this->assertEqual($result, $expected);
  1767. $_POST['_method'] = 'PUT';
  1768. $result = $dispatcher->parseParams('/posts/5');
  1769. $expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'form' => array(), 'url' => array());
  1770. $this->assertEqual($result, $expected);
  1771. $_POST['_method'] = 'POST';
  1772. $_POST['data'] = array('Post' => array('title' => 'New Post'));
  1773. $_POST['extra'] = 'data';
  1774. $_SERVER = array();
  1775. $result = $dispatcher->parseParams('/posts');
  1776. $expected = array(
  1777. 'pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add',
  1778. '[method]' => 'POST', 'form' => array('extra' => 'data'), 'data' => array('Post' => array('title' => 'New Post')),
  1779. 'url' => array()
  1780. );
  1781. $this->assertEqual($result, $expected);
  1782. unset($_POST['_method']);
  1783. }
  1784. /**
  1785. * Tests that invalid characters cannot be injected into the application base path.
  1786. *
  1787. * @return void
  1788. */
  1789. function testBasePathInjection() {
  1790. $self = $_SERVER['PHP_SELF'];
  1791. $_SERVER['PHP_SELF'] = urldecode(
  1792. "/index.php/%22%3E%3Ch1%20onclick=%22alert('xss');%22%3Eheya%3C/h1%3E"
  1793. );
  1794. $dispatcher =& new Dispatcher();
  1795. $result = $dispatcher->baseUrl();
  1796. $expected = '/index.php/h1 onclick=alert(xss);heya';
  1797. $this->assertEqual($result, $expected);
  1798. }
  1799. /**
  1800. * testEnvironmentDetection method
  1801. *
  1802. * @access public
  1803. * @return void
  1804. */
  1805. function testEnvironmentDetection() {
  1806. $dispatcher =& new Dispatcher();
  1807. $environments = array(
  1808. 'IIS' => array(
  1809. 'No rewrite base path' => array(
  1810. 'App' => array('base' => false, 'baseUrl' => '/index.php?', 'server' => 'IIS'),
  1811. 'SERVER' => array('HTTPS' => 'off', 'SCRIPT_NAME' => '/index.php', 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot', 'QUERY_STRING' => '', 'REMOTE_ADDR' => '127.0.0.1', 'REMOTE_HOST' => '127.0.0.1', 'REQUEST_METHOD' => 'GET', 'SERVER_NAME' => 'localhost', 'SERVER_PORT' => '80', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'SERVER_SOFTWARE' => 'Microsoft-IIS/5.1', 'APPL_PHYSICAL_PATH' => 'C:\\Inetpub\\wwwroot\\', 'REQUEST_URI' => '/index.php', 'URL' => '/index.php', 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php', 'ORIG_PATH_INFO' => '/index.php', 'PATH_INFO' => '', 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php', 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot', 'PHP_SELF' => '/index.php', 'HTTP_ACCEPT' => '*/*', 'HTTP_ACCEPT_LANGUAGE' => 'en-us', 'HTTP_CONNECTION' => 'Keep-Alive', 'HTTP_HOST' => 'localhost', 'HTTP_USER_AGENT' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)', 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate', 'argv' => array(), 'argc' => 0),
  1812. 'reload' => true,
  1813. 'path' => ''
  1814. ),
  1815. 'No rewrite with path' => array(
  1816. 'SERVER' => array('QUERY_STRING' => '/posts/add', 'REQUEST_URI' => '/index.php?/posts/add', 'URL' => '/index.php?/posts/add', 'argv' => array('/posts/add'), 'argc' => 1),
  1817. 'reload' => false,
  1818. 'path' => '/posts/add'
  1819. ),
  1820. 'No rewrite sub dir 1' => array(
  1821. 'GET' => array(),
  1822. 'SERVER' => array('QUERY_STRING' => '', 'REQUEST_URI' => '/index.php', 'URL' => '/index.php', 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php', 'ORIG_PATH_INFO' => '/index.php', 'PATH_INFO' => '', 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php', 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot', 'PHP_SELF' => '/index.php', 'argv' => array(), 'argc' => 0),
  1823. 'reload' => false,
  1824. 'path' => ''
  1825. ),
  1826. 'No rewrite sub dir 1 with path' => array(
  1827. 'GET' => array('/posts/add' => ''),
  1828. 'SERVER' => array('QUERY_STRING' => '/posts/add', 'REQUEST_URI' => '/index.php?/posts/add', 'URL' => '/index.php?/posts/add', 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php', 'argv' => array('/posts/add'), 'argc' => 1),
  1829. 'reload' => false,
  1830. 'path' => '/posts/add'
  1831. ),
  1832. 'No rewrite sub dir 2' => array(
  1833. 'App' => array('base' => false, 'baseUrl' => '/site/index.php?', 'dir' => 'app', 'webroot' => 'webroot', 'server' => 'IIS'),
  1834. 'GET' => array(),
  1835. 'POST' => array(),
  1836. 'SERVER' => array('SCRIPT_NAME' => '/site/index.php', 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot', 'QUERY_STRING' => '', 'REQUEST_URI' => '/site/index.php', 'URL' => '/site/index.php', 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php', 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot', 'PHP_SELF' => '/site/index.php', 'argv' => array(), 'argc' => 0),
  1837. 'reload' => false,
  1838. 'path' => ''
  1839. ),
  1840. 'No rewrite sub dir 2 with path' => array(
  1841. 'GET' => array('/posts/add' => ''),
  1842. 'SERVER' => array('SCRIPT_NAME' => '/site/index.php', 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot', 'QUERY_STRING' => '/posts/add', 'REQUEST_URI' => '/site/index.php?/posts/add', 'URL' => '/site/index.php?/posts/add', 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php', 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot', 'PHP_SELF' => '/site/index.php', 'argv' => array('/posts/add'), 'argc' => 1),
  1843. 'reload' => false,
  1844. 'path' => '/posts/add'
  1845. )
  1846. ),
  1847. 'Apache' => array(
  1848. 'No rewrite base path' => array(
  1849. 'App' => array('base' => false, 'baseUrl' => '/index.php', 'dir' => 'app', 'webroot' => 'webroot'),
  1850. 'SERVER' => array('SERVER_NAME' => 'localhost', 'SERVER_ADDR' => '::1', 'SERVER_PORT' => '80', 'REMOTE_ADDR' => '::1', 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/officespace/app/webroot', 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php', 'REQUEST_METHOD' => 'GET', 'QUERY_STRING' => '', 'REQUEST_URI' => '/', 'SCRIPT_NAME' => '/index.php', 'PHP_SELF' => '/index.php', 'argv' => array(), 'argc' => 0),
  1851. 'reload' => true,
  1852. 'path' => ''
  1853. ),
  1854. 'No rewrite with path' => array(
  1855. 'SERVER' => array('UNIQUE_ID' => 'VardGqn@17IAAAu7LY8AAAAK', 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-us) AppleWebKit/523.10.5 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6', 'HTTP_ACCEPT' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5', 'HTTP_ACCEPT_LANGUAGE' => 'en-us', 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate', 'HTTP_CONNECTION' => 'keep-alive', 'HTTP_HOST' => 'localhost', 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/officespace/app/webroot', 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/officespace/app/webroot/index.php', 'QUERY_STRING' => '', 'REQUEST_URI' => '/index.php/posts/add', 'SCRIPT_NAME' => '/index.php', 'PATH_INFO' => '/posts/add', 'PHP_SELF' => '/index.php/posts/add', 'argv' => array(), 'argc' => 0),
  1856. 'reload' => false,
  1857. 'path' => '/posts/add'
  1858. ),
  1859. 'GET Request at base domain' => array(
  1860. 'App' => array('base' => false, 'baseUrl' => null, 'dir' => 'app', 'webroot' => 'webroot'),
  1861. 'SERVER' => array('UNIQUE_ID' => '2A-v8sCoAQ8AAAc-2xUAAAAB', 'HTTP_ACCEPT_LANGUAGE' => 'en-us', 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate', 'HTTP_COOKIE' => 'CAKEPHP=jcbv51apn84kd9ucv5aj2ln3t3', 'HTTP_CONNECTION' => 'keep-alive', 'HTTP_HOST' => 'cake.1.2', 'SERVER_NAME' => 'cake.1.2', 'SERVER_ADDR' => '127.0.0.1', 'SERVER_PORT' => '80', 'REMOTE_ADDR' => '127.0.0.1', 'DOCUMENT_ROOT' => '/Volumes/Home/htdocs/cake/repo/branches/1.2.x.x/app/webroot', 'SERVER_ADMIN' => 'you@example.com', 'SCRIPT_FILENAME' => '/Volumes/Home/htdocs/cake/repo/branches/1.2.x.x/app/webroot/index.php', 'REMOTE_PORT' => '53550', 'GATEWAY_INTERFACE' => 'CGI/1.1', 'SERVER_PROTOCOL' => 'HTTP/1.1', 'REQUEST_METHOD' => 'GET', 'QUERY_STRING' => 'a=b', 'REQUEST_URI' => '/?a=b', 'SCRIPT_NAME' => '/index.php', 'PHP_SELF' => '/index.php'),
  1862. 'GET' => array('a' => 'b'),
  1863. 'POST' => array(),
  1864. 'reload' => true,
  1865. 'path' => '',
  1866. 'urlParams' => array('a' => 'b'),
  1867. 'environment' => array('CGI_MODE' => false)
  1868. ),
  1869. 'New CGI no mod_rewrite' => array(
  1870. 'App' => array('base' => false, 'baseUrl' => '/limesurvey20/index.php', 'dir' => 'app', 'webroot' => 'webroot'),
  1871. 'SERVER' => array('DOCUMENT_ROOT' => '/home/.sites/110/site313/web', 'PATH_INFO' => '/installations', 'PATH_TRANSLATED' => '/home/.sites/110/site313/web/limesurvey20/index.php', 'PHPRC' => '/home/.sites/110/site313', 'QUERY_STRING' => '', 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/limesurvey20/index.php/installations', 'SCRIPT_FILENAME' => '/home/.sites/110/site313/web/limesurvey20/index.php', 'SCRIPT_NAME' => '/limesurvey20/index.php', 'SCRIPT_URI' => 'http://www.gisdat-umfragen.at/limesurvey20/index.php/installations', 'PHP_SELF' => '/limesurvey20/index.php/installations', 'CGI_MODE' => true),
  1872. 'GET' => array(),
  1873. 'POST' => array(),
  1874. 'reload' => true,
  1875. 'path' => '/installations',
  1876. 'urlParams' => array(),
  1877. 'environment' => array('CGI_MODE' => true)
  1878. )
  1879. )
  1880. );
  1881. $backup = $this->__backupEnvironment();
  1882. foreach ($environments as $name => $env) {
  1883. foreach ($env as $descrip => $settings) {
  1884. if ($settings['reload']) {
  1885. $this->__reloadEnvironment();
  1886. }
  1887. $this->__loadEnvironment($settings);
  1888. $this->assertEqual($dispatcher->uri(), $settings['path'], "%s on environment: {$name}, on setting: {$descrip}");
  1889. if (isset($settings['urlParams'])) {
  1890. $this->assertEqual($_GET, $settings['urlParams'], "%s on environment: {$name}, on setting: {$descrip}");
  1891. }
  1892. if (isset($settings['environment'])) {
  1893. foreach ($settings['environment'] as $key => $val) {
  1894. $this->assertEqual(env($key), $val, "%s on key {$key} on environment: {$name}, on setting: {$descrip}");
  1895. }
  1896. }
  1897. }
  1898. }
  1899. $this->__loadEnvironment(array_merge(array('reload' => true), $backup));
  1900. }
  1901. /**
  1902. * Tests that the Dispatcher does not return an empty action
  1903. *
  1904. * @access private
  1905. * @return void
  1906. */
  1907. function testTrailingSlash() {
  1908. $_POST = array();
  1909. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1910. Router::reload();
  1911. $Dispatcher =& new TestDispatcher();
  1912. Router::connect('/myalias/:action/*', array('controller' => 'my_controller', 'action' => null));
  1913. $Dispatcher->base = false;
  1914. $url = 'myalias/'; //Fails
  1915. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1916. $result = $Dispatcher->parseParams($url);
  1917. $this->assertEqual('index', $result['action']);
  1918. $url = 'myalias'; //Passes
  1919. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1920. $result = $Dispatcher->parseParams($url);
  1921. $this->assertEqual('index', $result['action']);
  1922. }
  1923. /**
  1924. * backupEnvironment method
  1925. *
  1926. * @access private
  1927. * @return void
  1928. */
  1929. function __backupEnvironment() {
  1930. return array(
  1931. 'App' => Configure::read('App'),
  1932. 'GET' => $_GET,
  1933. 'POST' => $_POST,
  1934. 'SERVER'=> $_SERVER
  1935. );
  1936. }
  1937. /**
  1938. * reloadEnvironment method
  1939. *
  1940. * @access private
  1941. * @return void
  1942. */
  1943. function __reloadEnvironment() {
  1944. foreach ($_GET as $key => $val) {
  1945. unset($_GET[$key]);
  1946. }
  1947. foreach ($_POST as $key => $val) {
  1948. unset($_POST[$key]);
  1949. }
  1950. foreach ($_SERVER as $key => $val) {
  1951. unset($_SERVER[$key]);
  1952. }
  1953. Configure::write('App', array());
  1954. }
  1955. /**
  1956. * loadEnvironment method
  1957. *
  1958. * @param mixed $env
  1959. * @access private
  1960. * @return void
  1961. */
  1962. function __loadEnvironment($env) {
  1963. if ($env['reload']) {
  1964. $this->__reloadEnvironment();
  1965. }
  1966. if (isset($env['App'])) {
  1967. Configure::write('App', $env['App']);
  1968. }
  1969. if (isset($env['GET'])) {
  1970. foreach ($env['GET'] as $key => $val) {
  1971. $_GET[$key] = $val;
  1972. }
  1973. }
  1974. if (isset($env['POST'])) {
  1975. foreach ($env['POST'] as $key => $val) {
  1976. $_POST[$key] = $val;
  1977. }
  1978. }
  1979. if (isset($env['SERVER'])) {
  1980. foreach ($env['SERVER'] as $key => $val) {
  1981. $_SERVER[$key] = $val;
  1982. }
  1983. }
  1984. }
  1985. /**
  1986. * cachePath method
  1987. *
  1988. * @param mixed $her
  1989. * @access private
  1990. * @return string
  1991. */
  1992. function __cachePath($here) {
  1993. $path = $here;
  1994. if ($here == '/') {
  1995. $path = 'home';
  1996. }
  1997. $path = strtolower(Inflector::slug($path));
  1998. $filename = CACHE . 'views' . DS . $path . '.php';
  1999. if (!file_exists($filename)) {
  2000. $filename = CACHE . 'views' . DS . $path . '_index.php';
  2001. }
  2002. return $filename;
  2003. }
  2004. }
  2005. ?>