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