@@ -21,6 +21,12 @@ public function in_admin( $context = null ) {
2121 return $ context === 'user ' ;
2222 }
2323 };
24+
25+ // Reset the instance before each test
26+ $ reflection = new ReflectionClass ( Settings_Page::class );
27+ $ instanceProperty = $ reflection ->getProperty ( 'instance ' );
28+ $ instanceProperty ->setAccessible ( true );
29+ $ instanceProperty ->setValue ( null );
2430 }
2531
2632 public function tearDown () : void {
@@ -29,13 +35,40 @@ public function tearDown() : void {
2935 parent ::tearDown ();
3036 }
3137
38+ public function test_init_returns_null_if_not_admin () {
39+ // Unset the admin screen mock
40+ unset( $ GLOBALS ['current_screen ' ] );
41+
42+ $ instance = Settings_Page::init ();
43+ $ this ->assertNull ( $ instance , 'Settings_Page::init() should return null if not in admin. ' );
44+ }
45+
46+ public function test_init_returns_null_for_user_without_manage_options_cap () {
47+ $ user_id = self ::factory ()->user ->create ( [ 'role ' => 'subscriber ' ] );
48+ wp_set_current_user ( $ user_id );
49+
50+ $ instance = Settings_Page::init ();
51+ $ this ->assertNull ( $ instance , 'Settings_Page::init() should return null for users without manage_options capability. ' );
52+ }
53+
54+ public function test_init_returns_instance_for_user_with_manage_options_cap () {
55+ $ user_id = self ::factory ()->user ->create ( [ 'role ' => 'administrator ' ] );
56+ wp_set_current_user ( $ user_id );
57+
58+ $ instance = Settings_Page::init ();
59+ $ this ->assertInstanceOf ( Settings_Page::class, $ instance , 'Settings_Page::init() should return an instance for users with manage_options capability. ' );
60+ }
61+
3262 public function test_settings_page_instance () {
3363 $ reflection = new ReflectionClass ( Settings_Page::class );
3464 $ instanceProperty = $ reflection ->getProperty ( 'instance ' );
3565 $ instanceProperty ->setAccessible ( true );
36- $ instanceProperty ->setValue ( null );
3766
3867 $ this ->assertNull ( $ instanceProperty ->getValue () );
68+
69+ // To pass the capability check
70+ $ user_id = self ::factory ()->user ->create ( [ 'role ' => 'administrator ' ] );
71+ wp_set_current_user ( $ user_id );
3972 $ instance = Settings_Page::init ();
4073
4174 $ this ->assertInstanceOf ( Settings_Page::class, $ instanceProperty ->getValue () );
@@ -44,7 +77,11 @@ public function test_settings_page_instance() {
4477
4578 public function test_get_current_tab () {
4679 $ _GET ['attachment ' ] = 'attachment ' ;
47- $ settings_page = Settings_Page::init ();
80+
81+ // To pass the capability check
82+ $ user_id = self ::factory ()->user ->create ( [ 'role ' => 'administrator ' ] );
83+ wp_set_current_user ( $ user_id );
84+ $ settings_page = Settings_Page::init ();
4885
4986 $ post_preview_service = new Post_Preview_Service ();
5087 $ post_types = $ post_preview_service ->get_post_types ();
@@ -61,6 +98,9 @@ public function test_get_current_tab() {
6198 }
6299
63100 public function test_register_hooks () {
101+ // To pass the capability check
102+ $ user_id = self ::factory ()->user ->create ( [ 'role ' => 'administrator ' ] );
103+ wp_set_current_user ( $ user_id );
64104 $ settings_page = Settings_Page::init ();
65105 $ this ->assertNull ( $ settings_page ->register_settings_page () );
66106 $ this ->assertNull ( $ settings_page ->register_settings_fields () );
0 commit comments