Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / web / public_php / webtt / cake / tests / cases / dispatcher.test.php
blob942e1f7573a09329e76e9bb4f7ad058e64af2931
1 <?php
2 /**
3 * DispatcherTest file
5 * PHP versions 4 and 5
7 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10 * Licensed under The Open Group Test Suite License
11 * Redistributions of files must retain the above copyright notice.
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
15 * @package cake
16 * @subpackage cake.tests.cases
17 * @since CakePHP(tm) v 1.2.0.4206
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
20 require_once CAKE . 'dispatcher.php';
22 if (!class_exists('AppController')) {
23 require_once LIBS . 'controller' . DS . 'app_controller.php';
24 } elseif (!defined('APP_CONTROLLER_EXISTS')){
25 define('APP_CONTROLLER_EXISTS', true);
28 /**
29 * TestDispatcher class
31 * @package cake
32 * @subpackage cake.tests.cases
34 class TestDispatcher extends Dispatcher {
36 /**
37 * invoke method
39 * @param mixed $controller
40 * @param mixed $params
41 * @param mixed $missingAction
42 * @return void
43 * @access protected
45 function _invoke(&$controller, $params) {
46 restore_error_handler();
47 if ($result = parent::_invoke($controller, $params)) {
48 if ($result[0] === 'missingAction') {
49 return $result;
52 set_error_handler('simpleTestErrorHandler');
54 return $controller;
57 /**
58 * cakeError method
60 * @param mixed $filename
61 * @return void
62 * @access public
64 function cakeError($filename, $params) {
65 return array($filename, $params);
68 /**
69 * _stop method
71 * @return void
72 * @access protected
74 function _stop() {
75 $this->stopped = true;
76 return true;
80 /**
81 * MyPluginAppController class
83 * @package cake
84 * @subpackage cake.tests.cases
86 class MyPluginAppController extends AppController {
89 /**
90 * MyPluginController class
92 * @package cake
93 * @subpackage cake.tests.cases
95 class MyPluginController extends MyPluginAppController {
97 /**
98 * name property
100 * @var string 'MyPlugin'
101 * @access public
103 var $name = 'MyPlugin';
106 * uses property
108 * @var array
109 * @access public
111 var $uses = array();
114 * index method
116 * @return void
117 * @access public
119 function index() {
120 return true;
124 * add method
126 * @return void
127 * @access public
129 function add() {
130 return true;
134 * admin_add method
136 * @param mixed $id
137 * @return void
138 * @access public
140 function admin_add($id = null) {
141 return $id;
146 * SomePagesController class
148 * @package cake
149 * @subpackage cake.tests.cases
151 class SomePagesController extends AppController {
154 * name property
156 * @var string 'SomePages'
157 * @access public
159 var $name = 'SomePages';
162 * uses property
164 * @var array
165 * @access public
167 var $uses = array();
170 * display method
172 * @param mixed $page
173 * @return void
174 * @access public
176 function display($page = null) {
177 return $page;
181 * index method
183 * @return void
184 * @access public
186 function index() {
187 return true;
191 * protected method
193 * @return void
194 * @access protected
196 function _protected() {
197 return true;
201 * redirect method overriding
203 * @return void
204 * @access public
206 function redirect() {
207 echo 'this should not be accessible';
212 * OtherPagesController class
214 * @package cake
215 * @subpackage cake.tests.cases
217 class OtherPagesController extends MyPluginAppController {
220 * name property
222 * @var string 'OtherPages'
223 * @access public
225 var $name = 'OtherPages';
228 * uses property
230 * @var array
231 * @access public
233 var $uses = array();
236 * display method
238 * @param mixed $page
239 * @return void
240 * @access public
242 function display($page = null) {
243 return $page;
247 * index method
249 * @return void
250 * @access public
252 function index() {
253 return true;
258 * TestDispatchPagesController class
260 * @package cake
261 * @subpackage cake.tests.cases
263 class TestDispatchPagesController extends AppController {
266 * name property
268 * @var string 'TestDispatchPages'
269 * @access public
271 var $name = 'TestDispatchPages';
274 * uses property
276 * @var array
277 * @access public
279 var $uses = array();
282 * admin_index method
284 * @return void
285 * @access public
287 function admin_index() {
288 return true;
292 * camelCased method
294 * @return void
295 * @access public
297 function camelCased() {
298 return true;
303 * ArticlesTestAppController class
305 * @package cake
306 * @subpackage cake.tests.cases
308 class ArticlesTestAppController extends AppController {
312 * ArticlesTestController class
314 * @package cake
315 * @subpackage cake.tests.cases
317 class ArticlesTestController extends ArticlesTestAppController {
320 * name property
322 * @var string 'ArticlesTest'
323 * @access public
325 var $name = 'ArticlesTest';
328 * uses property
330 * @var array
331 * @access public
333 var $uses = array();
336 * admin_index method
338 * @return void
339 * @access public
341 function admin_index() {
342 return true;
345 * fake index method.
347 * @return void
349 function index() {
350 return true;
355 * SomePostsController class
357 * @package cake
358 * @subpackage cake.tests.cases
360 class SomePostsController extends AppController {
363 * name property
365 * @var string 'SomePosts'
366 * @access public
368 var $name = 'SomePosts';
371 * uses property
373 * @var array
374 * @access public
376 var $uses = array();
379 * autoRender property
381 * @var bool false
382 * @access public
384 var $autoRender = false;
387 * beforeFilter method
389 * @return void
390 * @access public
392 function beforeFilter() {
393 if ($this->params['action'] == 'index') {
394 $this->params['action'] = 'view';
395 } else {
396 $this->params['action'] = 'change';
398 $this->params['pass'] = array('changed');
402 * index method
404 * @return void
405 * @access public
407 function index() {
408 return true;
412 * change method
414 * @return void
415 * @access public
417 function change() {
418 return true;
423 * TestCachedPagesController class
425 * @package cake
426 * @subpackage cake.tests.cases
428 class TestCachedPagesController extends AppController {
431 * name property
433 * @var string 'TestCachedPages'
434 * @access public
436 var $name = 'TestCachedPages';
439 * uses property
441 * @var array
442 * @access public
444 var $uses = array();
447 * helpers property
449 * @var array
450 * @access public
452 var $helpers = array('Cache');
455 * cacheAction property
457 * @var array
458 * @access public
460 var $cacheAction = array(
461 'index' => '+2 sec',
462 'test_nocache_tags' => '+2 sec',
463 'view' => '+2 sec'
467 * viewPath property
469 * @var string 'posts'
470 * @access public
472 var $viewPath = 'posts';
475 * index method
477 * @return void
478 * @access public
480 function index() {
481 $this->render();
485 * test_nocache_tags method
487 * @return void
488 * @access public
490 function test_nocache_tags() {
491 $this->render();
495 * view method
497 * @return void
498 * @access public
500 function view($id = null) {
501 $this->render('index');
504 * test cached forms / tests view object being registered
506 * @return void
508 function cache_form() {
509 $this->cacheAction = 10;
510 $this->helpers[] = 'Form';
515 * TimesheetsController class
517 * @package cake
518 * @subpackage cake.tests.cases
520 class TimesheetsController extends AppController {
523 * name property
525 * @var string 'Timesheets'
526 * @access public
528 var $name = 'Timesheets';
531 * uses property
533 * @var array
534 * @access public
536 var $uses = array();
539 * index method
541 * @return void
542 * @access public
544 function index() {
545 return true;
550 * DispatcherTest class
552 * @package cake
553 * @subpackage cake.tests.cases
555 class DispatcherTest extends CakeTestCase {
558 * setUp method
560 * @return void
561 * @access public
563 function startTest() {
564 $this->_get = $_GET;
565 $_GET = array();
566 $this->_post = $_POST;
567 $this->_files = $_FILES;
568 $this->_server = $_SERVER;
570 $this->_app = Configure::read('App');
571 Configure::write('App.base', false);
572 Configure::write('App.baseUrl', false);
573 Configure::write('App.dir', 'app');
574 Configure::write('App.webroot', 'webroot');
576 $this->_cache = Configure::read('Cache');
577 Configure::write('Cache.disable', true);
579 $this->_debug = Configure::read('debug');
581 App::build(App::core());
585 * tearDown method
587 * @return void
588 * @access public
590 function endTest() {
591 $_GET = $this->_get;
592 $_POST = $this->_post;
593 $_FILES = $this->_files;
594 $_SERVER = $this->_server;
595 App::build();
596 Configure::write('App', $this->_app);
597 Configure::write('Cache', $this->_cache);
598 Configure::write('debug', $this->_debug);
602 * testParseParamsWithoutZerosAndEmptyPost method
604 * @return void
605 * @access public
607 function testParseParamsWithoutZerosAndEmptyPost() {
608 $Dispatcher =& new Dispatcher();
609 $test = $Dispatcher->parseParams("/testcontroller/testaction/params1/params2/params3");
610 $this->assertIdentical($test['controller'], 'testcontroller');
611 $this->assertIdentical($test['action'], 'testaction');
612 $this->assertIdentical($test['pass'][0], 'params1');
613 $this->assertIdentical($test['pass'][1], 'params2');
614 $this->assertIdentical($test['pass'][2], 'params3');
615 $this->assertFalse(!empty($test['form']));
619 * testParseParamsReturnsPostedData method
621 * @return void
622 * @access public
624 function testParseParamsReturnsPostedData() {
625 $_POST['testdata'] = "My Posted Content";
626 $Dispatcher =& new Dispatcher();
627 $test = $Dispatcher->parseParams("/");
628 $this->assertTrue($test['form'], "Parsed URL not returning post data");
629 $this->assertIdentical($test['form']['testdata'], "My Posted Content");
633 * testParseParamsWithSingleZero method
635 * @return void
636 * @access public
638 function testParseParamsWithSingleZero() {
639 $Dispatcher =& new Dispatcher();
640 $test = $Dispatcher->parseParams("/testcontroller/testaction/1/0/23");
641 $this->assertIdentical($test['controller'], 'testcontroller');
642 $this->assertIdentical($test['action'], 'testaction');
643 $this->assertIdentical($test['pass'][0], '1');
644 $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][1]);
645 $this->assertIdentical($test['pass'][2], '23');
649 * testParseParamsWithManySingleZeros method
651 * @return void
652 * @access public
654 function testParseParamsWithManySingleZeros() {
655 $Dispatcher =& new Dispatcher();
656 $test = $Dispatcher->parseParams("/testcontroller/testaction/0/0/0/0/0/0");
657 $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][0]);
658 $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][1]);
659 $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][2]);
660 $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][3]);
661 $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][4]);
662 $this->assertPattern('/\\A(?:0)\\z/', $test['pass'][5]);
666 * testParseParamsWithManyZerosInEachSectionOfUrl method
668 * @return void
669 * @access public
671 function testParseParamsWithManyZerosInEachSectionOfUrl() {
672 $Dispatcher =& new Dispatcher();
673 $test = $Dispatcher->parseParams("/testcontroller/testaction/000/0000/00000/000000/000000/0000000");
674 $this->assertPattern('/\\A(?:000)\\z/', $test['pass'][0]);
675 $this->assertPattern('/\\A(?:0000)\\z/', $test['pass'][1]);
676 $this->assertPattern('/\\A(?:00000)\\z/', $test['pass'][2]);
677 $this->assertPattern('/\\A(?:000000)\\z/', $test['pass'][3]);
678 $this->assertPattern('/\\A(?:000000)\\z/', $test['pass'][4]);
679 $this->assertPattern('/\\A(?:0000000)\\z/', $test['pass'][5]);
683 * testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl method
685 * @return void
686 * @access public
688 function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() {
689 $Dispatcher =& new Dispatcher();
690 $test = $Dispatcher->parseParams("/testcontroller/testaction/01/0403/04010/000002/000030/0000400");
691 $this->assertPattern('/\\A(?:01)\\z/', $test['pass'][0]);
692 $this->assertPattern('/\\A(?:0403)\\z/', $test['pass'][1]);
693 $this->assertPattern('/\\A(?:04010)\\z/', $test['pass'][2]);
694 $this->assertPattern('/\\A(?:000002)\\z/', $test['pass'][3]);
695 $this->assertPattern('/\\A(?:000030)\\z/', $test['pass'][4]);
696 $this->assertPattern('/\\A(?:0000400)\\z/', $test['pass'][5]);
700 * testQueryStringOnRoot method
702 * @return void
703 * @access public
705 function testQueryStringOnRoot() {
706 Router::reload();
707 Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
708 Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
710 $_GET = array('coffee' => 'life', 'sleep' => 'sissies');
711 $Dispatcher =& new Dispatcher();
712 $uri = 'posts/home/?coffee=life&sleep=sissies';
713 $result = $Dispatcher->parseParams($uri);
714 $this->assertPattern('/posts/', $result['controller']);
715 $this->assertPattern('/home/', $result['action']);
716 $this->assertTrue(isset($result['url']['sleep']));
717 $this->assertTrue(isset($result['url']['coffee']));
719 $Dispatcher =& new Dispatcher();
720 $uri = '/?coffee=life&sleep=sissy';
721 $result = $Dispatcher->parseParams($uri);
722 $this->assertPattern('/pages/', $result['controller']);
723 $this->assertPattern('/display/', $result['action']);
724 $this->assertTrue(isset($result['url']['sleep']));
725 $this->assertTrue(isset($result['url']['coffee']));
726 $this->assertEqual($result['url']['coffee'], 'life');
730 * testFileUploadArrayStructure method
732 * @return void
733 * @access public
735 function testFileUploadArrayStructure() {
736 $_FILES = array('data' => array('name' => array(
737 'File' => array(
738 array('data' => 'cake_mssql_patch.patch'),
739 array('data' => 'controller.diff'),
740 array('data' => ''),
741 array('data' => ''),
743 'Post' => array('attachment' => 'jquery-1.2.1.js'),
745 'type' => array(
746 'File' => array(
747 array('data' => ''),
748 array('data' => ''),
749 array('data' => ''),
750 array('data' => ''),
752 'Post' => array('attachment' => 'application/x-javascript'),
754 'tmp_name' => array(
755 'File' => array(
756 array('data' => '/private/var/tmp/phpy05Ywj'),
757 array('data' => '/private/var/tmp/php7MBztY'),
758 array('data' => ''),
759 array('data' => ''),
761 'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
763 'error' => array(
764 'File' => array(
765 array('data' => 0),
766 array('data' => 0),
767 array('data' => 4),
768 array('data' => 4)
770 'Post' => array('attachment' => 0)
772 'size' => array(
773 'File' => array(
774 array('data' => 6271),
775 array('data' => 350),
776 array('data' => 0),
777 array('data' => 0),
779 'Post' => array('attachment' => 80469)
783 $Dispatcher =& new Dispatcher();
784 $result = $Dispatcher->parseParams('/');
786 $expected = array(
787 'File' => array(
788 array('data' => array(
789 'name' => 'cake_mssql_patch.patch',
790 'type' => '',
791 'tmp_name' => '/private/var/tmp/phpy05Ywj',
792 'error' => 0,
793 'size' => 6271,
796 array('data' => array(
797 'name' => 'controller.diff',
798 'type' => '',
799 'tmp_name' => '/private/var/tmp/php7MBztY',
800 'error' => 0,
801 'size' => 350,
803 array('data' => array(
804 'name' => '',
805 'type' => '',
806 'tmp_name' => '',
807 'error' => 4,
808 'size' => 0,
810 array('data' => array(
811 'name' => '',
812 'type' => '',
813 'tmp_name' => '',
814 'error' => 4,
815 'size' => 0,
818 'Post' => array('attachment' => array(
819 'name' => 'jquery-1.2.1.js',
820 'type' => 'application/x-javascript',
821 'tmp_name' => '/private/var/tmp/phpEwlrIo',
822 'error' => 0,
823 'size' => 80469,
824 )));
825 $this->assertEqual($result['data'], $expected);
827 $_FILES = array(
828 'data' => array(
829 'name' => array(
830 'Document' => array(
831 1 => array(
832 'birth_cert' => 'born on.txt',
833 'passport' => 'passport.txt',
834 'drivers_license' => 'ugly pic.jpg'
836 2 => array(
837 'birth_cert' => 'aunt betty.txt',
838 'passport' => 'betty-passport.txt',
839 'drivers_license' => 'betty-photo.jpg'
843 'type' => array(
844 'Document' => array(
845 1 => array(
846 'birth_cert' => 'application/octet-stream',
847 'passport' => 'application/octet-stream',
848 'drivers_license' => 'application/octet-stream',
850 2 => array(
851 'birth_cert' => 'application/octet-stream',
852 'passport' => 'application/octet-stream',
853 'drivers_license' => 'application/octet-stream',
857 'tmp_name' => array(
858 'Document' => array(
859 1 => array(
860 'birth_cert' => '/private/var/tmp/phpbsUWfH',
861 'passport' => '/private/var/tmp/php7f5zLt',
862 'drivers_license' => '/private/var/tmp/phpMXpZgT',
864 2 => array(
865 'birth_cert' => '/private/var/tmp/php5kHZt0',
866 'passport' => '/private/var/tmp/phpnYkOuM',
867 'drivers_license' => '/private/var/tmp/php9Rq0P3',
871 'error' => array(
872 'Document' => array(
873 1 => array(
874 'birth_cert' => 0,
875 'passport' => 0,
876 'drivers_license' => 0,
878 2 => array(
879 'birth_cert' => 0,
880 'passport' => 0,
881 'drivers_license' => 0,
885 'size' => array(
886 'Document' => array(
887 1 => array(
888 'birth_cert' => 123,
889 'passport' => 458,
890 'drivers_license' => 875,
892 2 => array(
893 'birth_cert' => 876,
894 'passport' => 976,
895 'drivers_license' => 9783,
901 $Dispatcher =& new Dispatcher();
902 $result = $Dispatcher->parseParams('/');
903 $expected = array(
904 'Document' => array(
905 1 => array(
906 'birth_cert' => array(
907 'name' => 'born on.txt',
908 'tmp_name' => '/private/var/tmp/phpbsUWfH',
909 'error' => 0,
910 'size' => 123,
911 'type' => 'application/octet-stream',
913 'passport' => array(
914 'name' => 'passport.txt',
915 'tmp_name' => '/private/var/tmp/php7f5zLt',
916 'error' => 0,
917 'size' => 458,
918 'type' => 'application/octet-stream',
920 'drivers_license' => array(
921 'name' => 'ugly pic.jpg',
922 'tmp_name' => '/private/var/tmp/phpMXpZgT',
923 'error' => 0,
924 'size' => 875,
925 'type' => 'application/octet-stream',
928 2 => array(
929 'birth_cert' => array(
930 'name' => 'aunt betty.txt',
931 'tmp_name' => '/private/var/tmp/php5kHZt0',
932 'error' => 0,
933 'size' => 876,
934 'type' => 'application/octet-stream',
936 'passport' => array(
937 'name' => 'betty-passport.txt',
938 'tmp_name' => '/private/var/tmp/phpnYkOuM',
939 'error' => 0,
940 'size' => 976,
941 'type' => 'application/octet-stream',
943 'drivers_license' => array(
944 'name' => 'betty-photo.jpg',
945 'tmp_name' => '/private/var/tmp/php9Rq0P3',
946 'error' => 0,
947 'size' => 9783,
948 'type' => 'application/octet-stream',
953 $this->assertEqual($result['data'], $expected);
956 $_FILES = array(
957 'data' => array(
958 'name' => array('birth_cert' => 'born on.txt'),
959 'type' => array('birth_cert' => 'application/octet-stream'),
960 'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
961 'error' => array('birth_cert' => 0),
962 'size' => array('birth_cert' => 123)
966 $Dispatcher =& new Dispatcher();
967 $result = $Dispatcher->parseParams('/');
969 $expected = array(
970 'birth_cert' => array(
971 'name' => 'born on.txt',
972 'type' => 'application/octet-stream',
973 'tmp_name' => '/private/var/tmp/phpbsUWfH',
974 'error' => 0,
975 'size' => 123
979 $this->assertEqual($result['data'], $expected);
983 * testGetUrl method
985 * @return void
986 * @access public
988 function testGetUrl() {
989 $Dispatcher =& new Dispatcher();
990 $Dispatcher->base = '/app/webroot/index.php';
991 $uri = '/app/webroot/index.php/posts/add';
992 $result = $Dispatcher->getUrl($uri);
993 $expected = 'posts/add';
994 $this->assertEqual($expected, $result);
996 Configure::write('App.baseUrl', '/app/webroot/index.php');
998 $uri = '/posts/add';
999 $result = $Dispatcher->getUrl($uri);
1000 $expected = 'posts/add';
1001 $this->assertEqual($expected, $result);
1003 $_GET['url'] = array();
1004 Configure::write('App.base', '/control');
1005 $Dispatcher =& new Dispatcher();
1006 $Dispatcher->baseUrl();
1007 $uri = '/control/students/browse';
1008 $result = $Dispatcher->getUrl($uri);
1009 $expected = 'students/browse';
1010 $this->assertEqual($expected, $result);
1012 $_GET['url'] = array();
1013 $Dispatcher =& new Dispatcher();
1014 $Dispatcher->base = '';
1015 $uri = '/?/home';
1016 $result = $Dispatcher->getUrl($uri);
1017 $expected = '?/home';
1018 $this->assertEqual($expected, $result);
1020 $_GET['url'] = array();
1021 $Dispatcher =& new Dispatcher();
1022 $Dispatcher->base = '/shop';
1023 $uri = '/shop/fr/pages/shop';
1024 $result = $Dispatcher->getUrl($uri);
1025 $expected = 'fr/pages/shop';
1026 $this->assertEqual($expected, $result);
1030 * testBaseUrlAndWebrootWithModRewrite method
1032 * @return void
1033 * @access public
1035 function testBaseUrlAndWebrootWithModRewrite() {
1036 $Dispatcher =& new Dispatcher();
1038 $Dispatcher->base = false;
1039 $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
1040 $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/app/webroot/index.php';
1041 $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
1042 $result = $Dispatcher->baseUrl();
1043 $expected = '/1.2.x.x';
1044 $this->assertEqual($expected, $result);
1045 $expectedWebroot = '/1.2.x.x/';
1046 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1048 $Dispatcher->base = false;
1049 $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
1050 $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/app/webroot/index.php';
1051 $_SERVER['PHP_SELF'] = '/index.php';
1052 $result = $Dispatcher->baseUrl();
1053 $expected = '';
1054 $this->assertEqual($expected, $result);
1055 $expectedWebroot = '/';
1056 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1058 $Dispatcher->base = false;
1059 $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
1060 $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/1.2.x.x/test/webroot/index.php';
1061 $_SERVER['PHP_SELF'] = '/webroot/index.php';
1062 $result = $Dispatcher->baseUrl();
1063 $expected = '';
1064 $this->assertEqual($expected, $result);
1065 $expectedWebroot = '/';
1066 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1068 $Dispatcher->base = false;
1069 $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
1070 $_SERVER['SCRIPT_FILENAME'] = '/some/apps/where/app/webroot/index.php';
1071 $_SERVER['PHP_SELF'] = '/some/apps/where/app/webroot/index.php';
1072 $result = $Dispatcher->baseUrl();
1073 $expected = '/some/apps/where';
1074 $this->assertEqual($expected, $result);
1075 $expectedWebroot = '/some/apps/where/';
1076 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1079 Configure::write('App.dir', 'auth');
1081 $Dispatcher->base = false;
1082 $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
1083 $_SERVER['SCRIPT_FILENAME'] = '/cake/repo/branches/demos/auth/webroot/index.php';
1084 $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
1086 $result = $Dispatcher->baseUrl();
1087 $expected = '/demos/auth';
1088 $this->assertEqual($expected, $result);
1089 $expectedWebroot = '/demos/auth/';
1090 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1092 Configure::write('App.dir', 'code');
1094 $Dispatcher->base = false;
1095 $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
1096 $_SERVER['SCRIPT_FILENAME'] = '/Library/WebServer/Documents/clients/PewterReport/code/webroot/index.php';
1097 $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
1098 $result = $Dispatcher->baseUrl();
1099 $expected = '/clients/PewterReport/code';
1100 $this->assertEqual($expected, $result);
1101 $expectedWebroot = '/clients/PewterReport/code/';
1102 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1106 * testBaseUrlwithModRewriteAlias method
1108 * @return void
1109 * @access public
1111 function testBaseUrlwithModRewriteAlias() {
1112 $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
1113 $_SERVER['SCRIPT_FILENAME'] = '/home/aplusnur/cake2/app/webroot/index.php';
1114 $_SERVER['PHP_SELF'] = '/control/index.php';
1116 Configure::write('App.base', '/control');
1118 $Dispatcher =& new Dispatcher();
1119 $result = $Dispatcher->baseUrl();
1120 $expected = '/control';
1121 $this->assertEqual($expected, $result);
1122 $expectedWebroot = '/control/';
1123 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1125 Configure::write('App.base', false);
1126 Configure::write('App.dir', 'affiliate');
1127 Configure::write('App.webroot', 'newaffiliate');
1129 $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
1130 $_SERVER['SCRIPT_FILENAME'] = '/var/www/abtravaff/html/newaffiliate/index.php';
1131 $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
1132 $Dispatcher =& new Dispatcher();
1133 $result = $Dispatcher->baseUrl();
1134 $expected = '/newaffiliate';
1135 $this->assertEqual($expected, $result);
1136 $expectedWebroot = '/newaffiliate/';
1137 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1141 * testBaseUrlAndWebrootWithBaseUrl method
1143 * @return void
1144 * @access public
1146 function testBaseUrlAndWebrootWithBaseUrl() {
1147 $Dispatcher =& new Dispatcher();
1149 Configure::write('App.dir', 'app');
1151 Configure::write('App.baseUrl', '/app/webroot/index.php');
1152 $result = $Dispatcher->baseUrl();
1153 $expected = '/app/webroot/index.php';
1154 $this->assertEqual($expected, $result);
1155 $expectedWebroot = '/app/webroot/';
1156 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1158 Configure::write('App.baseUrl', '/app/webroot/test.php');
1159 $result = $Dispatcher->baseUrl();
1160 $expected = '/app/webroot/test.php';
1161 $this->assertEqual($expected, $result);
1162 $expectedWebroot = '/app/webroot/';
1163 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1165 Configure::write('App.baseUrl', '/app/index.php');
1166 $result = $Dispatcher->baseUrl();
1167 $expected = '/app/index.php';
1168 $this->assertEqual($expected, $result);
1169 $expectedWebroot = '/app/webroot/';
1170 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1172 Configure::write('App.baseUrl', '/index.php');
1173 $result = $Dispatcher->baseUrl();
1174 $expected = '/index.php';
1175 $this->assertEqual($expected, $result);
1176 $expectedWebroot = '/app/webroot/';
1177 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1179 Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
1180 $result = $Dispatcher->baseUrl();
1181 $expected = '/CakeBB/app/webroot/index.php';
1182 $this->assertEqual($expected, $result);
1183 $expectedWebroot = '/CakeBB/app/webroot/';
1184 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1186 Configure::write('App.baseUrl', '/CakeBB/app/index.php');
1187 $result = $Dispatcher->baseUrl();
1188 $expected = '/CakeBB/app/index.php';
1189 $this->assertEqual($expected, $result);
1190 $expectedWebroot = '/CakeBB/app/webroot/';
1191 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1193 Configure::write('App.baseUrl', '/CakeBB/index.php');
1194 $result = $Dispatcher->baseUrl();
1195 $expected = '/CakeBB/index.php';
1196 $this->assertEqual($expected, $result);
1197 $expectedWebroot = '/CakeBB/app/webroot/';
1198 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1200 Configure::write('App.baseUrl', '/dbhauser/index.php');
1201 $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
1202 $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
1203 $result = $Dispatcher->baseUrl();
1204 $expected = '/dbhauser/index.php';
1205 $this->assertEqual($expected, $result);
1206 $expectedWebroot = '/dbhauser/app/webroot/';
1207 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1211 * test baseUrl with no rewrite and using the top level index.php.
1213 * @return void
1215 function testBaseUrlNoRewriteTopLevelIndex() {
1216 $Dispatcher =& new Dispatcher();
1218 Configure::write('App.baseUrl', '/index.php');
1219 $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
1220 $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
1222 $result = $Dispatcher->baseUrl();
1223 $this->assertEqual('/index.php', $result);
1224 $this->assertEqual('/app/webroot/', $Dispatcher->webroot);
1225 $this->assertEqual('', $Dispatcher->base);
1229 * test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
1231 * @return void
1233 function testBaseUrlNoRewriteWebrootIndex() {
1234 $Dispatcher =& new Dispatcher();
1236 Configure::write('App.baseUrl', '/index.php');
1237 $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
1238 $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
1240 $result = $Dispatcher->baseUrl();
1241 $this->assertEqual('/index.php', $result);
1242 $this->assertEqual('/', $Dispatcher->webroot);
1243 $this->assertEqual('', $Dispatcher->base);
1247 * testBaseUrlAndWebrootWithBase method
1249 * @return void
1250 * @access public
1252 function testBaseUrlAndWebrootWithBase() {
1253 $Dispatcher =& new Dispatcher();
1254 $Dispatcher->base = '/app';
1255 $result = $Dispatcher->baseUrl();
1256 $expected = '/app';
1257 $this->assertEqual($expected, $result);
1258 $expectedWebroot = '/app/';
1259 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1261 $Dispatcher->base = '';
1262 $result = $Dispatcher->baseUrl();
1263 $expected = '';
1264 $this->assertEqual($expected, $result);
1265 $expectedWebroot = '/';
1266 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1268 Configure::write('App.dir', 'testbed');
1269 $Dispatcher->base = '/cake/testbed/webroot';
1270 $result = $Dispatcher->baseUrl();
1271 $expected = '/cake/testbed/webroot';
1272 $this->assertEqual($expected, $result);
1273 $expectedWebroot = '/cake/testbed/webroot/';
1274 $this->assertEqual($expectedWebroot, $Dispatcher->webroot);
1278 * testMissingController method
1280 * @return void
1281 * @access public
1283 function testMissingController() {
1284 $Dispatcher =& new TestDispatcher();
1285 Configure::write('App.baseUrl', '/index.php');
1286 $url = 'some_controller/home/param:value/param2:value2';
1287 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1288 $expected = array('missingController', array(array(
1289 'className' => 'SomeControllerController',
1290 'webroot' => '/app/webroot/',
1291 'url' => 'some_controller/home/param:value/param2:value2',
1292 'base' => '/index.php'
1293 )));
1294 $this->assertEqual($expected, $controller);
1298 * testPrivate method
1300 * @return void
1301 * @access public
1303 function testPrivate() {
1304 $Dispatcher =& new TestDispatcher();
1305 Configure::write('App.baseUrl','/index.php');
1306 $url = 'some_pages/_protected/param:value/param2:value2';
1308 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1310 $expected = array('privateAction', array(array(
1311 'className' => 'SomePagesController',
1312 'action' => '_protected',
1313 'webroot' => '/app/webroot/',
1314 'url' => 'some_pages/_protected/param:value/param2:value2',
1315 'base' => '/index.php'
1316 )));
1317 $this->assertEqual($controller, $expected);
1321 * testMissingAction method
1323 * @return void
1324 * @access public
1326 function testMissingAction() {
1327 $Dispatcher =& new TestDispatcher();
1328 Configure::write('App.baseUrl', '/index.php');
1329 $url = 'some_pages/home/param:value/param2:value2';
1331 $controller = $Dispatcher->dispatch($url, array('return'=> 1));
1333 $expected = array('missingAction', array(array(
1334 'className' => 'SomePagesController',
1335 'action' => 'home',
1336 'webroot' => '/app/webroot/',
1337 'url' => '/index.php/some_pages/home/param:value/param2:value2',
1338 'base' => '/index.php'
1339 )));
1340 $this->assertEqual($expected, $controller);
1342 $Dispatcher =& new TestDispatcher();
1343 Configure::write('App.baseUrl','/index.php');
1344 $url = 'some_pages/redirect/param:value/param2:value2';
1346 $controller = $Dispatcher->dispatch($url, array('return'=> 1));
1348 $expected = array('missingAction', array(array(
1349 'className' => 'SomePagesController',
1350 'action' => 'redirect',
1351 'webroot' => '/app/webroot/',
1352 'url' => '/index.php/some_pages/redirect/param:value/param2:value2',
1353 'base' => '/index.php'
1354 )));
1355 $this->assertEqual($expected, $controller);
1359 * testDispatch method
1361 * @return void
1362 * @access public
1364 function testDispatch() {
1365 $Dispatcher =& new TestDispatcher();
1366 Configure::write('App.baseUrl','/index.php');
1367 $url = 'pages/home/param:value/param2:value2';
1369 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1370 $expected = 'Pages';
1371 $this->assertEqual($expected, $controller->name);
1373 $expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2');
1374 $this->assertIdentical($expected, $controller->passedArgs);
1376 Configure::write('App.baseUrl','/pages/index.php');
1378 $url = 'pages/home';
1379 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1381 $expected = 'Pages';
1382 $this->assertEqual($expected, $controller->name);
1384 $url = 'pages/home/';
1385 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1386 $this->assertNull($controller->plugin);
1387 $this->assertNull($Dispatcher->params['plugin']);
1389 $expected = 'Pages';
1390 $this->assertEqual($expected, $controller->name);
1392 unset($Dispatcher);
1394 $Dispatcher =& new TestDispatcher();
1395 Configure::write('App.baseUrl','/timesheets/index.php');
1397 $url = 'timesheets';
1398 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1400 $expected = 'Timesheets';
1401 $this->assertEqual($expected, $controller->name);
1403 $url = 'timesheets/';
1404 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1406 $this->assertEqual('Timesheets', $controller->name);
1407 $this->assertEqual('/timesheets/index.php', $Dispatcher->base);
1410 $url = 'test_dispatch_pages/camelCased';
1411 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1412 $this->assertEqual('TestDispatchPages', $controller->name);
1414 $url = 'test_dispatch_pages/camelCased/something. .';
1415 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1416 $this->assertEqual($controller->params['pass'][0], 'something. .', 'Period was chopped off. %s');
1421 * testDispatchWithArray method
1423 * @return void
1424 * @access public
1426 function testDispatchWithArray() {
1427 $Dispatcher =& new TestDispatcher();
1428 $url = 'pages/home/param:value/param2:value2';
1430 $url = array('controller' => 'pages', 'action' => 'display');
1431 $controller = $Dispatcher->dispatch($url, array(
1432 'pass' => array('home'),
1433 'named' => array('param' => 'value', 'param2' => 'value2'),
1434 'return' => 1
1436 $expected = 'Pages';
1437 $this->assertEqual($expected, $controller->name);
1439 $expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2');
1440 $this->assertIdentical($expected, $controller->passedArgs);
1442 $this->assertEqual($Dispatcher->base . '/pages/display/home/param:value/param2:value2', $Dispatcher->here);
1446 * test that a garbage url doesn't cause errors.
1448 * @return void
1450 function testDispatchWithGarbageUrl() {
1451 Configure::write('App.baseUrl', '/index.php');
1453 $Dispatcher =& new TestDispatcher();
1454 $url = 'http://google.com';
1455 $result = $Dispatcher->dispatch($url);
1456 $expected = array('missingController', array(array(
1457 'className' => 'Controller',
1458 'webroot' => '/app/webroot/',
1459 'url' => 'http://google.com',
1460 'base' => '/index.php'
1461 )));
1462 $this->assertEqual($expected, $result);
1466 * testAdminDispatch method
1468 * @return void
1469 * @access public
1471 function testAdminDispatch() {
1472 $_POST = array();
1473 $Dispatcher =& new TestDispatcher();
1474 Configure::write('Routing.prefixes', array('admin'));
1475 Configure::write('App.baseUrl','/cake/repo/branches/1.2.x.x/index.php');
1476 $url = 'admin/test_dispatch_pages/index/param:value/param2:value2';
1478 Router::reload();
1479 $Router =& Router::getInstance();
1480 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1482 $this->assertEqual($controller->name, 'TestDispatchPages');
1484 $this->assertIdentical($controller->passedArgs, array('param' => 'value', 'param2' => 'value2'));
1485 $this->assertTrue($controller->params['admin']);
1487 $expected = '/cake/repo/branches/1.2.x.x/index.php/admin/test_dispatch_pages/index/param:value/param2:value2';
1488 $this->assertIdentical($expected, $controller->here);
1490 $expected = '/cake/repo/branches/1.2.x.x/index.php';
1491 $this->assertIdentical($expected, $controller->base);
1495 * testPluginDispatch method
1497 * @return void
1498 * @access public
1500 function testPluginDispatch() {
1501 $_POST = array();
1502 $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
1504 Router::reload();
1505 $Dispatcher =& new TestDispatcher();
1506 Router::connect(
1507 '/my_plugin/:controller/*',
1508 array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'display')
1511 $Dispatcher->base = false;
1512 $url = 'my_plugin/some_pages/home/param:value/param2:value2';
1513 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1515 $result = $Dispatcher->parseParams($url);
1516 $expected = array(
1517 'pass' => array('home'),
1518 'named' => array('param'=> 'value', 'param2'=> 'value2'), 'plugin'=> 'my_plugin',
1519 'controller'=> 'some_pages', 'action'=> 'display', 'form'=> null,
1520 'url'=> array('url'=> 'my_plugin/some_pages/home/param:value/param2:value2'),
1522 ksort($expected);
1523 ksort($result);
1525 $this->assertEqual($expected, $result);
1527 $this->assertIdentical($controller->plugin, 'my_plugin');
1528 $this->assertIdentical($controller->name, 'SomePages');
1529 $this->assertIdentical($controller->params['controller'], 'some_pages');
1530 $this->assertIdentical($controller->passedArgs, array('0' => 'home', 'param'=>'value', 'param2'=>'value2'));
1532 $expected = '/cake/repo/branches/1.2.x.x/my_plugin/some_pages/home/param:value/param2:value2';
1533 $this->assertIdentical($expected, $controller->here);
1535 $expected = '/cake/repo/branches/1.2.x.x';
1536 $this->assertIdentical($expected, $controller->base);
1540 * testAutomaticPluginDispatch method
1542 * @return void
1543 * @access public
1545 function testAutomaticPluginDispatch() {
1546 $_POST = array();
1547 $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
1549 Router::reload();
1550 $Dispatcher =& new TestDispatcher();
1551 Router::connect(
1552 '/my_plugin/:controller/:action/*',
1553 array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'display')
1556 $Dispatcher->base = false;
1558 $url = 'my_plugin/other_pages/index/param:value/param2:value2';
1559 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1561 $this->assertIdentical($controller->plugin, 'my_plugin');
1562 $this->assertIdentical($controller->name, 'OtherPages');
1563 $this->assertIdentical($controller->action, 'index');
1564 $this->assertIdentical($controller->passedArgs, array('param' => 'value', 'param2' => 'value2'));
1566 $expected = '/cake/repo/branches/1.2.x.x/my_plugin/other_pages/index/param:value/param2:value2';
1567 $this->assertIdentical($expected, $controller->here);
1569 $expected = '/cake/repo/branches/1.2.x.x';
1570 $this->assertIdentical($expected, $controller->base);
1574 * testAutomaticPluginControllerDispatch method
1576 * @return void
1577 * @access public
1579 function testAutomaticPluginControllerDispatch() {
1580 $_POST = array();
1581 $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
1583 $plugins = App::objects('plugin');
1584 $plugins[] = 'MyPlugin';
1585 $plugins[] = 'ArticlesTest';
1587 $app = App::getInstance();
1588 $app->__objects['plugin'] = $plugins;
1590 Router::reload();
1591 $Dispatcher =& new TestDispatcher();
1592 $Dispatcher->base = false;
1594 $url = 'my_plugin/my_plugin/add/param:value/param2:value2';
1596 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1598 $this->assertIdentical($controller->plugin, 'my_plugin');
1599 $this->assertIdentical($controller->name, 'MyPlugin');
1600 $this->assertIdentical($controller->action, 'add');
1601 $this->assertEqual($controller->params['named'], array('param' => 'value', 'param2' => 'value2'));
1604 Router::reload();
1605 $Dispatcher =& new TestDispatcher();
1606 $Dispatcher->base = false;
1608 // Simulates the Route for a real plugin, installed in APP/plugins
1609 Router::connect('/my_plugin/:controller/:action/*', array('plugin' => 'my_plugin'));
1611 $plugin = 'MyPlugin';
1612 $pluginUrl = Inflector::underscore($plugin);
1614 $url = $pluginUrl;
1615 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1616 $this->assertIdentical($controller->plugin, 'my_plugin');
1617 $this->assertIdentical($controller->name, 'MyPlugin');
1618 $this->assertIdentical($controller->action, 'index');
1620 $expected = $pluginUrl;
1621 $this->assertEqual($controller->params['controller'], $expected);
1624 Configure::write('Routing.prefixes', array('admin'));
1626 Router::reload();
1627 $Dispatcher =& new TestDispatcher();
1628 $Dispatcher->base = false;
1630 $url = 'admin/my_plugin/my_plugin/add/5/param:value/param2:value2';
1631 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1633 $this->assertEqual($controller->params['plugin'], 'my_plugin');
1634 $this->assertEqual($controller->params['controller'], 'my_plugin');
1635 $this->assertEqual($controller->params['action'], 'admin_add');
1636 $this->assertEqual($controller->params['pass'], array(5));
1637 $this->assertEqual($controller->params['named'], array('param' => 'value', 'param2' => 'value2'));
1638 $this->assertIdentical($controller->plugin, 'my_plugin');
1639 $this->assertIdentical($controller->name, 'MyPlugin');
1640 $this->assertIdentical($controller->action, 'admin_add');
1642 $expected = array(0 => 5, 'param'=>'value', 'param2'=>'value2');
1643 $this->assertEqual($controller->passedArgs, $expected);
1645 Configure::write('Routing.prefixes', array('admin'));
1646 Router::reload();
1648 $Dispatcher =& new TestDispatcher();
1649 $Dispatcher->base = false;
1651 $controller = $Dispatcher->dispatch('admin/articles_test', array('return' => 1));
1652 $this->assertIdentical($controller->plugin, 'articles_test');
1653 $this->assertIdentical($controller->name, 'ArticlesTest');
1654 $this->assertIdentical($controller->action, 'admin_index');
1656 $expected = array(
1657 'pass'=> array(),
1658 'named' => array(),
1659 'controller' => 'articles_test',
1660 'plugin' => 'articles_test',
1661 'action' => 'admin_index',
1662 'prefix' => 'admin',
1663 'admin' => true,
1664 'form' => array(),
1665 'url' => array('url' => 'admin/articles_test'),
1666 'return' => 1
1668 $this->assertEqual($controller->params, $expected);
1672 * test Plugin dispatching without controller name and using
1673 * plugin short form instead.
1675 * @return void
1676 * @access public
1678 function testAutomaticPluginDispatchWithShortAccess() {
1679 $_POST = array();
1680 $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
1681 $plugins = App::objects('plugin');
1682 $plugins[] = 'MyPlugin';
1684 $app = App::getInstance();
1685 $app->__objects['plugin'] = $plugins;
1687 Router::reload();
1689 $Dispatcher =& new TestDispatcher();
1690 $Dispatcher->base = false;
1692 $url = 'my_plugin/';
1693 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1694 $this->assertEqual($controller->params['controller'], 'my_plugin');
1695 $this->assertEqual($controller->params['plugin'], 'my_plugin');
1696 $this->assertEqual($controller->params['action'], 'index');
1697 $this->assertFalse(isset($controller->params['pass'][0]));
1701 * test plugin shortcut urls with controllers that need to be loaded,
1702 * the above test uses a controller that has already been included.
1704 * @return void
1706 function testPluginShortCutUrlsWithControllerThatNeedsToBeLoaded() {
1707 $loaded = class_exists('TestPluginController', false);
1708 if ($this->skipIf($loaded, 'TestPluginController already loaded, this test will always pass, skipping %s')) {
1709 return true;
1711 Router::reload();
1712 App::build(array(
1713 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
1714 ), true);
1715 App::objects('plugin', null, false);
1717 $Dispatcher =& new TestDispatcher();
1718 $Dispatcher->base = false;
1720 $url = 'test_plugin/';
1721 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1722 $this->assertEqual($controller->params['controller'], 'test_plugin');
1723 $this->assertEqual($controller->params['plugin'], 'test_plugin');
1724 $this->assertEqual($controller->params['action'], 'index');
1725 $this->assertFalse(isset($controller->params['pass'][0]));
1727 $url = '/test_plugin/tests/index';
1728 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1729 $this->assertEqual($controller->params['controller'], 'tests');
1730 $this->assertEqual($controller->params['plugin'], 'test_plugin');
1731 $this->assertEqual($controller->params['action'], 'index');
1732 $this->assertFalse(isset($controller->params['pass'][0]));
1734 $url = '/test_plugin/tests/index/some_param';
1735 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1736 $this->assertEqual($controller->params['controller'], 'tests');
1737 $this->assertEqual($controller->params['plugin'], 'test_plugin');
1738 $this->assertEqual($controller->params['action'], 'index');
1739 $this->assertEqual($controller->params['pass'][0], 'some_param');
1741 App::build();
1745 * testAutomaticPluginControllerMissingActionDispatch method
1747 * @return void
1748 * @access public
1750 function testAutomaticPluginControllerMissingActionDispatch() {
1751 $_POST = array();
1752 $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
1754 Router::reload();
1755 $Dispatcher =& new TestDispatcher();
1756 $Dispatcher->base = false;
1758 $url = 'my_plugin/not_here/param:value/param2:value2';
1759 $controller = $Dispatcher->dispatch($url, array('return'=> 1));
1761 $expected = array('missingAction', array(array(
1762 'className' => 'MyPluginController',
1763 'action' => 'not_here',
1764 'webroot' => '/cake/repo/branches/1.2.x.x/',
1765 'url' => '/cake/repo/branches/1.2.x.x/my_plugin/not_here/param:value/param2:value2',
1766 'base' => '/cake/repo/branches/1.2.x.x'
1767 )));
1768 $this->assertIdentical($expected, $controller);
1770 Router::reload();
1771 $Dispatcher =& new TestDispatcher();
1772 $Dispatcher->base = false;
1774 $url = 'my_plugin/param:value/param2:value2';
1775 $controller = $Dispatcher->dispatch($url, array('return'=> 1));
1777 $expected = array('missingAction', array(array(
1778 'className' => 'MyPluginController',
1779 'action' => 'param:value',
1780 'webroot' => '/cake/repo/branches/1.2.x.x/',
1781 'url' => '/cake/repo/branches/1.2.x.x/my_plugin/param:value/param2:value2',
1782 'base' => '/cake/repo/branches/1.2.x.x'
1783 )));
1784 $this->assertIdentical($expected, $controller);
1788 * testPrefixProtection method
1790 * @return void
1791 * @access public
1793 function testPrefixProtection() {
1794 $_POST = array();
1795 $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
1797 Router::reload();
1798 Router::connect('/admin/:controller/:action/*', array('prefix'=>'admin'), array('controller', 'action'));
1800 $Dispatcher =& new TestDispatcher();
1801 $Dispatcher->base = false;
1803 $url = 'test_dispatch_pages/admin_index/param:value/param2:value2';
1804 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1806 $expected = array('privateAction', array(array(
1807 'className' => 'TestDispatchPagesController',
1808 'action' => 'admin_index',
1809 'webroot' => '/cake/repo/branches/1.2.x.x/',
1810 'url' => 'test_dispatch_pages/admin_index/param:value/param2:value2',
1811 'base' => '/cake/repo/branches/1.2.x.x'
1812 )));
1813 $this->assertIdentical($expected, $controller);
1817 * Test dispatching into the TestPlugin in the test_app
1819 * @return void
1820 * @access public
1822 function testTestPluginDispatch() {
1823 $Dispatcher =& new TestDispatcher();
1824 App::build(array(
1825 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
1827 App::objects('plugin', null, false);
1828 Router::reload();
1829 Router::parse('/');
1831 $url = '/test_plugin/tests/index';
1832 $result = $Dispatcher->dispatch($url, array('return' => 1));
1833 $this->assertTrue(class_exists('TestsController'));
1834 $this->assertTrue(class_exists('TestPluginAppController'));
1835 $this->assertTrue(class_exists('OtherComponentComponent'));
1836 $this->assertTrue(class_exists('PluginsComponentComponent'));
1838 $this->assertEqual($result->params['controller'], 'tests');
1839 $this->assertEqual($result->params['plugin'], 'test_plugin');
1840 $this->assertEqual($result->params['action'], 'index');
1842 App::build();
1846 * testChangingParamsFromBeforeFilter method
1848 * @return void
1849 * @access public
1851 function testChangingParamsFromBeforeFilter() {
1852 $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
1853 $Dispatcher =& new TestDispatcher();
1854 $url = 'some_posts/index/param:value/param2:value2';
1855 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1857 $expected = array('missingAction', array(array(
1858 'className' => 'SomePostsController',
1859 'action' => 'view',
1860 'webroot' => '/cake/repo/branches/1.2.x.x/',
1861 'url' => '/cake/repo/branches/1.2.x.x/some_posts/index/param:value/param2:value2',
1862 'base' => '/cake/repo/branches/1.2.x.x'
1863 )));
1864 $this->assertEqual($expected, $controller);
1866 $url = 'some_posts/something_else/param:value/param2:value2';
1867 $controller = $Dispatcher->dispatch($url, array('return' => 1));
1869 $expected = 'SomePosts';
1870 $this->assertEqual($expected, $controller->name);
1872 $expected = 'change';
1873 $this->assertEqual($expected, $controller->action);
1875 $expected = array('changed');
1876 $this->assertIdentical($expected, $controller->params['pass']);
1880 * testStaticAssets method
1882 * @return void
1883 * @access public
1885 function testAssets() {
1886 Router::reload();
1887 $Configure =& Configure::getInstance();
1888 $Configure->__objects = null;
1890 App::build(array(
1891 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS),
1892 'vendors' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors'. DS),
1893 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
1896 $Dispatcher =& new TestDispatcher();
1897 $debug = Configure::read('debug');
1898 Configure::write('debug', 0);
1900 ob_start();
1901 $Dispatcher->dispatch('theme/test_theme/../webroot/css/test_asset.css');
1902 $result = ob_get_clean();
1903 $this->assertFalse($result);
1905 ob_start();
1906 $Dispatcher->dispatch('theme/test_theme/pdfs');
1907 $result = ob_get_clean();
1908 $this->assertFalse($result);
1910 ob_start();
1911 $Dispatcher->dispatch('theme/test_theme/flash/theme_test.swf');
1912 $result = ob_get_clean();
1913 $file = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'webroot' . DS . 'flash' . DS . 'theme_test.swf');
1914 $this->assertEqual($file, $result);
1915 $this->assertEqual('this is just a test to load swf file from the theme.', $result);
1917 ob_start();
1918 $Dispatcher->dispatch('theme/test_theme/pdfs/theme_test.pdf');
1919 $result = ob_get_clean();
1920 $file = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'webroot' . DS . 'pdfs' . DS . 'theme_test.pdf');
1921 $this->assertEqual($file, $result);
1922 $this->assertEqual('this is just a test to load pdf file from the theme.', $result);
1924 ob_start();
1925 $Dispatcher->dispatch('theme/test_theme/img/test.jpg');
1926 $result = ob_get_clean();
1927 $file = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS . 'themed' . DS . 'test_theme' . DS . 'webroot' . DS . 'img' . DS . 'test.jpg');
1928 $this->assertEqual($file, $result);
1930 $Dispatcher->params = $Dispatcher->parseParams('theme/test_theme/css/test_asset.css');
1931 ob_start();
1932 $Dispatcher->asset('theme/test_theme/css/test_asset.css');
1933 $result = ob_get_clean();
1934 $this->assertEqual('this is the test asset css file', $result);
1936 $Dispatcher->params = $Dispatcher->parseParams('theme/test_theme/js/theme.js');
1937 ob_start();
1938 $Dispatcher->asset('theme/test_theme/js/theme.js');
1939 $result = ob_get_clean();
1940 $this->assertEqual('root theme js file', $result);
1942 $Dispatcher->params = $Dispatcher->parseParams('theme/test_theme/js/one/theme_one.js');
1943 ob_start();
1944 $Dispatcher->asset('theme/test_theme/js/one/theme_one.js');
1945 $result = ob_get_clean();
1946 $this->assertEqual('nested theme js file', $result);
1948 ob_start();
1949 $Dispatcher->asset('test_plugin/root.js');
1950 $result = ob_get_clean();
1951 $expected = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'webroot' . DS . 'root.js');
1952 $this->assertEqual($result, $expected);
1954 ob_start();
1955 $Dispatcher->dispatch('test_plugin/flash/plugin_test.swf');
1956 $result = ob_get_clean();
1957 $file = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'webroot' . DS . 'flash' . DS . 'plugin_test.swf');
1958 $this->assertEqual($file, $result);
1959 $this->assertEqual('this is just a test to load swf file from the plugin.', $result);
1961 ob_start();
1962 $Dispatcher->dispatch('test_plugin/pdfs/plugin_test.pdf');
1963 $result = ob_get_clean();
1964 $file = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' . DS . 'webroot' . DS . 'pdfs' . DS . 'plugin_test.pdf');
1965 $this->assertEqual($file, $result);
1966 $this->assertEqual('this is just a test to load pdf file from the plugin.', $result);
1968 ob_start();
1969 $Dispatcher->asset('test_plugin/js/test_plugin/test.js');
1970 $result = ob_get_clean();
1971 $this->assertEqual('alert("Test App");', $result);
1973 $Dispatcher->params = $Dispatcher->parseParams('test_plugin/js/test_plugin/test.js');
1974 ob_start();
1975 $Dispatcher->asset('test_plugin/js/test_plugin/test.js');
1976 $result = ob_get_clean();
1977 $this->assertEqual('alert("Test App");', $result);
1979 $Dispatcher->params = $Dispatcher->parseParams('test_plugin/css/test_plugin_asset.css');
1980 ob_start();
1981 $Dispatcher->asset('test_plugin/css/test_plugin_asset.css');
1982 $result = ob_get_clean();
1983 $this->assertEqual('this is the test plugin asset css file', $result);
1985 $Dispatcher->params = $Dispatcher->parseParams('test_plugin/img/cake.icon.gif');
1986 ob_start();
1987 $Dispatcher->asset('test_plugin/img/cake.icon.gif');
1988 $result = ob_get_clean();
1989 $file = file_get_contents(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS . 'test_plugin' .DS . 'webroot' . DS . 'img' . DS . 'cake.icon.gif');
1990 $this->assertEqual($file, $result);
1992 $Dispatcher->params = $Dispatcher->parseParams('plugin_js/js/plugin_js.js');
1993 ob_start();
1994 $Dispatcher->asset('plugin_js/js/plugin_js.js');
1995 $result = ob_get_clean();
1996 $expected = "alert('win sauce');";
1997 $this->assertEqual($result, $expected);
1999 $Dispatcher->params = $Dispatcher->parseParams('plugin_js/js/one/plugin_one.js');
2000 ob_start();
2001 $Dispatcher->asset('plugin_js/js/one/plugin_one.js');
2002 $result = ob_get_clean();
2003 $expected = "alert('plugin one nested js file');";
2004 $this->assertEqual($result, $expected);
2005 Configure::write('debug', $debug);
2006 //reset the header content-type without page can render as plain text.
2007 header('Content-type: text/html');
2009 $Dispatcher->params = $Dispatcher->parseParams('test_plugin/css/theme_one.htc');
2010 ob_start();
2011 $Dispatcher->asset('test_plugin/css/unknown.extension');
2012 $result = ob_get_clean();
2013 $this->assertEqual('Testing a file with unknown extension to mime mapping.', $result);
2014 header('Content-type: text/html');
2016 $Dispatcher->params = $Dispatcher->parseParams('test_plugin/css/theme_one.htc');
2017 ob_start();
2018 $Dispatcher->asset('test_plugin/css/theme_one.htc');
2019 $result = ob_get_clean();
2020 $this->assertEqual('htc file', $result);
2021 header('Content-type: text/html');
2025 * test that missing asset processors trigger a 404 with no response body.
2027 * @return void
2029 function testMissingAssetProcessor404() {
2030 $Dispatcher =& new TestDispatcher();
2031 Configure::write('Asset.filter', array(
2032 'js' => '',
2033 'css' => null
2035 $this->assertNoErrors();
2037 ob_start();
2038 $Dispatcher->asset('ccss/cake.generic.css');
2039 $result = ob_get_clean();
2040 $this->assertTrue($Dispatcher->stopped);
2042 header('HTTP/1.1 200 Ok');
2046 * test that asset filters work for theme and plugin assets
2048 * @return void
2050 function testAssetFilterForThemeAndPlugins() {
2051 $Dispatcher =& new TestDispatcher();
2052 Configure::write('Asset.filter', array(
2053 'js' => '',
2054 'css' => ''
2056 $Dispatcher->asset('theme/test_theme/ccss/cake.generic.css');
2057 $this->assertTrue($Dispatcher->stopped);
2059 $Dispatcher->stopped = false;
2060 $Dispatcher->asset('theme/test_theme/cjs/debug_kit.js');
2061 $this->assertTrue($Dispatcher->stopped);
2063 $Dispatcher->stopped = false;
2064 $Dispatcher->asset('test_plugin/ccss/cake.generic.css');
2065 $this->assertTrue($Dispatcher->stopped);
2067 $Dispatcher->stopped = false;
2068 $Dispatcher->asset('test_plugin/cjs/debug_kit.js');
2069 $this->assertTrue($Dispatcher->stopped);
2071 $Dispatcher->stopped = false;
2072 $Dispatcher->asset('css/ccss/debug_kit.css');
2073 $this->assertFalse($Dispatcher->stopped);
2075 $Dispatcher->stopped = false;
2076 $Dispatcher->asset('js/cjs/debug_kit.js');
2077 $this->assertFalse($Dispatcher->stopped);
2080 * testFullPageCachingDispatch method
2082 * @return void
2083 * @access public
2085 function testFullPageCachingDispatch() {
2086 Configure::write('Cache.disable', false);
2087 Configure::write('Cache.check', true);
2088 Configure::write('debug', 2);
2090 $_POST = array();
2091 $_SERVER['PHP_SELF'] = '/';
2093 Router::reload();
2094 Router::connect('/', array('controller' => 'test_cached_pages', 'action' => 'index'));
2096 App::build(array(
2097 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS),
2098 ), true);
2100 $dispatcher =& new TestDispatcher();
2101 $dispatcher->base = false;
2103 $url = '/';
2105 ob_start();
2106 $dispatcher->dispatch($url);
2107 $out = ob_get_clean();
2109 ob_start();
2110 $dispatcher->cached($url);
2111 $cached = ob_get_clean();
2113 $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
2114 $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
2115 $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
2117 $this->assertEqual($result, $expected);
2119 $filename = $this->__cachePath($dispatcher->here);
2120 unlink($filename);
2122 $dispatcher->base = false;
2123 $url = 'test_cached_pages/index';
2125 ob_start();
2126 $dispatcher->dispatch($url);
2127 $out = ob_get_clean();
2129 ob_start();
2130 $dispatcher->cached($url);
2131 $cached = ob_get_clean();
2133 $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
2134 $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
2135 $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
2137 $this->assertEqual($result, $expected);
2138 $filename = $this->__cachePath($dispatcher->here);
2139 unlink($filename);
2141 $url = 'TestCachedPages/index';
2143 ob_start();
2144 $dispatcher->dispatch($url);
2145 $out = ob_get_clean();
2147 ob_start();
2148 $dispatcher->cached($url);
2149 $cached = ob_get_clean();
2151 $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
2152 $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
2153 $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
2155 $this->assertEqual($result, $expected);
2156 $filename = $this->__cachePath($dispatcher->here);
2157 unlink($filename);
2159 $url = 'TestCachedPages/test_nocache_tags';
2161 ob_start();
2162 $dispatcher->dispatch($url);
2163 $out = ob_get_clean();
2165 ob_start();
2166 $dispatcher->cached($url);
2167 $cached = ob_get_clean();
2169 $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
2170 $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
2171 $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
2173 $this->assertEqual($result, $expected);
2174 $filename = $this->__cachePath($dispatcher->here);
2175 unlink($filename);
2177 $url = 'test_cached_pages/view/param/param';
2179 ob_start();
2180 $dispatcher->dispatch($url);
2181 $out = ob_get_clean();
2183 ob_start();
2184 $dispatcher->cached($url);
2185 $cached = ob_get_clean();
2187 $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
2188 $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
2189 $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
2191 $this->assertEqual($result, $expected);
2192 $filename = $this->__cachePath($dispatcher->here);
2193 unlink($filename);
2195 $url = 'test_cached_pages/view/foo:bar/value:goo';
2197 ob_start();
2198 $dispatcher->dispatch($url);
2199 $out = ob_get_clean();
2201 ob_start();
2202 $dispatcher->cached($url);
2203 $cached = ob_get_clean();
2205 $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
2206 $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
2207 $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
2209 $this->assertEqual($result, $expected);
2210 $filename = $this->__cachePath($dispatcher->here);
2211 $this->assertTrue(file_exists($filename));
2212 unlink($filename);
2216 * test that cached() registers a view and un-registers it. Tests
2217 * that helpers using ClassRegistry::getObject('view'); don't fail
2219 * @return void
2221 function testCachedRegisteringViewObject() {
2222 Configure::write('Cache.disable', false);
2223 Configure::write('Cache.check', true);
2224 Configure::write('debug', 2);
2226 $_POST = array();
2227 $_SERVER['PHP_SELF'] = '/';
2229 Router::reload();
2230 App::build(array(
2231 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
2234 $dispatcher =& new TestDispatcher();
2235 $dispatcher->base = false;
2237 $url = 'test_cached_pages/cache_form';
2238 ob_start();
2239 $dispatcher->dispatch($url);
2240 $out = ob_get_clean();
2242 ClassRegistry::flush();
2244 ob_start();
2245 $dispatcher->cached($url);
2246 $cached = ob_get_clean();
2248 $result = str_replace(array("\t", "\r\n", "\n"), "", $out);
2249 $cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
2250 $expected = str_replace(array("\t", "\r\n", "\n"), "", $cached);
2252 $this->assertEqual($result, $expected);
2253 $filename = $this->__cachePath($dispatcher->here);
2254 @unlink($filename);
2255 ClassRegistry::flush();
2259 * testHttpMethodOverrides method
2261 * @return void
2262 * @access public
2264 function testHttpMethodOverrides() {
2265 Router::reload();
2266 Router::mapResources('Posts');
2268 $_SERVER['REQUEST_METHOD'] = 'POST';
2269 $dispatcher =& new Dispatcher();
2270 $dispatcher->base = false;
2272 $result = $dispatcher->parseParams('/posts');
2273 $expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST', 'form' => array(), 'url' => array());
2274 $this->assertEqual($result, $expected);
2276 $_SERVER['REQUEST_METHOD'] = 'GET';
2277 $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
2279 $result = $dispatcher->parseParams('/posts/5');
2280 $expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'form' => array(), 'url' => array());
2281 $this->assertEqual($result, $expected);
2283 unset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
2284 $_SERVER['REQUEST_METHOD'] = 'GET';
2286 $result = $dispatcher->parseParams('/posts/5');
2287 $expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '[method]' => 'GET', 'form' => array(), 'url' => array());
2288 $this->assertEqual($result, $expected);
2290 $_POST['_method'] = 'PUT';
2292 $result = $dispatcher->parseParams('/posts/5');
2293 $expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT', 'form' => array(), 'url' => array());
2294 $this->assertEqual($result, $expected);
2296 $_POST['_method'] = 'POST';
2297 $_POST['data'] = array('Post' => array('title' => 'New Post'));
2298 $_POST['extra'] = 'data';
2299 $_SERVER = array();
2301 $result = $dispatcher->parseParams('/posts');
2302 $expected = array(
2303 'pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add',
2304 '[method]' => 'POST', 'form' => array('extra' => 'data'), 'data' => array('Post' => array('title' => 'New Post')),
2305 'url' => array()
2307 $this->assertEqual($result, $expected);
2309 unset($_POST['_method']);
2313 * Tests that invalid characters cannot be injected into the application base path.
2315 * @return void
2316 * @access public
2318 function testBasePathInjection() {
2319 $self = $_SERVER['PHP_SELF'];
2320 $_SERVER['PHP_SELF'] = urldecode(
2321 "/index.php/%22%3E%3Ch1%20onclick=%22alert('xss');%22%3Eheya%3C/h1%3E"
2324 $dispatcher =& new Dispatcher();
2325 $result = $dispatcher->baseUrl();
2326 $expected = '/index.php/h1 onclick=alert(xss);heya';
2327 $this->assertEqual($result, $expected);
2331 * testEnvironmentDetection method
2333 * @return void
2334 * @access public
2336 function testEnvironmentDetection() {
2337 $dispatcher =& new Dispatcher();
2339 $environments = array(
2340 'IIS' => array(
2341 'No rewrite base path' => array(
2342 'App' => array('base' => false, 'baseUrl' => '/index.php?', 'server' => 'IIS'),
2343 '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', '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_HOST' => 'localhost', 'argv' => array(), 'argc' => 0),
2344 'reload' => true,
2345 'path' => ''
2347 'No rewrite with path' => array(
2348 'SERVER' => array('QUERY_STRING' => '/posts/add', 'REQUEST_URI' => '/index.php?/posts/add', 'URL' => '/index.php?/posts/add', 'argv' => array('/posts/add'), 'argc' => 1),
2349 'reload' => false,
2350 'path' => '/posts/add'
2352 'No rewrite sub dir 1' => array(
2353 'GET' => array(),
2354 '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),
2355 'reload' => false,
2356 'path' => ''
2358 'No rewrite sub dir 1 with path' => array(
2359 'GET' => array('/posts/add' => ''),
2360 '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),
2361 'reload' => false,
2362 'path' => '/posts/add'
2364 'No rewrite sub dir 2' => array(
2365 'App' => array('base' => false, 'baseUrl' => '/site/index.php?', 'dir' => 'app', 'webroot' => 'webroot', 'server' => 'IIS'),
2366 'GET' => array(),
2367 'POST' => array(),
2368 '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),
2369 'reload' => false,
2370 'path' => ''
2372 'No rewrite sub dir 2 with path' => array(
2373 'GET' => array('/posts/add' => ''),
2374 '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),
2375 'reload' => false,
2376 'path' => '/posts/add'
2379 'Apache' => array(
2380 'No rewrite base path' => array(
2381 'App' => array('base' => false, 'baseUrl' => '/index.php', 'dir' => 'app', 'webroot' => 'webroot'),
2382 'SERVER' => array(
2383 'SERVER_NAME' => 'localhost',
2384 'SERVER_ADDR' => '::1',
2385 'SERVER_PORT' => '80',
2386 'REMOTE_ADDR' => '::1',
2387 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/officespace/app/webroot',
2388 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
2389 'QUERY_STRING' => '',
2390 'REQUEST_URI' => '/',
2391 'SCRIPT_NAME' => '/index.php',
2392 'PHP_SELF' => '/index.php',
2393 'argv' => array(),
2394 'argc' => 0
2396 'reload' => true,
2397 'path' => ''
2399 'No rewrite with path' => array(
2400 'SERVER' => array(
2401 'HTTP_HOST' => 'localhost',
2402 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/officespace/app/webroot',
2403 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/officespace/app/webroot/index.php',
2404 'QUERY_STRING' => '',
2405 'REQUEST_URI' => '/index.php/posts/add',
2406 'SCRIPT_NAME' => '/index.php',
2407 'PATH_INFO' => '/posts/add',
2408 'PHP_SELF' => '/index.php/posts/add',
2409 'argv' => array(),
2410 'argc' => 0),
2411 'reload' => false,
2412 'path' => '/posts/add'
2414 'GET Request at base domain' => array(
2415 'App' => array('base' => false, 'baseUrl' => null, 'dir' => 'app', 'webroot' => 'webroot'),
2416 'SERVER' => array(
2417 'HTTP_HOST' => 'cake.1.2',
2418 'SERVER_NAME' => 'cake.1.2',
2419 'SERVER_ADDR' => '127.0.0.1',
2420 'SERVER_PORT' => '80',
2421 'REMOTE_ADDR' => '127.0.0.1',
2422 'DOCUMENT_ROOT' => '/Volumes/Home/htdocs/cake/repo/branches/1.2.x.x/app/webroot',
2423 'SCRIPT_FILENAME' => '/Volumes/Home/htdocs/cake/repo/branches/1.2.x.x/app/webroot/index.php',
2424 'REMOTE_PORT' => '53550',
2425 'QUERY_STRING' => 'a=b',
2426 'REQUEST_URI' => '/?a=b',
2427 'SCRIPT_NAME' => '/index.php',
2428 'PHP_SELF' => '/index.php'
2430 'GET' => array('a' => 'b'),
2431 'POST' => array(),
2432 'reload' => true,
2433 'path' => '',
2434 'urlParams' => array('a' => 'b'),
2435 'environment' => array('CGI_MODE' => false)
2437 'New CGI no mod_rewrite' => array(
2438 'App' => array('base' => false, 'baseUrl' => '/limesurvey20/index.php', 'dir' => 'app', 'webroot' => 'webroot'),
2439 'SERVER' => array(
2440 'DOCUMENT_ROOT' => '/home/.sites/110/site313/web',
2441 'PATH_INFO' => '/installations',
2442 'PATH_TRANSLATED' => '/home/.sites/110/site313/web/limesurvey20/index.php',
2443 'PHPRC' => '/home/.sites/110/site313',
2444 'QUERY_STRING' => '',
2445 'REQUEST_URI' => '/limesurvey20/index.php/installations',
2446 'SCRIPT_FILENAME' => '/home/.sites/110/site313/web/limesurvey20/index.php',
2447 'SCRIPT_NAME' => '/limesurvey20/index.php',
2448 'SCRIPT_URI' => 'http://www.gisdat-umfragen.at/limesurvey20/index.php/installations',
2449 'PHP_SELF' => '/limesurvey20/index.php/installations',
2450 'CGI_MODE' => true
2452 'GET' => array(),
2453 'POST' => array(),
2454 'reload' => true,
2455 'path' => '/installations',
2456 'urlParams' => array(),
2457 'environment' => array('CGI_MODE' => true)
2461 $backup = $this->__backupEnvironment();
2463 foreach ($environments as $name => $env) {
2464 foreach ($env as $descrip => $settings) {
2465 if ($settings['reload']) {
2466 $this->__reloadEnvironment();
2468 $this->__loadEnvironment($settings);
2469 $this->assertEqual($dispatcher->uri(), $settings['path'], "%s on environment: {$name}, on setting: {$descrip}");
2471 if (isset($settings['urlParams'])) {
2472 $this->assertEqual($_GET, $settings['urlParams'], "%s on environment: {$name}, on setting: {$descrip}");
2474 if (isset($settings['environment'])) {
2475 foreach ($settings['environment'] as $key => $val) {
2476 $this->assertEqual(env($key), $val, "%s on key {$key} on environment: {$name}, on setting: {$descrip}");
2481 $this->__loadEnvironment(array_merge(array('reload' => true), $backup));
2485 * Tests that the Dispatcher does not return an empty action
2487 * @return void
2488 * @access public
2490 function testTrailingSlash() {
2491 $_POST = array();
2492 $_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
2494 Router::reload();
2495 $Dispatcher =& new TestDispatcher();
2496 Router::connect('/myalias/:action/*', array('controller' => 'my_controller', 'action' => null));
2498 $Dispatcher->base = false;
2499 $url = 'myalias/'; //Fails
2500 $controller = $Dispatcher->dispatch($url, array('return' => 1));
2501 $result = $Dispatcher->parseParams($url);
2502 $this->assertEqual('index', $result['action']);
2504 $url = 'myalias'; //Passes
2505 $controller = $Dispatcher->dispatch($url, array('return' => 1));
2506 $result = $Dispatcher->parseParams($url);
2507 $this->assertEqual('index', $result['action']);
2511 * backupEnvironment method
2513 * @return void
2514 * @access private
2516 function __backupEnvironment() {
2517 return array(
2518 'App' => Configure::read('App'),
2519 'GET' => $_GET,
2520 'POST' => $_POST,
2521 'SERVER'=> $_SERVER
2526 * reloadEnvironment method
2528 * @return void
2529 * @access private
2531 function __reloadEnvironment() {
2532 foreach ($_GET as $key => $val) {
2533 unset($_GET[$key]);
2535 foreach ($_POST as $key => $val) {
2536 unset($_POST[$key]);
2538 foreach ($_SERVER as $key => $val) {
2539 unset($_SERVER[$key]);
2541 Configure::write('App', array());
2545 * loadEnvironment method
2547 * @param mixed $env
2548 * @return void
2549 * @access private
2551 function __loadEnvironment($env) {
2552 if ($env['reload']) {
2553 $this->__reloadEnvironment();
2556 if (isset($env['App'])) {
2557 Configure::write('App', $env['App']);
2560 if (isset($env['GET'])) {
2561 foreach ($env['GET'] as $key => $val) {
2562 $_GET[$key] = $val;
2566 if (isset($env['POST'])) {
2567 foreach ($env['POST'] as $key => $val) {
2568 $_POST[$key] = $val;
2572 if (isset($env['SERVER'])) {
2573 foreach ($env['SERVER'] as $key => $val) {
2574 $_SERVER[$key] = $val;
2580 * cachePath method
2582 * @param mixed $her
2583 * @return string
2584 * @access private
2586 function __cachePath($here) {
2587 $path = $here;
2588 if ($here == '/') {
2589 $path = 'home';
2591 $path = strtolower(Inflector::slug($path));
2593 $filename = CACHE . 'views' . DS . $path . '.php';
2595 if (!file_exists($filename)) {
2596 $filename = CACHE . 'views' . DS . $path . '_index.php';
2598 return $filename;