Skip to content

Commit c28f757

Browse files
committed
Added WP_Mock so we could test Faust with Preview. Refactored Faust tests to be able to test most of the functionality.
1 parent f87491a commit c28f757

File tree

5 files changed

+224
-35
lines changed

5 files changed

+224
-35
lines changed

plugins/hwp-previews/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"minimum-stability": "dev",
2323
"prefer-stable": true,
2424
"require-dev": {
25+
"10up/wp_mock": "^1.1",
2526
"automattic/vipwpcs": "^3.0",
2627
"codeception/lib-innerbrowser": "^1.0",
2728
"codeception/module-asserts": "^1.0",

plugins/hwp-previews/composer.lock

Lines changed: 103 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/hwp-previews/src/Integration/Faust_Integration.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use HWP\Previews\Admin\Settings\Fields\Settings_Field_Collection;
88
use HWP\Previews\Preview\Post\Post_Preview_Service;
99
use HWP\Previews\Preview\Post\Post_Settings_Service;
10+
use function WPE\FaustWP\Settings\faustwp_get_setting;
1011

1112
class Faust_Integration {
1213
/**
@@ -21,6 +22,11 @@ class Faust_Integration {
2122
*/
2223
public bool $faust_enabled = false;
2324

25+
/**
26+
* Whether Faust is enabled.
27+
*/
28+
public bool $faust_configured = false;
29+
2430
/**
2531
* The instance of the Faust integration.
2632
*
@@ -35,11 +41,6 @@ class Faust_Integration {
3541
*/
3642
public function __construct() {
3743
$this->faust_enabled = $this->is_faust_enabled();
38-
39-
if ( ! $this->get_faust_enabled() ) {
40-
return;
41-
}
42-
4344
$this->configure_faust();
4445
}
4546

@@ -58,10 +59,6 @@ public static function init(): Faust_Integration {
5859
* Checks if Faust is enabled.
5960
*/
6061
public function is_faust_enabled(): bool {
61-
if ( ! function_exists( 'is_plugin_active' ) ) {
62-
return false;
63-
}
64-
6562
return is_plugin_active( 'faustwp/faustwp.php' );
6663
}
6764

@@ -72,14 +69,21 @@ public function get_faust_enabled(): bool {
7269
return $this->faust_enabled;
7370
}
7471

72+
/**
73+
* Get the Faust configured status.
74+
*/
75+
public function get_faust_configured(): bool {
76+
return $this->faust_configured;
77+
}
78+
7579
/**
7680
* Returns the Faust frontend URL from settings or a default value.
7781
*/
7882
public function get_faust_frontend_url(): string {
7983
$default_value = 'http://localhost:3000';
8084

8185
if ( $this->get_faust_enabled() && function_exists( '\WPE\FaustWP\Settings\faustwp_get_setting' ) ) {
82-
$frontend_uri = \WPE\FaustWP\Settings\faustwp_get_setting( 'frontend_uri', '' );
86+
$frontend_uri = faustwp_get_setting( 'frontend_uri', '' );
8387

8488
if ( ! empty( $frontend_uri ) ) {
8589
return $frontend_uri;
@@ -177,6 +181,11 @@ public static function dismiss_faust_admin_notice(): void {
177181
* Configure Faust settings and remove conflicting filters.
178182
*/
179183
protected function configure_faust(): void {
184+
if ( ! $this->get_faust_enabled() ) {
185+
return;
186+
}
187+
$this->faust_configured = true;
188+
180189
$this->set_default_faust_settings();
181190

182191
// Remove FaustWP post preview link filter to avoid conflicts with our custom preview link generation.
Lines changed: 100 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,55 @@
11
<?php
22

3-
declare( strict_types=1 );
3+
declare(strict_types=1);
44

55
namespace HWP\Previews\Tests\Integration;
66

77
use HWP\Previews\Integration\Faust_Integration;
88
use 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
*/
1515
class 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
}

plugins/hwp-previews/tests/wpunit/Preview/Url/PreviewUrlResolverServiceTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public function test_resolve_default_parameters(): void {
3333
$service = new Preview_Url_Resolver_Service($registry);
3434

3535
$author = WPTestCase::factory()->user->create_and_get( [
36-
'user_login' => 'test_author',
37-
'user_email' => uniqid( 'test_author', true ) . '@example.com'
36+
'user_login' => 'test_author'
3837
]);
3938

4039
$post = WPTestCase::factory()->post->create_and_get( [

0 commit comments

Comments
 (0)