MvcTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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_Navigation
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2011 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. require_once 'Zend/Navigation/Page/Mvc.php';
  23. require_once 'Zend/Controller/Request/Http.php';
  24. require_once 'Zend/Controller/Router/Route.php';
  25. /**
  26. * Tests the class Zend_Navigation_Page_Mvc
  27. *
  28. * @category Zend
  29. * @package Zend_Navigation
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Navigation
  34. */
  35. class Zend_Navigation_Page_MvcTest extends PHPUnit_Framework_TestCase
  36. {
  37. protected $_front;
  38. protected $_oldRequest;
  39. protected $_oldRouter;
  40. protected function setUp()
  41. {
  42. $this->_front = Zend_Controller_Front::getInstance();
  43. $this->_oldRequest = $this->_front->getRequest();
  44. $this->_oldRouter = $this->_front->getRouter();
  45. $this->_front->resetInstance();
  46. $this->_front->setRequest(new Zend_Controller_Request_Http());
  47. $this->_front->getRouter()->addDefaultRoutes();
  48. }
  49. protected function tearDown()
  50. {
  51. if (null !== $this->_oldRequest) {
  52. $this->_front->setRequest($this->_oldRequest);
  53. } else {
  54. $this->_front->setRequest(new Zend_Controller_Request_Http());
  55. }
  56. $this->_front->setRouter($this->_oldRouter);
  57. }
  58. public function testHrefGeneratedByUrlHelperRequiresNoRoute()
  59. {
  60. $page = new Zend_Navigation_Page_Mvc(array(
  61. 'label' => 'foo',
  62. 'action' => 'index',
  63. 'controller' => 'index'
  64. ));
  65. $page->setAction('view');
  66. $page->setController('news');
  67. $this->assertEquals('/news/view', $page->getHref());
  68. }
  69. public function testHrefGeneratedIsRouteAware()
  70. {
  71. $page = new Zend_Navigation_Page_Mvc(array(
  72. 'label' => 'foo',
  73. 'action' => 'myaction',
  74. 'controller' => 'mycontroller',
  75. 'route' => 'myroute',
  76. 'params' => array(
  77. 'page' => 1337
  78. )
  79. ));
  80. $this->_front->getRouter()->addRoute(
  81. 'myroute',
  82. new Zend_Controller_Router_Route(
  83. 'lolcat/:action/:page',
  84. array(
  85. 'module' => 'default',
  86. 'controller' => 'foobar',
  87. 'action' => 'bazbat',
  88. 'page' => 1
  89. )
  90. )
  91. );
  92. $this->assertEquals('/lolcat/myaction/1337', $page->getHref());
  93. }
  94. public function testIsActiveReturnsTrueOnIdenticalModuleControllerAction()
  95. {
  96. $page = new Zend_Navigation_Page_Mvc(array(
  97. 'label' => 'foo',
  98. 'action' => 'index',
  99. 'controller' => 'index'
  100. ));
  101. $this->_front->getRequest()->setParams(array(
  102. 'module' => 'default',
  103. 'controller' => 'index',
  104. 'action' => 'index'
  105. ));
  106. $this->assertEquals(true, $page->isActive());
  107. }
  108. public function testIsActiveReturnsFalseOnDifferentModuleControllerAction()
  109. {
  110. $page = new Zend_Navigation_Page_Mvc(array(
  111. 'label' => 'foo',
  112. 'action' => 'bar',
  113. 'controller' => 'index'
  114. ));
  115. $this->_front->getRequest()->setParams(array(
  116. 'module' => 'default',
  117. 'controller' => 'index',
  118. 'action' => 'index'
  119. ));
  120. $this->assertEquals(false, $page->isActive());
  121. }
  122. public function testIsActiveReturnsTrueOnIdenticalIncludingPageParams()
  123. {
  124. $page = new Zend_Navigation_Page_Mvc(array(
  125. 'label' => 'foo',
  126. 'action' => 'view',
  127. 'controller' => 'post',
  128. 'module' => 'blog',
  129. 'params' => array(
  130. 'id' => '1337'
  131. )
  132. ));
  133. $this->_front->getRequest()->setParams(array(
  134. 'module' => 'blog',
  135. 'controller' => 'post',
  136. 'action' => 'view',
  137. 'id' => '1337'
  138. ));
  139. $this->assertEquals(true, $page->isActive());
  140. }
  141. public function testIsActiveReturnsTrueWhenRequestHasMoreParams()
  142. {
  143. $page = new Zend_Navigation_Page_Mvc(array(
  144. 'label' => 'foo',
  145. 'action' => 'view',
  146. 'controller' => 'post',
  147. 'module' => 'blog'
  148. ));
  149. $this->_front->getRequest()->setParams(array(
  150. 'module' => 'blog',
  151. 'controller' => 'post',
  152. 'action' => 'view',
  153. 'id' => '1337'
  154. ));
  155. $this->assertEquals(true, $page->isActive());
  156. }
  157. public function testIsActiveReturnsFalseWhenRequestHasLessParams()
  158. {
  159. $page = new Zend_Navigation_Page_Mvc(array(
  160. 'label' => 'foo',
  161. 'action' => 'view',
  162. 'controller' => 'post',
  163. 'module' => 'blog',
  164. 'params' => array(
  165. 'id' => '1337'
  166. )
  167. ));
  168. $this->_front->getRequest()->setParams(array(
  169. 'module' => 'blog',
  170. 'controller' => 'post',
  171. 'action' => 'view',
  172. 'id' => null
  173. ));
  174. $this->assertEquals(false, $page->isActive());
  175. }
  176. public function testIsActiveIsRouteAware()
  177. {
  178. $page = new Zend_Navigation_Page_Mvc(array(
  179. 'label' => 'foo',
  180. 'action' => 'myaction',
  181. 'route' => 'myroute',
  182. 'params' => array(
  183. 'page' => 1337
  184. )
  185. ));
  186. $this->_front->getRouter()->addRoute(
  187. 'myroute',
  188. new Zend_Controller_Router_Route(
  189. 'lolcat/:action/:page',
  190. array(
  191. 'module' => 'default',
  192. 'controller' => 'foobar',
  193. 'action' => 'bazbat',
  194. 'page' => 1
  195. )
  196. )
  197. );
  198. $this->_front->getRequest()->setParams(array(
  199. 'module' => 'default',
  200. 'controller' => 'foobar',
  201. 'action' => 'myaction',
  202. 'page' => 1337
  203. ));
  204. $this->assertEquals(true, $page->isActive());
  205. }
  206. public function testActionAndControllerAccessors()
  207. {
  208. $page = new Zend_Navigation_Page_Mvc(array(
  209. 'label' => 'foo',
  210. 'action' => 'index',
  211. 'controller' => 'index'
  212. ));
  213. $props = array('Action', 'Controller');
  214. $valids = array('index', 'help', 'home', 'default', '1', ' ', '', null);
  215. $invalids = array(42, (object) null);
  216. foreach ($props as $prop) {
  217. $setter = "set$prop";
  218. $getter = "get$prop";
  219. foreach ($valids as $valid) {
  220. $page->$setter($valid);
  221. $this->assertEquals($valid, $page->$getter());
  222. }
  223. foreach ($invalids as $invalid) {
  224. try {
  225. $page->$setter($invalid);
  226. $msg = "'$invalid' is invalid for $setter(), but no ";
  227. $msg .= 'Zend_Navigation_Exception was thrown';
  228. $this->fail($msg);
  229. } catch (Zend_Navigation_Exception $e) {
  230. }
  231. }
  232. }
  233. }
  234. public function testModuleAndRouteAccessors()
  235. {
  236. $page = new Zend_Navigation_Page_Mvc(array(
  237. 'label' => 'foo',
  238. 'action' => 'index',
  239. 'controller' => 'index'
  240. ));
  241. $props = array('Module', 'Route');
  242. $valids = array('index', 'help', 'home', 'default', '1', ' ', null);
  243. $invalids = array(42, (object) null);
  244. foreach ($props as $prop) {
  245. $setter = "set$prop";
  246. $getter = "get$prop";
  247. foreach ($valids as $valid) {
  248. $page->$setter($valid);
  249. $this->assertEquals($valid, $page->$getter());
  250. }
  251. foreach ($invalids as $invalid) {
  252. try {
  253. $page->$setter($invalid);
  254. $msg = "'$invalid' is invalid for $setter(), but no ";
  255. $msg .= 'Zend_Navigation_Exception was thrown';
  256. $this->fail($msg);
  257. } catch (Zend_Navigation_Exception $e) {
  258. }
  259. }
  260. }
  261. }
  262. public function testSetAndGetResetParams()
  263. {
  264. $page = new Zend_Navigation_Page_Mvc(array(
  265. 'label' => 'foo',
  266. 'action' => 'index',
  267. 'controller' => 'index'
  268. ));
  269. $valids = array(true, 1, '1', 3.14, 'true', 'yes');
  270. foreach ($valids as $valid) {
  271. $page->setResetParams($valid);
  272. $this->assertEquals(true, $page->getResetParams());
  273. }
  274. $invalids = array(false, 0, '0', 0.0, array());
  275. foreach ($invalids as $invalid) {
  276. $page->setResetParams($invalid);
  277. $this->assertEquals(false, $page->getResetParams());
  278. }
  279. }
  280. public function testSetAndGetParams()
  281. {
  282. $page = new Zend_Navigation_Page_Mvc(array(
  283. 'label' => 'foo',
  284. 'action' => 'index',
  285. 'controller' => 'index'
  286. ));
  287. $params = array('foo' => 'bar', 'baz' => 'bat');
  288. $page->setParams($params);
  289. $this->assertEquals($params, $page->getParams());
  290. $page->setParams();
  291. $this->assertEquals(array(), $page->getParams());
  292. $page->setParams($params);
  293. $this->assertEquals($params, $page->getParams());
  294. $page->setParams(array());
  295. $this->assertEquals(array(), $page->getParams());
  296. }
  297. public function testToArrayMethod()
  298. {
  299. $options = array(
  300. 'label' => 'foo',
  301. 'action' => 'index',
  302. 'controller' => 'index',
  303. 'module' => 'test',
  304. 'id' => 'my-id',
  305. 'class' => 'my-class',
  306. 'title' => 'my-title',
  307. 'target' => 'my-target',
  308. 'order' => 100,
  309. 'active' => true,
  310. 'visible' => false,
  311. 'foo' => 'bar',
  312. 'meaning' => 42
  313. );
  314. $page = new Zend_Navigation_Page_Mvc($options);
  315. $toArray = $page->toArray();
  316. $options['reset_params'] = true;
  317. $options['route'] = null;
  318. $options['params'] = array();
  319. $options['rel'] = array();
  320. $options['rev'] = array();
  321. $this->assertEquals(array(),
  322. array_diff_assoc($options, $page->toArray()));
  323. }
  324. public function testSpecifyingAnotherUrlHelperToGenerateHrefs()
  325. {
  326. $path = dirname(dirname(__FILE__)) . '/_files/My/UrlHelper.php';
  327. require_once $path;
  328. $newHelper = new My_UrlHelper();
  329. Zend_Navigation_Page_Mvc::setUrlHelper($newHelper);
  330. $page = new Zend_Navigation_Page_Mvc();
  331. $expected = My_UrlHelper::RETURN_URL;
  332. $actual = $page->getHref();
  333. $old = Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
  334. Zend_Navigation_Page_Mvc::setUrlHelper($old);
  335. $this->assertEquals($expected, $actual);
  336. }
  337. /**
  338. * @group ZF-11550
  339. */
  340. public function testNullValuesInMatchedRouteWillStillReturnMatchedPage()
  341. {
  342. $page = new Zend_Navigation_Page_Mvc(array(
  343. 'route' => 'default',
  344. 'module' => 'default',
  345. 'controller' => 'index',
  346. 'action' => 'index',
  347. 'label' => 'Home',
  348. 'title' => 'Home',
  349. ));
  350. $this->_front->getRouter()->addRoute(
  351. 'default',
  352. new Zend_Controller_Router_Route(
  353. ':locale/:module/:controller/:action/*',
  354. array(
  355. 'locale' => null,
  356. 'module' => 'default',
  357. 'controller' => 'index',
  358. 'action' => 'index',
  359. ),
  360. array(
  361. 'locale' => '.*',
  362. 'module' => '.*',
  363. 'controller' => '.*',
  364. 'action' => '.*',
  365. )
  366. )
  367. );
  368. $this->_front->getRequest()->setParams(array(
  369. 'locale' => 'en_US',
  370. 'module' => 'default',
  371. 'controller' => 'index',
  372. 'action' => 'index',
  373. ));
  374. $this->assertEquals(true, $page->isActive());
  375. }
  376. }