-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadmin_controller_test.php
More file actions
568 lines (503 loc) · 12.8 KB
/
admin_controller_test.php
File metadata and controls
568 lines (503 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
/**
*
* Progressive Web App Kit. An extension for the phpBB Forum Software package.
*
* @copyright (c) 2024 phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
*/
namespace phpbb\pwakit\controller;
use Exception;
use phpbb\db\driver\driver_interface as dbal;
use phpbb_mock_cache;
use phpbb_mock_event_dispatcher;
use PHPUnit\DbUnit\DataSet\DefaultDataSet;
use PHPUnit\DbUnit\DataSet\IDataSet;
use PHPUnit\DbUnit\DataSet\XmlDataSet;
use PHPUnit\Framework\MockObject\MockObject;
use phpbb\config\config;
use phpbb\exception\runtime_exception;
use phpbb\language\language;
use phpbb\language\language_file_loader;
use phpbb\pwakit\helper\helper;
use phpbb\pwakit\helper\upload;
use phpbb\request\request;
use phpbb\request\request_interface;
use phpbb\template\template;
use phpbb_database_test_case;
class admin_controller_test extends phpbb_database_test_case
{
public static bool $confirm;
public static bool $valid_form;
protected dbal $db;
protected config $config;
protected language $language;
protected request $request;
protected template|MockObject $template;
protected helper $helper;
protected upload $upload;
protected string $phpbb_root_path;
protected static function setup_extensions(): array
{
return ['phpbb/pwakit'];
}
protected function getDataSet(): IDataSet|XmlDataSet|DefaultDataSet
{
return $this->createXMLDataSet(__DIR__ . '/../fixtures/styles.xml');
}
/**
* Setup test environment
*/
protected function setUp(): void
{
parent::setUp();
global $phpbb_dispatcher, $phpbb_root_path, $phpEx;
$this->db = $this->new_dbal();
$this->config = new config([
'sitename' => 'phpBB',
'sitename_short' => 'phpBB',
'pwa_bg_color' => '',
'pwa_theme_color' => '',
]);
$this->language = new language(new language_file_loader($phpbb_root_path, $phpEx));
$this->request = $this->getMockBuilder(request::class)
->disableOriginalConstructor()
->getMock();
$this->template = $this->getMockBuilder(template::class)
->getMock();
$this->helper = $this->getMockBuilder(helper::class)
->disableOriginalConstructor()
->getMock();
$this->helper->method('is_storage_local')->willReturn(true);
$this->helper->method('get_storage_path')->willReturn('images/site_icons');
$this->helper->method('get_icons')->willReturn([]);
$this->upload = $this->getMockBuilder(upload::class)
->disableOriginalConstructor()
->getMock();
$this->phpbb_root_path = $phpbb_root_path;
$phpbb_dispatcher = new phpbb_mock_event_dispatcher();
self::$valid_form = true;
self::$confirm = true;
$this->admin_controller = new admin_controller(
new phpbb_mock_cache(),
$this->config,
$this->db,
$this->language,
$this->request,
$this->template,
$this->helper,
$this->upload,
$this->phpbb_root_path,
'adm/',
$phpEx
);
}
public function module_access_test_data(): array
{
return [
'correct mode' => ['settings', true],
'incorrect mode' => ['foobar', false],
];
}
/**
* @param $mode
* @param $expected
* @return void
* @dataProvider module_access_test_data
*/
public function test_module_access($mode, $expected)
{
$this->request->expects($expected ? $this->atLeastOnce() : $this->never())
->method('is_set_post');
$this->call_admin_controller($mode);
}
public function form_checks_data(): array
{
return [
'submit test' => ['submit'],
'upload test' => ['upload'],
'resync test' => ['resync'],
];
}
/**
* @param $action
* @dataProvider form_checks_data
*/
public function test_form_checks($action)
{
self::$valid_form = false;
$this->request_submit($action);
$this->setExpectedTriggerError(E_USER_WARNING, $this->language->lang('FORM_INVALID'));
$this->call_admin_controller();
}
public function display_settings_test_data(): array
{
return [
'site name and short name' => [
[
'sitename' => 'phpBB',
'sitename_short' => 'phpBB',
],
[
'sitename' => 'phpBB',
'sitename_short' => 'phpBB',
],
],
'site name only' => [
[
'sitename' => 'phpBB',
'sitename_short' => '',
],
[
'sitename' => 'phpBB',
'sitename_short' => 'phpBB',
],
],
'long site name only' => [
[
'sitename' => 'phpBB Long Site Name',
'sitename_short' => '',
],
[
'sitename' => 'phpBB Long Site Name',
'sitename_short' => 'phpBB Long S',
],
],
'long mb site name only' => [
[
'sitename' => utf8_encode_ucr('phpBB™ 😂😂😂😂😂😂😂😂'),
'sitename_short' => '',
],
[
'sitename' => utf8_encode_ucr('phpBB™ 😂😂😂😂😂😂😂😂'),
'sitename_short' => 'phpBB™ 😂😂😂😂😂',
],
],
];
}
/**
* @param $configs
* @param $expected
* @dataProvider display_settings_test_data
*/
public function test_display_settings($configs, $expected)
{
foreach ($configs as $key => $value)
{
$this->config->set($key, $value);
}
$expected_style_data = [
0 => [
'style_id' => 1,
'style_name' => 'prosilver',
'pwa_bg_color' => '#fff000',
'pwa_theme_color' => '#000fff',
],
1 => [
'style_id' => 2,
'style_name' => 'silverfoo',
'pwa_bg_color' => '',
'pwa_theme_color' => '',
],
];
$expectedCalls = [
[
'SITE_NAME' => $expected['sitename'],
'SITE_NAME_SHORT' => $expected['sitename_short'],
'PWA_IMAGES_DIR' => 'images/site_icons',
'PWA_KIT_ICONS' => [],
'STYLES' => $expected_style_data,
'S_STORAGE_LOCAL' => true,
'U_BOARD_SETTINGS' => "{$this->phpbb_root_path}adm/index.php?i=acp_board&mode=settings",
'U_STORAGE_SETTINGS'=> "{$this->phpbb_root_path}adm/index.php?i=acp_storage&mode=settings",
'U_ACTION' => '',
],
[
'S_ERROR' => false,
'ERROR_MSG' => '',
]
];
$callCount = 0;
$this->template->expects($this->exactly(2))
->method('assign_vars')
->willReturnCallback(function($params) use (&$callCount, $expectedCalls) {
$this->assertEquals($expectedCalls[$callCount], $params);
$callCount++;
});
$this->call_admin_controller();
}
public function submit_test_data(): array
{
return [
'all good inputs' => [
[
'pwa_bg_color_1' => '#000000',
'pwa_theme_color_1' => '#ffffff',
'pwa_bg_color_2' => '#cccccc',
'pwa_theme_color_2' => '#dddddd',
],
[
0 => ['pwa_bg_color' => '#000000', 'pwa_theme_color' => '#ffffff'],
1 => ['pwa_bg_color' => '#cccccc', 'pwa_theme_color' => '#dddddd'],
],
'CONFIG_UPDATED',
],
'one style with good inputs #1' => [
[
'pwa_bg_color_1' => '#000000',
'pwa_theme_color_1' => '#ffffff',
'pwa_bg_color_2' => '',
'pwa_theme_color_2' => '',
],
[
0 => ['pwa_bg_color' => '#000000', 'pwa_theme_color' => '#ffffff'],
1 => ['pwa_bg_color' => '', 'pwa_theme_color' => ''],
],
'CONFIG_UPDATED',
],
'one style with good inputs #2' => [
[
'pwa_bg_color_1' => '#000000',
'pwa_theme_color_1' => '',
'pwa_bg_color_2' => '',
'pwa_theme_color_2' => '#ffffff',
],
[
0 => ['pwa_bg_color' => '#000000', 'pwa_theme_color' => ''],
1 => ['pwa_bg_color' => '', 'pwa_theme_color' => '#ffffff'],
],
'CONFIG_UPDATED',
],
'one bad input' => [
[
'pwa_bg_color_1' => '#000000',
'pwa_theme_color_1' => 'fff',
'pwa_bg_color_2' => '#ffffff',
'pwa_theme_color_2' => '',
],
[
0 => ['pwa_bg_color' => '#000000', 'pwa_theme_color' => '#000fff'],
1 => ['pwa_bg_color' => '#ffffff', 'pwa_theme_color' => ''],
],
'ACP_PWA_INVALID_COLOR',
],
'all bad inputs' => [
[
'pwa_bg_color_1' => 'foo',
'pwa_theme_color_1' => 'bar',
'pwa_bg_color_2' => '123456',
'pwa_theme_color_2' => '######',
],
[
0 => ['pwa_bg_color' => '#fff000', 'pwa_theme_color' => '#000fff'],
1 => ['pwa_bg_color' => '', 'pwa_theme_color' => ''],
],
'ACP_PWA_INVALID_COLOR<br>ACP_PWA_INVALID_COLOR<br>ACP_PWA_INVALID_COLOR<br>ACP_PWA_INVALID_COLOR',
],
'all empty inputs' => [
[
'pwa_bg_color_1' => '',
'pwa_theme_color_1' => '',
'pwa_bg_color_2' => '',
'pwa_theme_color_2' => '',
],
[
0 => ['pwa_bg_color' => '', 'pwa_theme_color' => ''],
1 => ['pwa_bg_color' => '', 'pwa_theme_color' => ''],
],
'CONFIG_UPDATED'
],
];
}
/**
* Test submit/save settings
*
* @param $form_data
* @param $expected
* @param $expected_msg
* @dataProvider submit_test_data
*/
public function test_submit($form_data, $expected, $expected_msg)
{
if ($expected_msg !== 'CONFIG_UPDATED')
{
$firstCallDone = false;
$this->template->method('assign_vars')
->willReturnCallback(function ($params) use (&$firstCallDone, $expected_msg) {
if (!$firstCallDone)
{
$firstCallDone = true; //skip first call
}
else
{
$this->assertEquals([
'S_ERROR' => true,
'ERROR_MSG' => $expected_msg,
], $params);
}
return null;
})
;
}
$this->request->expects($this->exactly(4))
->method('variable')
->willReturnMap([
['pwa_bg_color_1', '', false, request_interface::REQUEST, $form_data['pwa_bg_color_1']],
['pwa_theme_color_1', '', false, request_interface::REQUEST, $form_data['pwa_theme_color_1']],
['pwa_bg_color_2', '', false, request_interface::REQUEST, $form_data['pwa_bg_color_2']],
['pwa_theme_color_2', '', false, request_interface::REQUEST, $form_data['pwa_theme_color_2']],
]);
try
{
$this->request_submit('submit');
$this->call_admin_controller();
}
catch (Exception $e)
{
$this->assertEquals($expected_msg, $e->getMessage());
}
$sql = 'SELECT pwa_bg_color, pwa_theme_color
FROM ' . STYLES_TABLE . '
WHERE style_active = 1
ORDER BY style_id';
$result = $this->db->sql_query($sql);
$rows = $this->db->sql_fetchrowset($result);
$this->db->sql_freeresult($result);
$this->assertSame($expected, $rows);
}
public function test_upload()
{
$this->setExpectedTriggerError(E_USER_NOTICE, 'ACP_PWA_IMG_UPLOAD_SUCCESS');
$this->request_submit('upload');
$this->upload->expects($this->once())
->method('upload')
->willReturn('test.png');
$this->call_admin_controller();
}
public function test_upload_error()
{
$this->request_submit('upload');
$this->upload->expects($this->once())
->method('upload')
->willThrowException(new runtime_exception());
$this->upload->expects($this->once())
->method('remove');
$this->call_admin_controller();
}
public function test_resync()
{
$this->request_submit('resync');
$this->helper->expects($this->once())
->method('resync_icons');
$this->call_admin_controller();
}
public function delete_test_data(): array
{
return [
'not confirmed' => [
'foo.png',
false,
false
],
'confirmed valid data' => [
'foo.png',
true,
false
],
'confirmed invalid data' => [
'',
true,
true
],
];
}
/**
* @param $image
* @param $confirmed
* @param $error
* @dataProvider delete_test_data
*/
public function test_delete($image, $confirmed, $error)
{
self::$confirm = $confirmed;
$this->request_submit('delete');
$this->request->expects($this->once())
->method('variable')
->with('delete')
->willReturn($image);
if ($confirmed)
{
$helperExpectation = $this->helper->expects($this->once())
->method('delete_icon')
->with($image);
if ($error)
{
$this->setExpectedTriggerError(E_USER_WARNING, 'ACP_PWA_IMG_DELETE_ERROR');
$helperExpectation->willThrowException(new runtime_exception());
}
else
{
$this->setExpectedTriggerError(E_USER_NOTICE, 'ACP_PWA_IMG_DELETED');
$helperExpectation->willReturn($image);
}
}
else
{
$this->helper->expects($this->never())->method('delete_icon');
}
$this->call_admin_controller();
}
/**
* @param string $mode
* @return void
*/
private function call_admin_controller(string $mode = 'settings'): void
{
$this->admin_controller->main('\\phpbb\\pwakit\\acp\\pwa_acp_module', $mode, '');
}
/**
* @param $value
* @return void
*/
private function request_submit($value): void
{
$this->request->expects($this->atLeastOnce())
->method('is_set_post')
->willReturnCallback(fn($param) => $param === $value);
}
}
/**
* Mock check_form_key()
* Note: use the same namespace as the admin_controller
*
* @return bool
*/
function check_form_key(): bool
{
return \phpbb\pwakit\controller\admin_controller_test::$valid_form;
}
/**
* Mock add_form_key()
* Note: use the same namespace as the admin_controller
*/
function add_form_key()
{
}
/**
* Mock confirm_box()
* Note: use the same namespace as the admin_controller
*
* @return bool
*/
function confirm_box(): bool
{
return \phpbb\pwakit\controller\admin_controller_test::$confirm;
}
/**
* Mock adm_back_link()
* Note: use the same namespace as the admin_controller
*/
function adm_back_link()
{
}