Skip to content

Commit 47c9f7e

Browse files
committed
test[faustwp]: widen AuthCallbacks coverage and document home_url() filter caveat
Three small additions on top of the regression-test commits: 1. Assert that the redirect_uri query arg is preserved through to the wp-login redirect_to param, in both the standard and Bedrock cases. A future change that successfully matched the request but mangled or dropped redirect_uri would previously slip through. 2. Add test_wp_in_subdirectory_install_matches_with_home_url for the Codex-documented 'Giving WordPress its own directory' pattern -- WP core in /wordpress, public site at root. Different prefix from Bedrock, same shape of divergence. Proves the fix is not a /wp-special case. Renames set_bedrock_siteurl() to set_split_install_siteurl($suffix) so both tests share the helper. 3. Document the home_url() filter dependency in handle_generate_endpoint()'s docblock. Multilingual plugins (WPML, Polylang, TranslatePress) that filter home_url() to prepend locale paths can still cause this match to miss for non-locale-prefixed frontend callers. Notes the likely follow-up direction (REST route migration) so the next person to touch this knows what they're inheriting. Suite is now 5 tests / 14 assertions. Verified red-on-revert: both the Bedrock and WP-in-subdirectory tests fail cleanly when callbacks.php is reverted to site_url(); restoring home_url() greens both single-site and multisite suites.
1 parent 4a595d4 commit 47c9f7e

2 files changed

Lines changed: 55 additions & 8 deletions

File tree

plugins/faustwp/includes/auth/callbacks.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
*
2020
* Generate an authorization code and redirect to the requested url.
2121
*
22+
* Note: matches REQUEST_URI against the filtered output of home_url(). Plugins
23+
* that filter home_url() to prepend locale paths (WPML, Polylang, TranslatePress)
24+
* may cause this match to fail when the frontend calls '/generate' directly
25+
* without a locale prefix. If that combination surfaces in support, the
26+
* follow-up will likely migrate '/generate' to a proper REST route so locale
27+
* filters cannot affect the match.
28+
*
2229
* @return void
2330
*/
2431
function handle_generate_endpoint() {

plugins/faustwp/tests/integration/AuthCallbacksTests.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,19 @@ public function tearDown(): void {
7171
}
7272

7373
/**
74-
* Reconfigure the WordPress siteurl option to mirror a Bedrock-style install
75-
* (WP core under /wp/, public site at root). Matches what
76-
* `composer create-project roots/bedrock` configures via WP_SITEURL in
77-
* wp-config.php.
74+
* Reconfigure the WordPress siteurl option to a split-install layout where WP
75+
* core lives in a subdirectory of the public site.
76+
*
77+
* Default suffix '/wp' mirrors Bedrock (what `composer create-project roots/bedrock`
78+
* sets via WP_SITEURL in wp-config.php). Pass '/wordpress' for the Codex-documented
79+
* "Giving WordPress its own directory" pattern, or any other prefix to exercise
80+
* other split-install configurations.
81+
*
82+
* @param string $suffix Path appended to the home option to form siteurl. Defaults to '/wp'.
7883
*/
79-
private function set_bedrock_siteurl(): void {
84+
private function set_split_install_siteurl( string $suffix = '/wp' ): void {
8085
$home = (string) get_option( 'home' );
81-
update_option( 'siteurl', rtrim( $home, '/' ) . '/wp' );
86+
update_option( 'siteurl', rtrim( $home, '/' ) . $suffix );
8287
}
8388

8489
/**
@@ -98,7 +103,10 @@ private function invoke_handler() {
98103

99104
/**
100105
* Standard install: REQUEST_URI matches the default search pattern; the
101-
* function proceeds to wp_safe_redirect (login-redirect branch).
106+
* function proceeds to wp_safe_redirect (login-redirect branch). The
107+
* redirect_uri query arg from the original request must be carried through
108+
* into the wp-login redirect_to param so the user lands back at the right
109+
* frontend after authenticating.
102110
*/
103111
public function test_standard_install_matches_and_redirects(): void {
104112
$_SERVER['REQUEST_URI'] = '/generate?redirect_uri=https://frontend.example/';
@@ -108,6 +116,11 @@ public function test_standard_install_matches_and_redirects(): void {
108116

109117
$this->assertNotNull( $redirect, 'Standard install must reach wp_safe_redirect.' );
110118
$this->assertStringContainsString( 'wp-login.php', $redirect );
119+
$this->assertStringContainsString(
120+
'frontend.example',
121+
$redirect,
122+
'redirect_uri value must be preserved in the wp-login redirect_to param.'
123+
);
111124
}
112125

113126
/**
@@ -119,7 +132,7 @@ public function test_standard_install_matches_and_redirects(): void {
119132
* the public REQUEST_URI.
120133
*/
121134
public function test_bedrock_divergence_matches_with_home_url(): void {
122-
$this->set_bedrock_siteurl();
135+
$this->set_split_install_siteurl( '/wp' );
123136

124137
// Sanity-check the divergence we just configured: site_url carries /wp,
125138
// home_url does not. If these fail, the test environment itself is broken
@@ -137,6 +150,33 @@ public function test_bedrock_divergence_matches_with_home_url(): void {
137150
'Bedrock layout (siteurl includes /wp, home does not) must still match REQUEST_URI=/generate when home_url() is used.'
138151
);
139152
$this->assertStringContainsString( 'wp-login.php', $redirect );
153+
$this->assertStringContainsString( 'frontend.example', $redirect );
154+
}
155+
156+
/**
157+
* "Giving WordPress its own directory" install: the Codex-documented pattern
158+
* where WP core is installed in a subdirectory (e.g. /wordpress) but the
159+
* site is served from the public root. Different prefix from Bedrock, same
160+
* shape of divergence -- proves the fix generalises beyond '/wp' specifically.
161+
*
162+
* Ref: https://wordpress.org/documentation/article/giving-wordpress-its-own-directory/
163+
*/
164+
public function test_wp_in_subdirectory_install_matches_with_home_url(): void {
165+
$this->set_split_install_siteurl( '/wordpress' );
166+
167+
$this->assertStringEndsWith( '/wordpress/generate', site_url( '/generate', 'relative' ) );
168+
$this->assertStringEndsWith( '/generate', home_url( '/generate', 'relative' ) );
169+
170+
$_SERVER['REQUEST_URI'] = '/generate?redirect_uri=https://frontend.example/';
171+
$_GET['redirect_uri'] = 'https://frontend.example/';
172+
173+
$redirect = $this->invoke_handler();
174+
175+
$this->assertNotNull(
176+
$redirect,
177+
'WP-in-subdirectory layout (siteurl includes /wordpress, home does not) must still match REQUEST_URI=/generate.'
178+
);
179+
$this->assertStringContainsString( 'wp-login.php', $redirect );
140180
}
141181

142182
/**

0 commit comments

Comments
 (0)