PageTest.php 33 KB

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