2
0

PageTest.php 32 KB

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