PageRenderTime 67ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/webpolis/liiv
PHP | 2182 lines | 1320 code | 228 blank | 634 comment | 17 complexity | bba7c247902db5911efa7b5c7e63b0e0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full 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-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. require_once CAKE . 'dispatcher.php';
  28. if (!class_exists('AppController')) {
  29. require_once LIBS . 'controller' . DS . 'app_controller.php';
  30. } elseif (!defined('APP_CONTROLLER_EXISTS')){
  31. define('APP_CONTROLLER_EXISTS', true);
  32. }
  33. /**
  34. * TestDispatcher class
  35. *
  36. * @package cake
  37. * @subpackage cake.tests.cases
  38. */
  39. class TestDispatcher extends Dispatcher {
  40. /**
  41. * invoke method
  42. *
  43. * @param mixed $controller
  44. * @param mixed $params
  45. * @param mixed $missingAction
  46. * @access protected
  47. * @return void
  48. */
  49. function _invoke(&$controller, $params) {
  50. restore_error_handler();
  51. if ($result = parent::_invoke($controller, $params)) {
  52. if ($result[0] === 'missingAction') {
  53. return $result;
  54. }
  55. }
  56. set_error_handler('simpleTestErrorHandler');
  57. return $controller;
  58. }
  59. /**
  60. * cakeError method
  61. *
  62. * @param mixed $filename
  63. * @access public
  64. * @return void
  65. */
  66. function cakeError($filename, $params) {
  67. return array($filename, $params);
  68. }
  69. /**
  70. * _stop method
  71. *
  72. * @access protected
  73. * @return void
  74. */
  75. function _stop() {
  76. return true;
  77. }
  78. }
  79. /**
  80. * MyPluginAppController class
  81. *
  82. * @package cake
  83. * @subpackage cake.tests.cases
  84. */
  85. class MyPluginAppController extends AppController {
  86. }
  87. /**
  88. * MyPluginController class
  89. *
  90. * @package cake
  91. * @subpackage cake.tests.cases
  92. */
  93. class MyPluginController extends MyPluginAppController {
  94. /**
  95. * name property
  96. *
  97. * @var string 'MyPlugin'
  98. * @access public
  99. */
  100. var $name = 'MyPlugin';
  101. /**
  102. * uses property
  103. *
  104. * @var array
  105. * @access public
  106. */
  107. var $uses = array();
  108. /**
  109. * index method
  110. *
  111. * @access public
  112. * @return void
  113. */
  114. function index() {
  115. return true;
  116. }
  117. /**
  118. * add method
  119. *
  120. * @access public
  121. * @return void
  122. */
  123. function add() {
  124. return true;
  125. }
  126. /**
  127. * admin_add method
  128. *
  129. * @param mixed $id
  130. * @access public
  131. * @return void
  132. */
  133. function admin_add($id = null) {
  134. return $id;
  135. }
  136. }
  137. /**
  138. * SomePagesController class
  139. *
  140. * @package cake
  141. * @subpackage cake.tests.cases
  142. */
  143. class SomePagesController extends AppController {
  144. /**
  145. * name property
  146. *
  147. * @var string 'SomePages'
  148. * @access public
  149. */
  150. var $name = 'SomePages';
  151. /**
  152. * uses property
  153. *
  154. * @var array
  155. * @access public
  156. */
  157. var $uses = array();
  158. /**
  159. * display method
  160. *
  161. * @param mixed $page
  162. * @access public
  163. * @return void
  164. */
  165. function display($page = null) {
  166. return $page;
  167. }
  168. /**
  169. * index method
  170. *
  171. * @access public
  172. * @return void
  173. */
  174. function index() {
  175. return true;
  176. }
  177. /**
  178. * protected method
  179. *
  180. * @access protected
  181. * @return void
  182. */
  183. function _protected() {
  184. return true;
  185. }
  186. /**
  187. * redirect method overriding
  188. *
  189. * @access public
  190. * @return void
  191. */
  192. function redirect() {
  193. echo 'this should not be accessible';
  194. }
  195. }
  196. /**
  197. * OtherPagesController class
  198. *
  199. * @package cake
  200. * @subpackage cake.tests.cases
  201. */
  202. class OtherPagesController extends MyPluginAppController {
  203. /**
  204. * name property
  205. *
  206. * @var string 'OtherPages'
  207. * @access public
  208. */
  209. var $name = 'OtherPages';
  210. /**
  211. * uses property
  212. *
  213. * @var array
  214. * @access public
  215. */
  216. var $uses = array();
  217. /**
  218. * display method
  219. *
  220. * @param mixed $page
  221. * @access public
  222. * @return void
  223. */
  224. function display($page = null) {
  225. return $page;
  226. }
  227. /**
  228. * index method
  229. *
  230. * @access public
  231. * @return void
  232. */
  233. function index() {
  234. return true;
  235. }
  236. }
  237. /**
  238. * TestDispatchPagesController class
  239. *
  240. * @package cake
  241. * @subpackage cake.tests.cases
  242. */
  243. class TestDispatchPagesController extends AppController {
  244. /**
  245. * name property
  246. *
  247. * @var string 'TestDispatchPages'
  248. * @access public
  249. */
  250. var $name = 'TestDispatchPages';
  251. /**
  252. * uses property
  253. *
  254. * @var array
  255. * @access public
  256. */
  257. var $uses = array();
  258. /**
  259. * admin_index method
  260. *
  261. * @access public
  262. * @return void
  263. */
  264. function admin_index() {
  265. return true;
  266. }
  267. /**
  268. * camelCased method
  269. *
  270. * @access public
  271. * @return void
  272. */
  273. function camelCased() {
  274. return true;
  275. }
  276. }
  277. /**
  278. * ArticlesTestAppController class
  279. *
  280. * @package cake
  281. * @subpackage cake.tests.cases
  282. */
  283. class ArticlesTestAppController extends AppController {
  284. }
  285. /**
  286. * ArticlesTestController class
  287. *
  288. * @package cake
  289. * @subpackage cake.tests.cases
  290. */
  291. class ArticlesTestController extends ArticlesTestAppController {
  292. /**
  293. * name property
  294. *
  295. * @var string 'ArticlesTest'
  296. * @access public
  297. */
  298. var $name = 'ArticlesTest';
  299. /**
  300. * uses property
  301. *
  302. * @var array
  303. * @access public
  304. */
  305. var $uses = array();
  306. /**
  307. * admin_index method
  308. *
  309. * @access public
  310. * @return void
  311. */
  312. function admin_index() {
  313. return true;
  314. }
  315. }
  316. /**
  317. * SomePostsController class
  318. *
  319. * @package cake
  320. * @subpackage cake.tests.cases
  321. */
  322. class SomePostsController extends AppController {
  323. /**
  324. * name property
  325. *
  326. * @var string 'SomePosts'
  327. * @access public
  328. */
  329. var $name = 'SomePosts';
  330. /**
  331. * uses property
  332. *
  333. * @var array
  334. * @access public
  335. */
  336. var $uses = array();
  337. /**
  338. * autoRender property
  339. *
  340. * @var bool false
  341. * @access public
  342. */
  343. var $autoRender = false;
  344. /**
  345. * beforeFilter method
  346. *
  347. * @access public
  348. * @return void
  349. */
  350. function beforeFilter() {
  351. if ($this->params['action'] == 'index') {
  352. $this->params['action'] = 'view';
  353. } else {
  354. $this->params['action'] = 'change';
  355. }
  356. $this->params['pass'] = array('changed');
  357. }
  358. /**
  359. * index method
  360. *
  361. * @access public
  362. * @return void
  363. */
  364. function index() {
  365. return true;
  366. }
  367. /**
  368. * change method
  369. *
  370. * @access public
  371. * @return void
  372. */
  373. function change() {
  374. return true;
  375. }
  376. }
  377. /**
  378. * TestCachedPagesController class
  379. *
  380. * @package cake
  381. * @subpackage cake.tests.cases
  382. */
  383. class TestCachedPagesController extends AppController {
  384. /**
  385. * name property
  386. *
  387. * @var string 'TestCachedPages'
  388. * @access public
  389. */
  390. var $name = 'TestCachedPages';
  391. /**
  392. * uses property
  393. *
  394. * @var array
  395. * @access public
  396. */
  397. var $uses = array();
  398. /**
  399. * helpers property
  400. *
  401. * @var array
  402. * @access public
  403. */
  404. var $helpers = array('Cache');
  405. /**
  406. * cacheAction property
  407. *
  408. * @var array
  409. * @access public
  410. */
  411. var $cacheAction = array(
  412. 'index'=> '+2 sec', 'test_nocache_tags'=>'+2 sec',
  413. 'view/' => '+2 sec'
  414. );
  415. /**
  416. * viewPath property
  417. *
  418. * @var string 'posts'
  419. * @access public
  420. */
  421. var $viewPath = 'posts';
  422. /**
  423. * index method
  424. *
  425. * @access public
  426. * @return void
  427. */
  428. function index() {
  429. $this->render();
  430. }
  431. /**
  432. * test_nocache_tags method
  433. *
  434. * @access public
  435. * @return void
  436. */
  437. function test_nocache_tags() {
  438. $this->render();
  439. }
  440. /**
  441. * view method
  442. *
  443. * @access public
  444. * @return void
  445. */
  446. function view($id = null) {
  447. $this->render('index');
  448. }
  449. }
  450. /**
  451. * TimesheetsController class
  452. *
  453. * @package cake
  454. * @subpackage cake.tests.cases
  455. */
  456. class TimesheetsController extends AppController {
  457. /**
  458. * name property
  459. *
  460. * @var string 'Timesheets'
  461. * @access public
  462. */
  463. var $name = 'Timesheets';
  464. /**
  465. * uses property
  466. *
  467. * @var array
  468. * @access public
  469. */
  470. var $uses = array();
  471. /**
  472. * index method
  473. *
  474. * @access public
  475. * @return void
  476. */
  477. function index() {
  478. return true;
  479. }
  480. }
  481. /**
  482. * DispatcherTest class
  483. *
  484. * @package cake
  485. * @subpackage cake.tests.cases
  486. */
  487. class DispatcherTest extends CakeTestCase {
  488. /**
  489. * setUp method
  490. *
  491. * @access public
  492. * @return void
  493. */
  494. function startTest() {
  495. $this->_get = $_GET;
  496. $_GET = array();
  497. $this->_post = $_POST;
  498. $this->_files = $_FILES;
  499. $this->_server = $_SERVER;
  500. $this->_app = Configure::read('App');
  501. Configure::write('App.base', false);
  502. Configure::write('App.baseUrl', false);
  503. Configure::write('App.dir', 'app');
  504. Configure::write('App.webroot', 'webroot');
  505. $this->_cache = Configure::read('Cache');
  506. Configure::write('Cache.disable', true);
  507. $this->_vendorPaths = Configure::read('vendorPaths');
  508. $this->_pluginPaths = Configure::read('pluginPaths');
  509. $this->_viewPaths = Configure::read('viewPaths');
  510. $this->_controllerPaths = Configure::read('controllerPaths');
  511. $this->_debug = Configure::read('debug');
  512. Configure::write('controllerPaths', Configure::corePaths('controller'));
  513. Configure::write('viewPaths', Configure::corePaths('view'));
  514. }
  515. /**
  516. * tearDown method
  517. *
  518. * @access public
  519. * @return void
  520. */
  521. function endTest() {
  522. $_GET = $this->_get;
  523. $_POST = $this->_post;
  524. $_FILES = $this->_files;
  525. $_SERVER = $this->_server;
  526. Configure::write('App', $this->_app);
  527. Configure::write('Cache', $this->_cache);
  528. Configure::write('vendorPaths', $this->_vendorPaths);
  529. Configure::write('pluginPaths', $this->_pluginPaths);
  530. Configure::write('viewPaths', $this->_viewPaths);
  531. Configure::write('controllerPaths', $this->_controllerPaths);
  532. Configure::write('debug', $this->_debug);
  533. }
  534. /**
  535. * testParseParamsWithoutZerosAndEmptyPost method
  536. *
  537. * @access public
  538. * @return void
  539. */
  540. function testParseParamsWithoutZerosAndEmptyPost() {
  541. $Dispatcher =& new Dispatcher();
  542. $test = $Dispatcher->parseParams("/testcontroller/testaction/params1/params2/params3");
  543. $this->assertIdentical($test['controller'], 'testcontroller');
  544. $this->assertIdentical($test['action'], 'testaction');
  545. $this->assertIdentical($test['pass'][0], 'params1');
  546. $this->assertIdentical($test['pass'][1], 'params2');
  547. $this->assertIdentical($test['pass'][2], 'params3');
  548. $this->assertFalse(!empty($test['form']));
  549. }
  550. /**
  551. * testParseParamsReturnsPostedData method
  552. *
  553. * @access public
  554. * @return void
  555. */
  556. function testParseParamsReturnsPostedData() {
  557. $_POST['testdata'] = "My Posted Content";
  558. $Dispatcher =& new Dispatcher();
  559. $test = $Dispatcher->parseParams("/");
  560. $this->assertTrue($test['form'], "Parsed URL not returning post data");
  561. $this->assertIdentical($test['form']['testdata'], "My Posted Content");
  562. }
  563. /**
  564. * testParseParamsWithSingleZero method
  565. *
  566. * @access public
  567. * @return void
  568. */
  569. function testParseParamsWithSingleZero() {
  570. $Dispatcher =& new Dispatcher();
  571. $test = $Dispatcher->parseParams("/testcontroller/testaction/1/0/23");
  572. $this->assertIdentical($test['controller'], 'testcontroller');
  573. $this->assertIdentical($test['action'], 'testaction');
  574. $this->assertIdentical($test['pass'][0], '1');
  575. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][1]);
  576. $this->assertIdentical($test['pass'][2], '23');
  577. }
  578. /**
  579. * testParseParamsWithManySingleZeros method
  580. *
  581. * @access public
  582. * @return void
  583. */
  584. function testParseParamsWithManySingleZeros() {
  585. $Dispatcher =& new Dispatcher();
  586. $test = $Dispatcher->parseParams("/testcontroller/testaction/0/0/0/0/0/0");
  587. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][0]);
  588. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][1]);
  589. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][2]);
  590. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][3]);
  591. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][4]);
  592. $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][5]);
  593. }
  594. /**
  595. * testParseParamsWithManyZerosInEachSectionOfUrl method
  596. *
  597. * @access public
  598. * @return void
  599. */
  600. function testParseParamsWithManyZerosInEachSectionOfUrl() {
  601. $Dispatcher =& new Dispatcher();
  602. $test = $Dispatcher->parseParams("/testcontroller/testaction/000/0000/00000/000000/000000/0000000");
  603. $this->assertPattern('/\\A(?:000)\\z/', $test['pass'][0]);
  604. $this->assertPattern('/\\A(?:0000)\\z/', $test['pass'][1]);
  605. $this->assertPattern('/\\A(?:00000)\\z/', $test['pass'][2]);
  606. $this->assertPattern('/\\A(?:000000)\\z/', $test['pass'][3]);
  607. $this->assertPattern('/\\A(?:000000)\\z/', $test['pass'][4]);
  608. $this->assertPattern('/\\A(?:0000000)\\z/', $test['pass'][5]);
  609. }
  610. /**
  611. * testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl method
  612. *
  613. * @access public
  614. * @return void
  615. */
  616. function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() {
  617. $Dispatcher =& new Dispatcher();
  618. $test = $Dispatcher->parseParams("/testcontroller/testaction/01/0403/04010/000002/000030/0000400");
  619. $this->assertPattern('/\\A(?:01)\\z/', $test['pass'][0]);
  620. $this->assertPattern('/\\A(?:0403)\\z/', $test['pass'][1]);
  621. $this->assertPattern('/\\A(?:04010)\\z/', $test['pass'][2]);
  622. $this->assertPattern('/\\A(?:000002)\\z/', $test['pass'][3]);
  623. $this->assertPattern('/\\A(?:000030)\\z/', $test['pass'][4]);
  624. $this->assertPattern('/\\A(?:0000400)\\z/', $test['pass'][5]);
  625. }
  626. /**
  627. * testQueryStringOnRoot method
  628. *
  629. * @access public
  630. * @return void
  631. */
  632. function testQueryStringOnRoot() {
  633. Router::reload();
  634. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  635. Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
  636. $_GET = array('coffee' => 'life', 'sleep' => 'sissies');
  637. $Dispatcher =& new Dispatcher();
  638. $uri = 'posts/home/?coffee=life&sleep=sissies';
  639. $result = $Dispatcher->parseParams($uri);
  640. $this->assertPattern('/posts/', $result['controller']);
  641. $this->assertPattern('/home/', $result['action']);
  642. $this->assertTrue(isset($result['url']['sleep']));
  643. $this->assertTrue(isset($result['url']['coffee']));
  644. $Dispatcher =& new Dispatcher();
  645. $uri = '/?coffee=life&sleep=sissy';
  646. $result = $Dispatcher->parseParams($uri);
  647. $this->assertPattern('/pages/', $result['controller']);
  648. $this->assertPattern('/display/', $result['action']);
  649. $this->assertTrue(isset($result['url']['sleep']));
  650. $this->assertTrue(isset($result['url']['coffee']));
  651. $this->assertEqual($result['url']['coffee'], 'life');
  652. }
  653. /**
  654. * testFileUploadArrayStructure method
  655. *
  656. * @access public
  657. * @return void
  658. */
  659. function testFileUploadArrayStructure() {
  660. $_FILES = array('data' => array('name' => array(
  661. 'File' => array(
  662. array('data' => 'cake_mssql_patch.patch'),
  663. array('data' => 'controller.diff'),
  664. array('data' => ''),
  665. array('data' => ''),
  666. ),
  667. 'Post' => array('attachment' => 'jquery-1.2.1.js'),
  668. ),
  669. 'type' => array(
  670. 'File' => array(
  671. array('data' => ''),
  672. array('data' => ''),
  673. array('data' => ''),
  674. array('data' => ''),
  675. ),
  676. 'Post' => array('attachment' => 'application/x-javascript'),
  677. ),
  678. 'tmp_name' => array(
  679. 'File' => array(
  680. array('data' => '/private/var/tmp/phpy05Ywj'),
  681. array('data' => '/private/var/tmp/php7MBztY'),
  682. array('data' => ''),
  683. array('data' => ''),
  684. ),
  685. 'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
  686. ),
  687. 'error' => array(
  688. 'File' => array(
  689. array('data' => 0),
  690. array('data' => 0),
  691. array('data' => 4),
  692. array('data' => 4)
  693. ),
  694. 'Post' => array('attachment' => 0)
  695. ),
  696. 'size' => array(
  697. 'File' => array(
  698. array('data' => 6271),
  699. array('data' => 350),
  700. array('data' => 0),
  701. array('data' => 0),
  702. ),
  703. 'Post' => array('attachment' => 80469)
  704. ),
  705. ));
  706. $Dispatcher =& new Dispatcher();
  707. $result = $Dispatcher->parseParams('/');
  708. $expected = array(
  709. 'File' => array(
  710. array('data' => array(
  711. 'name' => 'cake_mssql_patch.patch',
  712. 'type' => '',
  713. 'tmp_name' => '/private/var/tmp/phpy05Ywj',
  714. 'error' => 0,
  715. 'size' => 6271,
  716. ),
  717. ),
  718. array('data' => array(
  719. 'name' => 'controller.diff',
  720. 'type' => '',
  721. 'tmp_name' => '/private/var/tmp/php7MBztY',
  722. 'error' => 0,
  723. 'size' => 350,
  724. )),
  725. array('data' => array(
  726. 'name' => '',
  727. 'type' => '',
  728. 'tmp_name' => '',
  729. 'error' => 4,
  730. 'size' => 0,
  731. )),
  732. array('data' => array(
  733. 'name' => '',
  734. 'type' => '',
  735. 'tmp_name' => '',
  736. 'error' => 4,
  737. 'size' => 0,
  738. )),
  739. ),
  740. 'Post' => array('attachment' => array(
  741. 'name' => 'jquery-1.2.1.js',
  742. 'type' => 'application/x-javascript',
  743. 'tmp_name' => '/private/var/tmp/phpEwlrIo',
  744. 'error' => 0,
  745. 'size' => 80469,
  746. )));
  747. $this->assertEqual($result['data'], $expected);
  748. $_FILES = array(
  749. 'data' => array(
  750. 'name' => array(
  751. 'Document' => array(
  752. 1 => array(
  753. 'birth_cert' => 'born on.txt',
  754. 'passport' => 'passport.txt',
  755. 'drivers_license' => 'ugly pic.jpg'
  756. ),
  757. 2 => array(
  758. 'birth_cert' => 'aunt betty.txt',
  759. 'passport' => 'betty-passport.txt',
  760. 'drivers_license' => 'betty-photo.jpg'
  761. ),
  762. ),
  763. ),
  764. 'type' => array(
  765. 'Document' => array(
  766. 1 => array(
  767. 'birth_cert' => 'application/octet-stream',
  768. 'passport' => 'application/octet-stream',
  769. 'drivers_license' => 'application/octet-stream',
  770. ),
  771. 2 => array(
  772. 'birth_cert' => 'application/octet-stream',
  773. 'passport' => 'application/octet-stream',
  774. 'drivers_license' => 'application/octet-stream',
  775. )
  776. )
  777. ),
  778. 'tmp_name' => array(
  779. 'Document' => array(
  780. 1 => array(
  781. 'birth_cert' => '/private/var/tmp/phpbsUWfH',
  782. 'passport' => '/private/var/tmp/php7f5zLt',
  783. 'drivers_license' => '/private/var/tmp/phpMXpZgT',
  784. ),
  785. 2 => array(
  786. 'birth_cert' => '/private/var/tmp/php5kHZt0',
  787. 'passport' => '/private/var/tmp/phpnYkOuM',
  788. 'drivers_license' => '/private/var/tmp/php9Rq0P3',
  789. )
  790. )
  791. ),
  792. 'error' => array(
  793. 'Document' => array(
  794. 1 => array(
  795. 'birth_cert' => 0,
  796. 'passport' => 0,
  797. 'drivers_license' => 0,
  798. ),
  799. 2 => array(
  800. 'birth_cert' => 0,
  801. 'passport' => 0,
  802. 'drivers_license' => 0,
  803. )
  804. )
  805. ),
  806. 'size' => array(
  807. 'Document' => array(
  808. 1 => array(
  809. 'birth_cert' => 123,
  810. 'passport' => 458,
  811. 'drivers_license' => 875,
  812. ),
  813. 2 => array(
  814. 'birth_cert' => 876,
  815. 'passport' => 976,
  816. 'drivers_license' => 9783,
  817. )
  818. )
  819. )
  820. )
  821. );
  822. $Dispatcher =& new Dispatcher();
  823. $result = $Dispatcher->parseParams('/');
  824. $expected = array(
  825. 'Document' => array(
  826. 1 => array(
  827. 'birth_cert' => array(
  828. 'name' => 'born on.txt',
  829. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  830. 'error' => 0,
  831. 'size' => 123,
  832. 'type' => 'application/octet-stream',
  833. ),
  834. 'passport' => array(
  835. 'name' => 'passport.txt',
  836. 'tmp_name' => '/private/var/tmp/php7f5zLt',
  837. 'error' => 0,
  838. 'size' => 458,
  839. 'type' => 'application/octet-stream',
  840. ),
  841. 'drivers_license' => array(
  842. 'name' => 'ugly pic.jpg',
  843. 'tmp_name' => '/private/var/tmp/phpMXpZgT',
  844. 'error' => 0,
  845. 'size' => 875,
  846. 'type' => 'application/octet-stream',
  847. ),
  848. ),
  849. 2 => array(
  850. 'birth_cert' => array(
  851. 'name' => 'aunt betty.txt',
  852. 'tmp_name' => '/private/var/tmp/php5kHZt0',
  853. 'error' => 0,
  854. 'size' => 876,
  855. 'type' => 'application/octet-stream',
  856. ),
  857. 'passport' => array(
  858. 'name' => 'betty-passport.txt',
  859. 'tmp_name' => '/private/var/tmp/phpnYkOuM',
  860. 'error' => 0,
  861. 'size' => 976,
  862. 'type' => 'application/octet-stream',
  863. ),
  864. 'drivers_license' => array(
  865. 'name' => 'betty-photo.jpg',
  866. 'tmp_name' => '/private/var/tmp/php9Rq0P3',
  867. 'error' => 0,
  868. 'size' => 9783,
  869. 'type' => 'application/octet-stream',
  870. ),
  871. ),
  872. )
  873. );
  874. $this->assertEqual($result['data'], $expected);
  875. $_FILES = array(
  876. 'data' => array(
  877. 'name' => array('birth_cert' => 'born on.txt'),
  878. 'type' => array('birth_cert' => 'application/octet-stream'),
  879. 'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
  880. 'error' => array('birth_cert' => 0),
  881. 'size' => array('birth_cert' => 123)
  882. )
  883. );
  884. $Dispatcher =& new Dispatcher();
  885. $result = $Dispatcher->parseParams('/');
  886. $expected = array(
  887. 'birth_cert' => array(
  888. 'name' => 'born on.txt',
  889. 'type' => 'application/octet-stream',
  890. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  891. 'error' => 0,
  892. 'size' => 123
  893. )
  894. );
  895. $this->assertEqual($result['data'], $expected);
  896. }
  897. /**
  898. * testGetUrl method
  899. *
  900. * @access public
  901. * @return void
  902. */
  903. function testGetUrl() {
  904. $Dispatcher =& new Dispatcher();
  905. $Dispatcher->base = '/app/webroot/index.php';
  906. $uri = '/app/webroot/index.php/posts/add';
  907. $result = $Dispatcher->getUrl($uri);
  908. $expected = 'posts/add';
  909. $this->assertEqual($expected, $result);
  910. Configure::write('App.baseUrl', '/app/webroot/index.php');
  911. $uri = '/posts/add';
  912. $result = $Dispatcher->getUrl($uri);
  913. $expected = 'posts/add';
  914. $this->assertEqual($expected, $result);
  915. $_GET['url'] = array();
  916. Configure::write('App.base', '/control');
  917. $Dispatcher =& new Dispatcher();
  918. $Dispatcher->baseUrl();
  919. $uri = '/control/students/browse';
  920. $result = $Dispatcher->getUrl($uri);
  921. $expected = 'students/browse';
  922. $this->assertEqual($expected, $result);
  923. $_GET['url'] = array();
  924. $Dispatcher =& new Dispatcher();
  925. $Dispatcher->base = '';
  926. $uri = '/?/home';
  927. $result = $Dispatcher->getUrl($uri);
  928. $expected = '?/home';
  929. $this->assertEqual($expected, $result);
  930. }
  931. /**
  932. * testBaseUrlAndWebrootWithModRewrite method
  933. *
  934. * @access public
  935. * @return void
  936. */
  937. function testBaseUrlAndWebrootWithModRewrite() {
  938. $Dispatcher =& new Dispatcher();
  939. $Dispatcher->base = false;
  940. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  941. $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/app/webroot/index.php';
  942. $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
  943. $result = $Dispatcher->baseUrl();
  944. $expected = '/1.2.x.x';
  945. $this->assertEqual($expected, $result);
  946. $expectedWebroot = '/1.2.x.x/';
  947. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  948. $Dispatcher->base = false;
  949. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
  950. $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/app/webroot/index.php';
  951. $_SERVER['PHP_SELF'] = '/index.php';
  952. $result = $Dispatcher->baseUrl();
  953. $expected = '';
  954. $this->assertEqual($expected, $result);
  955. $expectedWebroot = '/';
  956. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  957. $Dispatcher->base = false;
  958. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  959. $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/test/webroot/index.php';
  960. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  961. $result = $Dispatcher->baseUrl();
  962. $expected = '';
  963. $this->assertEqual($expected, $result);
  964. $expectedWebroot = '/';
  965. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  966. $Dispatcher->base = false;;
  967. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  968. $_SERVER['SCRIPT_FILENAME'] = '/some/apps/where/app/webroot/index.php';
  969. $_SERVER['PHP_SELF'] = '/some/apps/where/app/webroot/index.php';
  970. $result = $Dispatcher->baseUrl();
  971. $expected = '/some/apps/where';
  972. $this->assertEqual($expected, $result);
  973. $expectedWebroot = '/some/apps/where/';
  974. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  975. Configure::write('App.dir', 'auth');
  976. $Dispatcher->base = false;;
  977. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  978. $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/demos/auth/webroot/index.php';
  979. $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
  980. $result = $Dispatcher->baseUrl();
  981. $expected = '/demos/auth';
  982. $this->assertEqual($expected, $result);
  983. $expectedWebroot = '/demos/auth/';
  984. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  985. Configure::write('App.dir', 'code');
  986. $Dispatcher->base = false;;
  987. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  988. $_SERVER['SCRIPT_FILENAME'] = '/Library/WebServer/Documents/clients/PewterReport/code/webroot/index.php';
  989. $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
  990. $result = $Dispatcher->baseUrl();
  991. $expected = '/clients/PewterReport/code';
  992. $this->assertEqual($expected, $result);
  993. $expectedWebroot = '/clients/PewterReport/code/';
  994. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  995. }
  996. /**
  997. * testBaseUrlwithModRewriteAlias method
  998. *
  999. * @access public
  1000. * @return void
  1001. */
  1002. function testBaseUrlwithModRewriteAlias() {
  1003. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  1004. $_SERVER['SCRIPT_FILENAME'] = '/home/aplusnur/cake2/app/webroot/index.php';
  1005. $_SERVER['PHP_SELF'] = '/control/index.php';
  1006. Configure::write('App.base', '/control');
  1007. $Dispatcher =& new Dispatcher();
  1008. $result = $Dispatcher->baseUrl();
  1009. $expected = '/control';
  1010. $this->assertEqual($expected, $result);
  1011. $expectedWebroot = '/control/';
  1012. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1013. Configure::write('App.base', false);
  1014. Configure::write('App.dir', 'affiliate');
  1015. Configure::write('App.webroot', 'newaffiliate');
  1016. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  1017. $_SERVER['SCRIPT_FILENAME'] = '/var/www/abtravaff/html/newaffiliate/index.php';
  1018. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  1019. $Dispatcher =& new Dispatcher();
  1020. $result = $Dispatcher->baseUrl();
  1021. $expected = '/newaffiliate';
  1022. $this->assertEqual($expected, $result);
  1023. $expectedWebroot = '/newaffiliate/';
  1024. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1025. }
  1026. /**
  1027. * testBaseUrlAndWebrootWithBaseUrl method
  1028. *
  1029. * @access public
  1030. * @return void
  1031. */
  1032. function testBaseUrlAndWebrootWithBaseUrl() {
  1033. $Dispatcher =& new Dispatcher();
  1034. Configure::write('App.dir', 'app');
  1035. Configure::write('App.baseUrl', '/app/webroot/index.php');
  1036. $result = $Dispatcher->baseUrl();
  1037. $expected = '/app/webroot/index.php';
  1038. $this->assertEqual($expected, $result);
  1039. $expectedWebroot = '/app/webroot/';
  1040. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1041. Configure::write('App.baseUrl', '/app/webroot/test.php');
  1042. $result = $Dispatcher->baseUrl();
  1043. $expected = '/app/webroot/test.php';
  1044. $this->assertEqual($expected, $result);
  1045. $expectedWebroot = '/app/webroot/';
  1046. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1047. Configure::write('App.baseUrl', '/app/index.php');
  1048. $result = $Dispatcher->baseUrl();
  1049. $expected = '/app/index.php';
  1050. $this->assertEqual($expected, $result);
  1051. $expectedWebroot = '/app/webroot/';
  1052. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1053. Configure::write('App.baseUrl', '/index.php');
  1054. $result = $Dispatcher->baseUrl();
  1055. $expected = '/index.php';
  1056. $this->assertEqual($expected, $result);
  1057. $expectedWebroot = '/app/webroot/';
  1058. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1059. Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
  1060. $result = $Dispatcher->baseUrl();
  1061. $expected = '/CakeBB/app/webroot/index.php';
  1062. $this->assertEqual($expected, $result);
  1063. $expectedWebroot = '/CakeBB/app/webroot/';
  1064. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1065. Configure::write('App.baseUrl', '/CakeBB/app/index.php');
  1066. $result = $Dispatcher->baseUrl();
  1067. $expected = '/CakeBB/app/index.php';
  1068. $this->assertEqual($expected, $result);
  1069. $expectedWebroot = '/CakeBB/app/webroot/';
  1070. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1071. Configure::write('App.baseUrl', '/CakeBB/index.php');
  1072. $result = $Dispatcher->baseUrl();
  1073. $expected = '/CakeBB/index.php';
  1074. $this->assertEqual($expected, $result);
  1075. $expectedWebroot = '/CakeBB/app/webroot/';
  1076. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1077. Configure::write('App.baseUrl', '/dbhauser/index.php');
  1078. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  1079. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  1080. $result = $Dispatcher->baseUrl();
  1081. $expected = '/dbhauser/index.php';
  1082. $this->assertEqual($expected, $result);
  1083. $expectedWebroot = '/dbhauser/app/webroot/';
  1084. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1085. }
  1086. /**
  1087. * testBaseUrlAndWebrootWithBase method
  1088. *
  1089. * @access public
  1090. * @return void
  1091. */
  1092. function testBaseUrlAndWebrootWithBase() {
  1093. $Dispatcher =& new Dispatcher();
  1094. $Dispatcher->base = '/app';
  1095. $result = $Dispatcher->baseUrl();
  1096. $expected = '/app';
  1097. $this->assertEqual($expected, $result);
  1098. $expectedWebroot = '/app/';
  1099. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1100. $Dispatcher->base = '';
  1101. $result = $Dispatcher->baseUrl();
  1102. $expected = '';
  1103. $this->assertEqual($expected, $result);
  1104. $expectedWebroot = '/';
  1105. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1106. Configure::write('App.dir', 'testbed');
  1107. $Dispatcher->base = '/cake/testbed/webroot';
  1108. $result = $Dispatcher->baseUrl();
  1109. $expected = '/cake/testbed/webroot';
  1110. $this->assertEqual($expected, $result);
  1111. $expectedWebroot = '/cake/testbed/webroot/';
  1112. $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
  1113. }
  1114. /**
  1115. * testMissingController method
  1116. *
  1117. * @access public
  1118. * @return void
  1119. */
  1120. function testMissingController() {
  1121. $Dispatcher =& new TestDispatcher();
  1122. Configure::write('App.baseUrl','/index.php');
  1123. $url = 'some_controller/home/param:value/param2:value2';
  1124. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1125. $expected = array('missingController', array(array(
  1126. 'className' => 'SomeControllerController',
  1127. 'webroot' => '/app/webroot/',
  1128. 'url' => 'some_controller/home/param:value/param2:value2',
  1129. 'base' => '/index.php'
  1130. )));
  1131. $this->assertEqual($expected, $controller);
  1132. }
  1133. /**
  1134. * testPrivate method
  1135. *
  1136. * @access public
  1137. * @return void
  1138. */
  1139. function testPrivate() {
  1140. $Dispatcher =& new TestDispatcher();
  1141. Configure::write('App.baseUrl','/index.php');
  1142. $url = 'some_pages/_protected/param:value/param2:value2';
  1143. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1144. $expected = array('privateAction', array(array(
  1145. 'className' => 'SomePagesController',
  1146. 'action' => '_protected',
  1147. 'webroot' => '/app/webroot/',
  1148. 'url' => 'some_pages/_protected/param:value/param2:value2',
  1149. 'base' => '/index.php'
  1150. )));
  1151. $this->assertEqual($controller, $expected);
  1152. }
  1153. /**
  1154. * testMissingAction method
  1155. *
  1156. * @access public
  1157. * @return void
  1158. */
  1159. function testMissingAction() {
  1160. $Dispatcher =& new TestDispatcher();
  1161. Configure::write('App.baseUrl','/index.php');
  1162. $url = 'some_pages/home/param:value/param2:value2';
  1163. $controller = $Dispatcher->dispatch($url, array('return'=> 1));
  1164. $expected = array('missingAction', array(array(
  1165. 'className' => 'SomePagesController',
  1166. 'action' => 'home',
  1167. 'webroot' => '/app/webroot/',
  1168. 'url' => '/index.php/some_pages/home/param:value/param2:value2',
  1169. 'base' => '/index.php'
  1170. )));
  1171. $this->assertEqual($expected, $controller);
  1172. $Dispatcher =& new TestDispatcher();
  1173. Configure::write('App.baseUrl','/index.php');
  1174. $url = 'some_pages/redirect/param:value/param2:value2';
  1175. $controller = $Dispatcher->dispatch($url, array('return'=> 1));
  1176. $expected = array('missingAction', array(array(
  1177. 'className' => 'SomePagesController',
  1178. 'action' => 'redirect',
  1179. 'webroot' => '/app/webroot/',
  1180. 'url' => '/index.php/some_pages/redirect/param:value/param2:value2',
  1181. 'base' => '/index.php'
  1182. )));
  1183. $this->assertEqual($expected, $controller);
  1184. }
  1185. /**
  1186. * testDispatch method
  1187. *
  1188. * @access public
  1189. * @return void
  1190. */
  1191. function testDispatch() {
  1192. $Dispatcher =& new TestDispatcher();
  1193. Configure::write('App.baseUrl','/index.php');
  1194. $url = 'pages/home/param:value/param2:value2';
  1195. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1196. $expected = 'Pages';
  1197. $this->assertEqual($expected, $controller->name);
  1198. $expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2');
  1199. $this->assertIdentical($expected, $controller->passedArgs);
  1200. Configure::write('App.baseUrl','/pages/index.php');
  1201. $url = 'pages/home';
  1202. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1203. $expected = 'Pages';
  1204. $this->assertEqual($expected, $controller->name);
  1205. $url = 'pages/home/';
  1206. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1207. $expected = 'Pages';
  1208. $this->assertEqual($expected, $controller->name);
  1209. unset($Dispatcher);
  1210. $Dispatcher =& new TestDispatcher();
  1211. Configure::write('App.baseUrl','/timesheets/index.php');
  1212. $url = 'timesheets';
  1213. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1214. $expected = 'Timesheets';
  1215. $this->assertEqual($expected, $controller->name);
  1216. $url = 'timesheets/';
  1217. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1218. $this->assertEqual('Timesheets', $controller->name);
  1219. $this->assertEqual('/timesheets/index.php', $Dispatcher->base);
  1220. $url = 'test_dispatch_pages/camelCased';
  1221. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1222. $this->assertEqual('TestDispatchPages', $controller->name);
  1223. }
  1224. /**
  1225. * testDispatchWithArray method
  1226. *
  1227. * @access public
  1228. * @return void
  1229. */
  1230. function testDispatchWithArray() {
  1231. $Dispatcher =& new TestDispatcher();
  1232. Configure::write('App.baseUrl','/index.php');
  1233. $url = 'pages/home/param:value/param2:value2';
  1234. $url = array('controller' => 'pages', 'action' => 'display');
  1235. $controller = $Dispatcher->dispatch($url, array('pass' => array('home'), 'named' => array('param' => 'value', 'param2' => 'value2'), 'return' => 1));
  1236. $expected = 'Pages';
  1237. $this->assertEqual($expected, $controller->name);
  1238. $expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2');
  1239. $this->assertIdentical($expected, $controller->passedArgs);
  1240. }
  1241. /**
  1242. * testAdminDispatch method
  1243. *
  1244. * @access public
  1245. * @return void
  1246. */
  1247. function testAdminDispatch() {
  1248. $_POST = array();
  1249. $Dispatcher =& new TestDispatcher();
  1250. Configure::write('Routing.admin', 'admin');
  1251. Configure::write('App.baseUrl','/cake/repo/branches/1.2.x.x/index.php');
  1252. $url = 'admin/test_dispatch_pages/index/param:value/param2:value2';
  1253. Router::reload();
  1254. $Router =& Router::getInstance();
  1255. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1256. $expected = 'TestDispatchPages';
  1257. $this->assertEqual($expected, $controller->name);
  1258. $expected = array('param' => 'value', 'param2' => 'value2');
  1259. $this->assertIdentical($expected, $controller->passedArgs);
  1260. $this->assertTrue($controller->params['admin']);
  1261. $expected = '/cake/repo/branches/1.2.x.x/index.php/admin/test_dispatch_pages/index/param:value/param2:value2';
  1262. $this->assertIdentical($expected, $controller->here);
  1263. $expected = '/cake/repo/branches/1.2.x.x/index.php';
  1264. $this->assertIdentical($expected, $controller->base);
  1265. }
  1266. /**
  1267. * testPluginDispatch method
  1268. *
  1269. * @access public
  1270. * @return void
  1271. */
  1272. function testPluginDispatch() {
  1273. $_POST = array();
  1274. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1275. Router::reload();
  1276. $Dispatcher =& new TestDispatcher();
  1277. Router::connect('/my_plugin/:controller/*', array('plugin'=>'my_plugin', 'controller'=>'pages', 'action'=>'display'));
  1278. $Dispatcher->base = false;
  1279. $url = 'my_plugin/some_pages/home/param:value/param2:value2';
  1280. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1281. $result = $Dispatcher->parseParams($url);
  1282. $expected = array(
  1283. 'pass' => array('home'),
  1284. 'named' => array('param'=> 'value', 'param2'=> 'value2'), 'plugin'=> 'my_plugin',
  1285. 'controller'=> 'some_pages', 'action'=> 'display', 'form'=> null,
  1286. 'url'=> array('url'=> 'my_plugin/some_pages/home/param:value/param2:value2'),
  1287. );
  1288. ksort($expected);
  1289. ksort($result);
  1290. $this->assertEqual($expected, $result);
  1291. $expected = 'my_plugin';
  1292. $this->assertIdentical($expected, $controller->plugin);
  1293. $expected = 'SomePages';
  1294. $this->assertIdentical($expected, $controller->name);
  1295. $expected = 'some_pages';
  1296. $this->assertIdentical($expected, $controller->params['controller']);
  1297. $expected = array('0' => 'home', 'param'=>'value', 'param2'=>'value2');
  1298. $this->assertIdentical($expected, $controller->passedArgs);
  1299. $expected = '/cake/repo/branches/1.2.x.x/my_plugin/some_pages/home/param:value/param2:value2';
  1300. $this->assertIdentical($expected, $controller->here);
  1301. $expected = '/cake/repo/branches/1.2.x.x';
  1302. $this->assertIdentical($expected, $controller->base);
  1303. }
  1304. /**
  1305. * testAutomaticPluginDispatch method
  1306. *
  1307. * @access public
  1308. * @return void
  1309. */
  1310. function testAutomaticPluginDispatch() {
  1311. $_POST = array();
  1312. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1313. Router::reload();
  1314. $Dispatcher =& new TestDispatcher();
  1315. Router::connect('/my_plugin/:controller/:action/*', array('plugin'=>'my_plugin', 'controller'=>'pages', 'action'=>'display'));
  1316. $Dispatcher->base = false;
  1317. $url = 'my_plugin/other_pages/index/param:value/param2:value2';
  1318. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1319. $expected = 'my_plugin';
  1320. $this->assertIdentical($expected, $controller->plugin);
  1321. $expected = 'OtherPages';
  1322. $this->assertIdentical($expected, $controller->name);
  1323. $expected = 'index';
  1324. $this->assertIdentical($expected, $controller->action);
  1325. $expected = array('param' => 'value', 'param2' => 'value2');
  1326. $this->assertIdentical($expected, $controller->passedArgs);
  1327. $expected = '/cake/repo/branches/1.2.x.x/my_plugin/other_pages/index/param:value/param2:value2';
  1328. $this->assertIdentical($expected, $controller->here);
  1329. $expected = '/cake/repo/branches/1.2.x.x';
  1330. $this->assertIdentical($expected, $controller->base);
  1331. }
  1332. /**
  1333. * testAutomaticPluginControllerDispatch method
  1334. *
  1335. * @access public
  1336. * @return void
  1337. */
  1338. function testAutomaticPluginControllerDispatch() {
  1339. $_POST = array();
  1340. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1341. Router::reload();
  1342. $Dispatcher =& new TestDispatcher();
  1343. $Dispatcher->base = false;
  1344. $url = 'my_plugin/add/param:value/param2:value2';
  1345. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1346. $expected = 'my_plugin';
  1347. $this->assertIdentical($controller->plugin, $expected);
  1348. $expected = 'MyPlugin';
  1349. $this->assertIdentical($controller->name, $expected);
  1350. $expected = 'add';
  1351. $this->assertIdentical($controller->action, $expected);
  1352. $expected = array('param' => 'value', 'param2' => 'value2');
  1353. $this->assertEqual($controller->params['named'], $expected);
  1354. Router::reload();
  1355. $Dispatcher =& new TestDispatcher();
  1356. $Dispatcher->base = false;
  1357. /* Simulates the Route for a real plugin, installed in APP/plugins */
  1358. Router::connect('/my_plugin/:controller/:action/*', array('plugin' => 'my_plugin'));
  1359. $plugin = 'MyPlugin';
  1360. $pluginUrl = Inflector::underscore($plugin);
  1361. $url = $pluginUrl;
  1362. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1363. $expected = $pluginUrl;
  1364. $this->assertIdentical($controller->plugin, $expected);
  1365. $expected = $plugin;
  1366. $this->assertIdentical($controller->name, $expected);
  1367. $expected = 'index';
  1368. $this->assertIdentical($controller->action, $expected);
  1369. $expected = $pluginUrl;
  1370. $this->assertEqual($controller->params['controller'], $expected);
  1371. Configure::write('Routing.admin', 'admin');
  1372. Router::reload();
  1373. $Dispatcher =& new TestDispatcher();
  1374. $Dispatcher->base = false;
  1375. $url = 'admin/my_plugin/add/5/param:value/param2:value2';
  1376. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1377. $expected = 'my_plugin';
  1378. $this->assertIdentical($controller->plugin, $expected);
  1379. $expected = 'MyPlugin';
  1380. $this->assertIdentical($controller->name, $expected);
  1381. $expected = 'admin_add';
  1382. $this->assertIdentical($controller->action, $expected);
  1383. $expected = array(0 => 5, 'param'=>'value', 'param2'=>'value2');
  1384. $this->assertEqual($controller->passedArgs, $expected);
  1385. Router::reload();
  1386. $Dispatcher =& new TestDispatcher();
  1387. $Dispatcher->base = false;
  1388. $controller = $Dispatcher->dispatch('admin/articles_test', array('return' => 1));
  1389. $expected = 'articles_test';
  1390. $this->assertIdentical($controller->plugin, $expected);
  1391. $expected = 'ArticlesTest';
  1392. $this->assertIdentical($controller->name, $expected);
  1393. $expected = 'admin_index';
  1394. $this->assertIdentical($controller->action, $expected);
  1395. $expected = array(
  1396. 'pass'=> array(), 'named' => array(), 'controller' => 'articles_test', 'plugin' => 'articles_test', 'action' => 'admin_index',
  1397. 'prefix' => 'admin', 'admin' => true, 'form' => array(), 'url' => array('url' => 'admin/articles_test'), 'return' => 1
  1398. );
  1399. $this->assertEqual($controller->params, $expected);
  1400. }
  1401. /**
  1402. * test Plugin dispatching without controller name and using
  1403. * plugin short form instead.
  1404. *
  1405. * @return void
  1406. **/
  1407. function testAutomaticPluginDispatchWithShortAccess() {
  1408. $_POST = array();
  1409. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1410. Router::reload();
  1411. Router::connect('/my_plugin/:controller/:action/*', array('plugin'=>'my_plugin'));
  1412. $Dispatcher =& new TestDispatcher();
  1413. $Dispatcher->base = false;
  1414. $url = 'my_plugin/my_plugin/add';
  1415. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1416. $this->assertFalse(isset($controller->params['pass'][0]));
  1417. $Dispatcher =& new TestDispatcher();
  1418. $Dispatcher->base = false;
  1419. $url = 'my_plugin/my_plugin/add/0';
  1420. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1421. $this->assertTrue(isset($controller->params['pass'][0]));
  1422. $Dispatcher =& new TestDispatcher();
  1423. $Dispatcher->base = false;
  1424. $url = 'my_plugin/add';
  1425. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1426. $this->assertFalse(isset($controller->params['pass'][0]));
  1427. $Dispatcher =& new TestDispatcher();
  1428. $Dispatcher->base = false;
  1429. $url = 'my_plugin/add/0';
  1430. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1431. $this->assertIdentical('0',$controller->params['pass'][0]);
  1432. $Dispatcher =& new TestDispatcher();
  1433. $Dispatcher->base = false;
  1434. $url = 'my_plugin/add/1';
  1435. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1436. $this->assertIdentical('1',$controller->params['pass'][0]);
  1437. }
  1438. /**
  1439. * testAutomaticPluginControllerMissingActionDispatch method
  1440. *
  1441. * @access public
  1442. * @return void
  1443. */
  1444. function testAutomaticPluginControllerMissingActionDispatch() {
  1445. $_POST = array();
  1446. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1447. Router::reload();
  1448. $Dispatcher =& new TestDispatcher();
  1449. $Dispatcher->base = false;
  1450. $url = 'my_plugin/not_here/param:value/param2:value2';
  1451. $controller = $Dispatcher->dispatch($url, array('return'=> 1));
  1452. $expected = array('missingAction', array(array(
  1453. 'className' => 'MyPluginController',
  1454. 'action' => 'not_here',
  1455. 'webroot' => '/cake/repo/branches/1.2.x.x/',
  1456. 'url' => '/cake/repo/branches/1.2.x.x/my_plugin/not_here/param:value/param2:value2',
  1457. 'base' => '/cake/repo/branches/1.2.x.x'
  1458. )));
  1459. $this->assertIdentical($expected, $controller);
  1460. Router::reload();
  1461. $Dispatcher =& new TestDispatcher();
  1462. $Dispatcher->base = false;
  1463. $url = 'my_plugin/param:value/param2:value2';
  1464. $controller = $Dispatcher->dispatch($url, array('return'=> 1));
  1465. $expected = array('missingAction', array(array(
  1466. 'className' => 'MyPluginController',
  1467. 'action' => 'param:value',
  1468. 'webroot' => '/cake/repo/branches/1.2.x.x/',
  1469. 'url' => '/cake/repo/branches/1.2.x.x/my_plugin/param:value/param2:value2',
  1470. 'base' => '/cake/repo/branches/1.2.x.x'
  1471. )));
  1472. $this->assertIdentical($expected, $controller);
  1473. }
  1474. /**
  1475. * testPrefixProtection method
  1476. *
  1477. * @access public
  1478. * @return void
  1479. */
  1480. function testPrefixProtection() {
  1481. $_POST = array();
  1482. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1483. Router::reload();
  1484. Router::connect('/admin/:controller/:action/*', array('prefix'=>'admin'), array('controller', 'action'));
  1485. $Dispatcher =& new TestDispatcher();
  1486. $Dispatcher->base = false;
  1487. $url = 'test_dispatch_pages/admin_index/param:value/param2:value2';
  1488. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1489. $expected = array('privateAction', array(array(
  1490. 'className' => 'TestDispatchPagesController',
  1491. 'action' => 'admin_index',
  1492. 'webroot' => '/cake/repo/branches/1.2.x.x/',
  1493. 'url' => 'test_dispatch_pages/admin_index/param:value/param2:value2',
  1494. 'base' => '/cake/repo/branches/1.2.x.x'
  1495. )));
  1496. $this->assertIdentical($expected, $controller);
  1497. }
  1498. /**
  1499. * undocumented function
  1500. *
  1501. * @return void
  1502. **/
  1503. function testTestPluginDispatch() {
  1504. $Dispatcher =& new TestDispatcher();
  1505. $_back = Configure::read('pluginPaths');
  1506. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  1507. $url = '/test_plugin/tests/index';
  1508. $result = $Dispatcher->dispatch($url, array('return' => 1));
  1509. $this->assertTrue(class_exists('TestsController'));
  1510. $this->assertTrue(class_exists('TestPluginAppController'));
  1511. $this->assertTrue(class_exists('OtherComponentComponent'));
  1512. $this->assertTrue(class_exists('PluginsComponentComponent'));
  1513. Configure::write('pluginPaths', $_back);
  1514. }
  1515. /**
  1516. * testChangingParamsFromBeforeFilter method
  1517. *
  1518. * @access public
  1519. * @return void
  1520. */
  1521. function testChangingParamsFromBeforeFilter() {
  1522. $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
  1523. $Dispatcher =& new TestDispatcher();
  1524. $url = 'some_posts/index/param:value/param2:value2';
  1525. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1526. $expected = array('missingAction', array(array(
  1527. 'className' => 'SomePostsController',
  1528. 'action' => 'view',
  1529. 'webroot' => '/cake/repo/branches/1.2.x.x/',
  1530. 'url' => '/cake/repo/branches/1.2.x.x/some_posts/index/param:value/param2:value2',
  1531. 'base' => '/cake/repo/branches/1.2.x.x'
  1532. )));
  1533. $this->assertEqual($expected, $controller);
  1534. $url = 'some_posts/something_else/param:value/param2:value2';
  1535. $controller = $Dispatcher->dispatch($url, array('return' => 1));
  1536. $expected = 'SomePosts';
  1537. $this->assertEqual($expected, $controller->name);
  1538. $expected = 'change';
  1539. $this->assertEqual($expected, $controller->action);
  1540. $expected = array('changed');
  1541. $this->assertIdentical($expected, $controller->params['pass']);
  1542. }
  1543. /**
  1544. * testStaticAssets method
  1545. *
  1546. * @access public
  1547. * @return void
  1548. */
  1549. function testStaticAssets() {
  1550. Router::reload();
  1551. $Configure = Configure::getInstance();
  1552. $Configure->__objects = null;
  1553. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  1554. Configure::write('vendorPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors'. DS));
  1555. $Dispatcher =& new TestDispatcher();
  1556. Configure::write('debug', 0);
  1557. ob_start();
  1558. $Dispatcher->dispatch('/img/test.jpg');
  1559. $result = ob_get_clean();
  1560. $file = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS . 'img' . DS . 'test.jpg');
  1561. $this->assertEqual($file, $result);
  1562. Configure::write('debug', 0);
  1563. $Dispatcher->params = $Dispatcher->parseParams('css/test_asset.css');
  1564. ob_start();
  1565. $Dispatcher->cached('css/test_asset.css');
  1566. $result = ob_get_clean();
  1567. $this->assertEqual('this is the test asset css file', $result);
  1568. ob_start();
  1569. $Dispatcher->cached('test_plugin/js/test_plugin/test.js');
  1570. $result = ob_get_clean();
  1571. $this->assertEqual('alert("Test App");', $result);
  1572. Configure::write('debug', 0);
  1573. $Dispatcher->params = $Dispatcher->parseParams('test_plugin/js/test_plugin/test.js');
  1574. ob_start();
  1575. $Dispatcher->cached('test_plugin/js/test_plugin/test.js');
  1576. $result = ob_get_clean();
  1577. $this->assertEqual('alert("Test App");', $result);
  1578. Configure::write('debug', 0);
  1579. $Dispatcher->params = $Dispatcher->parseParams('test_plugin/css/test_plugin_asset.css');
  1580. ob_start();
  1581. $Dispatcher->cached('test_plugin/css/test_plugin_asset.css');
  1582. $result = ob_get_clean();
  1583. $this->assertEqual('this is the test plugin asset css file', $result);
  1584. Configure::write('debug', 0);
  1585. $Dispatcher->params = $Dispatcher->parseParams('test_plugin/img/cake.icon.gif');
  1586. ob_start();
  1587. $Dispatcher->cached('test_plugin/img/cake.icon.gif');
  1588. $result = ob_get_clean();
  1589. $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');
  1590. $this->assertEqual($file, $result);
  1591. header('Content-type: text/html');//reset the header content-type without page can render as plain text.
  1592. }
  1593. /**
  1594. * testFullPageCachingDispatch method
  1595. *
  1596. * @access public
  1597. * @return void
  1598. */
  1599. function testFullPageCachingDispatch() {
  1600. Configure::write('Cache.disable', false);
  1601. Configure::write('Cache.check', true);
  1602. Configure::write('debug', 2);
  1603. $_POST = array();
  1604. $_SERVER['PHP_SELF'] = '/';
  1605. Router::reload();
  1606. Router::connect('/', array('controller' => 'test_cached_pages', 'action' => 'index'));
  1607. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS));
  1608. $dispatcher =& new Dispatcher();
  1609. $dispatcher->base = false;
  1610. $url = '/';
  1611. ob_start();
  1612. $dispatcher->dispatch($url);
  1613. $out = ob_get_clean();
  1614. ob_start();
  1615. $dispatcher->cached($url);
  1616. $cached = ob_get_clean();
  1617. $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
  1618. $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
  1619. $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
  1620. $this->assertEqual($result, $expected);
  1621. $filename = $this->__cachePath($dispatcher->here);
  1622. unlink($filename);
  1623. $dispatcher->base = false;
  1624. $url = 'test_cached_pages/index';
  1625. ob_start();
  1626. $dispatcher->dispatch($url);
  1627. $out = ob_get_clean();
  1628. ob_start();
  1629. $dispatcher->cached($url);
  1630. $cached = ob_get_clean();
  1631. $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
  1632. $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
  1633. $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
  1634. $this->assertEqual($result, $expected);
  1635. $filename = $this->__cachePath($dispatcher->here);
  1636. unlink($filename);
  1637. $url = 'TestCachedPages/index';
  1638. ob_start();
  1639. $dispatcher->dispatch($url);
  1640. $out = ob_get_clean();
  1641. ob_start();
  1642. $dispatcher->cached($url)

Large files files are truncated, but you can click here to view the full file