MvcTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. require_once 'Zend/Controller/Router/Route/Chain.php';
  27. /**
  28. * Tests the class Zend_Navigation_Page_Mvc
  29. *
  30. * @category Zend
  31. * @package Zend_Navigation
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Navigation
  36. */
  37. class Zend_Navigation_Page_MvcTest extends PHPUnit_Framework_TestCase
  38. {
  39. protected $_front;
  40. protected $_oldRequest;
  41. protected $_oldRouter;
  42. protected function setUp()
  43. {
  44. $this->_front = Zend_Controller_Front::getInstance();
  45. $this->_oldRequest = $this->_front->getRequest();
  46. $this->_oldRouter = $this->_front->getRouter();
  47. $this->_front->resetInstance();
  48. $this->_front->setRequest(new Zend_Controller_Request_Http());
  49. $this->_front->getRouter()->addDefaultRoutes();
  50. }
  51. protected function tearDown()
  52. {
  53. if (null !== $this->_oldRequest) {
  54. $this->_front->setRequest($this->_oldRequest);
  55. } else {
  56. $this->_front->setRequest(new Zend_Controller_Request_Http());
  57. }
  58. $this->_front->setRouter($this->_oldRouter);
  59. }
  60. public function testHrefGeneratedByUrlHelperRequiresNoRoute()
  61. {
  62. $page = new Zend_Navigation_Page_Mvc(array(
  63. 'label' => 'foo',
  64. 'action' => 'index',
  65. 'controller' => 'index'
  66. ));
  67. $page->setAction('view');
  68. $page->setController('news');
  69. $this->assertEquals('/news/view', $page->getHref());
  70. }
  71. public function testHrefGeneratedIsRouteAware()
  72. {
  73. $page = new Zend_Navigation_Page_Mvc(array(
  74. 'label' => 'foo',
  75. 'action' => 'myaction',
  76. 'controller' => 'mycontroller',
  77. 'route' => 'myroute',
  78. 'params' => array(
  79. 'page' => 1337
  80. )
  81. ));
  82. $this->_front->getRouter()->addRoute(
  83. 'myroute',
  84. new Zend_Controller_Router_Route(
  85. 'lolcat/:action/:page',
  86. array(
  87. 'module' => 'default',
  88. 'controller' => 'foobar',
  89. 'action' => 'bazbat',
  90. 'page' => 1
  91. )
  92. )
  93. );
  94. $this->assertEquals('/lolcat/myaction/1337', $page->getHref());
  95. }
  96. /**
  97. * @group ZF-8922
  98. */
  99. public function testGetHrefWithFragmentIdentifier()
  100. {
  101. $page = new Zend_Navigation_Page_Mvc(array(
  102. 'label' => 'foo',
  103. 'fragment' => 'qux',
  104. 'controller' => 'mycontroller',
  105. 'action' => 'myaction',
  106. 'route' => 'myroute',
  107. 'params' => array(
  108. 'page' => 1337
  109. )
  110. ));
  111. $this->_front->getRouter()->addRoute(
  112. 'myroute',
  113. new Zend_Controller_Router_Route(
  114. 'lolcat/:action/:page',
  115. array(
  116. 'module' => 'default',
  117. 'controller' => 'foobar',
  118. 'action' => 'bazbat',
  119. 'page' => 1
  120. )
  121. )
  122. );
  123. $this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref());
  124. }
  125. public function testIsActiveReturnsTrueOnIdenticalModuleControllerAction()
  126. {
  127. $page = new Zend_Navigation_Page_Mvc(array(
  128. 'label' => 'foo',
  129. 'action' => 'index',
  130. 'controller' => 'index'
  131. ));
  132. $this->_front->getRequest()->setParams(array(
  133. 'module' => 'default',
  134. 'controller' => 'index',
  135. 'action' => 'index'
  136. ));
  137. $this->assertEquals(true, $page->isActive());
  138. }
  139. public function testIsActiveReturnsFalseOnDifferentModuleControllerAction()
  140. {
  141. $page = new Zend_Navigation_Page_Mvc(array(
  142. 'label' => 'foo',
  143. 'action' => 'bar',
  144. 'controller' => 'index'
  145. ));
  146. $this->_front->getRequest()->setParams(array(
  147. 'module' => 'default',
  148. 'controller' => 'index',
  149. 'action' => 'index'
  150. ));
  151. $this->assertEquals(false, $page->isActive());
  152. }
  153. public function testIsActiveReturnsTrueOnIdenticalIncludingPageParams()
  154. {
  155. $page = new Zend_Navigation_Page_Mvc(array(
  156. 'label' => 'foo',
  157. 'action' => 'view',
  158. 'controller' => 'post',
  159. 'module' => 'blog',
  160. 'params' => array(
  161. 'id' => '1337'
  162. )
  163. ));
  164. $this->_front->getRequest()->setParams(array(
  165. 'module' => 'blog',
  166. 'controller' => 'post',
  167. 'action' => 'view',
  168. 'id' => '1337'
  169. ));
  170. $this->assertEquals(true, $page->isActive());
  171. }
  172. public function testIsActiveReturnsTrueWhenRequestHasMoreParams()
  173. {
  174. $page = new Zend_Navigation_Page_Mvc(array(
  175. 'label' => 'foo',
  176. 'action' => 'view',
  177. 'controller' => 'post',
  178. 'module' => 'blog'
  179. ));
  180. $this->_front->getRequest()->setParams(array(
  181. 'module' => 'blog',
  182. 'controller' => 'post',
  183. 'action' => 'view',
  184. 'id' => '1337'
  185. ));
  186. $this->assertEquals(true, $page->isActive());
  187. }
  188. public function testIsActiveReturnsFalseWhenRequestHasLessParams()
  189. {
  190. $page = new Zend_Navigation_Page_Mvc(array(
  191. 'label' => 'foo',
  192. 'action' => 'view',
  193. 'controller' => 'post',
  194. 'module' => 'blog',
  195. 'params' => array(
  196. 'id' => '1337'
  197. )
  198. ));
  199. $this->_front->getRequest()->setParams(array(
  200. 'module' => 'blog',
  201. 'controller' => 'post',
  202. 'action' => 'view',
  203. 'id' => null
  204. ));
  205. $this->assertEquals(false, $page->isActive());
  206. }
  207. public function testIsActiveIsRouteAware()
  208. {
  209. $page = new Zend_Navigation_Page_Mvc(array(
  210. 'label' => 'foo',
  211. 'action' => 'myaction',
  212. 'route' => 'myroute',
  213. 'params' => array(
  214. 'page' => 1337
  215. )
  216. ));
  217. $this->_front->getRouter()->addRoute(
  218. 'myroute',
  219. new Zend_Controller_Router_Route(
  220. 'lolcat/:action/:page',
  221. array(
  222. 'module' => 'default',
  223. 'controller' => 'foobar',
  224. 'action' => 'bazbat',
  225. 'page' => 1
  226. )
  227. )
  228. );
  229. $this->_front->getRequest()->setParams(array(
  230. 'module' => 'default',
  231. 'controller' => 'foobar',
  232. 'action' => 'myaction',
  233. 'page' => 1337
  234. ));
  235. $this->assertEquals(true, $page->isActive());
  236. }
  237. /**
  238. * @group ZF-11664
  239. */
  240. public function testIsActiveWithoutAndWithRecursiveOption()
  241. {
  242. // Parent
  243. $page = new Zend_Navigation_Page_Mvc(array(
  244. 'controller' => 'index',
  245. 'action' => 'index',
  246. ));
  247. // Child
  248. $page->addPage(new Zend_Navigation_Page_Mvc(array(
  249. 'controller' => 'index',
  250. 'action' => 'foo',
  251. )));
  252. // Front controller
  253. $this->_front->getRequest()->setParams(array(
  254. 'controller' => 'index',
  255. 'action' => 'foo'
  256. ));
  257. $this->assertFalse($page->isActive());
  258. $this->assertTrue($page->isActive(true));
  259. }
  260. public function testActionAndControllerAccessors()
  261. {
  262. $page = new Zend_Navigation_Page_Mvc(array(
  263. 'label' => 'foo',
  264. 'action' => 'index',
  265. 'controller' => 'index'
  266. ));
  267. $props = array('Action', 'Controller');
  268. $valids = array('index', 'help', 'home', 'default', '1', ' ', '', null);
  269. $invalids = array(42, (object) null);
  270. foreach ($props as $prop) {
  271. $setter = "set$prop";
  272. $getter = "get$prop";
  273. foreach ($valids as $valid) {
  274. $page->$setter($valid);
  275. $this->assertEquals($valid, $page->$getter());
  276. }
  277. foreach ($invalids as $invalid) {
  278. try {
  279. $page->$setter($invalid);
  280. $msg = "'$invalid' is invalid for $setter(), but no ";
  281. $msg .= 'Zend_Navigation_Exception was thrown';
  282. $this->fail($msg);
  283. } catch (Zend_Navigation_Exception $e) {
  284. }
  285. }
  286. }
  287. }
  288. public function testModuleAndRouteAccessors()
  289. {
  290. $page = new Zend_Navigation_Page_Mvc(array(
  291. 'label' => 'foo',
  292. 'action' => 'index',
  293. 'controller' => 'index'
  294. ));
  295. $props = array('Module', 'Route');
  296. $valids = array('index', 'help', 'home', 'default', '1', ' ', null);
  297. $invalids = array(42, (object) null);
  298. foreach ($props as $prop) {
  299. $setter = "set$prop";
  300. $getter = "get$prop";
  301. foreach ($valids as $valid) {
  302. $page->$setter($valid);
  303. $this->assertEquals($valid, $page->$getter());
  304. }
  305. foreach ($invalids as $invalid) {
  306. try {
  307. $page->$setter($invalid);
  308. $msg = "'$invalid' is invalid for $setter(), but no ";
  309. $msg .= 'Zend_Navigation_Exception was thrown';
  310. $this->fail($msg);
  311. } catch (Zend_Navigation_Exception $e) {
  312. }
  313. }
  314. }
  315. }
  316. public function testSetAndGetResetParams()
  317. {
  318. $page = new Zend_Navigation_Page_Mvc(array(
  319. 'label' => 'foo',
  320. 'action' => 'index',
  321. 'controller' => 'index'
  322. ));
  323. $valids = array(true, 1, '1', 3.14, 'true', 'yes');
  324. foreach ($valids as $valid) {
  325. $page->setResetParams($valid);
  326. $this->assertEquals(true, $page->getResetParams());
  327. }
  328. $invalids = array(false, 0, '0', 0.0, array());
  329. foreach ($invalids as $invalid) {
  330. $page->setResetParams($invalid);
  331. $this->assertEquals(false, $page->getResetParams());
  332. }
  333. }
  334. public function testSetAndGetParams()
  335. {
  336. $page = new Zend_Navigation_Page_Mvc(array(
  337. 'label' => 'foo',
  338. 'action' => 'index',
  339. 'controller' => 'index'
  340. ));
  341. $params = array('foo' => 'bar', 'baz' => 'bat');
  342. $page->setParams($params);
  343. $this->assertEquals($params, $page->getParams());
  344. $page->setParams();
  345. $this->assertEquals(array(), $page->getParams());
  346. $page->setParams($params);
  347. $this->assertEquals($params, $page->getParams());
  348. $page->setParams(array());
  349. $this->assertEquals(array(), $page->getParams());
  350. }
  351. /**
  352. * @group ZF-10727
  353. */
  354. public function testSetAndGetParam()
  355. {
  356. $page = new Zend_Navigation_Page_Mvc(array(
  357. 'label' => 'foo',
  358. 'action' => 'index',
  359. 'controller' => 'index'
  360. ));
  361. $page->setParam('foo', 'bar');
  362. $this->assertEquals('bar', $page->getParam('foo'));
  363. // Check type conversion
  364. $page->setParam(null, null);
  365. $this->assertEquals(null, $page->getParam('null'));
  366. }
  367. /**
  368. * @group ZF-10727
  369. */
  370. public function testAddParams()
  371. {
  372. $page = new Zend_Navigation_Page_Mvc(array(
  373. 'label' => 'foo',
  374. 'action' => 'index',
  375. 'controller' => 'index'
  376. ));
  377. $params = array('foo' => 'bar', 'baz' => 'bat');
  378. $page->addParams($params);
  379. $this->assertEquals($params, $page->getParams());
  380. $params2 = array('qux' => 'foobar');
  381. $page->addParams($params2);
  382. $this->assertEquals(array_merge($params, $params2), $page->getParams());
  383. }
  384. /**
  385. * @group ZF-10727
  386. */
  387. public function testRemoveParam()
  388. {
  389. $page = new Zend_Navigation_Page_Mvc(array(
  390. 'label' => 'foo',
  391. 'action' => 'index',
  392. 'controller' => 'index'
  393. ));
  394. $params = array('foo' => 'bar', 'baz' => 'bat');
  395. $page->setParams($params);
  396. $page->removeParam('foo');
  397. $this->assertEquals(array('baz' => 'bat'), $page->getParams());
  398. $this->assertNull($page->getParam('foo'));
  399. }
  400. /**
  401. * @group ZF-10727
  402. */
  403. public function testClearParams()
  404. {
  405. $page = new Zend_Navigation_Page_Mvc(array(
  406. 'label' => 'foo',
  407. 'action' => 'index',
  408. 'controller' => 'index'
  409. ));
  410. $params = array('foo' => 'bar', 'baz' => 'bat');
  411. $page->setParams($params);
  412. $page->clearParams();
  413. $this->assertEquals(array(), $page->getParams());
  414. }
  415. /**
  416. * @group ZF-11664
  417. */
  418. public function testSetActiveAndIsActive()
  419. {
  420. // Page
  421. $page = new Zend_Navigation_Page_Mvc(array(
  422. 'controller' => 'foo',
  423. 'action' => 'bar',
  424. ));
  425. // Front controller
  426. $this->_front->getRequest()->setParams(array(
  427. 'controller' => 'foo',
  428. 'action' => 'bar'
  429. ));
  430. $this->assertTrue($page->isActive());
  431. $page->setActive(false);
  432. $this->assertFalse($page->isActive());
  433. }
  434. /**
  435. * @group ZF-10465
  436. */
  437. public function testSetAndGetEncodeUrl()
  438. {
  439. $page = new Zend_Navigation_Page_Mvc(array(
  440. 'label' => 'foo',
  441. 'action' => 'index',
  442. 'controller' => 'index',
  443. ));
  444. $page->setEncodeUrl(false);
  445. $this->assertEquals(false, $page->getEncodeUrl());
  446. }
  447. /**
  448. * @group ZF-10465
  449. */
  450. public function testEncodeUrlIsRouteAware()
  451. {
  452. $page = new Zend_Navigation_Page_Mvc(array(
  453. 'label' => 'foo',
  454. 'route' => 'myroute',
  455. 'encodeUrl' => false,
  456. 'params' => array(
  457. 'contentKey' => 'pagexy/subpage',
  458. )
  459. ));
  460. $this->_front->getRouter()->addRoute(
  461. 'myroute',
  462. new Zend_Controller_Router_Route_Regex(
  463. '(.+)\.html',
  464. array(
  465. 'module' => 'default',
  466. 'controller' => 'foobar',
  467. 'action' => 'bazbat',
  468. ),
  469. array(
  470. 1 => 'contentKey'
  471. ),
  472. '%s.html'
  473. )
  474. );
  475. $this->assertEquals('/pagexy/subpage.html', $page->getHref());
  476. }
  477. /**
  478. * @group ZF-7794
  479. */
  480. public function testSetScheme()
  481. {
  482. $page = new Zend_Navigation_Page_Mvc();
  483. $page->setScheme('https');
  484. $this->assertEquals('https', $page->getScheme());
  485. }
  486. /**
  487. * @group ZF-7794
  488. */
  489. public function testOptionScheme()
  490. {
  491. $page = new Zend_Navigation_Page_Mvc(
  492. array(
  493. 'scheme' => 'https',
  494. )
  495. );
  496. $this->assertEquals('https', $page->getScheme());
  497. }
  498. /**
  499. * @group ZF-7794
  500. */
  501. public function testHrefGeneratedWithScheme()
  502. {
  503. $page = new Zend_Navigation_Page_Mvc(array(
  504. 'controller' => 'foo',
  505. 'action' => 'bar',
  506. 'scheme' => 'https',
  507. ));
  508. $_SERVER['HTTP_HOST'] = 'foobar.example.com';
  509. $this->assertEquals(
  510. 'https://foobar.example.com/foo/bar',
  511. $page->getHref()
  512. );
  513. }
  514. /**
  515. * @group ZF-7794
  516. */
  517. public function testHrefGeneratedWithSchemeIsRouteAware()
  518. {
  519. $page = new Zend_Navigation_Page_Mvc(array(
  520. 'action' => 'myaction',
  521. 'controller' => 'mycontroller',
  522. 'route' => 'myroute',
  523. 'params' => array(
  524. 'page' => 1337,
  525. ),
  526. 'scheme' => 'https',
  527. ));
  528. $this->_front->getRouter()->addRoute(
  529. 'myroute',
  530. new Zend_Controller_Router_Route(
  531. 'lolcat/:action/:page',
  532. array(
  533. 'module' => 'default',
  534. 'controller' => 'foobar',
  535. 'action' => 'bazbat',
  536. 'page' => 1,
  537. )
  538. )
  539. );
  540. $_SERVER['HTTP_HOST'] = 'foobar.example.com';
  541. $this->assertEquals(
  542. 'https://foobar.example.com/lolcat/myaction/1337',
  543. $page->getHref()
  544. );
  545. }
  546. public function testToArrayMethod()
  547. {
  548. $options = array(
  549. 'label' => 'foo',
  550. 'action' => 'index',
  551. 'controller' => 'index',
  552. 'module' => 'test',
  553. 'fragment' => 'bar',
  554. 'id' => 'my-id',
  555. 'class' => 'my-class',
  556. 'title' => 'my-title',
  557. 'target' => 'my-target',
  558. 'order' => 100,
  559. 'active' => true,
  560. 'visible' => false,
  561. 'encodeUrl' => false,
  562. 'scheme' => 'https',
  563. 'foo' => 'bar',
  564. 'meaning' => 42
  565. );
  566. $page = new Zend_Navigation_Page_Mvc($options);
  567. $toArray = $page->toArray();
  568. $options['reset_params'] = true;
  569. $options['route'] = null;
  570. $options['params'] = array();
  571. $options['rel'] = array();
  572. $options['rev'] = array();
  573. $this->assertEquals(
  574. array(),
  575. array_diff_assoc($options, $page->toArray())
  576. );
  577. }
  578. public function testSpecifyingAnotherUrlHelperToGenerateHrefs()
  579. {
  580. $path = dirname(dirname(__FILE__)) . '/_files/My/UrlHelper.php';
  581. require_once $path;
  582. $newHelper = new My_UrlHelper();
  583. Zend_Navigation_Page_Mvc::setUrlHelper($newHelper);
  584. $page = new Zend_Navigation_Page_Mvc();
  585. $expected = My_UrlHelper::RETURN_URL;
  586. $actual = $page->getHref();
  587. $old = Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
  588. Zend_Navigation_Page_Mvc::setUrlHelper($old);
  589. $this->assertEquals($expected, $actual);
  590. }
  591. /**
  592. * @group ZF-7794
  593. */
  594. public function testSpecifyingAnotherSchemeHelperToGenerateHrefs()
  595. {
  596. $path = dirname(dirname(__FILE__)) . '/_files/My/SchemeHelper.php';
  597. require_once $path;
  598. $newHelper = new My_SchemeHelper();
  599. Zend_Navigation_Page_Mvc::setSchemeHelper($newHelper);
  600. $page = new Zend_Navigation_Page_Mvc(
  601. array(
  602. 'scheme' => 'https',
  603. )
  604. );
  605. $expected = My_SchemeHelper::RETURN_URL;
  606. $actual = $page->getHref();
  607. $old = new Zend_View_Helper_ServerUrl();
  608. Zend_Navigation_Page_Mvc::setSchemeHelper($old);
  609. $this->assertEquals($expected, $actual);
  610. }
  611. /**
  612. * @group ZF-11550
  613. */
  614. public function testNullValuesInMatchedRouteWillStillReturnMatchedPage()
  615. {
  616. $page = new Zend_Navigation_Page_Mvc(array(
  617. 'route' => 'default',
  618. 'module' => 'default',
  619. 'controller' => 'index',
  620. 'action' => 'index',
  621. 'label' => 'Home',
  622. 'title' => 'Home',
  623. ));
  624. $this->_front->getRouter()->addRoute(
  625. 'default',
  626. new Zend_Controller_Router_Route(
  627. ':locale/:module/:controller/:action/*',
  628. array(
  629. 'locale' => null,
  630. 'module' => 'default',
  631. 'controller' => 'index',
  632. 'action' => 'index',
  633. ),
  634. array(
  635. 'locale' => '.*',
  636. 'module' => '.*',
  637. 'controller' => '.*',
  638. 'action' => '.*',
  639. )
  640. )
  641. );
  642. $this->_front->getRequest()->setParams(array(
  643. 'locale' => 'en_US',
  644. 'module' => 'default',
  645. 'controller' => 'index',
  646. 'action' => 'index',
  647. ));
  648. $this->assertEquals(true, $page->isActive());
  649. }
  650. /**
  651. * @group ZF-12414
  652. */
  653. public function testNullValueInParameters()
  654. {
  655. // Create pages
  656. $pages = array();
  657. $pages['home'] = new Zend_Navigation_Page_Mvc(
  658. array(
  659. 'label' => 'Home',
  660. 'route' => 'page',
  661. 'params' => array(
  662. 'slug' => '',
  663. ),
  664. )
  665. );
  666. $pages['news'] = new Zend_Navigation_Page_Mvc(
  667. array(
  668. 'label' => 'News',
  669. 'route' => 'page',
  670. 'params' => array(
  671. 'slug' => 'news',
  672. ),
  673. )
  674. );
  675. // Add route
  676. $this->_front->getRouter()->addRoute(
  677. 'page',
  678. new Zend_Controller_Router_Route_Regex(
  679. '((?!(admin|page)).*)',
  680. array(
  681. 'module' => 'page',
  682. 'controller' => 'index',
  683. 'action' => 'index',
  684. ),
  685. array(
  686. 1 => 'slug',
  687. ),
  688. '%s'
  689. )
  690. );
  691. // Set request
  692. $this->_front->getRequest()->setParams(
  693. array(
  694. 'module' => 'page',
  695. 'controller' => 'index',
  696. 'action' => 'index',
  697. 'slug' => 'news',
  698. )
  699. );
  700. $this->assertTrue($pages['news']->isActive());
  701. $this->assertFalse($pages['home']->isActive());
  702. }
  703. /**
  704. * @group ZF-11442
  705. */
  706. public function testIsActiveIsChainedRouteAware()
  707. {
  708. // Create page
  709. $page = new Zend_Navigation_Page_Mvc(
  710. array(
  711. 'action' => 'myaction',
  712. 'route' => 'myroute',
  713. 'params' => array(
  714. 'page' => 1337,
  715. 'item' => 1234
  716. )
  717. )
  718. );
  719. // Create chained route
  720. $chain = new Zend_Controller_Router_Route_Chain();
  721. $foo = new Zend_Controller_Router_Route(
  722. 'lolcat/:action',
  723. array(
  724. 'module' => 'default',
  725. 'controller' => 'foobar',
  726. 'action' => 'bazbat'
  727. )
  728. );
  729. $bar = new Zend_Controller_Router_Route(
  730. ':page/:item',
  731. array(
  732. 'page' => 1,
  733. 'item' => 1
  734. )
  735. );
  736. $chain->chain($foo)->chain($bar);
  737. // Set up router
  738. $this->_front->getRouter()->addRoute('myroute', $chain);
  739. $this->_front->getRequest()->setParams(
  740. array(
  741. 'module' => 'default',
  742. 'controller' => 'foobar',
  743. 'action' => 'myaction',
  744. 'page' => 1337,
  745. 'item' => 1234
  746. )
  747. );
  748. // Test
  749. $this->assertTrue($page->isActive());
  750. }
  751. }