123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- /**
- * Depends on elgg_normalize_url() in output.php
- */
- class ElggMenuItemTest extends \PHPUnit_Framework_TestCase {
- protected function setUp() {
- }
- public function testFactoryNoNameOrText() {
- $this->assertNull(\ElggMenuItem::factory(array('name' => 'test')));
- $this->assertNull(\ElggMenuItem::factory(array('text' => 'test')));
- }
- public function testFactoryNoHref() {
- $item = \ElggMenuItem::factory(array('name' => 'test','text' => 'test'));
- $this->assertEquals('', $item->getHref());
- }
- public function testFactoryAllOptions() {
- $params = array(
- 'name' => 'thename',
- 'text' => 'menu',
- 'href' => 'test',
- 'title' => 'tooltip',
- 'confirm' => 'delete?',
- 'contexts' => array('blog', 'bookmarks'),
- 'link_class' => 'elgg-link',
- 'item_class' => 'elgg-item',
- 'section' => 'main',
- 'priority' => 50,
- 'selected' => true,
- 'parent_name' => 'node',
- );
- $item = \ElggMenuItem::factory($params);
- $this->assertEquals($params['name'], $item->getName());
- $this->assertEquals($params['text'], $item->getText());
- $this->assertEquals(elgg_normalize_url($params['href']), $item->getHref());
- $this->assertEquals($params['title'], $item->getTooltip());
- $this->assertEquals($params['confirm'], $item->getConfirmText());
- $this->assertEquals($params['contexts'], $item->getContext());
- $this->assertEquals($params['link_class'], $item->getLinkClass());
- $this->assertEquals("elgg-menu-item-{$params['name']} {$params['item_class']}", $item->getItemClass());
- $this->assertEquals($params['section'], $item->getSection());
- $this->assertEquals($params['priority'], $item->getPriority());
- $this->assertEquals($params['selected'], $item->getSelected());
- $this->assertEquals($params['parent_name'], $item->getParentName());
- }
- public function testFactorySetData() {
- $params = array(
- 'name' => 'thename',
- 'text' => 'menu',
- 'href' => 'test',
- 'data' => array(
- 'section' => 'main',
- 'priority' => 50,
- 'selected' => true,
- 'parent_name' => 'node',
- ),
- );
- $item = \ElggMenuItem::factory($params);
- $this->assertEquals($params['data']['section'], $item->getSection());
- $this->assertEquals($params['data']['priority'], $item->getPriority());
- $this->assertEquals($params['data']['selected'], $item->getSelected());
- $this->assertEquals($params['data']['parent_name'], $item->getParentName());
- }
- public function testFactoryContextShortcut() {
- $params = array(
- 'name' => 'thename',
- 'text' => 'menu',
- 'href' => 'test',
- 'context' => array('blog', 'bookmarks'),
- );
- $item = \ElggMenuItem::factory($params);
- $this->assertEquals($params['context'], $item->getContext());
- }
- public function testConstructorUrlNormalization() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $url = elgg_normalize_url('url');
- $this->assertEquals($url, $item->getHref());
- }
- public function testSetDataWithSingleValue() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setData('foo', 'value');
- $this->assertEquals('value', $item->getData('foo'));
- $item->setData('name', 'new_name');
- $this->assertEquals('new_name', $item->getData('name'));
- $item->setData('name', 'new_name_again');
- $this->assertEquals('new_name_again', $item->getData('name'));
- }
- public function testSetDataWithArray() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setData(array(
- 'priority' => 88,
- 'new' => 64,
- ));
- $this->assertEquals(88, $item->getData('priority'));
- $this->assertEquals(88, $item->getPriority());
- $this->assertEquals(64, $item->getData('new'));
- }
- public function testGetDataNonExistentKey() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $this->assertNull($item->getData('blah'));
- }
- public function testSetContextWithString() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setContext('mine');
- $this->assertEquals(array('mine'), $item->getContext());
- }
- public function testSetContextWithArray() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setContext(array('mine'));
- $this->assertEquals(array('mine'), $item->getContext());
- }
- public function testInContextAll() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setContext('all');
- $this->assertTrue($item->inContext('blog'));
- $this->assertTrue($item->inContext(''));
- }
- public function testInContextWithParticularContext() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setContext(array('blog', 'bookmarks'));
- $this->assertTrue($item->inContext('blog'));
- $this->assertFalse($item->inContext('file'));
- }
- /*
- * This requires elgg_in_context()
- public function testInContextAgainstRequestContext() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setContext(array('blog', 'bookmarks'));
- }
- */
- public function testSetLinkClassWithString() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setLinkClass('elgg-link');
- $this->assertEquals('elgg-link', $item->getLinkClass());
- $item->setLinkClass('new-link');
- $this->assertEquals('new-link', $item->getLinkClass());
- }
- public function testSetLinkClassWithArray() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setLinkClass(array('elgg-link', '2nd-link'));
- $this->assertEquals('elgg-link 2nd-link', $item->getLinkClass());
- }
- public function testAddLinkClassWithString() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->addLinkClass('new-link');
- $item->addLinkClass('2nd-link');
- $this->assertEquals('new-link 2nd-link', $item->getLinkClass());
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setLinkClass('new-link');
- $item->addLinkClass('2nd-link');
- $this->assertEquals('new-link 2nd-link', $item->getLinkClass());
- }
- public function testAddLinkClassWithArray() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setLinkClass('new-link');
- $item->addLinkClass(array('2nd-link'));
- $this->assertEquals('new-link 2nd-link', $item->getLinkClass());
- }
- public function testGetItemClass() {
- $item = new \ElggMenuItem('name', 'text', 'url');
- $item->setItemClass('new-link');
- $item->addItemClass(array('2nd-link'));
- $this->assertEquals('elgg-menu-item-name new-link 2nd-link', $item->getItemClass());
- }
- public function testGetItemClassNormalizeName() {
- $item = new \ElggMenuItem('name_underscore', 'text', 'url');
- $this->assertEquals('elgg-menu-item-name-underscore', $item->getItemClass());
- $item = new \ElggMenuItem('name space', 'text', 'url');
- $this->assertEquals('elgg-menu-item-name-space', $item->getItemClass());
- $item = new \ElggMenuItem('name:colon', 'text', 'url');
- $this->assertEquals('elgg-menu-item-name-colon', $item->getItemClass());
- }
- public function testArgumentTypeValidationOnItemRegistration() {
- $this->assertTrue(elgg_register_menu_item('foo', new \ElggMenuItem('foo', 'bar', 'url')));
- $this->assertTrue(elgg_register_menu_item('foo', array('name' => 'foo', 'text' => 'bar')));
- $this->assertFalse(elgg_register_menu_item('foo', array()));
- $this->assertFalse(elgg_register_menu_item('foo', array('text' => 'bar')));
- $this->assertFalse(elgg_register_menu_item('foo', 'bar'));
- $this->assertFalse(elgg_register_menu_item('foo', new stdClass()));
- }
- }
|