PageTest.php 38 KB

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