Skip to content

Commit 9dfdf85

Browse files
committed
Tests: Clean test uploads before loading WordPress.
Delete the test uploads directory before loading plugins and themes. Calling `wp_upload_dir()` during loading can create and cache the directory; deleting it afterward leaves that cached path missing. Use PHP filesystem operations before WordPress functions are available, handle Windows directory symlinks, and report directory traversal exceptions. Follow-up to [65177]. Developed in: #15368 Props oaratovskyi, adrianmoldovanwp. See #63532. git-svn-id: https://develop.svn.wordpress.org/trunk@65190 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 24f9a3e commit 9dfdf85

1 file changed

Lines changed: 29 additions & 10 deletions

File tree

‎tests/phpunit/includes/bootstrap.php‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,26 +330,45 @@ function wp_tests_options( $value ) {
330330
}
331331
}
332332

333-
// Load WordPress.
334-
require_once ABSPATH . 'wp-settings.php';
335-
336-
// Process delete wp-content/test-suite-uploads folder.
333+
/*
334+
* Delete the wp-content/test-suite-uploads folder before WordPress loads.
335+
*
336+
* Plugins and themes may call wp_upload_dir() while loading, which creates the folder and
337+
* caches it as created. Deleting it afterwards would leave a cached path that no longer exists.
338+
* WordPress functions are not available yet, so this uses plain PHP.
339+
*/
337340
if ( ! empty( $delete_tests_uploads_dir ) ) {
338341
$uploads_test_run_dir = ABSPATH . UPLOADS;
339342

340343
if ( is_dir( $uploads_test_run_dir ) ) {
341-
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
342-
require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php';
344+
try {
345+
$uploads_test_run_files = new RecursiveIteratorIterator(
346+
new RecursiveDirectoryIterator( $uploads_test_run_dir, FilesystemIterator::SKIP_DOTS ),
347+
RecursiveIteratorIterator::CHILD_FIRST
348+
);
343349

344-
// Use the direct transport, as WP_Filesystem() may pick FTP and fail without credentials.
345-
$uploads_test_run_fs = new WP_Filesystem_Direct( null );
346-
$uploads_test_run_fs->delete( $uploads_test_run_dir, true );
350+
foreach ( $uploads_test_run_files as $uploads_test_run_file ) {
351+
// Windows removes symlinks to directories with rmdir(), not unlink().
352+
if ( $uploads_test_run_file->isDir() && ( ! $uploads_test_run_file->isLink() || 'Windows' === PHP_OS_FAMILY ) ) {
353+
rmdir( $uploads_test_run_file->getPathname() );
354+
} else {
355+
unlink( $uploads_test_run_file->getPathname() );
356+
}
357+
}
358+
359+
rmdir( $uploads_test_run_dir );
360+
} catch ( UnexpectedValueException $uploads_test_run_exception ) {
361+
echo 'Could not delete the test uploads directory: ' . $uploads_test_run_exception->getMessage() . PHP_EOL;
362+
}
347363
}
348364

349-
unset( $uploads_test_run_dir, $uploads_test_run_fs );
365+
unset( $uploads_test_run_dir, $uploads_test_run_files, $uploads_test_run_file, $uploads_test_run_exception );
350366
}
351367
unset( $delete_tests_uploads_dir );
352368

369+
// Load WordPress.
370+
require_once ABSPATH . 'wp-settings.php';
371+
353372
// Override the PHPMailer.
354373
require_once __DIR__ . '/mock-mailer.php';
355374

0 commit comments

Comments
 (0)