ModuleTest.php 16 KB

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