ModuleTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Test helper */
  23. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  24. require_once "PHPUnit/Framework/TestCase.php";
  25. require_once "PHPUnit/Framework/TestSuite.php";
  26. /** Zend_Controller_Router_Route_Module */
  27. require_once 'Zend/Controller/Router/Route/Module.php';
  28. /** Zend_Controller_Front */
  29. require_once 'Zend/Controller/Front.php';
  30. // Call Zend_Controller_Router_Route_ModuleTest::main() if this source file is executed directly.
  31. if (!defined("PHPUnit_MAIN_METHOD")) {
  32. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Router_Route_ModuleTest::main");
  33. }
  34. /**
  35. * @category Zend
  36. * @package Zend_Controller
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Controller
  41. * @group Zend_Controller_Router
  42. */
  43. class Zend_Controller_Router_Route_ModuleTest extends PHPUnit_Framework_TestCase
  44. {
  45. protected $_request;
  46. protected $_dispatcher;
  47. protected $route;
  48. /**
  49. * Runs the test methods of this class.
  50. *
  51. * @access public
  52. * @static
  53. */
  54. public static function main()
  55. {
  56. require_once "PHPUnit/TextUI/TestRunner.php";
  57. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Router_Route_ModuleTest");
  58. $result = PHPUnit_TextUI_TestRunner::run($suite);
  59. }
  60. public function setUp()
  61. {
  62. $front = Zend_Controller_Front::getInstance();
  63. $front->resetInstance();
  64. $front->setParam('noErrorHandler', true)
  65. ->setParam('noViewRenderer', true);
  66. $this->_dispatcher = $front->getDispatcher();
  67. $this->_dispatcher->setControllerDirectory(array(
  68. 'default' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files',
  69. 'mod' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Admin',
  70. ));
  71. $defaults = array(
  72. 'controller' => 'defctrl',
  73. 'action' => 'defact',
  74. 'module' => 'default'
  75. );
  76. require_once 'Zend/Controller/Request/Http.php';
  77. $this->_request = new Zend_Controller_Request_Http();
  78. $front->setRequest($this->_request);
  79. $this->route = new Zend_Controller_Router_Route_Module($defaults, $this->_dispatcher, $this->_request);
  80. }
  81. public function testModuleMatch()
  82. {
  83. $values = $this->route->match('mod');
  84. $this->assertType('array', $values);
  85. $this->assertTrue(isset($values['module']));
  86. $this->assertEquals('mod', $values['module']);
  87. }
  88. public function testModuleAndControllerMatch()
  89. {
  90. $values = $this->route->match('mod/con');
  91. $this->assertType('array', $values);
  92. $this->assertTrue(isset($values['module']));
  93. $this->assertEquals('mod', $values['module']);
  94. $this->assertTrue(isset($values['controller']));
  95. $this->assertEquals('con', $values['controller']);
  96. }
  97. public function testModuleControllerAndActionMatch()
  98. {
  99. $values = $this->route->match('mod/con/act');
  100. $this->assertType('array', $values);
  101. $this->assertTrue(isset($values['module']));
  102. $this->assertEquals('mod', $values['module']);
  103. $this->assertTrue(isset($values['controller']));
  104. $this->assertEquals('con', $values['controller']);
  105. $this->assertTrue(isset($values['action']));
  106. $this->assertEquals('act', $values['action']);
  107. }
  108. public function testModuleControllerActionAndParamsMatch()
  109. {
  110. $values = $this->route->match('mod/con/act/var/val/foo');
  111. $this->assertType('array', $values);
  112. $this->assertTrue(isset($values['module']));
  113. $this->assertEquals('mod', $values['module']);
  114. $this->assertTrue(isset($values['controller']));
  115. $this->assertEquals('con', $values['controller']);
  116. $this->assertTrue(isset($values['action']));
  117. $this->assertEquals('act', $values['action']);
  118. $this->assertTrue(isset($values['var']));
  119. $this->assertEquals('val', $values['var']);
  120. $this->assertTrue(array_key_exists('foo', $values), var_export($values, 1));
  121. $this->assertTrue(empty($values['foo']));
  122. }
  123. public function testControllerOnlyMatch()
  124. {
  125. $values = $this->route->match('con');
  126. $this->assertType('array', $values);
  127. $this->assertTrue(isset($values['controller']));
  128. $this->assertEquals('con', $values['controller']);
  129. }
  130. public function testControllerOnlyAndActionMatch()
  131. {
  132. $values = $this->route->match('con/act');
  133. $this->assertType('array', $values);
  134. $this->assertTrue(isset($values['controller']));
  135. $this->assertEquals('con', $values['controller']);
  136. $this->assertTrue(isset($values['action']));
  137. $this->assertEquals('act', $values['action']);
  138. }
  139. public function testControllerOnlyActionAndParamsMatch()
  140. {
  141. $values = $this->route->match('con/act/var/val/foo');
  142. $this->assertType('array', $values);
  143. $this->assertTrue(isset($values['controller']));
  144. $this->assertEquals('con', $values['controller']);
  145. $this->assertTrue(isset($values['action']));
  146. $this->assertEquals('act', $values['action']);
  147. $this->assertTrue(isset($values['var']));
  148. $this->assertEquals('val', $values['var']);
  149. $this->assertTrue(array_key_exists('foo', $values), var_export($values, 1));
  150. $this->assertTrue(empty($values['foo']));
  151. }
  152. public function testModuleMatchWithControlKeysChange()
  153. {
  154. $this->_request->setModuleKey('m');
  155. $this->_request->setControllerKey('c');
  156. $this->_request->setActionKey('a');
  157. $this->route = new Zend_Controller_Router_Route_Module(array(), $this->_dispatcher, $this->_request);
  158. $values = $this->route->match('mod/ctrl');
  159. $this->assertType('array', $values);
  160. $this->assertSame('mod', $values['m']);
  161. $this->assertSame('ctrl', $values['c']);
  162. $this->assertSame('index', $values['a']);
  163. }
  164. public function testModuleMatchWithLateControlKeysChange()
  165. {
  166. $this->_request->setModuleKey('m');
  167. $this->_request->setControllerKey('c');
  168. $this->_request->setActionKey('a');
  169. $values = $this->route->match('mod/ctrl');
  170. $this->assertType('array', $values);
  171. $this->assertSame('mod', $values['m'], var_export(array_keys($values), 1));
  172. $this->assertSame('ctrl', $values['c'], var_export(array_keys($values), 1));
  173. $this->assertSame('index', $values['a'], var_export(array_keys($values), 1));
  174. }
  175. public function testAssembleNoModuleOrController()
  176. {
  177. $params = array(
  178. 'action' => 'act',
  179. 'foo' => 'bar'
  180. );
  181. $url = $this->route->assemble($params);
  182. $this->assertEquals('defctrl/act/foo/bar', $url);
  183. }
  184. public function testAssembleControllerOnly()
  185. {
  186. $params = array(
  187. 'foo' => 'bar',
  188. 'action' => 'act',
  189. 'controller' => 'con'
  190. );
  191. $url = $this->route->assemble($params);
  192. $this->assertEquals('con/act/foo/bar', $url);
  193. }
  194. public function testAssembleModuleAndController()
  195. {
  196. $params = array(
  197. 'foo' => 'bar',
  198. 'action' => 'act',
  199. 'controller' => 'con',
  200. 'module' => 'mod'
  201. );
  202. $url = $this->route->assemble($params);
  203. $this->assertEquals('mod/con/act/foo/bar', $url);
  204. }
  205. public function testAssembleNoController()
  206. {
  207. $params = array(
  208. 'foo' => 'bar',
  209. 'action' => 'act',
  210. 'module' => 'mod'
  211. );
  212. $url = $this->route->assemble($params);
  213. $this->assertEquals('mod/defctrl/act/foo/bar', $url);
  214. }
  215. public function testAssembleNoAction()
  216. {
  217. $params = array(
  218. 'module' => 'mod',
  219. 'controller' => 'ctrl'
  220. );
  221. $url = $this->route->assemble($params);
  222. $this->assertEquals('mod/ctrl', $url);
  223. }
  224. public function testAssembleNoActionWithParams()
  225. {
  226. $params = array(
  227. 'foo' => 'bar',
  228. 'module' => 'mod',
  229. 'controller' => 'ctrl'
  230. );
  231. $url = $this->route->assemble($params);
  232. $this->assertEquals('mod/ctrl/defact/foo/bar', $url);
  233. }
  234. public function testAssembleNoModuleOrControllerMatched()
  235. {
  236. $this->route->match('');
  237. $params = array(
  238. 'action' => 'act',
  239. 'foo' => 'bar'
  240. );
  241. $url = $this->route->assemble($params);
  242. $this->assertEquals('defctrl/act/foo/bar', $url);
  243. }
  244. public function testAssembleControllerOnlyMatched()
  245. {
  246. $this->route->match('ctrl');
  247. $params = array(
  248. 'foo' => 'bar',
  249. 'action' => 'act',
  250. 'controller' => 'con'
  251. );
  252. $url = $this->route->assemble($params);
  253. $this->assertEquals('con/act/foo/bar', $url);
  254. }
  255. public function testAssembleModuleAndControllerMatched()
  256. {
  257. $this->route->match('mod/ctrl');
  258. $params = array(
  259. 'foo' => 'bar',
  260. 'action' => 'act',
  261. 'module' => 'm'
  262. );
  263. $url = $this->route->assemble($params);
  264. $this->assertEquals('m/ctrl/act/foo/bar', $url);
  265. }
  266. public function testAssembleNoControllerMatched()
  267. {
  268. $this->route->match('mod');
  269. $params = array(
  270. 'foo' => 'bar',
  271. 'action' => 'act',
  272. 'module' => 'mod'
  273. );
  274. $url = $this->route->assemble($params);
  275. $this->assertEquals('mod/defctrl/act/foo/bar', $url);
  276. }
  277. public function testAssembleNoActionMatched()
  278. {
  279. $this->route->match('mod/ctrl');
  280. $params = array(
  281. 'module' => 'def',
  282. 'controller' => 'con'
  283. );
  284. $url = $this->route->assemble($params);
  285. $this->assertEquals('def/con', $url);
  286. }
  287. public function testAssembleWithReset()
  288. {
  289. $values = $this->route->match('mod/con/act/sort/name');
  290. $url = $this->route->assemble(array('action' => 'new'), true);
  291. $this->assertSame('defctrl/new', $url);
  292. }
  293. public function testAssembleWithReset2()
  294. {
  295. $values = $this->route->match('mod/con/act/sort/name');
  296. $url = $this->route->assemble(array('controller' => 'new'), true);
  297. $this->assertSame('new', $url);
  298. }
  299. public function testAssembleWithReset3()
  300. {
  301. $values = $this->route->match('mod/con/act/sort/name');
  302. $url = $this->route->assemble(array('controller' => 'new', 'action' => 'test'), true);
  303. $this->assertSame('new/test', $url);
  304. }
  305. public function testAssembleResetOneVariable()
  306. {
  307. $values = $this->route->match('mod/con/act');
  308. $url = $this->route->assemble(array('action' => null), false);
  309. $this->assertSame('mod/con', $url);
  310. }
  311. public function testAssembleResetOneVariable2()
  312. {
  313. $values = $this->route->match('mod/con/act');
  314. $url = $this->route->assemble(array('controller' => null), false);
  315. $this->assertSame('mod/defctrl/act', $url);
  316. }
  317. public function testAssembleResetOneVariable3()
  318. {
  319. $values = $this->route->match('mod/con/act');
  320. $url = $this->route->assemble(array('module' => null), false);
  321. $this->assertSame('con/act', $url);
  322. }
  323. public function testAssembleDefaultModuleResetZF1415()
  324. {
  325. $values = $this->route->match('con/act');
  326. $url = $this->route->assemble(array('controller' => 'foo', 'action' => 'bar'), true);
  327. $this->assertSame('foo/bar', $url);
  328. }
  329. public function testAssembleDefaultModuleZF1415()
  330. {
  331. $values = $this->route->match('con/act');
  332. $url = $this->route->assemble(array('controller' => 'foo', 'action' => 'bar'), false);
  333. $this->assertSame('foo/bar', $url);
  334. }
  335. public function testAssembleDefaultModuleZF1415_2()
  336. {
  337. $values = $this->route->match('default/defctrl/defact');
  338. $url = $this->route->assemble();
  339. $this->assertSame('', $url);
  340. $values = $this->route->match('mod/defctrl/defact');
  341. $url = $this->route->assemble();
  342. $this->assertSame('mod', $url);
  343. }
  344. public function testGetInstance()
  345. {
  346. require_once 'Zend/Config.php';
  347. $routeConf = array(
  348. 'defaults' => array(
  349. 'controller' => 'ctrl'
  350. )
  351. );
  352. $config = new Zend_Config($routeConf);
  353. $route = Zend_Controller_Router_Route_Module::getInstance($config);
  354. $this->assertType('Zend_Controller_Router_Route_Module', $route);
  355. }
  356. public function testEncode()
  357. {
  358. $url = $this->route->assemble(array('controller' => 'My Controller'), false, true);
  359. $this->assertEquals('My+Controller', $url);
  360. $url = $this->route->assemble(array('controller' => 'My Controller'), false, false);
  361. $this->assertEquals('My Controller', $url);
  362. $token = $this->route->match('en/foo/id/My Value');
  363. $url = $this->route->assemble(array(), false, true);
  364. $this->assertEquals('en/foo/id/My+Value', $url);
  365. $url = $this->route->assemble(array('id' => 'My Other Value'), false, true);
  366. $this->assertEquals('en/foo/id/My+Other+Value', $url);
  367. }
  368. public function testArrayValues()
  369. {
  370. $url = $this->route->assemble(array('foo' => array('bar', 'baz')));
  371. $this->assertEquals('defctrl/defact/foo/bar/foo/baz', $url);
  372. $token = $this->route->match('defctrl/defact/foo/bar/foo/baz');
  373. $this->assertEquals('bar', $token['foo'][0]);
  374. $this->assertEquals('baz', $token['foo'][1]);
  375. }
  376. public function testGetInstanceMatching()
  377. {
  378. $this->route = Zend_Controller_Router_Route_Module::getInstance(new Zend_Config(array()));
  379. $this->_request->setModuleKey('m');
  380. $this->_request->setControllerKey('c');
  381. $this->_request->setActionKey('a');
  382. $values = $this->route->match('mod/ctrl');
  383. $this->assertType('array', $values);
  384. $this->assertSame('mod', $values['m'], var_export(array_keys($values), 1));
  385. $this->assertSame('ctrl', $values['c'], var_export(array_keys($values), 1));
  386. $this->assertSame('index', $values['a'], var_export(array_keys($values), 1));
  387. }
  388. /**
  389. * @group ZF-8029
  390. */
  391. public function testAssembleShouldUrlEncodeAllParameterNames()
  392. {
  393. $params = array(
  394. 'controller' => 'foo',
  395. 'action' => 'bar',
  396. '"><script>alert(11639)<' => 'script>',
  397. 'module' => 'default',
  398. );
  399. $url = $this->route->assemble($params);
  400. $this->assertNotContains('"><script>alert(11639)<', $url);
  401. }
  402. }
  403. // Call Zend_Controller_Router_Route_ModuleTest::main() if this source file is executed directly.
  404. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Router_Route_ModuleTest::main") {
  405. Zend_Controller_Router_Route_ModuleTest::main();
  406. }