Passed
Push — master ( 25c35d...bbde0f )
by John
05:52
created

Test::testIntegration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 13
rs 9.9332
c 0
b 0
f 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
		return [
12
			[
13
				'test',
14
				[
15
					'before' => [
16
						'inserted_test' => [
17
							'href' => 'link',
18
							'show' => true,
19
						],
20
						'test' => [
21
							'href' => 'link',
22
							'show' => true,
23
						],
24
						'test1' => [
25
							'href' => 'link1',
26
							'show' => true,
27
						],
28
					],
29
					'after' => [
30
						'test' => [
31
							'href' => 'link',
32
							'show' => true,
33
						],
34
						'inserted_test' => [
35
							'href' => 'link',
36
							'show' => true,
37
						],
38
						'test1' => [
39
							'href' => 'link1',
40
							'show' => true,
41
						],
42
					],
43
					'child_of' => [
44
						'test' => [
45
							'href' => 'link',
46
							'show' => true,
47
							'sub_buttons' => [
48
								'inserted_test' => [
49
									'href' => 'link',
50
									'show' => true,
51
								],
52
							],
53
						],
54
						'test1' => [
55
							'href' => 'link1',
56
							'show' => true,
57
						],
58
					],
59
				],
60
			],
61
			[
62
				'test1',
63
				[
64
					'before' => [
65
						'test' => [
66
							'href' => 'link',
67
							'show' => true,
68
						],
69
						'inserted_test1' => [
70
							'href' => 'link',
71
							'show' => true,
72
						],
73
						'test1' => [
74
							'href' => 'link1',
75
							'show' => true,
76
						],
77
					],
78
					'after' => [
79
						'test' => [
80
							'href' => 'link',
81
							'show' => true,
82
						],
83
						'test1' => [
84
							'href' => 'link1',
85
							'show' => true,
86
						],
87
						'inserted_test1' => [
88
							'href' => 'link',
89
							'show' => true,
90
						],
91
					],
92
					'child_of' => [
93
						'test' => [
94
							'href' => 'link',
95
							'show' => true,
96
						],
97
						'test1' => [
98
							'href' => 'link1',
99
							'show' => true,
100
							'sub_buttons' => [
101
								'inserted_test1' => [
102
									'href' => 'link',
103
									'show' => true,
104
								],
105
							],
106
						],
107
					],
108
				],
109
			],
110
			[
111
				'dungeon',
112
				[
113
					'before' => [
114
						'test' => [
115
							'href' => 'link',
116
							'show' => true,
117
						],
118
						'test1' => [
119
							'href' => 'link1',
120
							'show' => true,
121
						],
122
					],
123
					'after' => [
124
						'test' => [
125
							'href' => 'link',
126
							'show' => true,
127
						],
128
						'test1' => [
129
							'href' => 'link1',
130
							'show' => true,
131
						],
132
					],
133
					'child_of' => [
134
						'test' => [
135
							'href' => 'link',
136
							'show' => true,
137
						],
138
						'test1' => [
139
							'href' => 'link1',
140
							'show' => true,
141
						],
142
					],
143
				],
144
			],
145
		];
146
	}
147
148
	/**
149
	 * @dataProvider buttonProvider
150
	 */
151
	public function testInsertButton(string $insertion_point, array $expected): void
152
	{
153
		foreach (['before', 'after', 'child_of'] as $where)
154
		{
155
			$haystack = [
156
				'test' => [
157
					'href' => 'link',
158
					'show' => true,
159
				],
160
				'test1' => [
161
					'href' => 'link1',
162
					'show' => true,
163
				],
164
			];
165
			recursive_button(
166
				[
167
					'href' => 'link',
168
					'show' => true,
169
				],
170
				$haystack,
171
				$insertion_point,
172
				$where,
173
				'inserted_' . $insertion_point
174
			);
175
			$this->assertSame($expected[$where], $haystack);
176
		}
177
	}
178
179
	public function childButtonProvider(): array
