MvcTest.php 15 KB

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