Skip to content

Commit fe29677

Browse files
committed
test: add wu_create_site integration tests for subdomain slug sanitization
Adds two integration tests to Site_Functions_Extended_Test that verify the subdomain slug sanitization introduced in GH#812 (PR #822) works end-to-end at the wu_create_site() API level: - test_create_site_sanitizes_subdomain_slug: verifies that a path with special characters (e.g. '/My Cool Site!/') produces a sanitized subdomain hostname containing 'my-cool-site' and does NOT fail with 'invalid_site_path'. - test_create_site_returns_error_for_empty_subdomain_slug: verifies that a path of only invalid characters (e.g. '/!!!/') returns WP_Error with 'invalid_site_path', preventing malformed hostnames. To make these tests runnable in the subdirectory test environment (where is_subdomain_install() checks the SUBDOMAIN_INSTALL constant), a new filter 'wu_is_subdomain_install' wraps the is_subdomain_install() call in wu_create_site(). Tests override via add_filter('wu_is_subdomain_install', '__return_true') to force the subdomain code path without redefining constants. Resolves #826
1 parent e6629f5 commit fe29677

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

inc/functions/site.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ function wu_create_site($site_data) {
249249
: '';
250250
$domain_supplied = '' !== $normalized_input_domain && $normalized_input_domain !== $normalized_network_domain;
251251

252-
if (is_multisite() && is_subdomain_install() && $path_supplied && ! $domain_supplied) {
252+
if (is_multisite() && apply_filters('wu_is_subdomain_install', is_subdomain_install()) && $path_supplied && ! $domain_supplied) {
253253
$raw_slug = trim((string) $site_data['path'], '/');
254254
$slug = sanitize_title_with_dashes(wu_clean($raw_slug));
255255

tests/WP_Ultimo/Functions/Site_Functions_Extended_Test.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,64 @@ public function test_subdomain_slug_sanitization_handles_unicode(): void {
259259
'Slug should only contain valid hostname characters.'
260260
);
261261
}
262+
263+
/**
264+
* Test wu_create_site sanitizes subdomain slug from path on subdomain installs.
265+
*
266+
* When a path is provided on a subdomain install without an explicit domain,
267+
* the slug derived from the path should be sanitized before building the
268+
* subdomain hostname.
269+
*/
270+
public function test_create_site_sanitizes_subdomain_slug(): void {
271+
272+
// Force subdomain install mode via the wu_is_subdomain_install filter.
273+
add_filter('wu_is_subdomain_install', '__return_true');
274+
275+
$site = wu_create_site(
276+
[
277+
'title' => 'Sanitized Slug Test',
278+
'path' => '/My Cool Site!/',
279+
]
280+
);
281+
282+
remove_all_filters('wu_is_subdomain_install');
283+
284+
// The slug should be sanitized (lowercase, dashes, no special chars).
285+
// It may succeed as a site or fail for other reasons, but should NOT
286+
// fail with 'invalid_site_path'.
287+
if (is_wp_error($site)) {
288+
$this->assertNotEquals(
289+
'invalid_site_path',
290+
$site->get_error_code(),
291+
'A valid-ish path should not produce invalid_site_path after sanitization.'
292+
);
293+
} else {
294+
// If it succeeded, verify the domain contains the sanitized slug.
295+
$this->assertStringContainsString('my-cool-site', $site->get_domain());
296+
}
297+
}
298+
299+
/**
300+
* Test wu_create_site returns WP_Error for empty slug on subdomain installs.
301+
*
302+
* When the path contains only invalid characters that sanitize to an empty
303+
* string, wu_create_site should return a WP_Error with 'invalid_site_path'.
304+
*/
305+
public function test_create_site_returns_error_for_empty_subdomain_slug(): void {
306+
307+
// Force subdomain install mode via the wu_is_subdomain_install filter.
308+
add_filter('wu_is_subdomain_install', '__return_true');
309+
310+
$result = wu_create_site(
311+
[
312+
'title' => 'Empty Slug Test',
313+
'path' => '/!!!/',
314+
]
315+
);
316+
317+
remove_all_filters('wu_is_subdomain_install');
318+
319+
$this->assertWPError($result);
320+
$this->assertEquals('invalid_site_path', $result->get_error_code());
321+
}
262322
}

0 commit comments

Comments
 (0)