Skip to content

Commit 9b388df

Browse files
CopilotJJJ
andcommitted
Refactor tests: add helper method to eliminate code duplication
Add assertUploadPathNoDuplicates() helper method to reduce repeated code patterns across all test methods, improving maintainability Co-authored-by: JJJ <88951+JJJ@users.noreply.github.com>
1 parent 868e80e commit 9b388df

File tree

1 file changed

+43
-55
lines changed

1 file changed

+43
-55
lines changed

tests/integration/tests/test-upload-paths.php

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ public static function wpTearDownAfterClass() {
3939
}
4040
}
4141

42+
/**
43+
* Helper method to assert upload path has no duplicate site-specific directories.
44+
*
45+
* @param int $site_id The site ID to check.
46+
* @param string $upload_path The upload path to verify.
47+
* @param string $message Custom assertion message.
48+
*/
49+
protected function assertUploadPathNoDuplicates( $site_id, $upload_path, $message = '' ) {
50+
if ( empty( $upload_path ) ) {
51+
return;
52+
}
53+
54+
$site_suffix = '/sites/' . $site_id;
55+
$count = substr_count( $upload_path, $site_suffix );
56+
57+
if ( empty( $message ) ) {
58+
$message = "Upload path should not contain duplicate site-specific directories for site {$site_id}";
59+
}
60+
61+
$this->assertLessThanOrEqual( 1, $count, $message );
62+
63+
// If the path contains the suffix, it should appear exactly once.
64+
if ( false !== strpos( $upload_path, $site_suffix ) ) {
65+
$this->assertEquals( 1, $count, "Site-specific directory should appear exactly once in upload path for site {$site_id}" );
66+
}
67+
}
68+
4269
/**
4370
* Test that upload paths are correctly set when creating a new network.
4471
*
@@ -66,15 +93,7 @@ public function test_upload_path_without_duplication() {
6693
$upload_path = get_blog_option( $main_site_id, 'upload_path' );
6794

6895
// Check that the path doesn't contain duplicate site-specific directories.
69-
$site_suffix = '/sites/' . $main_site_id;
70-
$count = substr_count( $upload_path, $site_suffix );
71-
72-
$this->assertLessThanOrEqual( 1, $count, 'Upload path should not contain duplicate site-specific directories' );
73-
74-
// If the path contains the suffix, it should only appear once.
75-
if ( false !== strpos( $upload_path, $site_suffix ) ) {
76-
$this->assertEquals( 1, $count, 'Site-specific directory should appear exactly once in upload path' );
77-
}
96+
$this->assertUploadPathNoDuplicates( $main_site_id, $upload_path, 'Upload path should not contain duplicate site-specific directories' );
7897
}
7998

8099
/**
@@ -108,11 +127,7 @@ public function test_upload_path_with_files_rewriting() {
108127
$upload_path = get_blog_option( $main_site_id, 'upload_path' );
109128

110129
// The behavior with files rewriting may vary, but it should not contain duplicates.
111-
if ( ! empty( $upload_path ) ) {
112-
$site_suffix = '/sites/' . $main_site_id;
113-
$count = substr_count( $upload_path, $site_suffix );
114-
$this->assertLessThanOrEqual( 1, $count, 'Upload path should not contain duplicate site-specific directories with files rewriting' );
115-
}
130+
$this->assertUploadPathNoDuplicates( $main_site_id, $upload_path, 'Upload path should not contain duplicate site-specific directories with files rewriting' );
116131

117132
// Clean up.
118133
update_site_option( 'ms_files_rewriting', 0 );
@@ -151,11 +166,10 @@ public function test_upload_path_multisite() {
151166
// In multisite, the path should use /sites/ directory if present.
152167
if ( ! empty( $upload_path ) && false !== strpos( $upload_path, '/sites/' ) ) {
153168
$this->assertStringContainsString( '/sites/' . $main_site_id, $upload_path, 'Upload path should contain /sites/{blog_id} format in multisite' );
154-
155-
// Ensure no duplication.
156-
$count = substr_count( $upload_path, '/sites/' . $main_site_id );
157-
$this->assertEquals( 1, $count, 'Site-specific path should appear exactly once' );
158169
}
170+
171+
// Ensure no duplication.
172+
$this->assertUploadPathNoDuplicates( $main_site_id, $upload_path, 'Site-specific path should appear exactly once' );
159173
}
160174

161175
/**
@@ -199,11 +213,7 @@ public function test_upload_path_without_files_rewriting() {
199213

200214
// Verify no duplication of site-specific path.
201215
if ( defined( 'MULTISITE' ) && MULTISITE ) {
202-
$site_suffix = '/sites/' . $main_site_id;
203-
if ( false !== strpos( $upload_path, $site_suffix ) ) {
204-
$count = substr_count( $upload_path, $site_suffix );
205-
$this->assertEquals( 1, $count, 'Site-specific directory should not be duplicated' );
206-
}
216+
$this->assertUploadPathNoDuplicates( $main_site_id, $upload_path, 'Site-specific directory should not be duplicated' );
207217
}
208218
}
209219
}
@@ -276,13 +286,11 @@ public function test_upload_path_preservation() {
276286
// Get the initial upload path.
277287
$initial_upload_path = get_blog_option( $main_site_id, 'upload_path' );
278288

279-
// If a path was set and contains the site-specific directory, verify it's correct.
280-
if ( ! empty( $initial_upload_path ) && false !== strpos( $initial_upload_path, '/sites/' . $main_site_id ) ) {
281-
// Verify no duplication.
282-
$count = substr_count( $initial_upload_path, '/sites/' . $main_site_id );
283-
$this->assertEquals( 1, $count, 'Initial upload path should not have duplicated site-specific directory' );
289+
// Verify no duplication.
290+
$this->assertUploadPathNoDuplicates( $main_site_id, $initial_upload_path, 'Initial upload path should not have duplicated site-specific directory' );
284291

285-
// The path should be preserved and not contain any doubled segments.
292+
// The path should be preserved and not contain any doubled segments.
293+
if ( ! empty( $initial_upload_path ) ) {
286294
$this->assertStringNotContainsString( '/sites/' . $main_site_id . '/sites/' . $main_site_id, $initial_upload_path, 'Upload path should never contain doubled site-specific directories' );
287295
}
288296
}
@@ -311,11 +319,7 @@ public function test_upload_path_subdirectory_install() {
311319
$upload_path = get_blog_option( $main_site_id, 'upload_path' );
312320

313321
// Verify path doesn't have duplicates.
314-
if ( ! empty( $upload_path ) ) {
315-
$site_suffix = '/sites/' . $main_site_id;
316-
$count = substr_count( $upload_path, $site_suffix );
317-
$this->assertLessThanOrEqual( 1, $count, 'Subdirectory network upload path should not duplicate site-specific directories' );
318-
}
322+
$this->assertUploadPathNoDuplicates( $main_site_id, $upload_path, 'Subdirectory network upload path should not duplicate site-specific directories' );
319323
}
320324

321325
/**
@@ -352,12 +356,7 @@ public function test_upload_path_multiple_sites() {
352356
// Verify each site has proper upload path without duplication.
353357
foreach ( $site_ids as $site_id ) {
354358
$upload_path = get_blog_option( $site_id, 'upload_path' );
355-
356-
if ( ! empty( $upload_path ) ) {
357-
$site_suffix = '/sites/' . $site_id;
358-
$count = substr_count( $upload_path, $site_suffix );
359-
$this->assertLessThanOrEqual( 1, $count, "Site {$site_id} upload path should not have duplicate site-specific directories" );
360-
}
359+
$this->assertUploadPathNoDuplicates( $site_id, $upload_path, "Site {$site_id} upload path should not have duplicate site-specific directories" );
361360
}
362361
}
363362

@@ -395,10 +394,8 @@ public function test_upload_url_path_consistency() {
395394

396395
// Neither should have duplicates.
397396
if ( $path_has_suffix ) {
398-
$path_count = substr_count( $upload_path, $path_suffix );
399-
$url_count = substr_count( $upload_url_path, $path_suffix );
400-
$this->assertEquals( 1, $path_count, 'Upload path should not have duplicate site-specific directories' );
401-
$this->assertEquals( 1, $url_count, 'Upload URL path should not have duplicate site-specific directories' );
397+
$this->assertUploadPathNoDuplicates( $main_site_id, $upload_path, 'Upload path should not have duplicate site-specific directories' );
398+
$this->assertUploadPathNoDuplicates( $main_site_id, $upload_url_path, 'Upload URL path should not have duplicate site-specific directories' );
402399
}
403400
}
404401
}
@@ -444,16 +441,7 @@ public function test_upload_path_with_network_cloning() {
444441
$cloned_upload_path = get_blog_option( $cloned_site_id, 'upload_path' );
445442

446443
// Both should have proper paths without duplication.
447-
if ( ! empty( $source_upload_path ) ) {
448-
$source_suffix = '/sites/' . $source_site_id;
449-
$source_count = substr_count( $source_upload_path, $source_suffix );
450-
$this->assertLessThanOrEqual( 1, $source_count, 'Source network upload path should not have duplicates' );
451-
}
452-
453-
if ( ! empty( $cloned_upload_path ) ) {
454-
$cloned_suffix = '/sites/' . $cloned_site_id;
455-
$cloned_count = substr_count( $cloned_upload_path, $cloned_suffix );
456-
$this->assertLessThanOrEqual( 1, $cloned_count, 'Cloned network upload path should not have duplicates' );
457-
}
444+
$this->assertUploadPathNoDuplicates( $source_site_id, $source_upload_path, 'Source network upload path should not have duplicates' );
445+
$this->assertUploadPathNoDuplicates( $cloned_site_id, $cloned_upload_path, 'Cloned network upload path should not have duplicates' );
458446
}
459447
}

0 commit comments

Comments
 (0)