Passed
Push — master ( a92de5...15b5e9 )
by John
03:08
created

Test::testDispatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
use PHPUnit\Framework\TestCase;
6
7
final class Test extends TestCase
8
{
9
	public function buttonProvider(): array
10
	{
11
		$btn = ['href' => 'link', 'show' => true];
12
		$btn1 = ['href' => 'link1', 'show' => true];
13
14
		return [
15
			[
16
				'test',
17
				[
18
					'before' => [
19
						'inserted_test' => $btn,
20
						'test' => $btn,
21
						'test1' => $btn1,
22
					],
23
					'after' => [
24
						'test' => $btn,
25
						'inserted_test' => $btn,
26
						'test1' => $btn1,
27
					],
28
					'child_of' => [
29
						'test' => [
30
							'href' => 'link',
31
							'show' => true,
32
							'sub_buttons' => [
33
								'inserted_test' => $btn,
34
							],
35
						],
36
						'test1' => $btn1,
37
					],
38
				],
39
			],
40
			[
41
				'test1',
42
				[
43
					'before' => [
44
						'test' => $btn,
45
						'inserted_test1' => $btn,
46
						'test1' => $btn1,
47
					],
48
					'after' => [
49
						'test' => $btn,
50
						'test1' => $btn1,
51
						'inserted_test1' => $btn,
52
					],
53
					'child_of' => [
54
						'test' => $btn,
55
						'test1' => [
56
							'href' => 'link1',
57
							'show' => true,
58
							'sub_buttons' => [
59
								'inserted_test1' => $btn,
60
							],
61
						],
62
					],
63
				],
64
			],
65
			[
66
				'dungeon',
67
				[
68
					'before' => [
69
						'test' => $btn,
70
						'test1' => $btn1,
71
					],
72
					'after' => [
73
						'test' => $btn,
74
						'test1' => $btn1,
75
					],
76
					'child_of' => [
77
						'test' => $btn,
78
						'test1' => $btn1,
79
					],
80
				],
81
			],
82
		];
83
	}
84
85
	/**
86
	 * @dataProvider buttonProvider
87
	 */
88
	public function testInsertButton(string $insertion_point, array $expected): void
89
	{
90
		$btn = ['href' => 'link', 'show' => true];
91
		$btn1 = ['href' => 'link1', 'show' => true];
92
93
		foreach (['before', 'after', 'child_of'] as $where)
94
		{
95
			$haystack = ['test' => $btn, 'test1' => $btn1];
96
			recursive_button(
97
				$btn,
98
				$haystack,
99
				$insertion_point,
100
				$where,
101
				'inserted_' . $insertion_point
102
			);
103
			$this->assertSame($expected[$where], $haystack);
104
		}
105
	}
106
107
	public function childButtonProvider(): array
108
	{
109
		$btn = ['href' => 'link', 'show' => true];
110
		return [
111
			[
112
				'sub',
113
				[
114
					'before' => [
115
						'test' => [
116
							'href' => 'link',
117
							'show' => true,
118
							'sub_buttons' => [
119
								'inserted_sub' => $btn,
120
								'sub' => $btn ,
121
								'sub1' => $btn,
122
							],
123
						],
124
					],
125
					'after' => [
126
						'test' => [
127
							'href' => 'link',
128
							'show' => true,
129
							'sub_buttons' => [
130
								'sub' => $btn,
131
								'inserted_sub' => $btn,
132
								'sub1' => $btn,
133
							],
134
						],
135
					],
136
					'child_of' => [
137
						'test' => [
138
							'href' => 'link',
139
							'show' => true,
140
							'sub_buttons' => [
141
								'sub' => [
142
									'href' => 'link',
143
									'show' => true,
144
									'sub_buttons' => [
145
										'inserted_sub' => $btn,
146
									],
147
								],
148
								'sub1' => $btn,
149
							],
150
						],
151
					],
152
				],
153
			],
154
			[
155
				'sub1',
156
				[
157
					'before' => [
158
						'test' => [
159
							'href' => 'link',
160
							'show' => true,
161
							'sub_buttons' => [
162
								'sub' => $btn,
163
								'inserted_sub1' => $btn,
164
								'sub1' => $btn,
165
							],
166
						],
167
					],
168
					'after' => [
169
						'test' => [
170
							'href' => 'link',
171
							'show' => true,
172
							'sub_buttons' => [
173
								'sub' => $btn,
174
								'sub1' => $btn,
175
								'inserted_sub1' => $btn,
176
							],
177
						],
178
					],
179
					'child_of' => [
180
						'test' => [
181
							'href' => 'link',
182
							'show' => true,
183
							'sub_buttons' => [
184
								'sub' => $btn,
185
								'sub1' => [
186
									'href' => 'link',
187
									'show' => true,
188
									'sub_buttons' => [
189
										'inserted_sub1' => $btn,
190
									],
191
								],
192
							],
193
						],
194
					],
195
				],
196
			],
197
			[
198
				'dungeon',
199
				[
200
					'before' => [
201
						'test' => [
202
							'href' => 'link',
203
							'show' => true,
204
							'sub_buttons' => [
205
								'sub' => $btn, 'sub1' => $btn,
206
							],
207
						],
208
					],
209
					'after' => [
210
						'test' => [
211
							'href' => 'link',
212
							'show' => true,
213
							'sub_buttons' => [
214
								'sub' => $btn, 'sub1' => $btn,
215
							],
216
						],
217
					],
218
					'child_of' => [
219
						'test' => [
220
							'href' => 'link',
221
							'show' => true,
222
							'sub_buttons' => [
223
								'sub' => $btn, 'sub1' => $btn,
224
							],
225
						],
226
					],
227
				],
228
			],
229
		];
230
	}
231
232
	/**
233
	 * @dataProvider childButtonProvider
234
	 */
235
	public function testInsertChildButton(string $insertion_point, array $expected): void
236
	{
237
		foreach (['before', 'after', 'child_of'] as $where)
238
		{
239
			$haystack = [
240
				'test' => [
241
					'href' => 'link',
242
					'show' => true,
243
					'sub_buttons' => [
244
						'sub' => [
245
							'href' => 'link',
246
							'show' => true,
247
						],
248
						'sub1' => [
249
							'href' => 'link',
250
							'show' => true,
251
						],
252
					],
253
				],
254
			];
255
			recursive_button(
256
				[
257
					'href' => 'link',
258
					'show' => true,
259
				],
260
				$haystack,
261
				$insertion_point,
262
				$where,
263
				'inserted_' . $insertion_point
264
			);
265
			$this->assertSame($expected[$where], $haystack);
266
		}
267
	}
268
269
	public function testHook(): void
270
	{
271
		global $modSettings;
272
273
		add_integration_function('integrate_menu_buttons', 'um_load_menu');
274
		add_integration_function('integrate_menu_buttons', 'my_func');
275
		$this->assertEquals('um_load_menu,my_func', $modSettings['integrate_menu_buttons']);
276
		$dummy = [];
277
		um_load_menu($dummy);
278
		$this->assertEquals('my_func,um_load_menu', $modSettings['integrate_menu_buttons']);
279
		$dummy = [];
280
		um_load_menu($dummy);
281
		$this->assertEquals('my_func,um_load_menu', $modSettings['integrate_menu_buttons']);
282
		remove_integration_function('integrate_menu_buttons', 'um_load_menu');
283
		remove_integration_function('integrate_menu_buttons', 'my_func');
284
	}
285
286
	public function testMenu(): void
287
	{
288
		global $modSettings;
289
290
		$modSettings['um_count'] = 2;
291
		$modSettings['um_button_2'] = '{"name":"Test","type":"forum","target":"_self","position":"before","link":"t","active":true,"groups":[-1,0,2],"parent":"signup"}';
292
		$haystack = ['signup' => 'l'];
293
		um_load_menu($haystack);
294
		$this->assertCount(2, $haystack);
295
		$this->assertArrayHasKey('um_button_2', $haystack);
296
		$this->assertCount(4, $haystack['um_button_2']);
297
		$this->assertArrayHasKey('title', $haystack['um_button_2']);
298
		$this->assertArrayHasKey('href', $haystack['um_button_2']);
299
		$this->assertEquals('Test', $haystack['um_button_2']['title']);
300
		$this->assertEquals('?t', $haystack['um_button_2']['href']);
301
		unset($modSettings['um_count'], $modSettings['um_button_2']);
302
	}
303
304
	public function testListButtons(): void
305
	{
306
		global $modSettings;
307
308
		$modSettings['um_count'] = 2;
309
		$modSettings['um_button_2'] = '{"name":"Test","type":"forum","target":"_self","position":"before","link":"t","active":true,"groups":[-1,0,2],"parent":"signup"}';
310
		add_integration_function('integrate_menu_buttons', 'um_load_menu');
311
		$haystack = (new UltimateMenu)->getButtonNames();
312
		remove_integration_function('integrate_menu_buttons', 'um_load_menu');
313
		$this->assertCount(2, $haystack['um_button_2']);
314
		$this->assertSame([0, 'Test'], $haystack['um_button_2']);
315
		$this->assertArrayHasKey('admin', $haystack);
316
		$this->assertArrayHasKey('signup', $haystack);
317
		unset($modSettings['um_count'], $modSettings['um_button_2']);
318
	}
319
320
	public function testIntegration(): void
321
	{
322
		global $context, $modSettings;
323
324
		$modSettings['um_count'] = 2;
325
		$modSettings['um_button_2'] = '{"name":"Test","type":"forum","target":"_self","position":"before","link":"t","active":true,"groups":[0],"parent":"search"}';
326
		add_integration_function('integrate_menu_buttons', 'um_load_menu');
327
		setupMenuContext();
328
		remove_integration_function('integrate_menu_buttons', 'um_load_menu');
329
		$this->assertArrayHasKey('um_button_2', $context['menu_buttons']);
330
		$this->assertArrayHasKey('title', $context['menu_buttons']['um_button_2']);
331
		$this->assertArrayHasKey('href', $context['menu_buttons']['um_button_2']);
332
		$this->assertEquals('Test', $context['menu_buttons']['um_button_2']['title']);
333
		$this->assertEquals('?t', $context['menu_buttons']['um_button_2']['href']);
334
		unset($modSettings['um_count'], $modSettings['um_button_2']);
335
	}
336
337
	public function testDispatch(): void
338
	{
339
		$mock = $this->getMockBuilder('ManageUltimateMenu')
340
			->onlyMethods(array('ManageMenu'))
341
			->disableOriginalConstructor()
342
			->getMock();
343
344
		$mock->expects($this->once())
345
			 ->method('ManageMenu');
346
347
		$mock->__construct('');
348
	}
349
}
350