PageTest.php 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. $page->setVisible(array());
  609. $this->assertFalse($page->isVisible());
  610. }
  611. public function testMagicOverLoadsShouldSetAndGetNativeProperties()
  612. {
  613. $page = Zend_Navigation_Page::factory(array(
  614. 'label' => 'foo',
  615. 'uri' => 'foo'
  616. ));
  617. $this->assertSame('foo', $page->getUri());
  618. $this->assertSame('foo', $page->uri);
  619. $page->uri = 'bar';
  620. $this->assertSame('bar', $page->getUri());
  621. $this->assertSame('bar', $page->uri);
  622. }
  623. public function testMagicOverLoadsShouldCheckNativeProperties()
  624. {
  625. $page = Zend_Navigation_Page::factory(array(
  626. 'label' => 'foo',
  627. 'uri' => 'foo'
  628. ));
  629. $this->assertTrue(isset($page->uri));
  630. try {
  631. unset($page->uri);
  632. $this->fail('Should not be possible to unset native properties');
  633. } catch (Zend_Navigation_Exception $e) {
  634. $this->assertContains('Unsetting native property', $e->getMessage());
  635. }
  636. }
  637. public function testMagicOverLoadsShouldHandleCustomProperties()
  638. {
  639. $page = Zend_Navigation_Page::factory(array(
  640. 'label' => 'foo',
  641. 'uri' => 'foo'
  642. ));
  643. $this->assertFalse(isset($page->category));
  644. $page->category = 'music';
  645. $this->assertTrue(isset($page->category));
  646. $this->assertSame('music', $page->category);
  647. unset($page->category);
  648. $this->assertFalse(isset($page->category));
  649. }
  650. public function testMagicToStringMethodShouldReturnLabel()
  651. {
  652. $page = Zend_Navigation_Page::factory(array(
  653. 'label' => 'foo',
  654. 'uri' => '#'
  655. ));
  656. $this->assertEquals('foo', (string) $page);
  657. }
  658. public function testSetOptionsShouldTranslateToAccessor()
  659. {
  660. $page = Zend_Navigation_Page::factory(array(
  661. 'label' => 'foo',
  662. 'action' => 'index',
  663. 'controller' => 'index'
  664. ));
  665. $options = array(
  666. 'label' => 'bar',
  667. 'action' => 'baz',
  668. 'controller' => 'bat',
  669. 'module' => 'test',
  670. 'reset_params' => false,
  671. 'id' => 'foo-test'
  672. );
  673. $page->setOptions($options);
  674. $expected = array(
  675. 'label' => 'bar',
  676. 'action' => 'baz',
  677. 'controller' => 'bat',
  678. 'module' => 'test',
  679. 'resetParams' => false,
  680. 'id' => 'foo-test'
  681. );
  682. $actual = array(
  683. 'label' => $page->getLabel(),
  684. 'action' => $page->getAction(),
  685. 'controller' => $page->getController(),
  686. 'module' => $page->getModule(),
  687. 'resetParams' => $page->getResetParams(),
  688. 'id' => $page->getId()
  689. );
  690. $this->assertEquals($expected, $actual);
  691. }
  692. public function testSetConfig()
  693. {
  694. $page = Zend_Navigation_Page::factory(array(
  695. 'label' => 'foo',
  696. 'action' => 'index',
  697. 'controller' => 'index'
  698. ));
  699. $options = array(
  700. 'label' => 'bar',
  701. 'action' => 'baz',
  702. 'controller' => 'bat',
  703. 'module' => 'test',
  704. 'reset_params' => false,
  705. 'id' => 'foo-test'
  706. );
  707. $page->setConfig(new Zend_Config($options));
  708. $expected = array(
  709. 'label' => 'bar',
  710. 'action' => 'baz',
  711. 'controller' => 'bat',
  712. 'module' => 'test',
  713. 'resetParams' => false,
  714. 'id' => 'foo-test'
  715. );
  716. $actual = array(
  717. 'label' => $page->getLabel(),
  718. 'action' => $page->getAction(),
  719. 'controller' => $page->getController(),
  720. 'module' => $page->getModule(),
  721. 'resetParams' => $page->getResetParams(),
  722. 'id' => $page->getId()
  723. );
  724. $this->assertEquals($expected, $actual);
  725. }
  726. public function testSetOptionsShouldSetCustomProperties()
  727. {
  728. $page = Zend_Navigation_Page::factory(array(
  729. 'label' => 'foo',
  730. 'uri' => '#'
  731. ));
  732. $options = array(
  733. 'test' => 'test',
  734. 'meaning' => 42
  735. );
  736. $page->setOptions($options);
  737. $actual = array(
  738. 'test' => $page->test,
  739. 'meaning' => $page->meaning
  740. );
  741. $this->assertEquals($options, $actual);
  742. }
  743. public function testAddingRelations()
  744. {
  745. $page = Zend_Navigation_Page::factory(array(
  746. 'label' => 'page',
  747. 'uri' => '#'
  748. ));
  749. $page->addRel('alternate', 'foo');
  750. $page->addRev('alternate', 'bar');
  751. $expected = array(
  752. 'rel' => array('alternate' => 'foo'),
  753. 'rev' => array('alternate' => 'bar')
  754. );
  755. $actual = array(
  756. 'rel' => $page->getRel(),
  757. 'rev' => $page->getRev()
  758. );
  759. $this->assertEquals($expected, $actual);
  760. }
  761. public function testRemovingRelations()
  762. {
  763. $page = Zend_Navigation_Page::factory(array(
  764. 'label' => 'page',
  765. 'uri' => '#'
  766. ));
  767. $page->addRel('alternate', 'foo');
  768. $page->addRev('alternate', 'bar');
  769. $page->removeRel('alternate');
  770. $page->removeRev('alternate');
  771. $expected = array(
  772. 'rel' => array(),
  773. 'rev' => array()
  774. );
  775. $actual = array(
  776. 'rel' => $page->getRel(),
  777. 'rev' => $page->getRev()
  778. );
  779. $this->assertEquals($expected, $actual);
  780. }
  781. public function testSetRelShouldWorkWithArray()
  782. {
  783. $page = Zend_Navigation_Page::factory(array(
  784. 'type' => 'uri',
  785. 'rel' => array(
  786. 'foo' => 'bar',
  787. 'baz' => 'bat'
  788. )
  789. ));
  790. $value = array('alternate' => 'format/xml');
  791. $page->setRel($value);
  792. $this->assertEquals($value, $page->getRel());
  793. }
  794. public function testSetRelShouldWorkWithConfig()
  795. {
  796. $page = Zend_Navigation_Page::factory(array(
  797. 'type' => 'uri',
  798. 'rel' => array(
  799. 'foo' => 'bar',
  800. 'baz' => 'bat'
  801. )
  802. ));
  803. $value = array('alternate' => 'format/xml');
  804. $page->setRel(new Zend_Config($value));
  805. $this->assertEquals($value, $page->getRel());
  806. }
  807. public function testSetRelShouldWithNoParamsShouldResetRelations()
  808. {
  809. $page = Zend_Navigation_Page::factory(array(
  810. 'type' => 'uri',
  811. 'rel' => array(
  812. 'foo' => 'bar',
  813. 'baz' => 'bat'
  814. )
  815. ));
  816. $value = array();
  817. $page->setRel();
  818. $this->assertEquals($value, $page->getRel());
  819. }
  820. public function testSetRelShouldThrowExceptionWhenNotNullOrArrayOrConfig()
  821. {
  822. $page = Zend_Navigation_Page::factory(array('type' => 'uri'));
  823. try {
  824. $page->setRel('alternate');
  825. $this->fail('An invalid value was set, but a ' .
  826. 'Zend_Navigation_Exception was not thrown');
  827. } catch (Zend_Navigation_Exception $e) {
  828. $this->assertContains('Invalid argument: $relations', $e->getMessage());
  829. }
  830. }
  831. public function testSetRevShouldWorkWithArray()
  832. {
  833. $page = Zend_Navigation_Page::factory(array(
  834. 'type' => 'uri',
  835. 'rev' => array(
  836. 'foo' => 'bar',
  837. 'baz' => 'bat'
  838. )
  839. ));
  840. $value = array('alternate' => 'format/xml');
  841. $page->setRev($value);
  842. $this->assertEquals($value, $page->getRev());
  843. }
  844. public function testSetRevShouldWorkWithConfig()
  845. {
  846. $page = Zend_Navigation_Page::factory(array(
  847. 'type' => 'uri',
  848. 'rev' => array(
  849. 'foo' => 'bar',
  850. 'baz' => 'bat'
  851. )
  852. ));
  853. $value = array('alternate' => 'format/xml');
  854. $page->setRev(new Zend_Config($value));
  855. $this->assertEquals($value, $page->getRev());
  856. }
  857. public function testSetRevShouldWithNoParamsShouldResetRelations()
  858. {
  859. $page = Zend_Navigation_Page::factory(array(
  860. 'type' => 'uri',
  861. 'rev' => array(
  862. 'foo' => 'bar',
  863. 'baz' => 'bat'
  864. )
  865. ));
  866. $value = array();
  867. $page->setRev();
  868. $this->assertEquals($value, $page->getRev());
  869. }
  870. public function testSetRevShouldThrowExceptionWhenNotNullOrArrayOrConfig()
  871. {
  872. $page = Zend_Navigation_Page::factory(array('type' => 'uri'));
  873. try {
  874. $page->setRev('alternate');
  875. $this->fail('An invalid value was set, but a ' .
  876. 'Zend_Navigation_Exception was not thrown');
  877. } catch (Zend_Navigation_Exception $e) {
  878. $this->assertContains('Invalid argument: $relations', $e->getMessage());
  879. }
  880. }
  881. public function testGetRelWithArgumentShouldRetrieveSpecificRelation()
  882. {
  883. $page = Zend_Navigation_Page::factory(array(
  884. 'type' => 'uri',
  885. 'rel' => array(
  886. 'foo' => 'bar'
  887. )
  888. ));
  889. $this->assertEquals('bar', $page->getRel('foo'));
  890. }
  891. public function testGetRevWithArgumentShouldRetrieveSpecificRelation()
  892. {
  893. $page = Zend_Navigation_Page::factory(array(
  894. 'type' => 'uri',
  895. 'rev' => array(
  896. 'foo' => 'bar'
  897. )
  898. ));
  899. $this->assertEquals('bar', $page->getRev('foo'));
  900. }
  901. public function testGetDefinedRel()
  902. {
  903. $page = Zend_Navigation_Page::factory(array(
  904. 'type' => 'uri',
  905. 'rel' => array(
  906. 'alternate' => 'foo',
  907. 'foo' => 'bar'
  908. )
  909. ));
  910. $expected = array('alternate', 'foo');
  911. $this->assertEquals($expected, $page->getDefinedRel());
  912. }
  913. public function testGetDefinedRev()
  914. {
  915. $page = Zend_Navigation_Page::factory(array(
  916. 'type' => 'uri',
  917. 'rev' => array(
  918. 'alternate' => 'foo',
  919. 'foo' => 'bar'
  920. )
  921. ));
  922. $expected = array('alternate', 'foo');
  923. $this->assertEquals($expected, $page->getDefinedRev());
  924. }
  925. public function testGetCustomProperties()
  926. {
  927. $page = Zend_Navigation_Page::factory(array(
  928. 'label' => 'foo',
  929. 'uri' => '#',
  930. 'baz' => 'bat'
  931. ));
  932. $options = array(
  933. 'test' => 'test',
  934. 'meaning' => 42
  935. );
  936. $page->setOptions($options);
  937. $expected = array(
  938. 'baz' => 'bat',
  939. 'test' => 'test',
  940. 'meaning' => 42
  941. );
  942. $this->assertEquals($expected, $page->getCustomProperties());
  943. }
  944. public function testToArrayMethod()
  945. {
  946. $options = array(
  947. 'label' => 'foo',
  948. 'uri' => 'http://www.example.com/foo.html',
  949. 'fragmentIdentifier' => 'bar',
  950. 'id' => 'my-id',
  951. 'class' => 'my-class',
  952. 'title' => 'my-title',
  953. 'target' => 'my-target',
  954. 'rel' => array(),
  955. 'rev' => array(),
  956. 'order' => 100,
  957. 'active' => true,
  958. 'visible' => false,
  959. 'resource' => 'joker',
  960. 'privilege' => null,
  961. 'foo' => 'bar',
  962. 'meaning' => 42,
  963. 'pages' => array(
  964. array(
  965. 'label' => 'foo.bar',
  966. 'uri' => 'http://www.example.com/foo.html'
  967. ),
  968. array(
  969. 'label' => 'foo.baz',
  970. 'uri' => 'http://www.example.com/foo.html'
  971. )
  972. )
  973. );
  974. $page = Zend_Navigation_Page::factory($options);
  975. $toArray = $page->toArray();
  976. // tweak options to what we expect toArray() to contain
  977. $options['type'] = 'Zend_Navigation_Page_Uri';
  978. // calculate diff between toArray() and $options
  979. $diff = array_diff_assoc($toArray, $options);
  980. // should be no diff
  981. $this->assertEquals(array(), $diff);
  982. // $toArray should have 2 sub pages
  983. $this->assertEquals(2, count($toArray['pages']));
  984. // tweak options to what we expect sub page 1 to be
  985. $options['label'] = 'foo.bar';
  986. $options['fragmentIdentifier'] = null;
  987. $options['order'] = null;
  988. $options['id'] = null;
  989. $options['class'] = null;
  990. $options['title'] = null;
  991. $options['target'] = null;
  992. $options['resource'] = null;
  993. $options['active'] = false;
  994. $options['visible'] = true;
  995. unset($options['foo']);
  996. unset($options['meaning']);
  997. // assert that there is no diff from what we expect
  998. $subPageOneDiff = array_diff_assoc($toArray['pages'][0], $options);
  999. $this->assertEquals(array(), $subPageOneDiff);
  1000. // tweak options to what we expect sub page 2 to be
  1001. $options['label'] = 'foo.baz';
  1002. // assert that there is no diff from what we expect
  1003. $subPageTwoDiff = array_diff_assoc($toArray['pages'][1], $options);
  1004. $this->assertEquals(array(), $subPageTwoDiff);
  1005. }
  1006. }