PageTest.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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.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-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_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. public function testSetAndGetId()
  140. {
  141. $page = Zend_Navigation_Page::factory(array(
  142. 'label' => 'foo',
  143. 'uri' => '#'
  144. ));
  145. $this->assertEquals(null, $page->getId());
  146. $page->setId('bar');
  147. $this->assertEquals('bar', $page->getId());
  148. $invalids = array(true, (object) null);
  149. foreach ($invalids as $invalid) {
  150. try {
  151. $page->setId($invalid);
  152. $this->fail('An invalid value was set, but a ' .
  153. 'Zend_Navigation_Exception was not thrown');
  154. } catch (Zend_Navigation_Exception $e) {
  155. $this->assertContains('Invalid argument: $id', $e->getMessage());
  156. }
  157. }
  158. }
  159. public function testIdCouldBeAnInteger()
  160. {
  161. $page = Zend_Navigation_Page::factory(array(
  162. 'label' => 'foo',
  163. 'uri' => '#',
  164. 'id' => 10
  165. ));
  166. $this->assertEquals(10, $page->getId());
  167. }
  168. public function testSetAndGetClass()
  169. {
  170. $page = Zend_Navigation_Page::factory(array(
  171. 'label' => 'foo',
  172. 'uri' => '#'
  173. ));
  174. $this->assertEquals(null, $page->getClass());
  175. $page->setClass('bar');
  176. $this->assertEquals('bar', $page->getClass());
  177. $invalids = array(42, true, (object) null);
  178. foreach ($invalids as $invalid) {
  179. try {
  180. $page->setClass($invalid);
  181. $this->fail('An invalid value was set, but a ' .
  182. 'Zend_Navigation_Exception was not thrown');
  183. } catch (Zend_Navigation_Exception $e) {
  184. $this->assertContains('Invalid argument: $class', $e->getMessage());
  185. }
  186. }
  187. }
  188. public function testSetAndGetTitle()
  189. {
  190. $page = Zend_Navigation_Page::factory(array(
  191. 'label' => 'foo',
  192. 'uri' => '#'
  193. ));
  194. $this->assertEquals(null, $page->getTitle());
  195. $page->setTitle('bar');
  196. $this->assertEquals('bar', $page->getTitle());
  197. $invalids = array(42, true, (object) null);
  198. foreach ($invalids as $invalid) {
  199. try {
  200. $page->setTitle($invalid);
  201. $this->fail('An invalid value was set, but a ' .
  202. 'Zend_Navigation_Exception was not thrown');
  203. } catch (Zend_Navigation_Exception $e) {
  204. $this->assertContains('Invalid argument: $title', $e->getMessage());
  205. }
  206. }
  207. }
  208. public function testSetAndGetTarget()
  209. {
  210. $page = Zend_Navigation_Page::factory(array(
  211. 'label' => 'foo',
  212. 'uri' => '#'
  213. ));
  214. $this->assertEquals(null, $page->getTarget());
  215. $page->setTarget('bar');
  216. $this->assertEquals('bar', $page->getTarget());
  217. $invalids = array(42, true, (object) null);
  218. foreach ($invalids as $invalid) {
  219. try {
  220. $page->setTarget($invalid);
  221. $this->fail('An invalid value was set, but a ' .
  222. 'Zend_Navigation_Exception was not thrown');
  223. } catch (Zend_Navigation_Exception $e) {
  224. $this->assertContains('Invalid argument: $target', $e->getMessage());
  225. }
  226. }
  227. }
  228. public function testConstructingWithRelationsInArray()
  229. {
  230. $page = Zend_Navigation_Page::factory(array(
  231. 'label' => 'bar',
  232. 'uri' => '#',
  233. 'rel' => array(
  234. 'prev' => 'foo',
  235. 'next' => 'baz'
  236. ),
  237. 'rev' => array(
  238. 'alternate' => 'bat'
  239. )
  240. ));
  241. $expected = array(
  242. 'rel' => array(
  243. 'prev' => 'foo',
  244. 'next' => 'baz'
  245. ),
  246. 'rev' => array(
  247. 'alternate' => 'bat'
  248. )
  249. );
  250. $actual = array(
  251. 'rel' => $page->getRel(),
  252. 'rev' => $page->getRev()
  253. );
  254. $this->assertEquals($expected, $actual);
  255. }
  256. public function testConstructingWithRelationsInConfig()
  257. {
  258. $page = Zend_Navigation_Page::factory(new Zend_Config(array(
  259. 'label' => 'bar',
  260. 'uri' => '#',
  261. 'rel' => array(
  262. 'prev' => 'foo',
  263. 'next' => 'baz'
  264. ),
  265. 'rev' => array(
  266. 'alternate' => 'bat'
  267. )
  268. )));
  269. $expected = array(
  270. 'rel' => array(
  271. 'prev' => 'foo',
  272. 'next' => 'baz'
  273. ),
  274. 'rev' => array(
  275. 'alternate' => 'bat'
  276. )
  277. );
  278. $actual = array(
  279. 'rel' => $page->getRel(),
  280. 'rev' => $page->getRev()
  281. );
  282. $this->assertEquals($expected, $actual);
  283. }
  284. public function testGettingSpecificRelations()
  285. {
  286. $page = Zend_Navigation_Page::factory(array(
  287. 'label' => 'bar',
  288. 'uri' => '#',
  289. 'rel' => array(
  290. 'prev' => 'foo',
  291. 'next' => 'baz'
  292. ),
  293. 'rev' => array(
  294. 'next' => 'foo'
  295. )
  296. ));
  297. $expected = array(
  298. 'foo', 'foo'
  299. );
  300. $actual = array(
  301. $page->getRel('prev'),
  302. $page->getRev('next')
  303. );
  304. $this->assertEquals($expected, $actual);
  305. }
  306. public function testSetAndGetOrder()
  307. {
  308. $page = Zend_Navigation_Page::factory(array(
  309. 'label' => 'foo',
  310. 'uri' => '#'
  311. ));
  312. $this->assertEquals(null, $page->getOrder());
  313. $page->setOrder('1');
  314. $this->assertEquals(1, $page->getOrder());
  315. $page->setOrder(1337);
  316. $this->assertEquals(1337, $page->getOrder());
  317. $page->setOrder('-25');
  318. $this->assertEquals(-25, $page->getOrder());
  319. $invalids = array(3.14, 'e', "\n", '0,4', true, (object) null);
  320. foreach ($invalids as $invalid) {
  321. try {
  322. $page->setOrder($invalid);
  323. $this->fail('An invalid value was set, but a ' .
  324. 'Zend_Navigation_Exception was not thrown');
  325. } catch (Zend_Navigation_Exception $e) {
  326. $this->assertContains('Invalid argument: $order', $e->getMessage());
  327. }
  328. }
  329. }
  330. public function testSetResourceString()
  331. {
  332. $page = Zend_Navigation_Page::factory(array(
  333. 'type' => 'uri',
  334. 'label' => 'hello'
  335. ));
  336. $page->setResource('foo');
  337. $this->assertEquals('foo', $page->getResource());
  338. }
  339. public function testSetResourceNoParam()
  340. {
  341. $page = Zend_Navigation_Page::factory(array(
  342. 'type' => 'uri',
  343. 'label' => 'hello',
  344. 'resource' => 'foo'
  345. ));
  346. $page->setResource();
  347. $this->assertEquals(null, $page->getResource());
  348. }
  349. public function testSetResourceNull()
  350. {
  351. $page = Zend_Navigation_Page::factory(array(
  352. 'type' => 'uri',
  353. 'label' => 'hello',
  354. 'resource' => 'foo'
  355. ));
  356. $page->setResource(null);
  357. $this->assertEquals(null, $page->getResource());
  358. }
  359. public function testSetResourceInterface()
  360. {
  361. $page = Zend_Navigation_Page::factory(array(
  362. 'type' => 'uri',
  363. 'label' => 'hello'
  364. ));
  365. require_once 'Zend/Acl/Resource.php';
  366. $resource = new Zend_Acl_Resource('bar');
  367. $page->setResource($resource);
  368. $this->assertEquals($resource, $page->getResource());
  369. }
  370. public function testSetResourceShouldThrowExceptionWhenGivenInteger()
  371. {
  372. $page = Zend_Navigation_Page::factory(array(
  373. 'type' => 'uri',
  374. 'label' => 'hello'
  375. ));
  376. try {
  377. $page->setResource(0);
  378. $this->fail('An invalid value was set, but a ' .
  379. 'Zend_Navigation_Exception was not thrown');
  380. } catch (Zend_Navigation_Exception $e) {
  381. $this->assertContains('Invalid argument: $resource', $e->getMessage());
  382. }
  383. }
  384. public function testSetResourceShouldThrowExceptionWhenGivenObject()
  385. {
  386. $page = Zend_Navigation_Page::factory(array(
  387. 'type' => 'uri',
  388. 'label' => 'hello'
  389. ));
  390. try {
  391. $page->setResource(new stdClass());
  392. $this->fail('An invalid value was set, but a ' .
  393. 'Zend_Navigation_Exception was not thrown');
  394. } catch (Zend_Navigation_Exception $e) {
  395. $this->assertContains('Invalid argument: $resource', $e->getMessage());
  396. }
  397. }
  398. public function testSetPrivilegeNoParams()
  399. {
  400. $page = Zend_Navigation_Page::factory(array(
  401. 'type' => 'uri',
  402. 'label' => 'hello',
  403. 'privilege' => 'foo'
  404. ));
  405. $page->setPrivilege();
  406. $this->assertEquals(null, $page->getPrivilege());
  407. }
  408. public function testSetPrivilegeNull()
  409. {
  410. $page = Zend_Navigation_Page::factory(array(
  411. 'type' => 'uri',
  412. 'label' => 'hello',
  413. 'privilege' => 'foo'
  414. ));
  415. $page->setPrivilege(null);
  416. $this->assertEquals(null, $page->getPrivilege());
  417. }
  418. public function testSetPrivilegeString()
  419. {
  420. $page = Zend_Navigation_Page::factory(array(
  421. 'type' => 'uri',
  422. 'label' => 'hello',
  423. 'privilege' => 'foo'
  424. ));
  425. $page->setPrivilege('bar');
  426. $this->assertEquals('bar', $page->getPrivilege());
  427. }
  428. public function testGetActiveOnNewlyConstructedPageShouldReturnFalse()
  429. {
  430. $page = new Zend_Navigation_Page_Uri();
  431. $this->assertFalse($page->getActive());
  432. }
  433. public function testIsActiveOnNewlyConstructedPageShouldReturnFalse()
  434. {
  435. $page = new Zend_Navigation_Page_Uri();
  436. $this->assertFalse($page->isActive());
  437. }
  438. public function testGetActiveShouldReturnTrueIfPageIsActive()
  439. {
  440. $page = new Zend_Navigation_Page_Uri(array('active' => true));
  441. $this->assertTrue($page->getActive());
  442. }
  443. public function testIsActiveShouldReturnTrueIfPageIsActive()
  444. {
  445. $page = new Zend_Navigation_Page_Uri(array('active' => true));
  446. $this->assertTrue($page->isActive());
  447. }
  448. public function testIsActiveWithRecursiveTrueShouldReturnTrueIfChildActive()
  449. {
  450. $page = new Zend_Navigation_Page_Uri(array(
  451. 'label' => 'Page 1',
  452. 'active' => false,
  453. 'pages' => array(
  454. new Zend_Navigation_Page_Uri(array(
  455. 'label' => 'Page 1.1',
  456. 'active' => false,
  457. 'pages' => array(
  458. new Zend_Navigation_Page_Uri(array(
  459. 'label' => 'Page 1.1',
  460. 'active' => true
  461. ))
  462. )
  463. ))
  464. )
  465. ));
  466. $this->assertFalse($page->isActive(false));
  467. $this->assertTrue($page->isActive(true));
  468. }
  469. public function testGetActiveWithRecursiveTrueShouldReturnTrueIfChildActive()
  470. {
  471. $page = new Zend_Navigation_Page_Uri(array(
  472. 'label' => 'Page 1',
  473. 'active' => false,
  474. 'pages' => array(
  475. new Zend_Navigation_Page_Uri(array(
  476. 'label' => 'Page 1.1',
  477. 'active' => false,
  478. 'pages' => array(
  479. new Zend_Navigation_Page_Uri(array(
  480. 'label' => 'Page 1.1',
  481. 'active' => true
  482. ))
  483. )
  484. ))
  485. )
  486. ));
  487. $this->assertFalse($page->getActive(false));
  488. $this->assertTrue($page->getActive(true));
  489. }
  490. public function testSetActiveWithNoParamShouldSetFalse()
  491. {
  492. $page = new Zend_Navigation_Page_Uri();
  493. $page->setActive();
  494. $this->assertTrue($page->getActive());
  495. }
  496. public function testSetActiveShouldJuggleValue()
  497. {
  498. $page = new Zend_Navigation_Page_Uri();
  499. $page->setActive(1);
  500. $this->assertTrue($page->getActive());
  501. $page->setActive('true');
  502. $this->assertTrue($page->getActive());
  503. $page->setActive(0);
  504. $this->assertFalse($page->getActive());
  505. $page->setActive(array());
  506. $this->assertFalse($page->getActive());
  507. }
  508. public function testIsVisibleOnNewlyConstructedPageShouldReturnTrue()
  509. {
  510. $page = new Zend_Navigation_Page_Uri();
  511. $this->assertTrue($page->isVisible());
  512. }
  513. public function testGetVisibleOnNewlyConstructedPageShouldReturnTrue()
  514. {
  515. $page = new Zend_Navigation_Page_Uri();
  516. $this->assertTrue($page->getVisible());
  517. }
  518. public function testIsVisibleShouldReturnFalseIfPageIsNotVisible()
  519. {
  520. $page = new Zend_Navigation_Page_Uri(array('visible' => false));
  521. $this->assertFalse($page->isVisible());
  522. }
  523. public function testGetVisibleShouldReturnFalseIfPageIsNotVisible()
  524. {
  525. $page = new Zend_Navigation_Page_Uri(array('visible' => false));
  526. $this->assertFalse($page->getVisible());
  527. }
  528. public function testIsVisibleRecursiveTrueShouldReturnFalseIfParentInivisble()
  529. {
  530. $page = new Zend_Navigation_Page_Uri(array(
  531. 'label' => 'Page 1',
  532. 'visible' => false,
  533. 'pages' => array(
  534. new Zend_Navigation_Page_Uri(array(
  535. 'label' => 'Page 1.1',
  536. 'pages' => array(
  537. new Zend_Navigation_Page_Uri(array(
  538. 'label' => 'Page 1.1'
  539. ))
  540. )
  541. ))
  542. )
  543. ));
  544. $childPage = $page->findOneByLabel('Page 1.1');
  545. $this->assertTrue($childPage->isVisible(false));
  546. $this->assertFalse($childPage->isVisible(true));
  547. }
  548. public function testGetVisibleRecursiveTrueShouldReturnFalseIfParentInivisble()
  549. {
  550. $page = new Zend_Navigation_Page_Uri(array(
  551. 'label' => 'Page 1',
  552. 'visible' => false,
  553. 'pages' => array(
  554. new Zend_Navigation_Page_Uri(array(
  555. 'label' => 'Page 1.1',
  556. 'pages' => array(
  557. new Zend_Navigation_Page_Uri(array(
  558. 'label' => 'Page 1.1'
  559. ))
  560. )
  561. ))
  562. )
  563. ));
  564. $childPage = $page->findOneByLabel('Page 1.1');
  565. $this->assertTrue($childPage->getVisible(false));
  566. $this->assertFalse($childPage->getVisible(true));
  567. }
  568. public function testSetVisibleWithNoParamShouldSetVisble()
  569. {
  570. $page = new Zend_Navigation_Page_Uri(array('visible' => false));
  571. $page->setVisible();
  572. $this->assertTrue($page->isVisible());
  573. }
  574. public function testSetVisibleShouldJuggleValue()
  575. {
  576. $page = new Zend_Navigation_Page_Uri();
  577. $page->setVisible(1);
  578. $this->assertTrue($page->isVisible());
  579. $page->setVisible('true');
  580. $this->assertTrue($page->isVisible());
  581. $page->setVisible(0);
  582. $this->assertFalse($page->isVisible());
  583. $page->setVisible(array());
  584. $this->assertFalse($page->isVisible());
  585. }
  586. public function testMagicOverLoadsShouldSetAndGetNativeProperties()
  587. {
  588. $page = Zend_Navigation_Page::factory(array(
  589. 'label' => 'foo',
  590. 'uri' => 'foo'
  591. ));
  592. $this->assertSame('foo', $page->getUri());
  593. $this->assertSame('foo', $page->uri);
  594. $page->uri = 'bar';
  595. $this->assertSame('bar', $page->getUri());
  596. $this->assertSame('bar', $page->uri);
  597. }
  598. public function testMagicOverLoadsShouldCheckNativeProperties()
  599. {
  600. $page = Zend_Navigation_Page::factory(array(
  601. 'label' => 'foo',
  602. 'uri' => 'foo'
  603. ));
  604. $this->assertTrue(isset($page->uri));
  605. try {
  606. unset($page->uri);
  607. $this->fail('Should not be possible to unset native properties');
  608. } catch (Zend_Navigation_Exception $e) {
  609. $this->assertContains('Unsetting native property', $e->getMessage());
  610. }
  611. }
  612. public function testMagicOverLoadsShouldHandleCustomProperties()
  613. {
  614. $page = Zend_Navigation_Page::factory(array(
  615. 'label' => 'foo',
  616. 'uri' => 'foo'
  617. ));
  618. $this->assertFalse(isset($page->category));
  619. $page->category = 'music';
  620. $this->assertTrue(isset($page->category));
  621. $this->assertSame('music', $page->category);
  622. unset($page->category);
  623. $this->assertFalse(isset($page->category));
  624. }
  625. public function testMagicToStringMethodShouldReturnLabel()
  626. {
  627. $page = Zend_Navigation_Page::factory(array(
  628. 'label' => 'foo',
  629. 'uri' => '#'
  630. ));
  631. $this->assertEquals('foo', (string) $page);
  632. }
  633. public function testSetOptionsShouldTranslateToAccessor()
  634. {
  635. $page = Zend_Navigation_Page::factory(array(
  636. 'label' => 'foo',
  637. 'action' => 'index',
  638. 'controller' => 'index'
  639. ));
  640. $options = array(
  641. 'label' => 'bar',
  642. 'action' => 'baz',
  643. 'controller' => 'bat',
  644. 'module' => 'test',
  645. 'reset_params' => false,
  646. 'id' => 'foo-test'
  647. );
  648. $page->setOptions($options);
  649. $expected = array(
  650. 'label' => 'bar',
  651. 'action' => 'baz',
  652. 'controller' => 'bat',
  653. 'module' => 'test',
  654. 'resetParams' => false,
  655. 'id' => 'foo-test'
  656. );
  657. $actual = array(
  658. 'label' => $page->getLabel(),
  659. 'action' => $page->getAction(),
  660. 'controller' => $page->getController(),
  661. 'module' => $page->getModule(),
  662. 'resetParams' => $page->getResetParams(),
  663. 'id' => $page->getId()
  664. );
  665. $this->assertEquals($expected, $actual);
  666. }
  667. public function testSetConfig()
  668. {
  669. $page = Zend_Navigation_Page::factory(array(
  670. 'label' => 'foo',
  671. 'action' => 'index',
  672. 'controller' => 'index'
  673. ));
  674. $options = array(
  675. 'label' => 'bar',
  676. 'action' => 'baz',
  677. 'controller' => 'bat',
  678. 'module' => 'test',
  679. 'reset_params' => false,
  680. 'id' => 'foo-test'
  681. );
  682. $page->setConfig(new Zend_Config($options));
  683. $expected = array(
  684. 'label' => 'bar',
  685. 'action' => 'baz',
  686. 'controller' => 'bat',
  687. 'module' => 'test',
  688. 'resetParams' => false,
  689. 'id' => 'foo-test'
  690. );
  691. $actual = array(
  692. 'label' => $page->getLabel(),
  693. 'action' => $page->getAction(),
  694. 'controller' => $page->getController(),
  695. 'module' => $page->getModule(),
  696. 'resetParams' => $page->getResetParams(),
  697. 'id' => $page->getId()
  698. );
  699. $this->assertEquals($expected, $actual);
  700. }
  701. public function testSetOptionsShouldSetCustomProperties()
  702. {
  703. $page = Zend_Navigation_Page::factory(array(
  704. 'label' => 'foo',
  705. 'uri' => '#'
  706. ));
  707. $options = array(
  708. 'test' => 'test',
  709. 'meaning' => 42
  710. );
  711. $page->setOptions($options);
  712. $actual = array(
  713. 'test' => $page->test,
  714. 'meaning' => $page->meaning
  715. );
  716. $this->assertEquals($options, $actual);
  717. }
  718. public function testAddingRelations()
  719. {
  720. $page = Zend_Navigation_Page::factory(array(
  721. 'label' => 'page',
  722. 'uri' => '#'
  723. ));
  724. $page->addRel('alternate', 'foo');
  725. $page->addRev('alternate', 'bar');
  726. $expected = array(
  727. 'rel' => array('alternate' => 'foo'),
  728. 'rev' => array('alternate' => 'bar')
  729. );
  730. $actual = array(
  731. 'rel' => $page->getRel(),
  732. 'rev' => $page->getRev()
  733. );
  734. $this->assertEquals($expected, $actual);
  735. }
  736. public function testRemovingRelations()
  737. {
  738. $page = Zend_Navigation_Page::factory(array(
  739. 'label' => 'page',
  740. 'uri' => '#'
  741. ));
  742. $page->addRel('alternate', 'foo');
  743. $page->addRev('alternate', 'bar');
  744. $page->removeRel('alternate');
  745. $page->removeRev('alternate');
  746. $expected = array(
  747. 'rel' => array(),
  748. 'rev' => array()
  749. );
  750. $actual = array(
  751. 'rel' => $page->getRel(),
  752. 'rev' => $page->getRev()
  753. );
  754. $this->assertEquals($expected, $actual);
  755. }
  756. public function testSetRelShouldWorkWithArray()
  757. {
  758. $page = Zend_Navigation_Page::factory(array(
  759. 'type' => 'uri',
  760. 'rel' => array(
  761. 'foo' => 'bar',
  762. 'baz' => 'bat'
  763. )
  764. ));
  765. $value = array('alternate' => 'format/xml');
  766. $page->setRel($value);
  767. $this->assertEquals($value, $page->getRel());
  768. }
  769. public function testSetRelShouldWorkWithConfig()
  770. {
  771. $page = Zend_Navigation_Page::factory(array(
  772. 'type' => 'uri',
  773. 'rel' => array(
  774. 'foo' => 'bar',
  775. 'baz' => 'bat'
  776. )
  777. ));
  778. $value = array('alternate' => 'format/xml');
  779. $page->setRel(new Zend_Config($value));
  780. $this->assertEquals($value, $page->getRel());
  781. }
  782. public function testSetRelShouldWithNoParamsShouldResetRelations()
  783. {
  784. $page = Zend_Navigation_Page::factory(array(
  785. 'type' => 'uri',
  786. 'rel' => array(
  787. 'foo' => 'bar',
  788. 'baz' => 'bat'
  789. )
  790. ));
  791. $value = array();
  792. $page->setRel();
  793. $this->assertEquals($value, $page->getRel());
  794. }
  795. public function testSetRelShouldThrowExceptionWhenNotNullOrArrayOrConfig()
  796. {
  797. $page = Zend_Navigation_Page::factory(array('type' => 'uri'));
  798. try {
  799. $page->setRel('alternate');
  800. $this->fail('An invalid value was set, but a ' .
  801. 'Zend_Navigation_Exception was not thrown');
  802. } catch (Zend_Navigation_Exception $e) {
  803. $this->assertContains('Invalid argument: $relations', $e->getMessage());
  804. }
  805. }
  806. public function testSetRevShouldWorkWithArray()
  807. {
  808. $page = Zend_Navigation_Page::factory(array(
  809. 'type' => 'uri',
  810. 'rev' => array(
  811. 'foo' => 'bar',
  812. 'baz' => 'bat'
  813. )
  814. ));
  815. $value = array('alternate' => 'format/xml');
  816. $page->setRev($value);
  817. $this->assertEquals($value, $page->getRev());
  818. }
  819. public function testSetRevShouldWorkWithConfig()
  820. {
  821. $page = Zend_Navigation_Page::factory(array(
  822. 'type' => 'uri',
  823. 'rev' => array(
  824. 'foo' => 'bar',
  825. 'baz' => 'bat'
  826. )
  827. ));
  828. $value = array('alternate' => 'format/xml');
  829. $page->setRev(new Zend_Config($value));
  830. $this->assertEquals($value, $page->getRev());
  831. }
  832. public function testSetRevShouldWithNoParamsShouldResetRelations()
  833. {
  834. $page = Zend_Navigation_Page::factory(array(
  835. 'type' => 'uri',
  836. 'rev' => array(
  837. 'foo' => 'bar',
  838. 'baz' => 'bat'
  839. )
  840. ));
  841. $value = array();
  842. $page->setRev();
  843. $this->assertEquals($value, $page->getRev());
  844. }
  845. public function testSetRevShouldThrowExceptionWhenNotNullOrArrayOrConfig()
  846. {
  847. $page = Zend_Navigation_Page::factory(array('type' => 'uri'));
  848. try {
  849. $page->setRev('alternate');
  850. $this->fail('An invalid value was set, but a ' .
  851. 'Zend_Navigation_Exception was not thrown');
  852. } catch (Zend_Navigation_Exception $e) {
  853. $this->assertContains('Invalid argument: $relations', $e->getMessage());
  854. }
  855. }
  856. public function testGetRelWithArgumentShouldRetrieveSpecificRelation()
  857. {
  858. $page = Zend_Navigation_Page::factory(array(
  859. 'type' => 'uri',
  860. 'rel' => array(
  861. 'foo' => 'bar'
  862. )
  863. ));
  864. $this->assertEquals('bar', $page->getRel('foo'));
  865. }
  866. public function testGetRevWithArgumentShouldRetrieveSpecificRelation()
  867. {
  868. $page = Zend_Navigation_Page::factory(array(
  869. 'type' => 'uri',
  870. 'rev' => array(
  871. 'foo' => 'bar'
  872. )
  873. ));
  874. $this->assertEquals('bar', $page->getRev('foo'));
  875. }
  876. public function testGetDefinedRel()
  877. {
  878. $page = Zend_Navigation_Page::factory(array(
  879. 'type' => 'uri',
  880. 'rel' => array(
  881. 'alternate' => 'foo',
  882. 'foo' => 'bar'
  883. )
  884. ));
  885. $expected = array('alternate', 'foo');
  886. $this->assertEquals($expected, $page->getDefinedRel());
  887. }
  888. public function testGetDefinedRev()
  889. {
  890. $page = Zend_Navigation_Page::factory(array(
  891. 'type' => 'uri',
  892. 'rev' => array(
  893. 'alternate' => 'foo',
  894. 'foo' => 'bar'
  895. )
  896. ));
  897. $expected = array('alternate', 'foo');
  898. $this->assertEquals($expected, $page->getDefinedRev());
  899. }
  900. public function testGetCustomProperties()
  901. {
  902. $page = Zend_Navigation_Page::factory(array(
  903. 'label' => 'foo',
  904. 'uri' => '#',
  905. 'baz' => 'bat'
  906. ));
  907. $options = array(
  908. 'test' => 'test',
  909. 'meaning' => 42
  910. );
  911. $page->setOptions($options);
  912. $expected = array(
  913. 'baz' => 'bat',
  914. 'test' => 'test',
  915. 'meaning' => 42
  916. );
  917. $this->assertEquals($expected, $page->getCustomProperties());
  918. }
  919. public function testToArrayMethod()
  920. {
  921. $options = array(
  922. 'label' => 'foo',
  923. 'uri' => '#',
  924. 'id' => 'my-id',
  925. 'class' => 'my-class',
  926. 'title' => 'my-title',
  927. 'target' => 'my-target',
  928. 'rel' => array(),
  929. 'rev' => array(),
  930. 'order' => 100,
  931. 'active' => true,
  932. 'visible' => false,
  933. 'resource' => 'joker',
  934. 'privilege' => null,
  935. 'foo' => 'bar',
  936. 'meaning' => 42,
  937. 'pages' => array(
  938. array(
  939. 'label' => 'foo.bar',
  940. 'uri' => '#'
  941. ),
  942. array(
  943. 'label' => 'foo.baz',
  944. 'uri' => '#'
  945. )
  946. )
  947. );
  948. $page = Zend_Navigation_Page::factory($options);
  949. $toArray = $page->toArray();
  950. // tweak options to what we expect toArray() to contain
  951. $options['type'] = 'Zend_Navigation_Page_Uri';
  952. // calculate diff between toArray() and $options
  953. $diff = array_diff_assoc($toArray, $options);
  954. // should be no diff
  955. $this->assertEquals(array(), $diff);
  956. // $toArray should have 2 sub pages
  957. $this->assertEquals(2, count($toArray['pages']));
  958. // tweak options to what we expect sub page 1 to be
  959. $options['label'] = 'foo.bar';
  960. $options['order'] = null;
  961. $options['id'] = null;
  962. $options['class'] = null;
  963. $options['title'] = null;
  964. $options['target'] = null;
  965. $options['resource'] = null;
  966. $options['active'] = false;
  967. $options['visible'] = true;
  968. unset($options['foo']);
  969. unset($options['meaning']);
  970. // assert that there is no diff from what we expect
  971. $subPageOneDiff = array_diff_assoc($toArray['pages'][0], $options);
  972. $this->assertEquals(array(), $subPageOneDiff);
  973. // tweak options to what we expect sub page 2 to be
  974. $options['label'] = 'foo.baz';
  975. // assert that there is no diff from what we expect
  976. $subPageTwoDiff = array_diff_assoc($toArray['pages'][1], $options);
  977. $this->assertEquals(array(), $subPageTwoDiff);
  978. }
  979. }