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