Debian Codegraph @

skins/MinervaNeue/tests/phpunit/menu/GroupTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Minerva\Menu;
4
5 use DomainException;
6 use MediaWiki\Minerva\Menu\Entries\IMenuEntry;
7 use MediaWiki\Minerva\Menu\Entries\SingleMenuEntry;
8 use MediaWikiIntegrationTestCase;
9
10 /**
11 * @group MinervaNeue
12 * @coversDefaultClass \MediaWiki\Minerva\Menu\Group
13 */
14 class GroupTest extends MediaWikiIntegrationTestCase {
15 /** @var string[] */
16 private $homeComponent = [
17 'text' => 'Home',
18 'href' => '/Main_page',
19 'class' => '',
20 'data-event-name' => 'menu.home',
21 'icon' => 'home'
22 ];
23
24 /** @var string[] */
25 private $nearbyComponent = [
26 'text' => 'Nearby',
27 'href' => '/wiki/Special:Nearby',
28 'class' => '',
29 'icon' => 'nearby'
30 ];
31
32 /**
33 * @covers ::getEntries
34 * @covers ::hasEntries
35 */
36 public function testItShouldntHaveEntriesByDefault() {
37 $menu = new Group( 'p-test' );
38
39 $this->assertSame( [], $menu->getEntries() );
40 $this->assertFalse( $menu->hasEntries() );
41 }
42
43 /**
44 * @covers ::insertEntry
45 * @covers ::search
46 * @covers ::getEntries
47 * @covers ::hasEntries
48 */
49 public function testInsertingAnEntry() {
50 $menu = new Group( 'p-test' );
51 $entry = SingleMenuEntry::create(
52 'home',
53 $this->homeComponent['text'],
54 $this->homeComponent['href'],
55 $this->homeComponent['class'],
56 $this->homeComponent['icon'],
57 true
58 );
59 $menu->insertEntry( $entry );
60
61 $expectedEntries = [
62 [
63 'name' => 'home',
64 'components' => [
65 [
66 'tag-name' => 'a',
67 'label' => $this->homeComponent['text'],
68 'array-attributes' => [
69 [
70 'key' => 'href',
71 'value' => $this->homeComponent['href'],
72 ],
73 [
74 'key' => 'data-event-name',
75 'value' => 'menu.home'
76 ],
77 [
78 'key' => 'data-mw',
79 'value' => 'interface'
80 ],
81 ],
82 'classes' => 'menu__item--home',
83 'data-icon' => [
84 'icon' => 'home',
85 ],
86 ]
87 ],
88 ],
89 ];
90
91 $this->assertEquals( $expectedEntries, $menu->getEntries() );
92 $this->assertTrue( $menu->hasEntries() );
93 }
94
95 /**
96 * @covers ::insertEntry
97 */
98 public function testInsertingAnEntryWithAnExistingName() {
99 $menu = new Group( 'p-test' );
100 $entryHome = SingleMenuEntry::create(
101 'home',
102 $this->homeComponent['text'],
103 $this->homeComponent['href'],
104 $this->homeComponent['class']
105 );
106 $menu->insertEntry( $entryHome );
107 $this->expectException( DomainException::class );
108 $this->expectExceptionMessage( 'The "home" entry already exists.' );
109 $menu->insertEntry( $entryHome );
110 }
111
112 /**
113 * @covers ::insertEntry
114 * @covers ::getEntries
115 */
116 public function testInsertingAJavascriptOnlyEntry() {
117 $menu = new Group( 'p-test' );
118 $entryHome = SingleMenuEntry::create(
119 'nearby',
120 $this->nearbyComponent['text'],
121 $this->nearbyComponent['href'],
122 $this->nearbyComponent['class']
123 );
124 $entryHome->setJSOnly();
125 $menu->insertEntry( $entryHome );
126
127 $expectedEntries = [
128 [
129 'name' => 'nearby',
130 'components' => [
131 [
132 'tag-name' => 'a',
133 'label' => $this->nearbyComponent['text'],
134 'array-attributes' => [
135 [
136 'key' => 'href',
137 'value' => $this->nearbyComponent['href'],
138 ],
139 [
140 'key' => 'data-mw',
141 'value' => 'interface'
142 ],
143 ],
144 'classes' => 'menu__item--nearby',
145 'data-icon' => [
146 'icon' => 'nearby',
147 ]
148 ]
149 ],
150 'class' => 'skin-minerva-list-item-jsonly'
151 ],
152 ];
153
154 $this->assertEquals( $expectedEntries, $menu->getEntries() );
155 }
156
157 /**
158 * @covers ::getEntryByName
159 * @covers ::search
160 */
161 public function testGetEntryByName() {
162 $menu = new Group( 'p-test' );
163 $entryHome = SingleMenuEntry::create(
164 'home',
165 $this->homeComponent['text'],
166 $this->homeComponent['href'],
167 $this->homeComponent['class']
168 );
169 $menu->insertEntry( $entryHome );
170 $this->assertInstanceOf( IMenuEntry::class, $menu->getEntryByName( 'home' ) );
171 }
172
173 /**
174 * @covers ::getEntryByName
175 * @covers ::search
176 */
177 public function testGetEntryByNameException() {
178 $menu = new Group( 'p-test' );
179 $this->expectException( DomainException::class );
180 $menu->getEntryByName( 'home' );
181 }
182
183 }