@@ -23,7 +23,7 @@ class event_listener_test extends phpbb_test_case
2323{
2424 protected user |MockObject $ user ;
2525 protected template |MockObject $ template ;
26- protected helper $ helper ;
26+ protected helper $ pwa_helper ;
2727
2828 /**
2929 * Setup test environment
@@ -32,16 +32,20 @@ protected function setUp(): void
3232 {
3333 parent ::setUp ();
3434
35- $ this ->user = $ this ->createMock (user::class);
36- $ this ->user ->optionset ('user_id ' , 2 );
35+ $ this ->user = $ this ->getMockBuilder (user::class)
36+ ->disableOriginalConstructor ()
37+ ->getMock ();
38+ $ this ->user ->method ('optionset ' )
39+ ->with ('user_id ' , 2 )
40+ ->willReturn (null );
3741 $ this ->user ->data ['user_id ' ] = 2 ;
3842 $ this ->user ->style ['pwa_bg_color ' ] = '' ;
3943 $ this ->user ->style ['pwa_theme_color ' ] = '' ;
4044
4145 $ this ->template = $ this ->getMockBuilder (template::class)
4246 ->getMock ();
4347
44- $ this ->helper = $ this ->getMockBuilder (helper::class)
48+ $ this ->pwa_helper = $ this ->getMockBuilder (helper::class)
4549 ->disableOriginalConstructor ()
4650 ->getMock ();
4751 }
@@ -54,7 +58,7 @@ protected function setUp(): void
5458 protected function get_listener (): main_listener
5559 {
5660 return new main_listener (
57- $ this ->helper ,
61+ $ this ->pwa_helper ,
5862 $ this ->template ,
5963 $ this ->user
6064 );
@@ -73,42 +77,44 @@ public function test_construct()
7377 */
7478 public function test_getSubscribedEvents ()
7579 {
80+ $ events = main_listener::getSubscribedEvents ();
7681 static ::assertEquals ([
77- 'core.page_header ' ,
78- 'core.modify_manifest ' ,
79- ], array_keys (\ phpbb \ pwakit \ event \main_listener:: getSubscribedEvents ()) );
82+ 'core.page_header ' => ' header_updates ' ,
83+ 'core.modify_manifest ' => ' manifest_updates ' ,
84+ ], $ events );
8085 }
8186
8287 public function header_updates_test_data (): array
8388 {
8489 return [
85- 'header with data ' => [
86- [
87- 'pwa_theme_color ' => '#foobar ' ,
88- 'pwa_bg_color ' => '#barfoo ' ,
89- ],
90+ 'valid hex colors ' => [
9091 [
91- [
92- 'src ' => 'images/site_icons/touch-icon-192.png ' ,
93- 'sizes ' => '192x192 ' ,
94- 'type ' => 'image/png '
95- ],
96- [
97- 'src ' => 'images/site_icons/touch-icon-512.png ' ,
98- 'sizes ' => '512x512 ' ,
99- 'type ' => 'image/png '
100- ],
92+ 'pwa_theme_color ' => '#ffffff ' ,
93+ 'pwa_bg_color ' => '#000000 ' ,
10194 ],
95+ self ::getValidIcons (),
10296 [
103- 'pwa_theme_color ' => '#foobar ' ,
104- 'pwa_bg_color ' => '#barfoo ' ,
97+ 'pwa_theme_color ' => '#ffffff ' ,
98+ 'pwa_bg_color ' => '#000000 ' ,
10599 'icons ' => [
106100 'images/site_icons/touch-icon-192.png ' ,
107101 'images/site_icons/touch-icon-512.png ' ,
108102 ]
109103 ],
110104 ],
111- 'header without data ' => [
105+ 'invalid hex colors ' => [
106+ [
107+ 'pwa_theme_color ' => '#gggggg ' ,
108+ 'pwa_bg_color ' => 'invalid ' ,
109+ ],
110+ [],
111+ [
112+ 'pwa_theme_color ' => '#gggggg ' ,
113+ 'pwa_bg_color ' => 'invalid ' ,
114+ 'icons ' => [],
115+ ],
116+ ],
117+ 'empty values ' => [
112118 [
113119 'pwa_theme_color ' => '' ,
114120 'pwa_bg_color ' => '' ,
@@ -123,6 +129,22 @@ public function header_updates_test_data(): array
123129 ];
124130 }
125131
132+ private static function getValidIcons (): array
133+ {
134+ return [
135+ [
136+ 'src ' => 'images/site_icons/touch-icon-192.png ' ,
137+ 'sizes ' => '192x192 ' ,
138+ 'type ' => 'image/png '
139+ ],
140+ [
141+ 'src ' => 'images/site_icons/touch-icon-512.png ' ,
142+ 'sizes ' => '512x512 ' ,
143+ 'type ' => 'image/png '
144+ ],
145+ ];
146+ }
147+
126148 /**
127149 * @param $configs
128150 * @param $icons
@@ -132,24 +154,32 @@ public function header_updates_test_data(): array
132154 */
133155 public function test_header_updates ($ configs , $ icons , $ expected )
134156 {
135- foreach ($ configs as $ key => $ value )
136- {
137- $ this ->user ->style [$ key ] = $ value ;
138- }
139-
140- $ this ->helper ->expects (static ::once ())
157+ // Setup expectations
158+ $ this ->pwa_helper ->expects (static ::once ())
141159 ->method ('get_icons ' )
142160 ->willReturn ($ icons );
143161
162+ $ templateVars = [
163+ 'PWA_THEME_COLOR ' => $ expected ['pwa_theme_color ' ],
164+ 'PWA_BG_COLOR ' => $ expected ['pwa_bg_color ' ],
165+ 'U_TOUCH_ICONS ' => $ expected ['icons ' ],
166+ ];
167+
144168 $ this ->template ->expects (static ::once ())
145169 ->method ('assign_vars ' )
146- ->with ([
147- 'PWA_THEME_COLOR ' => $ expected ['pwa_theme_color ' ],
148- 'PWA_BG_COLOR ' => $ expected ['pwa_bg_color ' ],
149- 'U_TOUCH_ICONS ' => $ expected ['icons ' ],
150- ]);
170+ ->with (static ::identicalTo ($ templateVars ));
171+
172+ // Apply configurations
173+ foreach ($ configs as $ key => $ value ) {
174+ $ this ->user ->style [$ key ] = $ value ;
175+ }
151176
152- $ this ->get_listener ()->header_updates ();
177+ $ listener = $ this ->get_listener ();
178+ $ listener ->header_updates ();
179+
180+ // Verify the final state
181+ $ this ->assertEquals ($ configs ['pwa_theme_color ' ], $ this ->user ->style ['pwa_theme_color ' ]);
182+ $ this ->assertEquals ($ configs ['pwa_bg_color ' ], $ this ->user ->style ['pwa_bg_color ' ]);
153183 }
154184
155185 public function manifest_updates_test_data (): array
@@ -213,32 +243,56 @@ public function manifest_updates_test_data(): array
213243 */
214244 public function test_manifest_updates ($ board_path , $ configs , $ expected )
215245 {
246+ $ initialManifest = [
247+ 'name ' => 'Test Site ' ,
248+ 'short_name ' => 'TestSite ' ,
249+ 'display ' => 'standalone ' ,
250+ 'orientation ' => 'portrait ' ,
251+ 'start_url ' => './ ' ,
252+ 'scope ' => './ ' ,
253+ ];
254+
216255 $ event = new data ([
217- 'manifest ' => [
218- 'name ' => 'Test Site ' ,
219- 'short_name ' => 'TestSite ' ,
220- 'display ' => 'standalone ' ,
221- 'orientation ' => 'portrait ' ,
222- 'start_url ' => './ ' ,
223- 'scope ' => './ ' ,
224- ],
256+ 'manifest ' => $ initialManifest ,
225257 'board_path ' => $ board_path ,
226258 ]);
227259
228- foreach ($ configs as $ key => $ value )
229- {
260+ // Set up and verify the initial state
261+ $ this ->assertSame ($ initialManifest , $ event ['manifest ' ]);
262+
263+ foreach ($ configs as $ key => $ value ) {
230264 $ this ->user ->style [$ key ] = $ value ;
231265 }
232266
233- $ expected = array_merge ($ event [ ' manifest ' ] , $ expected );
267+ $ expected = array_merge ($ initialManifest , $ expected );
234268
235- $ this ->helper ->expects (static ::once ())
269+ // Verify helper method call
270+ $ this ->pwa_helper ->expects (static ::once ())
236271 ->method ('get_icons ' )
237272 ->with ($ board_path )
238273 ->willReturn ($ expected ['icons ' ] ?? []);
239274
240- $ this ->get_listener ()->manifest_updates ($ event );
275+ // Execute test
276+ $ listener = $ this ->get_listener ();
277+ $ listener ->manifest_updates ($ event );
241278
279+ // Verify the final state
242280 $ this ->assertSame ($ expected , $ event ['manifest ' ]);
281+
282+ // Verify manifest structure
283+ if (!empty ($ event ['manifest ' ])) {
284+ $ this ->assertArrayHasKey ('name ' , $ event ['manifest ' ]);
285+ $ this ->assertArrayHasKey ('short_name ' , $ event ['manifest ' ]);
286+ $ this ->assertArrayHasKey ('display ' , $ event ['manifest ' ]);
287+ $ this ->assertArrayHasKey ('orientation ' , $ event ['manifest ' ]);
288+ }
289+
290+ // Verify color format if present
291+ if (isset ($ event ['manifest ' ]['theme_color ' ])) {
292+ $ this ->assertMatchesRegularExpression ('/^#[0-9a-f]{6}$/i ' , $ event ['manifest ' ]['theme_color ' ]);
293+ }
294+ if (isset ($ event ['manifest ' ]['background_color ' ])) {
295+ $ this ->assertMatchesRegularExpression ('/^#[0-9a-f]{6}$/i ' , $ event ['manifest ' ]['background_color ' ]);
296+ }
243297 }
244298}
0 commit comments