ModuleTest.php 16 KB

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