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