ElggMenuItemTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * Depends on elgg_normalize_url() in output.php
  4. */
  5. class ElggMenuItemTest extends \PHPUnit_Framework_TestCase {
  6. protected function setUp() {
  7. }
  8. public function testFactoryNoNameOrText() {
  9. $this->assertNull(\ElggMenuItem::factory(array('name' => 'test')));
  10. $this->assertNull(\ElggMenuItem::factory(array('text' => 'test')));
  11. }
  12. public function testFactoryNoHref() {
  13. $item = \ElggMenuItem::factory(array('name' => 'test','text' => 'test'));
  14. $this->assertEquals('', $item->getHref());
  15. }
  16. public function testFactoryAllOptions() {
  17. $params = array(
  18. 'name' => 'thename',
  19. 'text' => 'menu',
  20. 'href' => 'test',
  21. 'title' => 'tooltip',
  22. 'confirm' => 'delete?',
  23. 'contexts' => array('blog', 'bookmarks'),
  24. 'link_class' => 'elgg-link',
  25. 'item_class' => 'elgg-item',
  26. 'section' => 'main',
  27. 'priority' => 50,
  28. 'selected' => true,
  29. 'parent_name' => 'node',
  30. );
  31. $item = \ElggMenuItem::factory($params);
  32. $this->assertEquals($params['name'], $item->getName());
  33. $this->assertEquals($params['text'], $item->getText());
  34. $this->assertEquals(elgg_normalize_url($params['href']), $item->getHref());
  35. $this->assertEquals($params['title'], $item->getTooltip());
  36. $this->assertEquals($params['confirm'], $item->getConfirmText());
  37. $this->assertEquals($params['contexts'], $item->getContext());
  38. $this->assertEquals($params['link_class'], $item->getLinkClass());
  39. $this->assertEquals("elgg-menu-item-{$params['name']} {$params['item_class']}", $item->getItemClass());
  40. $this->assertEquals($params['section'], $item->getSection());
  41. $this->assertEquals($params['priority'], $item->getPriority());
  42. $this->assertEquals($params['selected'], $item->getSelected());
  43. $this->assertEquals($params['parent_name'], $item->getParentName());
  44. }
  45. public function testFactorySetData() {
  46. $params = array(
  47. 'name' => 'thename',
  48. 'text' => 'menu',
  49. 'href' => 'test',
  50. 'data' => array(
  51. 'section' => 'main',
  52. 'priority' => 50,
  53. 'selected' => true,
  54. 'parent_name' => 'node',
  55. ),
  56. );
  57. $item = \ElggMenuItem::factory($params);
  58. $this->assertEquals($params['data']['section'], $item->getSection());
  59. $this->assertEquals($params['data']['priority'], $item->getPriority());
  60. $this->assertEquals($params['data']['selected'], $item->getSelected());
  61. $this->assertEquals($params['data']['parent_name'], $item->getParentName());
  62. }
  63. public function testFactoryContextShortcut() {
  64. $params = array(
  65. 'name' => 'thename',
  66. 'text' => 'menu',
  67. 'href' => 'test',
  68. 'context' => array('blog', 'bookmarks'),
  69. );
  70. $item = \ElggMenuItem::factory($params);
  71. $this->assertEquals($params['context'], $item->getContext());
  72. }
  73. public function testConstructorUrlNormalization() {
  74. $item = new \ElggMenuItem('name', 'text', 'url');
  75. $url = elgg_normalize_url('url');
  76. $this->assertEquals($url, $item->getHref());
  77. }
  78. public function testSetDataWithSingleValue() {
  79. $item = new \ElggMenuItem('name', 'text', 'url');
  80. $item->setData('foo', 'value');
  81. $this->assertEquals('value', $item->getData('foo'));
  82. $item->setData('name', 'new_name');
  83. $this->assertEquals('new_name', $item->getData('name'));
  84. $item->setData('name', 'new_name_again');
  85. $this->assertEquals('new_name_again', $item->getData('name'));
  86. }
  87. public function testSetDataWithArray() {
  88. $item = new \ElggMenuItem('name', 'text', 'url');
  89. $item->setData(array(
  90. 'priority' => 88,
  91. 'new' => 64,
  92. ));
  93. $this->assertEquals(88, $item->getData('priority'));
  94. $this->assertEquals(88, $item->getPriority());
  95. $this->assertEquals(64, $item->getData('new'));
  96. }
  97. public function testGetDataNonExistentKey() {
  98. $item = new \ElggMenuItem('name', 'text', 'url');
  99. $this->assertNull($item->getData('blah'));
  100. }
  101. public function testSetContextWithString() {
  102. $item = new \ElggMenuItem('name', 'text', 'url');
  103. $item->setContext('mine');
  104. $this->assertEquals(array('mine'), $item->getContext());
  105. }
  106. public function testSetContextWithArray() {
  107. $item = new \ElggMenuItem('name', 'text', 'url');
  108. $item->setContext(array('mine'));
  109. $this->assertEquals(array('mine'), $item->getContext());
  110. }
  111. public function testInContextAll() {
  112. $item = new \ElggMenuItem('name', 'text', 'url');
  113. $item->setContext('all');
  114. $this->assertTrue($item->inContext('blog'));
  115. $this->assertTrue($item->inContext(''));
  116. }
  117. public function testInContextWithParticularContext() {
  118. $item = new \ElggMenuItem('name', 'text', 'url');
  119. $item->setContext(array('blog', 'bookmarks'));
  120. $this->assertTrue($item->inContext('blog'));
  121. $this->assertFalse($item->inContext('file'));
  122. }
  123. /*
  124. * This requires elgg_in_context()
  125. public function testInContextAgainstRequestContext() {
  126. $item = new \ElggMenuItem('name', 'text', 'url');
  127. $item->setContext(array('blog', 'bookmarks'));
  128. }
  129. */
  130. public function testSetLinkClassWithString() {
  131. $item = new \ElggMenuItem('name', 'text', 'url');
  132. $item->setLinkClass('elgg-link');
  133. $this->assertEquals('elgg-link', $item->getLinkClass());
  134. $item->setLinkClass('new-link');
  135. $this->assertEquals('new-link', $item->getLinkClass());
  136. }
  137. public function testSetLinkClassWithArray() {
  138. $item = new \ElggMenuItem('name', 'text', 'url');
  139. $item->setLinkClass(array('elgg-link', '2nd-link'));
  140. $this->assertEquals('elgg-link 2nd-link', $item->getLinkClass());
  141. }
  142. public function testAddLinkClassWithString() {
  143. $item = new \ElggMenuItem('name', 'text', 'url');
  144. $item->addLinkClass('new-link');
  145. $item->addLinkClass('2nd-link');
  146. $this->assertEquals('new-link 2nd-link', $item->getLinkClass());
  147. $item = new \ElggMenuItem('name', 'text', 'url');
  148. $item->setLinkClass('new-link');
  149. $item->addLinkClass('2nd-link');
  150. $this->assertEquals('new-link 2nd-link', $item->getLinkClass());
  151. }
  152. public function testAddLinkClassWithArray() {
  153. $item = new \ElggMenuItem('name', 'text', 'url');
  154. $item->setLinkClass('new-link');
  155. $item->addLinkClass(array('2nd-link'));
  156. $this->assertEquals('new-link 2nd-link', $item->getLinkClass());
  157. }
  158. public function testGetItemClass() {
  159. $item = new \ElggMenuItem('name', 'text', 'url');
  160. $item->setItemClass('new-link');
  161. $item->addItemClass(array('2nd-link'));
  162. $this->assertEquals('elgg-menu-item-name new-link 2nd-link', $item->getItemClass());
  163. }
  164. public function testGetItemClassNormalizeName() {
  165. $item = new \ElggMenuItem('name_underscore', 'text', 'url');
  166. $this->assertEquals('elgg-menu-item-name-underscore', $item->getItemClass());
  167. $item = new \ElggMenuItem('name space', 'text', 'url');
  168. $this->assertEquals('elgg-menu-item-name-space', $item->getItemClass());
  169. $item = new \ElggMenuItem('name:colon', 'text', 'url');
  170. $this->assertEquals('elgg-menu-item-name-colon', $item->getItemClass());
  171. }
  172. public function testArgumentTypeValidationOnItemRegistration() {
  173. $this->assertTrue(elgg_register_menu_item('foo', new \ElggMenuItem('foo', 'bar', 'url')));
  174. $this->assertTrue(elgg_register_menu_item('foo', array('name' => 'foo', 'text' => 'bar')));
  175. $this->assertFalse(elgg_register_menu_item('foo', array()));
  176. $this->assertFalse(elgg_register_menu_item('foo', array('text' => 'bar')));
  177. $this->assertFalse(elgg_register_menu_item('foo', 'bar'));
  178. $this->assertFalse(elgg_register_menu_item('foo', new stdClass()));
  179. }
  180. }