xref: /webtrees/tests/app/MenuTest.php (revision cbc1590a8c715aa2d88bd745610b899587bd9563)
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	public function setUp() {
29	}
30
31	/**
32	 * Test the constructor with default parameters.
33	 */
34	public function testConstructorDefaults() {
35		$menu = new Menu('Test!');
36
37		$this->assertSame('Test!', $menu->getLabel());
38		$this->assertSame('#', $menu->getLink());
39		$this->assertSame('', $menu->getCLass());
40		$this->assertSame('', $menu->getOnclick());
41		$this->assertSame(array(), $menu->getSubmenus());
42	}
43
44	/**
45	 * Test the constructor with non-default parameters.
46	 */
47	public function testConstructorNonDefaults() {
48		$submenus = array(new Menu('Submenu'));
49		$menu     = new Menu('Test!', 'link.html', 'link-class', 'test();', $submenus);
50
51		$this->assertSame('Test!', $menu->getLabel());
52		$this->assertSame('link.html', $menu->getLink());
53		$this->assertSame('link-class', $menu->getClass());
54		$this->assertSame('test();', $menu->getOnclick());
55		$this->assertSame($submenus, $menu->getSubmenus());
56	}
57
58	/**
59	 * Test the getter/setter for the label.
60	 */
61	public function testGetterSetterLabel() {
62		$menu = new Menu('Test!');
63
64		$return = $menu->setLabel('Label');
65
66		$this->assertSame($return, $menu);
67		$this->assertSame('Label', $menu->getLabel());
68	}
69
70	/**
71	 * Test the getter/setter for the link.
72	 */
73	public function testGetterSetterLink() {
74		$menu = new Menu('Test!');
75
76		$return = $menu->setLink('link.html');
77
78		$this->assertSame($return, $menu);
79		$this->assertSame('link.html', $menu->getLink());
80	}
81
82	/**
83	 * Test the getter/setter for the ID.
84	 */
85	public function testGetterSetterId() {
86		$menu = new Menu('Test!');
87
88		$return = $menu->setClass('link-class');
89
90		$this->assertSame($return, $menu);
91		$this->assertSame('link-class', $menu->getClass());
92	}
93
94	/**
95	 * Test the getter/setter for the Onclick event.
96	 */
97	public function testGetterSetterOnclick() {
98		$menu = new Menu('Test!');
99
100		$return = $menu->setOnclick('test();');
101
102		$this->assertSame($return, $menu);
103		$this->assertSame('test();', $menu->getOnclick());
104	}
105
106	/**
107	 * Test the getter/setter for the submenus.
108	 */
109	public function testGetterSetterSubmenus() {
110		$menu     = new Menu('Test!');
111		$submenus = array(
112			new Menu('Sub1'),
113			new Menu('Sub2'),
114		);
115
116		$return = $menu->setSubmenus($submenus);
117
118		$this->assertSame($return, $menu);
119		$this->assertSame($submenus, $menu->getSubmenus());
120	}
121
122	/**
123	 * Test the string cast.
124	 */
125	public function testStringCast() {
126		$menu = new Menu('Test!');
127
128		$this->assertSame((string) $menu, $menu->getMenuAsList());
129	}
130
131	/**
132	 * Test the list rendering for a simple link.
133	 */
134	public function testFormatAsList() {
135		$menu = new Menu('Test!', 'link.html');
136
137		$this->assertSame('<li class=""><a href="link.html">Test!</a></li>', $menu->getMenuAsList());
138	}
139
140	/**
141	 * Test the list rendering for a simple link with a CSS ID.
142	 */
143	public function testFormatAsListWithClass() {
144		$menu = new Menu('Test!', 'link.html', 'link-class');
145
146		$this->assertSame('<li class="link-class"><a href="link.html">Test!</a></li>', $menu->getMenuAsList());
147	}
148
149	/**
150	 * Test the list rendering for an empty target.
151	 */
152	public function testFormatAsListWithNoTarget() {
153		$menu = new Menu('Test!', '');
154
155		$this->assertSame('<li class=""><a>Test!</a></li>', $menu->getMenuAsList());
156	}
157
158	/**
159	 * Test the list rendering for a default (hash) target.
160	 */
161	public function testFormatAsListWithHashTarget() {
162		$menu = new Menu('Test!');
163
164		$this->assertSame('<li class=""><a href="#">Test!</a></li>', $menu->getMenuAsList());
165	}
166
167	/**
168	 * Test the list rendering for an onclick link.
169	 */
170	public function testFormatAsListWithOnclick() {
171		$menu = new Menu('Test!', '#', '', 'return test();');
172
173		$this->assertSame('<li class=""><a href="#" onclick="return test();">Test!</a></li>', $menu->getMenuAsList());
174	}
175
176	/**
177	 * Test the list rendering for an onclick link.
178	 */
179	public function testFormatAsListWithOnclickAndId() {
180		$menu = new Menu('Test!', '#', 'link-class', 'return test();');
181
182		$this->assertSame('<li class="link-class"><a href="#" onclick="return test();">Test!</a></li>', $menu->getMenuAsList());
183	}
184}
185