11<?php
22
3- declare ( strict_types=1 );
3+ declare (strict_types=1 );
44
55namespace HWP \Previews \Tests \Integration ;
66
77use HWP \Previews \Integration \Faust_Integration ;
88use lucatume \WPBrowser \TestCase \WPTestCase ;
9+ use ReflectionClass ;
10+ use WP_Mock ;
911
1012/**
11- * Note: A lot of the functionality is tested with e2e tests
12- *
13- * Test class for Faust_Integration
13+ * Test class for Faust_Integration.
1414 */
1515class Faust_Integration_Test extends WPTestCase {
1616
17- protected function setUp (): void {
17+ public function setUp () : void {
1818 parent ::setUp ();
1919
20- // Reset the singleton instance before each test
21- $ reflection = new \ReflectionClass ( Faust_Integration::class );
22- $ instance_property = $ reflection ->getProperty ( 'instance ' );
23- $ instance_property ->setAccessible ( true );
24- $ instance_property ->setValue ( null , null );
20+ // Note: We need WP_Mock so we can test frontend URL resolution for Faust
21+ if (class_exists ('\WP_Mock ' )) {
22+ \WP_Mock::setUp ();
23+ }
24+
25+
26+ // Reset singleton instance before each test
27+ $ reflection = new ReflectionClass ( Faust_Integration::class );
28+ $ instanceProperty = $ reflection ->getProperty ( 'instance ' );
29+ $ instanceProperty ->setAccessible ( true );
30+ $ instanceProperty ->setValue ( null , null );
2531 }
2632
27- /**
28- * Test that init() returns a singleton instance
29- */
30- public function test_init_returns_singleton_instance (): void {
31- $ instance1 = Faust_Integration::init ();
32- $ instance2 = Faust_Integration::init ();
3333
34- $ this ->assertInstanceOf ( Faust_Integration::class, $ instance1 );
35- $ this ->assertSame ( $ instance1 , $ instance2 , 'init() should return the same singleton instance ' );
34+ public function test_instance_creates_and_sets_up_faust_integration_when_not_set () {
35+ $ reflection = new ReflectionClass ( Faust_Integration::class );
36+ $ instanceProperty = $ reflection ->getProperty ( 'instance ' );
37+ $ instanceProperty ->setAccessible ( true );
38+ $ instanceProperty ->setValue ( null );
39+
40+ $ this ->assertNull ( $ instanceProperty ->getValue () );
41+ $ instance = Faust_Integration::init ();
42+
43+ $ this ->assertInstanceOf ( Faust_Integration::class, $ instanceProperty ->getValue () );
44+ $ this ->assertSame ( $ instance , $ instanceProperty ->getValue (), 'Faust_Integration::init() should set the static instance property ' );
45+
46+ $ this ->assertFalse ( $ instance ->get_faust_enabled () );
47+ $ this ->assertFalse ( $ instance ->get_faust_configured () );
48+
3649 }
3750
3851
39- public function test_is_faust_enabled_asserts_true () {
52+ public function test_instance_configure_faust () {
4053
4154 // Mock FaustWP exists
4255 tests_add_filter ( 'pre_option_active_plugins ' , function ( $ plugins ) {
@@ -46,12 +59,77 @@ public function test_is_faust_enabled_asserts_true() {
4659 } );
4760
4861 $ instance = Faust_Integration::init ();
49- $ this ->assertTrue ( $ instance ->is_faust_enabled () );
62+ $ this ->assertTrue ( $ instance ->get_faust_enabled () );
63+ $ this ->assertTrue ( $ instance ->get_faust_configured () );
64+ }
65+
66+
67+ public function test_dismiss_faust_notice_meta_value () {
68+ $ instance = Faust_Integration::init ();
69+
70+ $ admin_user = WPTestCase::factory ()->user ->create_and_get ( [
71+ 'role ' => 'administrator ' ,
72+ 'meta_input ' => [
73+ 'first_name ' => 'Test ' ,
74+ 'last_name ' => 'User ' ,
75+ ],
76+ 'user_login ' => 'testuser '
77+ ] );
78+
79+ // Set the current user to the admin user
80+ $ original_user_id = get_current_user_id ();
81+ wp_set_current_user ( $ admin_user ->ID );
82+
83+ // Set the user meta and check
84+ $ instance ::dismiss_faust_admin_notice ();
85+ $ this ->assertEquals (
86+ 1 ,
87+ get_user_meta ( $ admin_user ->ID , Faust_Integration::FAUST_NOTICE_KEY , true )
88+ );
89+
90+ $ this ->assertFalse (
91+ get_user_meta ( $ original_user_id , Faust_Integration::FAUST_NOTICE_KEY , true )
92+ );
93+
94+ // Reset the current user
95+ wp_set_current_user ( $ original_user_id );
96+ }
97+
98+
99+ public function test_faust_frontend_url_default_url () {
100+
101+ $ instance = Faust_Integration::init ();
102+ $ this ->assertEquals ( $ instance ->get_faust_frontend_url (), 'http://localhost:3000 ' );
103+
50104 }
51105
52- public function test_is_faust_enabled_asserts_false () {
106+ public function test_faust_frontend_url_with_faust_setting () {
107+
108+ // Mock FaustWP exists
109+ tests_add_filter ( 'pre_option_active_plugins ' , function ( $ plugins ) {
110+ $ plugins [] = 'faustwp/faustwp.php ' ;
111+
112+ return $ plugins ;
113+ } );
114+
115+ $ frontend_uri = 'https://mocked-frontend.com ' ;
116+
117+ // We need to Mock each type so that the function can be called in different ways
118+ \WP_Mock::userFunction ('\WPE\FaustWP\Settings\faustwp_get_setting ' , [
119+ 'return ' => $ frontend_uri
120+ ]);
121+
122+ \WP_Mock::userFunction ('faustwp_get_setting ' , [
123+ 'return ' => $ frontend_uri
124+ ]);
125+
126+ \WP_Mock::userFunction ('WPE\FaustWP\Settings\faustwp_get_setting ' , [
127+ 'return ' => $ frontend_uri
128+ ]);
53129
54130 $ instance = Faust_Integration::init ();
55- $ this ->assertFalse ( $ instance ->is_faust_enabled () );
131+ $ this ->assertTrue (function_exists ( '\WPE\FaustWP\Settings\faustwp_get_setting ' ) );
132+ $ this ->assertEquals ( $ frontend_uri , $ instance ->get_faust_frontend_url () );
133+
56134 }
57135}
0 commit comments