180
	{
181
		return [
182
			[
183
				'sub',
184
				[
185
					'before' => [
186
						'test' => [
187
							'href' => 'link',
188
							'show' => true,
189
							'sub_buttons' => [
190
								'inserted_sub' => [
191
									'href' => 'link',
192
									'show' => true,
193
								],
194
								'sub' => [
195
									'href' => 'link',
196
									'show' => true,
197
								],
198
								'sub1' => [
199
									'href' => 'link',
200
									'show' => true,
201
								],
202
							],
203
						],
204
					],
205
					'after' => [
206
						'test' => [
207
							'href' => 'link',
208
							'show' => true,
209
							'sub_buttons' => [
210
								'sub' => [
211
									'href' => 'link',
212
									'show' => true,
213
								],
214
								'inserted_sub' => [
215
									'href' => 'link',
216
									'show' => true,
217
								],
218
								'sub1' => [
219
									'href' => 'link',
220
									'show' => true,
221
								],
222
							],
223
						],
224
					],
225
					'child_of' => [
226
						'test' => [
227
							'href' => 'link',
228
							'show' => true,
229
							'sub_buttons' => [
230
								'sub' => [
231
									'href' => 'link',
232
									'show' => true,
233
									'sub_buttons' => [
234
										'inserted_sub' => [
235
											'href' => 'link',
236
											'show' => true,
237
										],
238
									],
239
								],
240
								'sub1' => [
241
									'href' => 'link',
242
									'show' => true,
243
								],
244
							],
245
						],
246
					],
247
				],
248
			],
249
			[
250
				'sub1',
251
				[
252
					'before' => [
253
						'test' => [
254
							'href' => 'link',
255
							'show' => true,
256
							'sub_buttons' => [
257
								'sub' => [
258
									'href' => 'link',
259
									'show' => true,
260
								],
261
								'inserted_sub1' => [
262
									'href' => 'link',
263
									'show' => true,
264
								],
265
								'sub1' => [
266
									'href' => 'link',
267
									'show' => true,
268
								],
269
							],
270
						],
271
					],
272
					'after' => [
273
						'test' => [
274
							'href' => 'link',
275
							'show' => true,
276
							'sub_buttons' => [
277
								'sub' => [
278
									'href' => 'link',
279
									'show' => true,
280
								],
281
								'sub1' => [
282
									'href' => 'link',
283
									'show' => true,
284
								],
285
								'inserted_sub1' => [
286
									'href' => 'link',
287
									'show' => true,
288
								],
289
							],
290
						],
291
					],
292
					'child_of' => [
293
						'test' => [
294
							'href' => 'link',
295
							'show' => true,
296
							'sub_buttons' => [
297
								'sub' => [
298
									'href' => 'link',
299
									'show' => true,
300
								],
301
								'sub1' => [
302
									'href' => 'link',
303
									'show' => true,
304
									'sub_buttons' => [
305
										'inserted_sub1' => [
306
											'href' => 'link',
307
											'show' => true,
308
										],
309
									],
310
								],
311
							],
312
						],
313
					],
314
				],
315
			],
316
			[
317
				'dungeon',
318
				[
319
					'before' => [
320
						'test' => [
321
							'href' => 'link',
322
							'show' => true,
323
							'sub_buttons' => [
324
								'sub' => [
325
									'href' => 'link',
326
									'show' => true,
327
								],
328
								'sub1' => [
329
									'href' => 'link',
330
									'show' => true,
331
								],
332
							],
333
						],
334
					],
335
					'after' => [
336
						'test' => [
337
							'href' => 'link',
338
							'show' => true,
339
							'sub_buttons' => [
340
								'sub' => [
341
									'href' => 'link',
342
									'show' => true,
343
								],
344
								'sub1' => [
345
									'href' => 'link',
346
									'show' => true,
347
								],
348
							],
349
						],
350
					],
351
					'child_of' => [
352
						'test' => [
353
							'href' => 'link',
354
							'show' => true,
355
							'sub_buttons' => [
356
								'sub' => [
357
									'href' => 'link',
358
									'show' => true,
359
								],
360
								'sub1' => [
361
									'href' => 'link',
362
									'show' => true,
363
								],
364
							],
365
						],
366
					],
367
				],
368
			],
369
		];
370
	}
371
372
	/**
373
	 * @dataProvider childButtonProvider
374
	 */
375
	public function testInsertChildButton(string $insertion_point, array $expected): void
