1010
1111namespace phpbb \webpushnotifications \tests \event ;
1212
13+ require_once __DIR__ . '/../../../../../includes/functions_acp.php ' ;
14+
1315class listener_test extends \phpbb_database_test_case
1416{
1517 /** @var \phpbb\webpushnotifications\event\listener */
@@ -21,6 +23,9 @@ class listener_test extends \phpbb_database_test_case
2123 /* @var \phpbb\webpushnotifications\form\form_helper */
2224 protected $ form_helper ;
2325
26+ /** @var \FastImageSize\FastImageSize|\PHPUnit\Framework\MockObject\MockObject */
27+ protected $ imagesize ;
28+
2429 /** @var \phpbb\language\language */
2530 protected $ language ;
2631
@@ -91,6 +96,9 @@ protected function setUp(): void
9196 ->disableOriginalConstructor ()
9297 ->getMock ();
9398
99+ $ this ->imagesize = $ this ->getMockBuilder ('\FastImageSize\FastImageSize ' )
100+ ->getMock ();
101+
94102 $ this ->notification_method_webpush = new \phpbb \webpushnotifications \notification \method \webpush (
95103 $ this ->config ,
96104 $ db ,
@@ -113,7 +121,7 @@ protected function set_listener()
113121 $ this ->listener = new \phpbb \webpushnotifications \event \listener (
114122 $ this ->config ,
115123 $ this ->controller_helper ,
116- new \ FastImageSize \ FastImageSize () ,
124+ $ this -> imagesize ,
117125 $ this ->form_helper ,
118126 $ this ->language ,
119127 $ this ->template ,
@@ -245,4 +253,118 @@ public function test_get_ucp_template_data($user_id, $method_data, $subscription
245253 $ dispatcher ->addListener ('core.page_header_after ' , [$ this ->listener , 'load_template_data ' ]);
246254 $ dispatcher ->trigger_event ('core.page_header_after ' );
247255 }
256+
257+ public function acp_pwa_options_data ()
258+ {
259+ return [
260+ [ // expected config and mode
261+ 'settings ' ,
262+ ['vars ' => ['legend4 ' => []]],
263+ ['legend_pwa_settings ' , 'pwa_short_name ' , 'pwa_icon_small ' , 'pwa_icon_large ' , 'legend4 ' ],
264+ ],
265+ [ // unexpected mode
266+ 'foobar ' ,
267+ ['vars ' => ['legend4 ' => []]],
268+ ['legend4 ' ],
269+ ],
270+ [ // unexpected config
271+ 'post ' ,
272+ ['vars ' => ['foobar ' => []]],
273+ ['foobar ' ],
274+ ],
275+ [ // unexpected config and mode
276+ 'foobar ' ,
277+ ['vars ' => ['foobar ' => []]],
278+ ['foobar ' ],
279+ ],
280+ ];
281+ }
282+
283+ /**
284+ * @dataProvider acp_pwa_options_data
285+ */
286+ public function test_acp_pwa_options ($ mode , $ display_vars , $ expected_keys )
287+ {
288+ $ this ->set_listener ();
289+
290+ $ dispatcher = new \phpbb \event \dispatcher ();
291+ $ dispatcher ->addListener ('core.acp_board_config_edit_add ' , [$ this ->listener , 'acp_pwa_options ' ]);
292+
293+ $ event_data = ['display_vars ' , 'mode ' ];
294+ $ event_data_after = $ dispatcher ->trigger_event ('core.acp_board_config_edit_add ' , compact ($ event_data ));
295+
296+ foreach ($ event_data as $ expected )
297+ {
298+ self ::assertArrayHasKey ($ expected , $ event_data_after );
299+ }
300+ extract ($ event_data_after , EXTR_OVERWRITE );
301+
302+ $ keys = array_keys ($ display_vars ['vars ' ]);
303+
304+ self ::assertEquals ($ expected_keys , $ keys );
305+
306+ }
307+
308+ public function validate_pwa_options_data ()
309+ {
310+ return [
311+ [
312+ ['pwa_icon_small ' => '192.png ' , 'pwa_icon_large ' => '512.png ' ],
313+ ['PWA_IMAGE_NOT_FOUND ' ],
314+ ],
315+ [
316+ ['pwa_icon_small ' => '1.png ' , 'pwa_icon_large ' => '512.png ' ],
317+ ['PWA_IMAGE_NOT_FOUND ' , 'PWA_ICON_SIZE_INVALID ' ],
318+ ],
319+ [
320+ ['pwa_icon_small ' => '1.png ' , 'pwa_icon_large ' => '12.png ' ],
321+ ['PWA_IMAGE_NOT_FOUND ' , 'PWA_ICON_SIZE_INVALID ' ],
322+ ],
323+ [
324+ ['pwa_icon_small ' => '192.jpg ' , 'pwa_icon_large ' => '512.gif ' ],
325+ ['PWA_IMAGE_NOT_FOUND ' , 'PWA_ICON_MIME_INVALID ' ],
326+ ],
327+ [
328+ ['pwa_icon_small ' => '' , 'pwa_icon_large ' => '' ],
329+ [],
330+ ],
331+ ];
332+ }
333+
334+ /**
335+ * @dataProvider validate_pwa_options_data
336+ */
337+ public function test_validate_pwa_options ($ cfg_array , $ expected_error )
338+ {
339+ $ this ->config ['icons_path ' ] = 'images/icons ' ;
340+ $ config_name = key ($ cfg_array );
341+ $ config_definition = ['validate ' => 'pwa_options ' ];
342+ $ small_image = $ cfg_array ['pwa_icon_small ' ] ? explode ('. ' , $ cfg_array ['pwa_icon_small ' ]) : ['' , '' ];
343+ $ large_image = $ cfg_array ['pwa_icon_large ' ] ? explode ('. ' , $ cfg_array ['pwa_icon_large ' ]) : ['' , '' ];
344+ $ error = [];
345+
346+ $ this ->set_listener ();
347+
348+ $ this ->imagesize ->expects (self ::any ())
349+ ->method ('getImageSize ' )
350+ ->willReturnMap ([
351+ [$ this ->root_path . $ this ->config ['icons_path ' ] . '/ ' , '' , false ],
352+ [$ this ->root_path . $ this ->config ['icons_path ' ] . '/ ' . $ cfg_array ['pwa_icon_small ' ], '' , ['width ' => (int ) $ small_image [0 ], 'height ' => (int ) $ small_image [0 ], 'type ' => $ small_image [1 ] === 'png ' ? IMAGETYPE_PNG : IMAGETYPE_UNKNOWN ]],
353+ [$ this ->root_path . $ this ->config ['icons_path ' ] . '/ ' . $ cfg_array ['pwa_icon_large ' ], '' , ['width ' => (int ) $ large_image [0 ], 'height ' => (int ) $ large_image [0 ], 'type ' => $ large_image [1 ] === 'png ' ? IMAGETYPE_PNG : IMAGETYPE_UNKNOWN ]],
354+ ]);
355+
356+ $ dispatcher = new \phpbb \event \dispatcher ();
357+ $ dispatcher ->addListener ('core.validate_config_variable ' , [$ this ->listener , 'validate_pwa_options ' ]);
358+
359+ $ event_data = ['cfg_array ' , 'config_name ' , 'config_definition ' , 'error ' ];
360+ $ event_data_after = $ dispatcher ->trigger_event ('core.validate_config_variable ' , compact ($ event_data ));
361+
362+ foreach ($ event_data as $ expected )
363+ {
364+ self ::assertArrayHasKey ($ expected , $ event_data_after );
365+ }
366+ extract ($ event_data_after , EXTR_OVERWRITE );
367+
368+ self ::assertEquals ($ expected_error , $ error );
369+ }
248370}
0 commit comments