PageTest.php 34 KB

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