MvcTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. /**
  237. * @group ZF-11664
  238. */
  239. public function testIsActiveWithoutAndWithRecursiveOption()
  240. {
  241. // Parent
  242. $page = new Zend_Navigation_Page_Mvc(array(
  243. 'controller' => 'index',
  244. 'action' => 'index',
  245. ));
  246. // Child
  247. $page->addPage(new Zend_Navigation_Page_Mvc(array(
  248. 'controller' => 'index',
  249. 'action' => 'foo',
  250. )));
  251. // Front controller
  252. $this->_front->getRequest()->setParams(array(
  253. 'controller' => 'index',
  254. 'action' => 'foo'
  255. ));
  256. $this->assertFalse($page->isActive());
  257. $this->assertTrue($page->isActive(true));
  258. }
  259. public function testActionAndControllerAccessors()
  260. {
  261. $page = new Zend_Navigation_Page_Mvc(array(
  262. 'label' => 'foo',
  263. 'action' => 'index',
  264. 'controller' => 'index'
  265. ));
  266. $props = array('Action', 'Controller');
  267. $valids = array('index', 'help', 'home', 'default', '1', ' ', '', null);
  268. $invalids = array(42, (object) null);
  269. foreach ($props as $prop) {
  270. $setter = "set$prop";
  271. $getter = "get$prop";
  272. foreach ($valids as $valid) {
  273. $page->$setter($valid);
  274. $this->assertEquals($valid, $page->$getter());
  275. }
  276. foreach ($invalids as $invalid) {
  277. try {
  278. $page->$setter($invalid);
  279. $msg = "'$invalid' is invalid for $setter(), but no ";
  280. $msg .= 'Zend_Navigation_Exception was thrown';
  281. $this->fail($msg);
  282. } catch (Zend_Navigation_Exception $e) {
  283. }
  284. }
  285. }
  286. }
  287. public function testModuleAndRouteAccessors()
  288. {
  289. $page = new Zend_Navigation_Page_Mvc(array(
  290. 'label' => 'foo',
  291. 'action' => 'index',
  292. 'controller' => 'index'
  293. ));
  294. $props = array('Module', 'Route');
  295. $valids = array('index', 'help', 'home', 'default', '1', ' ', null);
  296. $invalids = array(42, (object) null);
  297. foreach ($props as $prop) {
  298. $setter = "set$prop";
  299. $getter = "get$prop";
  300. foreach ($valids as $valid) {
  301. $page->$setter($valid);
  302. $this->assertEquals($valid, $page->$getter());
  303. }
  304. foreach ($invalids as $invalid) {
  305. try {
  306. $page->$setter($invalid);
  307. $msg = "'$invalid' is invalid for $setter(), but no ";
  308. $msg .= 'Zend_Navigation_Exception was thrown';
  309. $this->fail($msg);
  310. } catch (Zend_Navigation_Exception $e) {
  311. }
  312. }
  313. }
  314. }
  315. public function testSetAndGetResetParams()
  316. {
  317. $page = new Zend_Navigation_Page_Mvc(array(
  318. 'label' => 'foo',
  319. 'action' => 'index',
  320. 'controller' => 'index'
  321. ));
  322. $valids = array(true, 1, '1', 3.14, 'true', 'yes');
  323. foreach ($valids as $valid) {
  324. $page->setResetParams($valid);
  325. $this->assertEquals(true, $page->getResetParams());
  326. }
  327. $invalids = array(false, 0, '0', 0.0, array());
  328. foreach ($invalids as $invalid) {
  329. $page->setResetParams($invalid);
  330. $this->assertEquals(false, $page->getResetParams());
  331. }
  332. }
  333. public function testSetAndGetParams()
  334. {
  335. $page = new Zend_Navigation_Page_Mvc(array(
  336. 'label' => 'foo',
  337. 'action' => 'index',
  338. 'controller' => 'index'
  339. ));
  340. $params = array('foo' => 'bar', 'baz' => 'bat');
  341. $page->setParams($params);
  342. $this->assertEquals($params, $page->getParams());
  343. $page->setParams();
  344. $this->assertEquals(array(), $page->getParams());
  345. $page->setParams($params);
  346. $this->assertEquals($params, $page->getParams());
  347. $page->setParams(array());
  348. $this->assertEquals(array(), $page->getParams());
  349. }
  350. /**
  351. * @group ZF-11664
  352. */
  353. public function testSetActiveAndIsActive()
  354. {
  355. // Page
  356. $page = new Zend_Navigation_Page_Mvc(array(
  357. 'controller' => 'foo',
  358. 'action' => 'bar',
  359. ));
  360. // Front controller
  361. $this->_front->getRequest()->setParams(array(
  362. 'controller' => 'foo',
  363. 'action' => 'bar'
  364. ));
  365. $this->assertTrue($page->isActive());
  366. $page->setActive(false);
  367. $this->assertFalse($page->isActive());
  368. }
  369. /**
  370. * @group ZF-10465
  371. */
  372. public function testSetAndGetEncodeUrl()
  373. {
  374. $page = new Zend_Navigation_Page_Mvc(array(
  375. 'label' => 'foo',
  376. 'action' => 'index',
  377. 'controller' => 'index',
  378. ));
  379. $page->setEncodeUrl(false);
  380. $this->assertEquals(false, $page->getEncodeUrl());
  381. }
  382. /**
  383. * @group ZF-10465
  384. */
  385. public function testEncodeUrlIsRouteAware()
  386. {
  387. $page = new Zend_Navigation_Page_Mvc(array(
  388. 'label' => 'foo',
  389. 'route' => 'myroute',
  390. 'encodeUrl' => false,
  391. 'params' => array(
  392. 'contentKey' => 'pagexy/subpage',
  393. )
  394. ));
  395. $this->_front->getRouter()->addRoute(
  396. 'myroute',
  397. new Zend_Controller_Router_Route_Regex(
  398. '(.+)\.html',
  399. array(
  400. 'module' => 'default',
  401. 'controller' => 'foobar',
  402. 'action' => 'bazbat',
  403. ),
  404. array(
  405. 1 => 'contentKey'
  406. ),
  407. '%s.html'
  408. )
  409. );
  410. $this->assertEquals('/pagexy/subpage.html', $page->getHref());
  411. }
  412. public function testToArrayMethod()
  413. {
  414. $options = array(
  415. 'label' => 'foo',
  416. 'action' => 'index',
  417. 'controller' => 'index',
  418. 'module' => 'test',
  419. 'fragment' => 'bar',
  420. 'id' => 'my-id',
  421. 'class' => 'my-class',
  422. 'title' => 'my-title',
  423. 'target' => 'my-target',
  424. 'order' => 100,
  425. 'active' => true,
  426. 'visible' => false,
  427. 'encodeUrl' => false,
  428. 'foo' => 'bar',
  429. 'meaning' => 42
  430. );
  431. $page = new Zend_Navigation_Page_Mvc($options);
  432. $toArray = $page->toArray();
  433. $options['reset_params'] = true;
  434. $options['route'] = null;
  435. $options['params'] = array();
  436. $options['rel'] = array();
  437. $options['rev'] = array();
  438. $this->assertEquals(array(),
  439. array_diff_assoc($options, $page->toArray()));
  440. }
  441. public function testSpecifyingAnotherUrlHelperToGenerateHrefs()
  442. {
  443. $path = dirname(dirname(__FILE__)) . '/_files/My/UrlHelper.php';
  444. require_once $path;
  445. $newHelper = new My_UrlHelper();
  446. Zend_Navigation_Page_Mvc::setUrlHelper($newHelper);
  447. $page = new Zend_Navigation_Page_Mvc();
  448. $expected = My_UrlHelper::RETURN_URL;
  449. $actual = $page->getHref();
  450. $old = Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
  451. Zend_Navigation_Page_Mvc::setUrlHelper($old);
  452. $this->assertEquals($expected, $actual);
  453. }
  454. /**
  455. * @group ZF-11550
  456. */
  457. public function testNullValuesInMatchedRouteWillStillReturnMatchedPage()
  458. {
  459. $page = new Zend_Navigation_Page_Mvc(array(
  460. 'route' => 'default',
  461. 'module' => 'default',
  462. 'controller' => 'index',
  463. 'action' => 'index',
  464. 'label' => 'Home',
  465. 'title' => 'Home',
  466. ));
  467. $this->_front->getRouter()->addRoute(
  468. 'default',
  469. new Zend_Controller_Router_Route(
  470. ':locale/:module/:controller/:action/*',
  471. array(
  472. 'locale' => null,
  473. 'module' => 'default',
  474. 'controller' => 'index',
  475. 'action' => 'index',
  476. ),
  477. array(
  478. 'locale' => '.*',
  479. 'module' => '.*',
  480. 'controller' => '.*',
  481. 'action' => '.*',
  482. )
  483. )
  484. );
  485. $this->_front->getRequest()->setParams(array(
  486. 'locale' => 'en_US',
  487. 'module' => 'default',
  488. 'controller' => 'index',
  489. 'action' => 'index',
  490. ));
  491. $this->assertEquals(true, $page->isActive());
  492. }
  493. }