MvcTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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-2010 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-2010 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 testActionAndControllerAccessors()
  177. {
  178. $page = new Zend_Navigation_Page_Mvc(array(
  179. 'label' => 'foo',
  180. 'action' => 'index',
  181. 'controller' => 'index'
  182. ));
  183. $props = array('Action', 'Controller');
  184. $valids = array('index', 'help', 'home', 'default', '1', ' ', '', null);
  185. $invalids = array(42, (object) null);
  186. foreach ($props as $prop) {
  187. $setter = "set$prop";
  188. $getter = "get$prop";
  189. foreach ($valids as $valid) {
  190. $page->$setter($valid);
  191. $this->assertEquals($valid, $page->$getter());
  192. }
  193. foreach ($invalids as $invalid) {
  194. try {
  195. $page->$setter($invalid);
  196. $msg = "'$invalid' is invalid for $setter(), but no ";
  197. $msg .= 'Zend_Navigation_Exception was thrown';
  198. $this->fail($msg);
  199. } catch (Zend_Navigation_Exception $e) {
  200. }
  201. }
  202. }
  203. }
  204. public function testModuleAndRouteAccessors()
  205. {
  206. $page = new Zend_Navigation_Page_Mvc(array(
  207. 'label' => 'foo',
  208. 'action' => 'index',
  209. 'controller' => 'index'
  210. ));
  211. $props = array('Module', 'Route');
  212. $valids = array('index', 'help', 'home', 'default', '1', ' ', null);
  213. $invalids = array(42, (object) null);
  214. foreach ($props as $prop) {
  215. $setter = "set$prop";
  216. $getter = "get$prop";
  217. foreach ($valids as $valid) {
  218. $page->$setter($valid);
  219. $this->assertEquals($valid, $page->$getter());
  220. }
  221. foreach ($invalids as $invalid) {
  222. try {
  223. $page->$setter($invalid);
  224. $msg = "'$invalid' is invalid for $setter(), but no ";
  225. $msg .= 'Zend_Navigation_Exception was thrown';
  226. $this->fail($msg);
  227. } catch (Zend_Navigation_Exception $e) {
  228. }
  229. }
  230. }
  231. }
  232. public function testSetAndGetResetParams()
  233. {
  234. $page = new Zend_Navigation_Page_Mvc(array(
  235. 'label' => 'foo',
  236. 'action' => 'index',
  237. 'controller' => 'index'
  238. ));
  239. $valids = array(true, 1, '1', 3.14, 'true', 'yes');
  240. foreach ($valids as $valid) {
  241. $page->setResetParams($valid);
  242. $this->assertEquals(true, $page->getResetParams());
  243. }
  244. $invalids = array(false, 0, '0', 0.0, array());
  245. foreach ($invalids as $invalid) {
  246. $page->setResetParams($invalid);
  247. $this->assertEquals(false, $page->getResetParams());
  248. }
  249. }
  250. public function testSetAndGetParams()
  251. {
  252. $page = new Zend_Navigation_Page_Mvc(array(
  253. 'label' => 'foo',
  254. 'action' => 'index',
  255. 'controller' => 'index'
  256. ));
  257. $params = array('foo' => 'bar', 'baz' => 'bat');
  258. $page->setParams($params);
  259. $this->assertEquals($params, $page->getParams());
  260. $page->setParams();
  261. $this->assertEquals(array(), $page->getParams());
  262. $page->setParams($params);
  263. $this->assertEquals($params, $page->getParams());
  264. $page->setParams(array());
  265. $this->assertEquals(array(), $page->getParams());
  266. }
  267. public function testToArrayMethod()
  268. {
  269. $options = array(
  270. 'label' => 'foo',
  271. 'action' => 'index',
  272. 'controller' => 'index',
  273. 'module' => 'test',
  274. 'id' => 'my-id',
  275. 'class' => 'my-class',
  276. 'title' => 'my-title',
  277. 'target' => 'my-target',
  278. 'order' => 100,
  279. 'active' => true,
  280. 'visible' => false,
  281. 'foo' => 'bar',
  282. 'meaning' => 42
  283. );
  284. $page = new Zend_Navigation_Page_Mvc($options);
  285. $toArray = $page->toArray();
  286. $options['reset_params'] = true;
  287. $options['route'] = null;
  288. $options['params'] = array();
  289. $options['rel'] = array();
  290. $options['rev'] = array();
  291. $this->assertEquals(array(),
  292. array_diff_assoc($options, $page->toArray()));
  293. }
  294. public function testSpecifyingAnotherUrlHelperToGenerateHrefs()
  295. {
  296. $path = dirname(dirname(__FILE__)) . '/_files/My/UrlHelper.php';
  297. require_once $path;
  298. $newHelper = new My_UrlHelper();
  299. Zend_Navigation_Page_Mvc::setUrlHelper($newHelper);
  300. $page = new Zend_Navigation_Page_Mvc();
  301. $expected = My_UrlHelper::RETURN_URL;
  302. $actual = $page->getHref();
  303. $old = Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
  304. Zend_Navigation_Page_Mvc::setUrlHelper($old);
  305. $this->assertEquals($expected, $actual);
  306. }
  307. }