376
	{
377
		foreach (['before', 'after', 'child_of'] as $where)
378
		{
379
			$haystack = [
380
				'test' => [
381
					'href' => 'link',
382
					'show' => true,
383
					'sub_buttons' => [
384
						'sub' => [
385
							'href' => 'link',
386
							'show' => true,
387
						],
388
						'sub1' => [
389
							'href' => 'link',
390
							'show' => true,
391
						],
392
					],
393
				],
394
			];
395
			recursive_button(
396
				[
397
					'href' => 'link',
398
					'show' => true,
399
				],
400
				$haystack,
401
				$insertion_point,
402
				$where,
403
				'inserted_' . $insertion_point
404
			);
405
			$this->assertSame($expected[$where], $haystack);
406
		}
407
	}
408
409
	public function testHook(): void
410
	{
411
		global $modSettings;
412
413
		add_integration_function('integrate_menu_buttons', 'um_load_menu');
414
		add_integration_function('integrate_menu_buttons', 'my_func');
415
		$this->assertEquals('um_load_menu,my_func', $modSettings['integrate_menu_buttons']);
416
		$dummy = [];
417
		um_load_menu($dummy);
418
		$this->assertEquals('my_func,um_load_menu', $modSettings['integrate_menu_buttons']);
419
		$dummy = [];
420
		um_load_menu($dummy);
421
		$this->assertEquals('my_func,um_load_menu', $modSettings['integrate_menu_buttons']);
422
	}
423
424
	public function testMenu(): void
425
	{
426
		global $modSettings;
427
428
		$modSettings['um_count'] = 2;
429
		$modSettings['um_button_2'] = '{"name":"Test","type":"forum","target":"_self","position":"before","link":"t","active":true,"groups":[-1,0,2],"parent":"register"}';
430
		$haystack = ['register' => 'l'];
431
		um_load_menu($haystack);
432
		$this->assertCount(2, $haystack);
433
		$this->assertArrayHasKey('um_button_2', $haystack);
434
		$this->assertCount(4, $haystack['um_button_2']);
435
		$this->assertArrayHasKey('title', $haystack['um_button_2']);
436
		$this->assertArrayHasKey('href', $haystack['um_button_2']);
437
		$this->assertEquals('Test', $haystack['um_button_2']['title']);
438
		$this->assertEquals('?t', $haystack['um_button_2']['href']);
439
		unset($modSettings['um_count'], $modSettings['um_button_2']);
440
	}
441
442
	public function testListButtons(): void
443
	{
444
		global $modSettings;
445
446
		$modSettings['um_count'] = 2;
447
		$modSettings['um_button_2'] = '{"name":"Test","type":"forum","target":"_self","position":"before","link":"t","active":true,"groups":[-1,0,2],"parent":"register"}';
448
		$haystack = (new UltimateMenu)->getButtonNames();
449
		$this->assertArrayHasKey('um_button_2', $haystack);
450
		$this->assertCount(2, $haystack['um_button_2']);
451
		$this->assertSame([0, 'Test'], $haystack['um_button_2']);
452
		$this->assertArrayHasKey('help', $haystack);
453
		$this->assertArrayHasKey('admin', $haystack);
454
		$this->assertArrayHasKey('profile', $haystack);
455
		$this->assertArrayHasKey('logout', $haystack);
456
		$this->assertArrayHasKey('register', $haystack);
457
		unset($modSettings['um_count'], $modSettings['um_button_2']);
458
	}
459
460
	public function testIntegration(): void
461
	{
462
		global $context, $modSettings;
463
464
		$modSettings['um_count'] = 2;
465
		$modSettings['um_button_2'] = '{"name":"Test","type":"forum","target":"_self","position":"before","link":"t","active":true,"groups":[0],"parent":"help"}';
466
		setupMenuContext();
467
		$this->assertArrayHasKey('um_button_2', $context['menu_buttons']);
468
		$this->assertArrayHasKey('title', $context['menu_buttons']['um_button_2']);
469
		$this->assertArrayHasKey('href', $context['menu_buttons']['um_button_2']);
470
		$this->assertEquals('Test', $context['menu_buttons']['um_button_2']['title']);
471
		$this->assertEquals('?t', $context['menu_buttons']['um_button_2']['href']);
472
		unset($modSettings['um_count'], $modSettings['um_button_2']);
473
	}
474
}
475