Make WordPress Core

Changeset 65190


Ignore:
Timestamp:
11/02/2026 03:54:19 PM (2 days ago)
Author:
lancewillett
Message:

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: ​https://github.com/WordPress/wordpress-develop/pull/15368

Props oaratovskyi, adrianmoldovanwp.
See #63532.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/bootstrap.php

    r65177 r65190  
    331331}
    332332
     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 */
     340if ( ! empty( $delete_tests_uploads_dir ) ) {
     341        $uploads_test_run_dir = ABSPATH . UPLOADS;
     342
     343        if ( is_dir( $uploads_test_run_dir ) ) {
     344                try {
     345                        $uploads_test_run_files = new RecursiveIteratorIterator(
     346                                new RecursiveDirectoryIterator( $uploads_test_run_dir, FilesystemIterator::SKIP_DOTS ),
     347                                RecursiveIteratorIterator::CHILD_FIRST
     348                        );
     349
     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                }
     363        }
     364
     365        unset( $uploads_test_run_dir, $uploads_test_run_files, $uploads_test_run_file, $uploads_test_run_exception );
     366}
     367unset( $delete_tests_uploads_dir );
     368
    333369// Load WordPress.
    334370require_once ABSPATH . 'wp-settings.php';
    335 
    336 // Process delete wp-content/test-suite-uploads folder.
    337 if ( ! empty( $delete_tests_uploads_dir ) ) {
    338         $uploads_test_run_dir = ABSPATH . UPLOADS;
    339 
    340         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';
    343 
    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 );
    347         }
    348 
    349         unset( $uploads_test_run_dir, $uploads_test_run_fs );
    350 }
    351 unset( $delete_tests_uploads_dir );
    352371
    353372// Override the PHPMailer.
Note: See TracChangeset for help on using the changeset viewer.