1<?php 2namespace Fisharebest\Webtrees; 3 4/** 5 * webtrees: online genealogy 6 * Copyright (C) 2015 webtrees development team 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19use PHPUnit_Framework_TestCase; 20 21/** 22 * Test harness for the class Menu 23 */ 24class MenuTest extends PHPUnit_Framework_TestCase { 25 /** 26 * Prepare the environment for these tests. 27 * 28 * @return void 29 */ 30 public function setUp() { 31 } 32 33 /** 34 * Test the constructor with default parameters. 35 * 36 * @return void 37 */ 38 public function testConstructorDefaults() { 39 $menu = new Menu('Test!'); 40 41 $this->assertSame('Test!', $menu->getLabel()); 42 $this->assertSame('#', $menu->getLink()); 43 $this->assertSame('', $menu->getCLass()); 44 $this->assertSame('', $menu->getOnclick()); 45 $this->assertSame(array(), $menu->getSubmenus()); 46 } 47 48 /** 49 * Test the constructor with non-default parameters. 50 * 51 * @return void 52 */ 53 public function testConstructorNonDefaults() { 54 $submenus = array(new Menu('Submenu')); 55 $menu = new Menu('Test!', 'link.html', 'link-class', 'test();', $submenus); 56 57 $this->assertSame('Test!', $menu->getLabel()); 58 $this->assertSame('link.html', $menu->getLink()); 59 $this->assertSame('link-class', $menu->getClass()); 60 $this->assertSame('test();', $menu->getOnclick()); 61 $this->assertSame($submenus, $menu->getSubmenus()); 62 } 63 64 /** 65 * Test the getter/setter for the label. 66 * 67 * @return void 68 */ 69 public function testGetterSetterLabel() { 70 $menu = new Menu('Test!'); 71 72 $return = $menu->setLabel('Label'); 73 74 $this->assertSame($return, $menu); 75 $this->assertSame('Label', $menu->getLabel()); 76 } 77 78 /** 79 * Test the getter/setter for the link. 80 * 81 * @return void 82 */ 83 public function testGetterSetterLink() { 84 $menu = new Menu('Test!'); 85 86 $return = $menu->setLink('link.html'); 87 88 $this->assertSame($return, $menu); 89 $this->assertSame('link.html', $menu->getLink()); 90 } 91 92 /** 93 * Test the getter/setter for the ID. 94 * 95 * @return void 96 */ 97 public function testGetterSetterId() { 98 $menu = new Menu('Test!'); 99 100 $return = $menu->setClass('link-class'); 101 102 $this->assertSame($return, $menu); 103 $this->assertSame('link-class', $menu->getClass()); 104 } 105 106 /** 107 * Test the getter/setter for the Onclick event. 108 * 109 * @return void 110 */ 111 public function testGetterSetterOnclick() { 112 $menu = new Menu('Test!'); 113 114 $return = $menu->setOnclick('test();'); 115 116 $this->assertSame($return, $menu); 117 $this->assertSame('test();', $menu->getOnclick()); 118 } 119 120 /** 121 * Test the getter/setter for the submenus. 122 * 123 * @return void 124 */ 125 public function testGetterSetterSubmenus() { 126 $menu = new Menu('Test!'); 127 $submenus = array( 128 new Menu('Sub1'), 129 new Menu('Sub2'), 130 ); 131 132 $return = $menu->setSubmenus($submenus); 133 134 $this->assertSame($return, $menu); 135 $this->assertSame($submenus, $menu->getSubmenus()); 136 } 137 138 /** 139 * Test the string cast. 140 * 141 * @return void 142 */ 143 public function testStringCast() { 144 $menu = new Menu('Test!'); 145 146 $this->assertSame((string)$menu, $menu->getMenuAsList()); 147 } 148 149 /** 150 * Test the list rendering for a simple link. 151 * 152 * @return void 153 */ 154 public function testFormatAsList() { 155 $menu = new Menu('Test!', 'link.html'); 156 157 $this->assertSame('<li class=""><a href="link.html">Test!</a></li>', $menu->getMenuAsList()); 158 } 159 160 /** 161 * Test the list rendering for a simple link with a CSS ID. 162 * 163 * @return void 164 */ 165 public function testFormatAsListWithClass() { 166 $menu = new Menu('Test!', 'link.html', 'link-class'); 167 168 $this->assertSame('<li class="link-class"><a href="link.html">Test!</a></li>', $menu->getMenuAsList()); 169 } 170 171 /** 172 * Test the list rendering for an empty target. 173 * 174 * @return void 175 */ 176 public function testFormatAsListWithNoTarget() { 177 $menu = new Menu('Test!', ''); 178 179 $this->assertSame('<li class=""><a>Test!</a></li>', $menu->getMenuAsList()); 180 } 181 182 /** 183 * Test the list rendering for a default (hash) target. 184 * 185 * @return void 186 */ 187 public function testFormatAsListWithHashTarget() { 188 $menu = new Menu('Test!'); 189 190 $this->assertSame('<li class=""><a href="#">Test!</a></li>', $menu->getMenuAsList()); 191 } 192 193 /** 194 * Test the list rendering for an onclick link. 195 * 196 * @return void 197 */ 198 public function testFormatAsListWithOnclick() { 199 $menu = new Menu('Test!', '#', '', 'return test();'); 200 201 $this->assertSame('<li class=""><a href="#" onclick="return test();">Test!</a></li>', $menu->getMenuAsList()); 202 } 203 204 /** 205 * Test the list rendering for an onclick link. 206 * 207 * @return void 208 */ 209 public function testFormatAsListWithOnclickAndId() { 210 $menu = new Menu('Test!', '#', 'link-class', 'return test();'); 211 212 $this->assertSame('<li class="link-class"><a href="#" onclick="return test();">Test!</a></li>', $menu->getMenuAsList()); 213 } 214} 215