@@ -311,6 +311,195 @@ public function test_get_bf_notices_banner_dismissal_values() {
311311 }
312312 }
313313
314+ // -------------------------------------------------------------------------
315+ // get_permissions_policy() tests
316+ // -------------------------------------------------------------------------
317+
318+ /**
319+ * Helper: set scale, network_optimization and retina_images settings, then
320+ * return a fresh Optml_Admin instance that reads those values.
321+ *
322+ * @param string $scale 'enabled' or 'disabled'.
323+ * @param string $network_opt 'enabled' or 'disabled'.
324+ * @param string $retina 'enabled' or 'disabled'.
325+ * @return Optml_Admin
326+ */
327+ private function make_admin_with_policy_settings ( string $ scale , string $ network_opt , string $ retina ): Optml_Admin {
328+ $ settings = new Optml_Settings ();
329+ $ settings ->update ( 'scale ' , $ scale );
330+ $ settings ->update ( 'network_optimization ' , $ network_opt );
331+ $ settings ->update ( 'retina_images ' , $ retina );
332+ Optml_Config::$ service_url = 'https://test123.i.optimole.com ' ;
333+ return new Optml_Admin ();
334+ }
335+
336+ /**
337+ * All three settings off → empty string.
338+ */
339+ public function test_permissions_policy_empty_when_all_off () {
340+ $ admin = $ this ->make_admin_with_policy_settings ( 'enabled ' , 'disabled ' , 'disabled ' );
341+ $ this ->assertSame ( '' , $ admin ->get_permissions_policy () );
342+ }
343+
344+ /**
345+ * Scale ON → ch-viewport-width present; ch-dpr and ch-ect absent.
346+ */
347+ public function test_permissions_policy_ch_viewport_width_when_scale_on () {
348+ $ admin = $ this ->make_admin_with_policy_settings ( 'disabled ' , 'disabled ' , 'disabled ' );
349+ $ policy = $ admin ->get_permissions_policy ();
350+ $ this ->assertStringContainsString ( 'ch-viewport-width ' , $ policy );
351+ $ this ->assertStringNotContainsString ( 'ch-dpr ' , $ policy );
352+ $ this ->assertStringNotContainsString ( 'ch-ect ' , $ policy );
353+ }
354+
355+ /**
356+ * Scale OFF → ch-viewport-width absent.
357+ */
358+ public function test_permissions_policy_no_ch_viewport_width_when_scale_off () {
359+ $ admin = $ this ->make_admin_with_policy_settings ( 'enabled ' , 'disabled ' , 'disabled ' );
360+ $ this ->assertStringNotContainsString ( 'ch-viewport-width ' , $ admin ->get_permissions_policy () );
361+ }
362+
363+ /**
364+ * Retina ON → ch-dpr present; ch-viewport-width absent.
365+ */
366+ public function test_permissions_policy_ch_dpr_when_retina_on () {
367+ $ admin = $ this ->make_admin_with_policy_settings ( 'enabled ' , 'disabled ' , 'enabled ' );
368+ $ policy = $ admin ->get_permissions_policy ();
369+ $ this ->assertStringContainsString ( 'ch-dpr ' , $ policy );
370+ $ this ->assertStringNotContainsString ( 'ch-viewport-width ' , $ policy );
371+ }
372+
373+ /**
374+ * Retina OFF → ch-dpr absent.
375+ */
376+ public function test_permissions_policy_no_ch_dpr_when_retina_off () {
377+ $ admin = $ this ->make_admin_with_policy_settings ( 'enabled ' , 'disabled ' , 'disabled ' );
378+ $ this ->assertStringNotContainsString ( 'ch-dpr ' , $ admin ->get_permissions_policy () );
379+ }
380+
381+ /**
382+ * Network optimization ON → ch-ect present.
383+ */
384+ public function test_permissions_policy_ch_ect_when_network_optimization_on () {
385+ $ admin = $ this ->make_admin_with_policy_settings ( 'enabled ' , 'enabled ' , 'disabled ' );
386+ $ policy = $ admin ->get_permissions_policy ();
387+ $ this ->assertStringContainsString ( 'ch-ect ' , $ policy );
388+ }
389+
390+ /**
391+ * All three ON → all three directives present in a single string.
392+ */
393+ public function test_permissions_policy_all_features_on () {
394+ $ admin = $ this ->make_admin_with_policy_settings ( 'disabled ' , 'enabled ' , 'enabled ' );
395+ $ policy = $ admin ->get_permissions_policy ();
396+ $ this ->assertStringContainsString ( 'ch-viewport-width ' , $ policy );
397+ $ this ->assertStringContainsString ( 'ch-ect ' , $ policy );
398+ $ this ->assertStringContainsString ( 'ch-dpr ' , $ policy );
399+ }
400+
401+ /**
402+ * Service URL is embedded in the policy when any feature is ON.
403+ */
404+ public function test_permissions_policy_service_url_embedded () {
405+ $ admin = $ this ->make_admin_with_policy_settings ( 'disabled ' , 'disabled ' , 'disabled ' );
406+ $ policy = $ admin ->get_permissions_policy ();
407+ $ this ->assertStringContainsString ( 'test123.i.optimole.com ' , $ policy );
408+ }
409+
410+ // -------------------------------------------------------------------------
411+ // get_accept_ch_hints() tests
412+ // -------------------------------------------------------------------------
413+
414+ /**
415+ * All three settings off → empty string.
416+ * Note: for the scale setting, 'enabled' = scale IS off (legacy inversion).
417+ */
418+ public function test_accept_ch_hints_empty_when_all_off () {
419+ $ admin = $ this ->make_admin_with_policy_settings ( 'enabled ' , 'disabled ' , 'disabled ' );
420+ $ this ->assertSame ( '' , $ admin ->get_accept_ch_hints () );
421+ }
422+
423+ /**
424+ * Scale ON → Viewport-Width present; DPR and ECT absent.
425+ * Note: 'disabled' raw value = scale IS on (legacy inversion).
426+ */
427+ public function test_accept_ch_hints_viewport_width_when_scale_on () {
428+ $ admin = $ this ->make_admin_with_policy_settings ( 'disabled ' , 'disabled ' , 'disabled ' );
429+ $ hints = $ admin ->get_accept_ch_hints ();
430+ $ this ->assertStringContainsString ( 'Viewport-Width ' , $ hints );
431+ $ this ->assertStringNotContainsString ( 'DPR ' , $ hints );
432+ $ this ->assertStringNotContainsString ( 'ECT ' , $ hints );
433+ }
434+
435+ /**
436+ * Scale OFF → Viewport-Width absent.
437+ * Note: 'enabled' raw value = scale IS off (legacy inversion).
438+ */
439+ public function test_accept_ch_hints_no_viewport_width_when_scale_off () {
440+ $ admin = $ this ->make_admin_with_policy_settings ( 'enabled ' , 'disabled ' , 'disabled ' );
441+ $ this ->assertStringNotContainsString ( 'Viewport-Width ' , $ admin ->get_accept_ch_hints () );
442+ }
443+
444+ /**
445+ * Retina ON → DPR present; Viewport-Width absent (scale kept off).
446+ */
447+ public function test_accept_ch_hints_dpr_when_retina_on () {
448+ $ admin = $ this ->make_admin_with_policy_settings ( 'enabled ' , 'disabled ' , 'enabled ' );
449+ $ hints = $ admin ->get_accept_ch_hints ();
450+ $ this ->assertStringContainsString ( 'DPR ' , $ hints );
451+ $ this ->assertStringNotContainsString ( 'Viewport-Width ' , $ hints );
452+ }
453+
454+ /**
455+ * Retina OFF → DPR absent.
456+ */
457+ public function test_accept_ch_hints_no_dpr_when_retina_off () {
458+ $ admin = $ this ->make_admin_with_policy_settings ( 'enabled ' , 'disabled ' , 'disabled ' );
459+ $ this ->assertStringNotContainsString ( 'DPR ' , $ admin ->get_accept_ch_hints () );
460+ }
461+
462+ /**
463+ * Network optimization ON → ECT present.
464+ */
465+ public function test_accept_ch_hints_ect_when_network_optimization_on () {
466+ $ admin = $ this ->make_admin_with_policy_settings ( 'enabled ' , 'enabled ' , 'disabled ' );
467+ $ this ->assertStringContainsString ( 'ECT ' , $ admin ->get_accept_ch_hints () );
468+ }
469+
470+ /**
471+ * All three ON → all three tokens present.
472+ * Note: 'disabled' raw value = scale IS on (legacy inversion).
473+ */
474+ public function test_accept_ch_hints_all_features_on () {
475+ $ admin = $ this ->make_admin_with_policy_settings ( 'disabled ' , 'enabled ' , 'enabled ' );
476+ $ hints = $ admin ->get_accept_ch_hints ();
477+ $ this ->assertStringContainsString ( 'Viewport-Width ' , $ hints );
478+ $ this ->assertStringContainsString ( 'ECT ' , $ hints );
479+ $ this ->assertStringContainsString ( 'DPR ' , $ hints );
480+ }
481+
482+ /**
483+ * Hints and policy directives stay in sync: each setting contributes
484+ * to both get_accept_ch_hints() and get_permissions_policy().
485+ * Note: scale uses legacy inversion — 'disabled' raw = scale ON.
486+ */
487+ public function test_accept_ch_and_policy_are_in_sync () {
488+ foreach ( [ 'scale ' , 'network_optimization ' , 'retina_images ' ] as $ setting ) {
489+ // Legacy: 'disabled' = scale ON, 'enabled' = scale OFF.
490+ $ scale = $ setting === 'scale ' ? 'disabled ' : 'enabled ' ;
491+ $ net_opt = $ setting === 'network_optimization ' ? 'enabled ' : 'disabled ' ;
492+ $ retina = $ setting === 'retina_images ' ? 'enabled ' : 'disabled ' ;
493+
494+ $ admin = $ this ->make_admin_with_policy_settings ( $ scale , $ net_opt , $ retina );
495+ $ hints = $ admin ->get_accept_ch_hints ();
496+ $ policy = $ admin ->get_permissions_policy ();
497+
498+ $ this ->assertNotEmpty ( $ hints , "Expected non-empty hints for setting: $ setting " );
499+ $ this ->assertNotEmpty ( $ policy , "Expected non-empty policy for setting: $ setting " );
500+ }
501+ }
502+
314503 /**
315504 * Test get_bf_notices at exact boundary conditions.
316505 */
0 commit comments