PageTest.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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-2015 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.php';
  23. require_once 'Zend/Config.php';
  24. /**
  25. * Tests the class Zend_Navigation_Page
  26. *
  27. * @author Robin Skoglund
  28. * @category Zend
  29. * @package Zend_Navigation
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2015 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_PageTest extends PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * Prepares the environment before running a test.
  39. *
  40. */
  41. protected function setUp()
  42. {
  43. }
  44. /**
  45. * Tear down the environment after running a test
  46. *
  47. */
  48. protected function tearDown()
  49. {
  50. // setConfig, setOptions
  51. }
  52. public function testSetShouldMapToNativeProperties()
  53. {
  54. $page = Zend_Navigation_Page::factory(array(
  55. 'type' => 'mvc'
  56. ));
  57. $page->set('action', 'foo');
  58. $this->assertEquals('foo', $page->getAction());
  59. $page->set('Action', 'bar');
  60. $this->assertEquals('bar', $page->getAction());
  61. }
  62. public function testGetShouldMapToNativeProperties()
  63. {
  64. $page = Zend_Navigation_Page::factory(array(
  65. 'type' => 'mvc'
  66. ));
  67. $page->setAction('foo');
  68. $this->assertEquals('foo', $page->get('action'));
  69. $page->setAction('bar');
  70. $this->assertEquals('bar', $page->get('Action'));
  71. }
  72. public function testSetShouldNormalizePropertyName()
  73. {
  74. $page = Zend_Navigation_Page::factory(array(
  75. 'type' => 'mvc'
  76. ));
  77. $page->setResetParams(false);
  78. $page->set('reset_params', true);
  79. $this->assertTrue($page->getResetParams());
  80. }
  81. public function testGetShouldNormalizePropertyName()
  82. {
  83. $page = Zend_Navigation_Page::factory(array(
  84. 'type' => 'mvc'
  85. ));
  86. $page->setResetParams(false);
  87. $this->assertFalse($page->get('reset_params'));
  88. }
  89. public function testShouldSetAndGetShouldMapToProperties()
  90. {
  91. $page = Zend_Navigation_Page::factory(array(
  92. 'type' => 'uri'
  93. ));
  94. $page->set('action', 'Laughing Out Loud');
  95. $this->assertEquals('Laughing Out Loud', $page->get('action'));
  96. }
  97. public function testSetShouldNotMapToSetOptionsToPreventRecursion()
  98. {
  99. $page = Zend_Navigation_Page::factory(array(
  100. 'type' => 'uri',
  101. 'label' => 'foo'
  102. ));
  103. $options = array('label' => 'bar');
  104. $page->set('options', $options);
  105. $this->assertEquals('foo', $page->getLabel());
  106. $this->assertEquals($options, $page->get('options'));
  107. }
  108. public function testSetShouldNotMapToSetConfigToPreventRecursion()
  109. {
  110. $page = Zend_Navigation_Page::factory(array(
  111. 'type' => 'uri',
  112. 'label' => 'foo'
  113. ));
  114. $options = array('label' => 'bar');
  115. $page->set('config', $options);
  116. $this->assertEquals('foo', $page->getLabel());
  117. $this->assertEquals($options, $page->get('config'));
  118. }
  119. public function testSetAndGetLabel()
  120. {
  121. $page = Zend_Navigation_Page::factory(array(
  122. 'label' => 'foo',
  123. 'uri' => '#'
  124. ));
  125. $this->assertEquals('foo', $page->getLabel());
  126. $page->setLabel('bar');
  127. $this->assertEquals('bar', $page->getLabel());
  128. $invalids = array(42, (object) null);
  129. foreach ($invalids as $invalid) {
  130. try {
  131. $page->setLabel($invalid);
  132. $this->fail('An invalid value was set, but a ' .
  133. 'Zend_Navigation_Exception was not thrown');
  134. } catch (Zend_Navigation_Exception $e) {
  135. $this->assertContains('Invalid argument: $label', $e->getMessage());
  136. }
  137. }
  138. }
  139. /**
  140. * @group ZF-8922
  141. */
  142. public function testSetAndGetFragmentIdentifier()
  143. {
  144. $page = Zend_Navigation_Page::factory(array(
  145. 'uri' => '#',
  146. 'fragment' => 'foo',
  147. ));
  148. $this->assertEquals('foo', $page->getFragment());
  149. $page->setFragment('bar');
  150. $this->assertEquals('bar', $page->getFragment());
  151. $invalids = array(42, (object) null);
  152. foreach ($invalids as $invalid) {
  153. try {
  154. $page->setFragment($invalid);
  155. $this->fail('An invalid value was set, but a ' .
  156. 'Zend_Navigation_Exception was not thrown');
  157. } catch (Zend_Navigation_Exception $e) {
  158. $this->assertContains(
  159. 'Invalid argument: $fragment', $e->getMessage()
  160. );
  161. }
  162. }
  163. }
  164. public function testSetAndGetId()
  165. {
  166. $page = Zend_Navigation_Page::factory(array(
  167. 'label' => 'foo',
  168. 'uri' => '#'
  169. ));
  170. $this->assertEquals(null, $page->getId());
  171. $page->setId('bar');
  172. $this->assertEquals('bar', $page->getId());
  173. $invalids = array(true, (object) null);
  174. foreach ($invalids as $invalid) {
  175. try {
  176. $page->setId($invalid);
  177. $this->fail('An invalid value was set, but a ' .
  178. 'Zend_Navigation_Exception was not thrown');
  179. } catch (Zend_Navigation_Exception $e) {
  180. $this->assertContains('Invalid argument: $id', $e->getMessage());
  181. }
  182. }
  183. }
  184. public function testIdCouldBeAnInteger()
  185. {
  186. $page = Zend_Navigation_Page::factory(array(
  187. 'label' => 'foo',
  188. 'uri' => '#',
  189. 'id' => 10
  190. ));
  191. $this->assertEquals(10, $page->getId());
  192. }
  193. public function testSetAndGetClass()
  194. {
  195. $page = Zend_Navigation_Page::factory(array(
  196. 'label' => 'foo',
  197. 'uri' => '#'
  198. ));
  199. $this->assertEquals(null, $page->getClass());
  200. $page->setClass('bar');
  201. $this->assertEquals('bar', $page->getClass());
  202. $invalids = array(42, true, (object) null);
  203. foreach ($invalids as $invalid) {
  204. try {
  205. $page->setClass($invalid);
  206. $this->fail('An invalid value was set, but a ' .
  207. 'Zend_Navigation_Exception was not thrown');
  208. } catch (Zend_Navigation_Exception $e) {
  209. $this->assertContains('Invalid argument: $class', $e->getMessage());
  210. }
  211. }
  212. }
  213. public function testSetAndGetTitle()
  214. {
  215. $page = Zend_Navigation_Page::factory(array(
  216. 'label' => 'foo',
  217. 'uri' => '#'
  218. ));
  219. $this->assertEquals(null, $page->getTitle());
  220. $page->setTitle('bar');
  221. $this->assertEquals('bar', $page->getTitle());
  222. $invalids = array(42, true, (object) null);
  223. foreach ($invalids as $invalid) {
  224. try {
  225. $page->setTitle($invalid);
  226. $this->fail('An invalid value was set, but a ' .
  227. 'Zend_Navigation_Exception was not thrown');
  228. } catch (Zend_Navigation_Exception $e) {
  229. $this->assertContains('Invalid argument: $title', $e->getMessage());
  230. }
  231. }
  232. }
  233. public function testSetAndGetTarget()
  234. {
  235. $page = Zend_Navigation_Page::factory(array(
  236. 'label' => 'foo',
  237. 'uri' => '#'
  238. ));
  239. $this->assertEquals(null, $page->getTarget());
  240. $page->setTarget('bar');
  241. $this->assertEquals('bar', $page->getTarget());
  242. $invalids = array(42, true, (object) null);
  243. foreach ($invalids as $invalid) {
  244. try {
  245. $page->setTarget($invalid);
  246. $this->fail('An invalid value was set, but a ' .
  247. 'Zend_Navigation_Exception was not thrown');
  248. } catch (Zend_Navigation_Exception $e) {
  249. $this->assertContains('Invalid argument: $target', $e->getMessage());
  250. }
  251. }
  252. }
  253. /**
  254. * @group ZF-9746
  255. */
  256. public function testSetAndGetAccesskey()
  257. {
  258. $page = Zend_Navigation_Page::factory(array(
  259. 'label' => 'foo',
  260. 'uri' => '#',
  261. ));
  262. $this->assertEquals(null, $page->getAccesskey());
  263. $page->setAccesskey('b');
  264. $this->assertEquals('b', $page->getAccesskey());
  265. $invalids = array('bar', 42, true, (object) null);
  266. foreach ($invalids as $invalid) {
  267. try {
  268. $page->setAccesskey($invalid);
  269. $this->fail('An invalid value was set, but a ' .
  270. 'Zend_Navigation_Exception was not thrown');
  271. } catch (Zend_Navigation_Exception $e) {
  272. $this->assertContains(
  273. 'Invalid argument: $character',
  274. $e->getMessage()
  275. );
  276. }
  277. }
  278. }
  279. public function testConstructingWithRelationsInArray()
  280. {
  281. $page = Zend_Navigation_Page::factory(array(
  282. 'label' => 'bar',
  283. 'uri' => '#',
  284. 'rel' => array(
  285. 'prev' => 'foo',
  286. 'next' => 'baz'
  287. ),
  288. 'rev' => array(
  289. 'alternate' => 'bat'
  290. )
  291. ));
  292. $expected = array(
  293. 'rel' => array(
  294. 'prev' => 'foo',
  295. 'next' => 'baz'
  296. ),
  297. 'rev' => array(
  298. 'alternate' => 'bat'
  299. )
  300. );
  301. $actual = array(
  302. 'rel' => $page->getRel(),
  303. 'rev' => $page->getRev()
  304. );
  305. $this->assertEquals($expected, $actual);
  306. }
  307. public function testConstructingWithRelationsInConfig()
  308. {
  309. $page = Zend_Navigation_Page::factory(new Zend_Config(array(
  310. 'label' => 'bar',
  311. 'uri' => '#',
  312. 'rel' => array(
  313. 'prev' => 'foo',
  314. 'next' => 'baz'
  315. ),
  316. 'rev' => array(
  317. 'alternate' => 'bat'
  318. )
  319. )));
  320. $expected = array(
  321. 'rel' => array(
  322. 'prev' => 'foo',
  323. 'next' => 'baz'
  324. ),
  325. 'rev' => array(
  326. 'alternate' => 'bat'
  327. )
  328. );
  329. $actual = array(
  330. 'rel' => $page->getRel(),
  331. 'rev' => $page->getRev()
  332. );
  333. $this->assertEquals($expected, $actual);
  334. }
  335. public function testGettingSpecificRelations()
  336. {
  337. $page = Zend_Navigation_Page::factory(array(
  338. 'label' => 'bar',
  339. 'uri' => '#',
  340. 'rel' => array(
  341. 'prev' => 'foo',
  342. 'next' => 'baz'
  343. ),
  344. 'rev' => array(
  345. 'next' => 'foo'
  346. )
  347. ));
  348. $expected = array(
  349. 'foo', 'foo'
  350. );
  351. $actual = array(
  352. $page->getRel('prev'),
  353. $page->getRev('next')
  354. );
  355. $this->assertEquals($expected, $actual);
  356. }
  357. public function testSetAndGetOrder()
  358. {
  359. $page = Zend_Navigation_Page::factory(array(
  360. 'label' => 'foo',
  361. 'uri' => '#'
  362. ));
  363. $this->assertEquals(null, $page->getOrder());
  364. $page->setOrder('1');
  365. $this->assertEquals(1, $page->getOrder());
  366. $page->setOrder(1337);
  367. $this->assertEquals(1337, $page->getOrder());
  368. $page->setOrder('-25');
  369. $this->assertEquals(-25, $page->getOrder());
  370. $invalids = array(3.14, 'e', "\n", '0,4', true, (object) null);
  371. foreach ($invalids as $invalid) {
  372. try {
  373. $page->setOrder($invalid);
  374. $this->fail('An invalid value was set, but a ' .
  375. 'Zend_Navigation_Exception was not thrown');
  376. } catch (Zend_Navigation_Exception $e) {
  377. $this->assertContains('Invalid argument: $order', $e->getMessage());
  378. }
  379. }
  380. }
  381. public function testSetResourceString()
  382. {
  383. $page = Zend_Navigation_Page::factory(array(
  384. 'type' => 'uri',
  385. 'label' => 'hello'
  386. ));
  387. $page->setResource('foo');
  388. $this->assertEquals('foo', $page->getResource());
  389. }
  390. public function testSetResourceNoParam()
  391. {
  392. $page = Zend_Navigation_Page::factory(array(
  393. 'type' => 'uri',
  394. 'label' => 'hello',
  395. 'resource' => 'foo'
  396. ));
  397. $page->setResource();
  398. $this->assertEquals(null, $page->getResource());
  399. }
  400. public function testSetResourceNull()
  401. {
  402. $page = Zend_Navigation_Page::factory(array(
  403. 'type' => 'uri',
  404. 'label' => 'hello',
  405. 'resource' => 'foo'
  406. ));
  407. $page->setResource(null);
  408. $this->assertEquals(null, $page->getResource());
  409. }
  410. public function testSetResourceInterface()
  411. {
  412. $page = Zend_Navigation_Page::factory(array(
  413. 'type' => 'uri',
  414. 'label' => 'hello'
  415. ));
  416. require_once 'Zend/Acl/Resource.php';
  417. $resource = new Zend_Acl_Resource('bar');
  418. $page->setResource($resource);
  419. $this->assertEquals($resource, $page->getResource());
  420. }
  421. public function testSetResourceShouldThrowExceptionWhenGivenInteger()
  422. {
  423. $page = Zend_Navigation_Page::factory(array(
  424. 'type' => 'uri',
  425. 'label' => 'hello'
  426. ));
  427. try {
  428. $page->setResource(0);
  429. $this->fail('An invalid value was set, but a ' .
  430. 'Zend_Navigation_Exception was not thrown');
  431. } catch (Zend_Navigation_Exception $e) {
  432. $this->assertContains('Invalid argument: $resource', $e->getMessage());
  433. }
  434. }
  435. public function testSetResourceShouldThrowExceptionWhenGivenObject()
  436. {
  437. $page = Zend_Navigation_Page::factory(array(
  438. 'type' => 'uri',
  439. 'label' => 'hello'
  440. ));
  441. try {
  442. $page->setResource(new stdClass());
  443. $this->fail('An invalid value was set, but a ' .
  444. 'Zend_Navigation_Exception was not thrown');
  445. } catch (Zend_Navigation_Exception $e) {
  446. $this->assertContains('Invalid argument: $resource', $e->getMessage());
  447. }
  448. }
  449. public function testSetPrivilegeNoParams()
  450. {
  451. $page = Zend_Navigation_Page::factory(array(
  452. 'type' => 'uri',
  453. 'label' => 'hello',
  454. 'privilege' => 'foo'
  455. ));
  456. $page->setPrivilege();
  457. $this->assertEquals(null, $page->getPrivilege());
  458. }
  459. public function testSetPrivilegeNull()
  460. {
  461. $page = Zend_Navigation_Page::factory(array(
  462. 'type' => 'uri',
  463. 'label' => 'hello',
  464. 'privilege' => 'foo'
  465. ));
  466. $page->setPrivilege(null);
  467. $this->assertEquals(null, $page->getPrivilege());
  468. }
  469. public function testSetPrivilegeString()
  470. {
  471. $page = Zend_Navigation_Page::factory(array(
  472. 'type' => 'uri',
  473. 'label' => 'hello',
  474. 'privilege' => 'foo'
  475. ));
  476. $page->setPrivilege('bar');
  477. $this->assertEquals('bar', $page->getPrivilege());
  478. }
  479. public function testGetActiveOnNewlyConstructedPageShouldReturnFalse()
  480. {
  481. $page = new Zend_Navigation_Page_Uri();
  482. $this->assertFalse($page->getActive());
  483. }
  484. public function testIsActiveOnNewlyConstructedPageShouldReturnFalse()
  485. {
  486. $page = new Zend_Navigation_Page_Uri();
  487. $this->assertFalse($page->isActive());
  488. }
  489. public function testGetActiveShouldReturnTrueIfPageIsActive()
  490. {
  491. $page = new Zend_Navigation_Page_Uri(array('active' => true));
  492. $this->assertTrue($page->getActive());
  493. }
  494. public function testIsActiveShouldReturnTrueIfPageIsActive()
  495. {
  496. $page = new Zend_Navigation_Page_Uri(array('active' => true));
  497. $this->assertTrue($page->isActive());
  498. }
  499. public function testIsActiveWithRecursiveTrueShouldReturnTrueIfChildActive()
  500. {
  501. $page = new Zend_Navigation_Page_Uri(array(
  502. 'label' => 'Page 1',
  503. 'active' => false,
  504. 'pages' => array(
  505. new Zend_Navigation_Page_Uri(array(
  506. 'label' => 'Page 1.1',
  507. 'active' => false,
  508. 'pages' => array(
  509. new Zend_Navigation_Page_Uri(array(
  510. 'label' => 'Page 1.1',
  511. 'active' => true
  512. ))
  513. )
  514. ))
  515. )
  516. ));
  517. $this->assertFalse($page->isActive(false));
  518. $this->assertTrue($page->isActive(true));
  519. }
  520. public function testGetActiveWithRecursiveTrueShouldReturnTrueIfChildActive()
  521. {
  522. $page = new Zend_Navigation_Page_Uri(array(
  523. 'label' => 'Page 1',
  524. 'active' => false,
  525. 'pages' => array(
  526. new Zend_Navigation_Page_Uri(array(
  527. 'label' => 'Page 1.1',
  528. 'active' => false,
  529. 'pages' => array(
  530. new Zend_Navigation_Page_Uri(array(
  531. 'label' => 'Page 1.1',
  532. 'active' => true
  533. ))
  534. )
  535. ))
  536. )
  537. ));
  538. $this->assertFalse($page->getActive(false));
  539. $this->assertTrue($page->getActive(true));
  540. }
  541. public function testSetActiveWithNoParamShouldSetFalse()
  542. {
  543. $page = new Zend_Navigation_Page_Uri();
  544. $page->setActive();
  545. $this->assertTrue($page->getActive());
  546. }
  547. public function testSetActiveShouldJuggleValue()
  548. {
  549. $page = new Zend_Navigation_Page_Uri();
  550. $page->setActive(1);
  551. $this->assertTrue($page->getActive());
  552. $page->setActive('true');
  553. $this->assertTrue($page->getActive());
  554. $page->setActive(0);
  555. $this->assertFalse($page->getActive());
  556. $page->setActive(array());
  557. $this->assertFalse($page->getActive());
  558. }
  559. public function testIsVisibleOnNewlyConstructedPageShouldReturnTrue()
  560. {
  561. $page = new Zend_Navigation_Page_Uri();
  562. $this->assertTrue($page->isVisible());
  563. }
  564. public function testGetVisibleOnNewlyConstructedPageShouldReturnTrue()
  565. {
  566. $page = new Zend_Navigation_Page_Uri();
  567. $this->assertTrue($page->getVisible());
  568. }
  569. public function testIsVisibleShouldReturnFalseIfPageIsNotVisible()
  570. {
  571. $page = new Zend_Navigation_Page_Uri(array('visible' => false));
  572. $this->assertFalse($page->isVisible());
  573. }
  574. public function testGetVisibleShouldReturnFalseIfPageIsNotVisible()
  575. {
  576. $page = new Zend_Navigation_Page_Uri(array('visible' => false));
  577. $this->assertFalse($page->getVisible());
  578. }
  579. public function testIsVisibleRecursiveTrueShouldReturnFalseIfParentInivisble()
  580. {
  581. $page = new Zend_Navigation_Page_Uri(array(
  582. 'label' => 'Page 1',
  583. 'visible' => false,
  584. 'pages' => array(
  585. new Zend_Navigation_Page_Uri(array(
  586. 'label' => 'Page 1.1',
  587. 'pages' => array(
  588. new Zend_Navigation_Page_Uri(array(
  589. 'label' => 'Page 1.1'
  590. ))
  591. )
  592. ))
  593. )
  594. ));
  595. $childPage = $page->findOneByLabel('Page 1.1');
  596. $this->assertTrue($childPage->isVisible(false));
  597. $this->assertFalse($childPage->isVisible(true));
  598. }
  599. public function testGetVisibleRecursiveTrueShouldReturnFalseIfParentInivisble()
  600. {
  601. $page = new Zend_Navigation_Page_Uri(array(
  602. 'label' => 'Page 1',
  603. 'visible' => false,
  604. 'pages' => array(
  605. new Zend_Navigation_Page_Uri(array(
  606. 'label' => 'Page 1.1',
  607. 'pages' => array(
  608. new Zend_Navigation_Page_Uri(array(
  609. 'label' => 'Page 1.1'
  610. ))
  611. )
  612. ))
  613. )
  614. ));
  615. $childPage = $page->findOneByLabel('Page 1.1');
  616. $this->assertTrue($childPage->getVisible(false));
  617. $this->assertFalse($childPage->getVisible(true));
  618. }
  619. public function testSetVisibleWithNoParamShouldSetVisble()
  620. {
  621. $page = new Zend_Navigation_Page_Uri(array('visible' => false));
  622. $page->setVisible();
  623. $this->assertTrue($page->isVisible());
  624. }
  625. public function testSetVisibleShouldJuggleValue()
  626. {
  627. $page = new Zend_Navigation_Page_Uri();
  628. $page->setVisible(1);
  629. $this->assertTrue($page->isVisible());
  630. $page->setVisible('true');
  631. $this->assertTrue($page->isVisible());
  632. $page->setVisible(0);
  633. $this->assertFalse($page->isVisible());
  634. /**
  635. * ZF-10146
  636. *
  637. * @link http://framework.zend.com/issues/browse/ZF-10146
  638. */
  639. $page->setVisible('False');
  640. $this->assertFalse($page->isVisible());
  641. $page->setVisible(array());
  642. $this->assertFalse($page->isVisible());
  643. }
  644. public function testMagicOverLoadsShouldSetAndGetNativeProperties()
  645. {
  646. $page = Zend_Navigation_Page::factory(array(
  647. 'label' => 'foo',
  648. 'uri' => 'foo'
  649. ));
  650. $this->assertSame('foo', $page->getUri());
  651. $this->assertSame('foo', $page->uri);
  652. $page->uri = 'bar';
  653. $this->assertSame('bar', $page->getUri());
  654. $this->assertSame('bar', $page->uri);
  655. }
  656. public function testMagicOverLoadsShouldCheckNativeProperties()
  657. {
  658. $page = Zend_Navigation_Page::factory(array(
  659. 'label' => 'foo',
  660. 'uri' => 'foo'
  661. ));
  662. $this->assertTrue(isset($page->uri));
  663. try {
  664. unset($page->uri);
  665. $this->fail('Should not be possible to unset native properties');
  666. } catch (Zend_Navigation_Exception $e) {
  667. $this->assertContains('Unsetting native property', $e->getMessage());
  668. }
  669. }
  670. public function testMagicOverLoadsShouldHandleCustomProperties()
  671. {
  672. $page = Zend_Navigation_Page::factory(array(
  673. 'label' => 'foo',
  674. 'uri' => 'foo'
  675. ));
  676. $this->assertFalse(isset($page->category));
  677. $page->category = 'music';
  678. $this->assertTrue(isset($page->category));
  679. $this->assertSame('music', $page->category);
  680. unset($page->category);
  681. $this->assertFalse(isset($page->category));
  682. }
  683. public function testMagicToStringMethodShouldReturnLabel()
  684. {
  685. $page = Zend_Navigation_Page::factory(array(
  686. 'label' => 'foo',
  687. 'uri' => '#'
  688. ));
  689. $this->assertEquals('foo', (string) $page);
  690. }
  691. public function testSetOptionsShouldTranslateToAccessor()
  692. {
  693. $page = Zend_Navigation_Page::factory(array(
  694. 'label' => 'foo',
  695. 'action' => 'index',
  696. 'controller' => 'index'
  697. ));
  698. $options = array(
  699. 'label' => 'bar',
  700. 'action' => 'baz',
  701. 'controller' => 'bat',
  702. 'module' => 'test',
  703. 'reset_params' => false,
  704. 'id' => 'foo-test'
  705. );
  706. $page->setOptions($options);
  707. $expected = array(
  708. 'label' => 'bar',
  709. 'action' => 'baz',
  710. 'controller' => 'bat',
  711. 'module' => 'test',
  712. 'resetParams' => false,
  713. 'id' => 'foo-test'
  714. );
  715. $actual = array(
  716. 'label' => $page->getLabel(),
  717. 'action' => $page->getAction(),
  718. 'controller' => $page->getController(),
  719. 'module' => $page->getModule(),
  720. 'resetParams' => $page->getResetParams(),
  721. 'id' => $page->getId()
  722. );
  723. $this->assertEquals($expected, $actual);
  724. }
  725. public function testSetConfig()
  726. {
  727. $page = Zend_Navigation_Page::factory(array(
  728. 'label' => 'foo',
  729. 'action' => 'index',
  730. 'controller' => 'index'
  731. ));
  732. $options = array(
  733. 'label' => 'bar',
  734. 'action' => 'baz',
  735. 'controller' => 'bat',
  736. 'module' => 'test',
  737. 'reset_params' => false,
  738. 'id' => 'foo-test'
  739. );
  740. $page->setConfig(new Zend_Config($options));
  741. $expected = array(
  742. 'label' => 'bar',
  743. 'action' => 'baz',
  744. 'controller' => 'bat',
  745. 'module' => 'test',
  746. 'resetParams' => false,
  747. 'id' => 'foo-test'
  748. );
  749. $actual = array(
  750. 'label' => $page->getLabel(),
  751. 'action' => $page->getAction(),
  752. 'controller' => $page->getController(),
  753. 'module' => $page->getModule(),
  754. 'resetParams' => $page->getResetParams(),
  755. 'id' => $page->getId()
  756. );
  757. $this->assertEquals($expected, $actual);
  758. }
  759. public function testSetOptionsShouldSetCustomProperties()
  760. {
  761. $page = Zend_Navigation_Page::factory(array(
  762. 'label' => 'foo',
  763. 'uri' => '#'
  764. ));
  765. $options = array(
  766. 'test' => 'test',
  767. 'meaning' => 42
  768. );
  769. $page->setOptions($options);
  770. $actual = array(
  771. 'test' => $page->test,
  772. 'meaning' => $page->meaning
  773. );
  774. $this->assertEquals($options, $actual);
  775. }
  776. public function testAddingRelations()
  777. {
  778. $page = Zend_Navigation_Page::factory(array(
  779. 'label' => 'page',
  780. 'uri' => '#'
  781. ));
  782. $page->addRel('alternate', 'foo');
  783. $page->addRev('alternate', 'bar');
  784. $expected = array(
  785. 'rel' => array('alternate' => 'foo'),
  786. 'rev' => array('alternate' => 'bar')
  787. );
  788. $actual = array(
  789. 'rel' => $page->getRel(),
  790. 'rev' => $page->getRev()
  791. );
  792. $this->assertEquals($expected, $actual);
  793. }
  794. public function testRemovingRelations()
  795. {
  796. $page = Zend_Navigation_Page::factory(array(
  797. 'label' => 'page',
  798. 'uri' => '#'
  799. ));
  800. $page->addRel('alternate', 'foo');
  801. $page->addRev('alternate', 'bar');
  802. $page->removeRel('alternate');
  803. $page->removeRev('alternate');
  804. $expected = array(
  805. 'rel' => array(),
  806. 'rev' => array()
  807. );
  808. $actual = array(
  809. 'rel' => $page->getRel(),
  810. 'rev' => $page->getRev()
  811. );
  812. $this->assertEquals($expected, $actual);
  813. }
  814. public function testSetRelShouldWorkWithArray()
  815. {
  816. $page = Zend_Navigation_Page::factory(array(
  817. 'type' => 'uri',
  818. 'rel' => array(
  819. 'foo' => 'bar',
  820. 'baz' => 'bat'
  821. )
  822. ));
  823. $value = array('alternate' => 'format/xml');
  824. $page->setRel($value);
  825. $this->assertEquals($value, $page->getRel());
  826. }
  827. public function testSetRelShouldWorkWithConfig()
  828. {
  829. $page = Zend_Navigation_Page::factory(array(
  830. 'type' => 'uri',
  831. 'rel' => array(
  832. 'foo' => 'bar',
  833. 'baz' => 'bat'
  834. )
  835. ));
  836. $value = array('alternate' => 'format/xml');
  837. $page->setRel(new Zend_Config($value));
  838. $this->assertEquals($value, $page->getRel());
  839. }
  840. public function testSetRelShouldWithNoParamsShouldResetRelations()
  841. {
  842. $page = Zend_Navigation_Page::factory(array(
  843. 'type' => 'uri',
  844. 'rel' => array(
  845. 'foo' => 'bar',
  846. 'baz' => 'bat'
  847. )
  848. ));
  849. $value = array();
  850. $page->setRel();
  851. $this->assertEquals($value, $page->getRel());
  852. }
  853. public function testSetRelShouldThrowExceptionWhenNotNullOrArrayOrConfig()
  854. {
  855. $page = Zend_Navigation_Page::factory(array('type' => 'uri'));
  856. try {
  857. $page->setRel('alternate');
  858. $this->fail('An invalid value was set, but a ' .
  859. 'Zend_Navigation_Exception was not thrown');
  860. } catch (Zend_Navigation_Exception $e) {
  861. $this->assertContains('Invalid argument: $relations', $e->getMessage());
  862. }
  863. }
  864. public function testSetRevShouldWorkWithArray()
  865. {
  866. $page = Zend_Navigation_Page::factory(array(
  867. 'type' => 'uri',
  868. 'rev' => array(
  869. 'foo' => 'bar',
  870. 'baz' => 'bat'
  871. )
  872. ));
  873. $value = array('alternate' => 'format/xml');
  874. $page->setRev($value);
  875. $this->assertEquals($value, $page->getRev());
  876. }
  877. public function testSetRevShouldWorkWithConfig()
  878. {
  879. $page = Zend_Navigation_Page::factory(array(
  880. 'type' => 'uri',
  881. 'rev' => array(
  882. 'foo' => 'bar',
  883. 'baz' => 'bat'
  884. )
  885. ));
  886. $value = array('alternate' => 'format/xml');
  887. $page->setRev(new Zend_Config($value));
  888. $this->assertEquals($value, $page->getRev());
  889. }
  890. public function testSetRevShouldWithNoParamsShouldResetRelations()
  891. {
  892. $page = Zend_Navigation_Page::factory(array(
  893. 'type' => 'uri',
  894. 'rev' => array(
  895. 'foo' => 'bar',
  896. 'baz' => 'bat'
  897. )
  898. ));
  899. $value = array();
  900. $page->setRev();
  901. $this->assertEquals($value, $page->getRev());
  902. }
  903. public function testSetRevShouldThrowExceptionWhenNotNullOrArrayOrConfig()
  904. {
  905. $page = Zend_Navigation_Page::factory(array('type' => 'uri'));
  906. try {
  907. $page->setRev('alternate');
  908. $this->fail('An invalid value was set, but a ' .
  909. 'Zend_Navigation_Exception was not thrown');
  910. } catch (Zend_Navigation_Exception $e) {
  911. $this->assertContains('Invalid argument: $relations', $e->getMessage());
  912. }
  913. }
  914. public function testGetRelWithArgumentShouldRetrieveSpecificRelation()
  915. {
  916. $page = Zend_Navigation_Page::factory(array(
  917. 'type' => 'uri',
  918. 'rel' => array(
  919. 'foo' => 'bar'
  920. )
  921. ));
  922. $this->assertEquals('bar', $page->getRel('foo'));
  923. }
  924. public function testGetRevWithArgumentShouldRetrieveSpecificRelation()
  925. {
  926. $page = Zend_Navigation_Page::factory(array(
  927. 'type' => 'uri',
  928. 'rev' => array(
  929. 'foo' => 'bar'
  930. )
  931. ));
  932. $this->assertEquals('bar', $page->getRev('foo'));
  933. }
  934. public function testGetDefinedRel()
  935. {
  936. $page = Zend_Navigation_Page::factory(array(
  937. 'type' => 'uri',
  938. 'rel' => array(
  939. 'alternate' => 'foo',
  940. 'foo' => 'bar'
  941. )
  942. ));
  943. $expected = array('alternate', 'foo');
  944. $this->assertEquals($expected, $page->getDefinedRel());
  945. }
  946. public function testGetDefinedRev()
  947. {
  948. $page = Zend_Navigation_Page::factory(array(
  949. 'type' => 'uri',
  950. 'rev' => array(
  951. 'alternate' => 'foo',
  952. 'foo' => 'bar'
  953. )
  954. ));
  955. $expected = array('alternate', 'foo');
  956. $this->assertEquals($expected, $page->getDefinedRev());
  957. }
  958. public function testGetCustomProperties()
  959. {
  960. $page = Zend_Navigation_Page::factory(array(
  961. 'label' => 'foo',
  962. 'uri' => '#',
  963. 'baz' => 'bat'
  964. ));
  965. $options = array(
  966. 'test' => 'test',
  967. 'meaning' => 42
  968. );
  969. $page->setOptions($options);
  970. $expected = array(
  971. 'baz' => 'bat',
  972. 'test' => 'test',
  973. 'meaning' => 42
  974. );
  975. $this->assertEquals($expected, $page->getCustomProperties());
  976. }
  977. /**
  978. * @group ZF-11876
  979. */
  980. public function testSetCustomHtmlAttrib()
  981. {
  982. $page = Zend_Navigation_Page::factory(
  983. array(
  984. 'type' => 'uri',
  985. )
  986. );
  987. $page->setCustomHtmlAttrib('rel', 'nofollow');
  988. $this->assertEquals('nofollow', $page->getCustomHtmlAttrib('rel'));
  989. }
  990. /**
  991. * @group ZF-11876
  992. */
  993. public function testSetCustomHtmlAttribWithNull()
  994. {
  995. $page = Zend_Navigation_Page::factory(
  996. array(
  997. 'type' => 'uri',
  998. )
  999. );
  1000. $page->setCustomHtmlAttrib('rel', 'nofollow');
  1001. $page->setCustomHtmlAttrib('rel', null);
  1002. $this->assertNull($page->getCustomHtmlAttrib('rel'));
  1003. }
  1004. /**
  1005. * @group ZF-11876
  1006. */
  1007. public function testSetCustomHtmlAttribs()
  1008. {
  1009. $page = Zend_Navigation_Page::factory(
  1010. array(
  1011. 'type' => 'uri',
  1012. )
  1013. );
  1014. $attribs = array(
  1015. 'rel' => 'nofollow',
  1016. 'style' => 'font-weight: bold;',
  1017. );
  1018. $page->setCustomHtmlAttribs($attribs);
  1019. $this->assertEquals($attribs, $page->getCustomHtmlAttribs());
  1020. }
  1021. /**
  1022. * @group ZF-11876
  1023. */
  1024. public function testRemoveCustomHtmlAttrib()
  1025. {
  1026. $page = Zend_Navigation_Page::factory(
  1027. array(
  1028. 'type' => 'uri',
  1029. )
  1030. );
  1031. $page->setCustomHtmlAttrib('rel', 'nofollow');
  1032. $page->removeCustomHtmlAttrib('rel');
  1033. $this->assertEquals(array(), $page->getCustomHtmlAttribs());
  1034. }
  1035. /**
  1036. * @group ZF-11876
  1037. */
  1038. public function testClearCustomHtmlAttribs()
  1039. {
  1040. $page = Zend_Navigation_Page::factory(
  1041. array(
  1042. 'type' => 'uri',
  1043. )
  1044. );
  1045. $page->setCustomHtmlAttribs(
  1046. array(
  1047. 'rel' => 'nofollow',
  1048. 'style' => 'font-weight: bold;',
  1049. )
  1050. );
  1051. $page->clearCustomHtmlAttribs();
  1052. $this->assertEquals(array(), $page->getCustomHtmlAttribs());
  1053. }
  1054. public function testToArrayMethod()
  1055. {
  1056. $options = array(
  1057. 'label' => 'foo',
  1058. 'uri' => 'http://www.example.com/foo.html',
  1059. 'fragment' => 'bar',
  1060. 'id' => 'my-id',
  1061. 'class' => 'my-class',
  1062. 'title' => 'my-title',
  1063. 'target' => 'my-target',
  1064. 'accesskey' => 'f',
  1065. 'rel' => array(),
  1066. 'rev' => array(),
  1067. 'order' => 100,
  1068. 'active' => true,
  1069. 'visible' => false,
  1070. 'resource' => 'joker',
  1071. 'privilege' => null,
  1072. 'foo' => 'bar',
  1073. 'meaning' => 42,
  1074. 'pages' => array(
  1075. array(
  1076. 'label' => 'foo.bar',
  1077. 'uri' => 'http://www.example.com/foo.html',
  1078. 'fragment' => null,
  1079. 'id' => null,
  1080. 'class' => null,
  1081. 'title' => null,
  1082. 'target' => null,
  1083. 'accesskey' => null,
  1084. 'rel' => array(),
  1085. 'rev' => array(),
  1086. 'customHtmlAttribs' => array(),
  1087. 'order' => null,
  1088. 'resource' => null,
  1089. 'privilege' => null,
  1090. 'active' => false,
  1091. 'visible' => true,
  1092. 'type' => 'Zend_Navigation_Page_Uri',
  1093. 'pages' => array (),
  1094. ),
  1095. array(
  1096. 'label' => 'foo.baz',
  1097. 'uri' => 'http://www.example.com/foo.html',
  1098. 'fragment' => null,
  1099. 'id' => null,
  1100. 'class' => null,
  1101. 'title' => null,
  1102. 'target' => null,
  1103. 'accesskey' => null,
  1104. 'rel' => array(),
  1105. 'rev' => array(),
  1106. 'customHtmlAttribs' => array (),
  1107. 'order' => null,
  1108. 'resource' => null,
  1109. 'privilege' => null,
  1110. 'active' => false,
  1111. 'visible' => true,
  1112. 'type' => 'Zend_Navigation_Page_Uri',
  1113. 'pages' => array (),
  1114. )
  1115. ),
  1116. 'customHtmlAttribs' => array(
  1117. 'rel' => 'nofollow',
  1118. 'style' => 'font-weight: bold;',
  1119. ),
  1120. );
  1121. $page = Zend_Navigation_Page::factory($options);
  1122. $toArray = $page->toArray();
  1123. // tweak options to what we expect toArray() to contain
  1124. $options['type'] = 'Zend_Navigation_Page_Uri';
  1125. // should be same
  1126. $this->assertEquals($options, $toArray);
  1127. // $toArray should have 2 sub pages
  1128. $this->assertEquals(2, count($toArray['pages']));
  1129. // tweak options to what we expect sub page 1 to be
  1130. $options['label'] = 'foo.bar';
  1131. $options['fragment'] = null;
  1132. $options['order'] = null;
  1133. $options['id'] = null;
  1134. $options['class'] = null;
  1135. $options['title'] = null;
  1136. $options['target'] = null;
  1137. $options['accesskey'] = null;
  1138. $options['resource'] = null;
  1139. $options['active'] = false;
  1140. $options['visible'] = true;
  1141. $options['customHtmlAttribs'] = array();
  1142. $options['pages'] = array();
  1143. unset($options['foo']);
  1144. unset($options['meaning']);
  1145. // assert that there is no diff from what we expect
  1146. $this->assertEquals($options, $toArray['pages'][0]);
  1147. // tweak options to what we expect sub page 2 to be
  1148. $options['label'] = 'foo.baz';
  1149. // assert that there is no diff from what we expect
  1150. $this->assertEquals($options, $toArray['pages'][1]);
  1151. }
  1152. /**
  1153. * @group ZF-11805
  1154. */
  1155. public function testFactoryExceptionWhenNoPageTypeDetected()
  1156. {
  1157. // Without label
  1158. try {
  1159. $page = Zend_Navigation_Page::factory(array());
  1160. $this->fail('An invalid value was set, but a ' .
  1161. 'Zend_Navigation_Exception was not thrown');
  1162. } catch (Zend_Navigation_Exception $e) {
  1163. $this->assertSame(
  1164. 'Invalid argument: Unable to determine class to instantiate',
  1165. $e->getMessage()
  1166. );
  1167. }
  1168. // With label
  1169. try {
  1170. $page = Zend_Navigation_Page::factory(array(
  1171. 'label' => 'Foo',
  1172. ));
  1173. $this->fail('An invalid value was set, but a ' .
  1174. 'Zend_Navigation_Exception was not thrown');
  1175. } catch (Zend_Navigation_Exception $e) {
  1176. $this->assertSame(
  1177. 'Invalid argument: Unable to determine class to instantiate'
  1178. . ' (Page label: Foo)',
  1179. $e->getMessage()
  1180. );
  1181. }
  1182. }
  1183. }