diff --git a/.gitignore b/.gitignore index 15876fa47fee8..85b2aa7ff4f49 100644 --- a/.gitignore +++ b/.gitignore @@ -87,6 +87,7 @@ wp-tests-config.php /src/wp-content/object-cache.php /src/wp-content/php-error.php /src/wp-content/sunrise.php +/src/wp-content/test-suite-uploads # Files and folders relating to wp-content build tools. /src/wp-content/themes/twentynineteen/node_modules diff --git a/tests/phpunit/includes/bootstrap.php b/tests/phpunit/includes/bootstrap.php index b3835a1ce415b..62e5761719526 100644 --- a/tests/phpunit/includes/bootstrap.php +++ b/tests/phpunit/includes/bootstrap.php @@ -236,6 +236,34 @@ define( 'REST_TESTS_IMPOSSIBLY_HIGH_NUMBER', 99999999 ); +if ( ! defined( 'UPLOADS' ) ) { + define( 'UPLOADS', 'wp-content/test-suite-uploads' ); + + /* + * Delete the test uploads dir exactly once per run, via a lock file ( we need it cos each + * @runInSeparateProcess test re-executes this in its own process). The filename is hidden + * ( dot-prefixed ) and suffixed with a hash of the current date so the lock is scoped to + * a single day's run rather than using a fixed, guessable filename. + */ + $uploads_hash = substr( md5( gmdate( 'Y-m-d' ) ), 0, 12 ); + $uploads_test_run_lock = ABSPATH . 'wp-content/uploads/.test-suite-uploads-' . $uploads_hash . '.lock'; + $delete_tests_uploads_dir = ! file_exists( $uploads_test_run_lock ); + + if ( $delete_tests_uploads_dir ) { + touch( $uploads_test_run_lock ); + + register_shutdown_function( + function () use ( $uploads_test_run_lock ) { + if ( file_exists( $uploads_test_run_lock ) ) { + unlink( $uploads_test_run_lock ); + } + } + ); + } + + unset( $uploads_test_run_lock, $uploads_hash ); +} + $PHP_SELF = '/index.php'; $GLOBALS['PHP_SELF'] = '/index.php'; $_SERVER['PHP_SELF'] = '/index.php'; @@ -300,6 +328,22 @@ function wp_tests_options( $value ) { // Load WordPress. require_once ABSPATH . 'wp-settings.php'; +// Process delete wp-content/test-suite-uploads folder. +if ( ! empty( $delete_tests_uploads_dir ) ) { + $uploads_test_run_dir = ABSPATH . UPLOADS; + + if ( is_dir( $uploads_test_run_dir ) ) { + require_once ABSPATH . 'wp-admin/includes/file.php'; + WP_Filesystem(); + + global $wp_filesystem; + $wp_filesystem->delete( $uploads_test_run_dir, true ); + } + + unset( $uploads_test_run_dir ); +} +unset( $delete_tests_uploads_dir ); + // Override the PHPMailer. require_once __DIR__ . '/mock-mailer.php'; diff --git a/tests/phpunit/includes/functions.php b/tests/phpunit/includes/functions.php index d27af5c172a7a..b10aa65ba5f6e 100644 --- a/tests/phpunit/includes/functions.php +++ b/tests/phpunit/includes/functions.php @@ -329,6 +329,17 @@ function _upload_dir_https( $uploads ) { return $uploads; } +/** + * Builds the upload base URL used throughout the test suite. + * + * @param string $path Path/filename to append after the uploads URL. No leading slash needed. + * @param string $scheme URL scheme, 'http' or 'https'. + * @return string The upload URL. + */ +function _upload_get_url( $path = '', $scheme = 'http' ) { + return $scheme . '://' . WP_TESTS_DOMAIN . '/' . trailingslashit( UPLOADS ) . ltrim( $path, '/' ); +} + /** * Use the Spy_REST_Server class for the REST server. * diff --git a/tests/phpunit/tests/fonts/font-library/wpFontsDir.php b/tests/phpunit/tests/fonts/font-library/wpFontsDir.php index 22208ca7b2e26..806b52dd4f9bb 100644 --- a/tests/phpunit/tests/fonts/font-library/wpFontsDir.php +++ b/tests/phpunit/tests/fonts/font-library/wpFontsDir.php @@ -45,17 +45,18 @@ public function test_fonts_dir() { * @group ms-required */ public function test_fonts_dir_for_multisite() { - $blog_id = self::factory()->blog->create(); - $main_site_upload_dir = wp_get_upload_dir(); + $blog_id = self::factory()->blog->create(); switch_to_blog( $blog_id ); - $actual = wp_get_font_dir(); - $expected = array( - 'path' => untrailingslashit( $main_site_upload_dir['basedir'] ) . "/sites/{$blog_id}/fonts", - 'url' => untrailingslashit( $main_site_upload_dir['baseurl'] ) . "/sites/{$blog_id}/fonts", + // Fetch the switched-to site's own upload dir, since its URL depends on its own site path. + $upload_dir = wp_get_upload_dir(); + $actual = wp_get_font_dir(); + $expected = array( + 'path' => untrailingslashit( $upload_dir['basedir'] ) . '/fonts', + 'url' => untrailingslashit( $upload_dir['baseurl'] ) . '/fonts', 'subdir' => '', - 'basedir' => untrailingslashit( $main_site_upload_dir['basedir'] ) . "/sites/{$blog_id}/fonts", - 'baseurl' => untrailingslashit( $main_site_upload_dir['baseurl'] ) . "/sites/{$blog_id}/fonts", + 'basedir' => untrailingslashit( $upload_dir['basedir'] ) . '/fonts', + 'baseurl' => untrailingslashit( $upload_dir['baseurl'] ) . '/fonts', 'error' => false, ); diff --git a/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index 8d66243668c46..0c632bf80d447 100644 --- a/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -452,7 +452,7 @@ function ( $font_dir ) { 'The font_face_settings data should match the expected data.' ); - $expected_file_path = WP_CONTENT_DIR . '/uploads/fonts/subdir/' . reset( $files )['name']; + $expected_file_path = ABSPATH . '/' . UPLOADS . '/fonts/subdir/' . reset( $files )['name']; $expected_post_meta = 'subdir/' . reset( $files )['name']; $this->assertFileExists( $expected_file_path, 'The font file should exist in the expected subdirectory.' ); $this->assertSame( $expected_post_meta, get_post_meta( $data['id'], '_wp_font_face_file', true ), 'The post meta should match the expected subdirectory.' ); diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 5aee3f8b6955f..d6953e24d51e2 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -648,7 +648,7 @@ public function test_wp_prepare_attachment_for_js_without_image_sizes() { 'post_type' => 'attachment', 'post_parent' => 0, 'post_mime_type' => 'image/jpeg', - 'guid' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-image.jpg', + 'guid' => _upload_get_url( 'test-image.jpg' ), ) ); @@ -1051,7 +1051,7 @@ public function test_post_galleries_images() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids1[] = $attachment_id; - $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $ids1_srcs[] = _upload_get_url( "image$i.jpg" ); } $ids2 = array(); @@ -1068,7 +1068,7 @@ public function test_post_galleries_images() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids2[] = $attachment_id; - $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $ids2_srcs[] = _upload_get_url( "image$i.jpg" ); } $ids1_joined = implode( ',', array_slice( $ids1, 0, 3 ) ); @@ -1102,7 +1102,7 @@ public function test_post_gallery_images() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids1[] = $attachment_id; - $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $ids1_srcs[] = _upload_get_url( "image$i.jpg" ); } $ids2 = array(); @@ -1119,7 +1119,7 @@ public function test_post_gallery_images() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids2[] = $attachment_id; - $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $ids2_srcs[] = _upload_get_url( "image$i.jpg" ); } $ids1_joined = implode( ',', $ids1 ); @@ -1152,7 +1152,7 @@ public function test_block_post_gallery_images() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids[] = $attachment_id; - $url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $url = _upload_get_url( "image$i.jpg" ); $ids_srcs[] = $url; $imgs[] = '
'; } @@ -1190,7 +1190,7 @@ public function test_block_post_gallery_images_json() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids[] = $attachment_id; - $url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $url = _upload_get_url( "image$i.jpg" ); $ids_srcs[] = $url; $imgs[] = '
'; @@ -1232,7 +1232,7 @@ public function test_mixed_post_gallery_images() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids[] = $attachment_id; - $url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $url = _upload_get_url( "image$i.jpg" ); $ids_srcs[] = $url; $imgs[] = '
'; } @@ -1275,7 +1275,7 @@ public function test_block_inner_post_gallery_images() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids[] = $attachment_id; - $url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $url = _upload_get_url( "image$i.jpg" ); $ids_srcs[] = $url; $imgs[] = '
'; @@ -1318,7 +1318,7 @@ public function test_block_post_gallery_innerblock_images() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids[] = $attachment_id; - $url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $url = _upload_get_url( "image$i.jpg" ); $ids_srcs[] = $url; $imgs[] = '
'; @@ -1724,7 +1724,7 @@ public function test_attachment_url_to_postid() { ) ); - $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path; + $image_url = _upload_get_url( $image_path ); $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) ); } @@ -1742,7 +1742,7 @@ public function test_attachment_url_to_postid_with_different_scheme() { ) ); - $image_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path; + $image_url = _upload_get_url( $image_path, 'https' ); $this->assertSame( $attachment_id, attachment_url_to_postid( $image_url ) ); } @@ -1770,7 +1770,7 @@ public function test_attachment_url_to_postid_should_be_case_sensitive() { ) ); - $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path_upper_case; + $image_url = _upload_get_url( $image_path_upper_case ); $this->assertSame( $attachment_id_upper_case, attachment_url_to_postid( $image_url ) ); } @@ -2301,7 +2301,7 @@ public function test_wp_calculate_image_srcset() { $year_month = gmdate( 'Y/m' ); $image_meta = wp_get_attachment_metadata( self::$large_id ); - $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; + $uploads_dir_url = _upload_get_url(); // Set up test cases for all expected size names. $intermediates = array( 'medium', 'medium_large', 'large', 'full' ); @@ -2355,7 +2355,7 @@ public function test_wp_calculate_image_srcset_no_date_uploads() { $id = self::factory()->attachment->create_upload_object( $filename ); $image_meta = wp_get_attachment_metadata( $id ); - $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; + $uploads_dir_url = _upload_get_url(); // Set up test cases for all expected size names. $intermediates = array( 'medium', 'medium_large', 'large', 'full' ); @@ -2446,7 +2446,7 @@ public function test_wp_calculate_image_srcset_with_absolute_path_in_meta() { $year_month = gmdate( 'Y/m' ); $image_meta = wp_get_attachment_metadata( self::$large_id ); - $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; + $uploads_dir_url = _upload_get_url(); // Set up test cases for all expected size names. $intermediates = array( 'medium', 'medium_large', 'large', 'full' ); @@ -2547,7 +2547,7 @@ public function test_wp_calculate_image_srcset_no_width() { public function test_wp_calculate_image_srcset_ratio_variance() { // Mock data for this test. $size_array = array( 218, 300 ); - $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png'; + $image_src = _upload_get_url( '2015/12/test-768x1055-218x300.png' ); $image_meta = array( 'width' => 768, 'height' => 1055, @@ -2580,7 +2580,7 @@ public function test_wp_calculate_image_srcset_ratio_variance() { ), ); - $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/'; + $uploads_url = _upload_get_url( '2015/12/' ); $expected_srcset = $uploads_url . 'test-768x1055-218x300.png 218w, ' . $uploads_url . 'test-768x1055-600x824.png 600w, ' . @@ -2596,7 +2596,7 @@ public function test_wp_calculate_image_srcset_ratio_variance() { public function test_wp_calculate_image_srcset_include_src() { // Mock data for this test. $size_array = array( 2000, 1000 ); - $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png'; + $image_src = _upload_get_url( '2015/12/test.png' ); $image_meta = array( 'width' => 2000, 'height' => 1000, @@ -2629,7 +2629,7 @@ public function test_wp_calculate_image_srcset_include_src() { ), ); - $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/'; + $uploads_url = _upload_get_url( '2015/12/' ); $expected_srcset = $uploads_url . 'test.png 2000w, ' . $uploads_url . 'test-300x150.png 300w, ' . @@ -2644,7 +2644,7 @@ public function test_wp_calculate_image_srcset_include_src() { */ public function test_wp_calculate_image_srcset_corrupted_image_meta() { $size_array = array( 300, 150 ); - $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png'; + $image_src = _upload_get_url( '2015/12/test-300x150.png' ); $image_meta = array( 'width' => 1600, 'height' => 800, @@ -2678,10 +2678,10 @@ public function test_wp_calculate_image_srcset_corrupted_image_meta() { ); $srcset = array( - 300 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w', - 768 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w', - 1024 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w', - 1600 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 1600w', + 300 => _upload_get_url( '2015/12/test-300x150.png' ) . ' 300w', + 768 => _upload_get_url( '2015/12/test-768x384.png' ) . ' 768w', + 1024 => _upload_get_url( '2015/12/test-1024x512.png' ) . ' 1024w', + 1600 => _upload_get_url( '2015/12/test.png' ) . ' 1600w', ); // No sizes array. @@ -2718,7 +2718,7 @@ public function test_wp_calculate_image_srcset_corrupted_image_meta() { */ public function test_wp_calculate_image_srcset_with_spaces_in_filenames() { // Mock data for this test. - $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test image-300x150.png'; + $image_src = _upload_get_url( '2015/12/test image-300x150.png' ); $image_meta = array( 'width' => 3000, 'height' => 1500, @@ -2751,7 +2751,7 @@ public function test_wp_calculate_image_srcset_with_spaces_in_filenames() { ), ); - $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/'; + $uploads_url = _upload_get_url( '2015/12/' ); $expected_srcset = $uploads_url . 'test%20image-300x150.png 300w, ' . $uploads_url . 'test%20image-768x384.png 768w, ' . @@ -2773,7 +2773,7 @@ public function test_wp_get_attachment_image_srcset() { $srcset = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ); $year_month = gmdate( 'Y/m' ); - $uploads_dir = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; + $uploads_dir = _upload_get_url(); // Set up test cases for all expected size names. $intermediates = array( 'medium', 'medium_large', 'large', 'full' ); @@ -2976,7 +2976,7 @@ public function test_wp_filter_content_tags_srcset_sizes_wrong() { $img = wp_img_tag_add_loading_optimization_attrs( $img, 'test' ); // Replace the src URL. - $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $img ); + $image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="' . _upload_get_url( 'foo.jpg' ) . '"', $img ); $this->assertSame( $image_wrong_src, wp_filter_content_tags( $image_wrong_src ) ); } @@ -3105,8 +3105,8 @@ public function test_wp_calculate_image_srcset_animated_gifs() { ), ); - $full_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file']; - $large_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file']; + $full_src = _upload_get_url( $image_meta['file'] ); + $large_src = _upload_get_url( $image_meta['sizes']['large']['file'] ); // Test with soft resized size array. $size_array = array( 900, 450 ); @@ -3203,11 +3203,11 @@ public function test_wp_get_attachment_image_with_https_on() { // Test using the large file size. $size_array = array( 1024, 512 ); - $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file']; + $image_url = _upload_get_url( $image_meta['sizes']['large']['file'] ); $_SERVER['HTTPS'] = 'on'; - $uploads_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; + $uploads_url = _upload_get_url( '', 'https' ); $expected = $uploads_url . 'test-1024x512.jpg 1024w, ' . $uploads_url . 'test-300x150.jpg 300w, ' . @@ -3336,7 +3336,7 @@ public function test_wp_get_attachment_image_should_use_wp_get_attachment_metada $basename = wp_basename( self::$large_filename, '.jpg' ); $year_month = gmdate( 'Y/m' ); - $uploads_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/'; + $uploads_url = _upload_get_url( $year_month . '/' ); $expected = ' array( 'current_attachment_index' => 3, 'expected_attachment_index' => 2, - 'expected' => '', + 'expected' => '', ), 'with text when has previous link' => array( 'current_attachment_index' => 3, @@ -43,7 +43,7 @@ public function data_get_adjacent_image_link() { 'when has next link' => array( 'current_attachment_index' => 4, 'expected_attachment_index' => 5, - 'expected' => '', + 'expected' => '', 'args' => array( 'prev' => false ), ), 'with text when has next link' => array( diff --git a/tests/phpunit/tests/media/getNextImageLink.php b/tests/phpunit/tests/media/getNextImageLink.php index 3d4ccacc1b307..48806043d9bd6 100644 --- a/tests/phpunit/tests/media/getNextImageLink.php +++ b/tests/phpunit/tests/media/getNextImageLink.php @@ -31,7 +31,7 @@ public function data_get_next_image_link() { 'when has next link' => array( 'current_attachment_index' => 4, 'expected_attachment_index' => 5, - 'expected' => '', + 'expected' => '', ), 'with text when has next link' => array( 'current_attachment_index' => 4, diff --git a/tests/phpunit/tests/media/getPostGalleries.php b/tests/phpunit/tests/media/getPostGalleries.php index 1ba21013e19a9..83570ea9141da 100644 --- a/tests/phpunit/tests/media/getPostGalleries.php +++ b/tests/phpunit/tests/media/getPostGalleries.php @@ -58,7 +58,7 @@ public function test_returns_only_galleries( $content, $needle ) { ) ); - $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg'; + $image_url = _upload_get_url( 'test.jpg' ); $content = str_replace( array( 'IMAGE_ID', 'IMAGE_URL' ), @@ -240,7 +240,7 @@ public function test_returns_no_srcs_with_block_v2_in_post_with_no_attached_imag ) ); - $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg'; + $image_url = _upload_get_url( 'test.jpg' ); $blob = <<< BLOB @@ -330,7 +330,7 @@ public function test_returns_html_with_shortcode_gallery() { ) ); - $expected = 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg"'; + $expected = 'src="' . _upload_get_url( 'test.jpg' ) . '"'; $galleries = get_post_galleries( $post_id_two ); // The method can return an empty array. @@ -379,7 +379,7 @@ public function test_returns_html_with_block_gallery() { ) ); - $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg'; + $image_url = _upload_get_url( 'test.jpg' ); $blob = <<< BLOB @@ -393,7 +393,7 @@ public function test_returns_html_with_block_gallery() { ) ); - $expected = 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg"'; + $expected = 'src="' . _upload_get_url( 'test.jpg' ) . '"'; $galleries = get_post_galleries( $post_id_two ); // The method can return an empty array. @@ -435,7 +435,7 @@ public function test_returns_html_with_block_gallery_v2() { ) ); - $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg'; + $image_url = _upload_get_url( 'test.jpg' ); $blob = <<< BLOB @@ -461,7 +461,7 @@ class="wp-image-$image_id" ) ); - $expected = 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg"'; + $expected = 'src="' . _upload_get_url( 'test.jpg' ) . '"'; $galleries = get_post_galleries( $post_id ); // The method can return an empty array. @@ -514,7 +514,7 @@ public function test_respects_post_id_with_shortcode_gallery() { ) ); $expected_srcs = array( - 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg', + _upload_get_url( 'test.jpg' ), ); // Set the global $post context to the other post. @@ -569,7 +569,7 @@ public function test_respects_post_id_with_block_gallery() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids[] = $attachment_id; - $url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $url = _upload_get_url( "image$i.jpg" ); $ids_srcs[] = $url; $imgs[] = '
'; @@ -602,7 +602,7 @@ public function test_respects_post_id_with_block_gallery() { ) ); $expected_srcs = array( - 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg', + _upload_get_url( 'test.jpg' ), ); // Set the global $post context to the other post. @@ -656,7 +656,7 @@ public function test_respects_post_id_with_block_gallery_v2() { ) ); $metadata = array_merge( array( 'file' => 'image1.jpg' ), self::IMG_META ); - $url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . 'image1.jpg'; + $url = _upload_get_url( 'image1.jpg' ); $global_post_id = self::factory()->post->create( array( 'post_content' => 'Global Post', @@ -697,7 +697,7 @@ class="wp-image-$attachment_id" ) ); $expected_srcs = array( - 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg', + _upload_get_url( 'test.jpg' ), ); // Set the global $post context to the other post. @@ -761,7 +761,7 @@ public function test_respects_shortcode_id_attribute() { ) ); $expected_srcs = array( - 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg', + _upload_get_url( 'test.jpg' ), ); $galleries = get_post_galleries( $post_id_two, false ); @@ -838,7 +838,7 @@ public function test_respects_shortcode_and_block_id_attributes() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids[] = $attachment_id; - $url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $url = _upload_get_url( "image$i.jpg" ); $ids_srcs[] = $url; $imgs[] = '
'; @@ -901,7 +901,7 @@ public function test_respects_additional_shortcode_and_block_attributes() { $metadata = array_merge( array( 'file' => "image$i.jpg" ), self::IMG_META ); wp_update_attachment_metadata( $attachment_id, $metadata ); $ids[] = $attachment_id; - $url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg"; + $url = _upload_get_url( "image$i.jpg" ); $ids_srcs[] = $url; $imgs[] = '
'; @@ -957,7 +957,7 @@ public function test_returns_srcs_from_html_with_block_with_no_json_blob() { ) ); - $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg'; + $image_url = _upload_get_url( 'test.jpg' ); $blob = <<< BLOB diff --git a/tests/phpunit/tests/media/getPreviousImageLink.php b/tests/phpunit/tests/media/getPreviousImageLink.php index b2b49d28bca41..6192bbb440627 100644 --- a/tests/phpunit/tests/media/getPreviousImageLink.php +++ b/tests/phpunit/tests/media/getPreviousImageLink.php @@ -31,7 +31,7 @@ public function data_get_previous_image_link() { 'when has previous link' => array( 'current_attachment_index' => 3, 'expected_attachment_index' => 2, - 'expected' => '', + 'expected' => '', ), 'with text when has previous link' => array( 'current_attachment_index' => 3, diff --git a/tests/phpunit/tests/media/nextImageLink.php b/tests/phpunit/tests/media/nextImageLink.php index 5e2282e43c485..f7ca1a9e88eb4 100644 --- a/tests/phpunit/tests/media/nextImageLink.php +++ b/tests/phpunit/tests/media/nextImageLink.php @@ -30,7 +30,7 @@ public function data_next_image_link() { 'when has next link' => array( 'current_attachment_index' => 4, 'expected_attachment_index' => 5, - 'expected' => '', + 'expected' => '', ), 'with text when has next link' => array( 'current_attachment_index' => 4, diff --git a/tests/phpunit/tests/media/previousImageLink.php b/tests/phpunit/tests/media/previousImageLink.php index ae1b18fe3c708..6ba1560cae656 100644 --- a/tests/phpunit/tests/media/previousImageLink.php +++ b/tests/phpunit/tests/media/previousImageLink.php @@ -30,7 +30,7 @@ public function data_previous_image_link() { 'when has previous link' => array( 'current_attachment_index' => 3, 'expected_attachment_index' => 2, - 'expected' => '', + 'expected' => '', ), 'with text when has previous link' => array( 'current_attachment_index' => 3, diff --git a/tests/phpunit/tests/multisite/msFilesRewriting.php b/tests/phpunit/tests/multisite/msFilesRewriting.php index 92b8e60118416..3119b0c206e41 100644 --- a/tests/phpunit/tests/multisite/msFilesRewriting.php +++ b/tests/phpunit/tests/multisite/msFilesRewriting.php @@ -21,14 +21,13 @@ public function set_up() { public function test_switch_upload_dir() { $this->assertTrue( is_main_site() ); - $site = get_current_site(); $date = date_format( date_create( 'now' ), 'Y/m' ); $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) ); $blog_id2 = self::factory()->blog->create( array( 'user_id' => $user_id ) ); $info = wp_upload_dir(); - $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] ); - $this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] ); + $this->assertSame( _upload_get_url( $date ), $info['url'] ); + $this->assertSame( ABSPATH . UPLOADS . '/' . $date, $info['path'] ); $this->assertSame( '/' . $date, $info['subdir'] ); $this->assertFalse( $info['error'] ); diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index 920a76f6a7e30..9b4e278455395 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -562,12 +562,11 @@ public function test_is_main_site_is_false_after_switch_to_blog() { public function test_switch_upload_dir() { $this->assertTrue( is_main_site() ); - $site = get_current_site(); $date = date_format( date_create( 'now' ), 'Y/m' ); $info = wp_upload_dir(); - $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] ); - $this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] ); + $this->assertSame( _upload_get_url( $date ), $info['url'] ); + $this->assertSame( ABSPATH . UPLOADS . '/' . $date, $info['path'] ); $this->assertSame( '/' . $date, $info['subdir'] ); $this->assertFalse( $info['error'] ); @@ -575,15 +574,15 @@ public function test_switch_upload_dir() { switch_to_blog( $blog_id ); $info = wp_upload_dir(); - $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/sites/' . get_current_blog_id() . '/' . $date, $info['url'] ); - $this->assertSame( ABSPATH . 'wp-content/uploads/sites/' . get_current_blog_id() . '/' . $date, $info['path'] ); + $this->assertSame( trailingslashit( get_option( 'siteurl' ) ) . UPLOADS . '/sites/' . get_current_blog_id() . '/' . $date, $info['url'] ); + $this->assertSame( ABSPATH . UPLOADS . '/sites/' . get_current_blog_id() . '/' . $date, $info['path'] ); $this->assertSame( '/' . $date, $info['subdir'] ); $this->assertFalse( $info['error'] ); restore_current_blog(); $info = wp_upload_dir(); - $this->assertSame( 'http://' . $site->domain . '/wp-content/uploads/' . $date, $info['url'] ); - $this->assertSame( ABSPATH . 'wp-content/uploads/' . $date, $info['path'] ); + $this->assertSame( _upload_get_url( $date ), $info['url'] ); + $this->assertSame( ABSPATH . UPLOADS . '/' . $date, $info['path'] ); $this->assertSame( '/' . $date, $info['subdir'] ); $this->assertFalse( $info['error'] ); } diff --git a/tests/phpunit/tests/speculative-loading/wpGetSpeculationRules.php b/tests/phpunit/tests/speculative-loading/wpGetSpeculationRules.php index a4854db0e41d6..fd6a89c3fa8a8 100644 --- a/tests/phpunit/tests/speculative-loading/wpGetSpeculationRules.php +++ b/tests/phpunit/tests/speculative-loading/wpGetSpeculationRules.php @@ -216,7 +216,7 @@ function () { array( '/wp-*.php', '/wp-admin/*', - '/wp-content/uploads/*', + '/' . UPLOADS . '/*', '/wp-content/*', '/wp-content/plugins/*', '/wp-content/themes/stylesheet/*', @@ -246,7 +246,7 @@ static function () { array( '/wp-*.php', '/wp-admin/*', - '/wp-content/uploads/*', + '/' . UPLOADS . '/*', '/wp-content/*', '/wp-content/plugins/*', '/wp-content/themes/stylesheet/*', @@ -284,7 +284,7 @@ function () { array( '/wp-*.php', '/wp-admin/*', - '/wp-content/uploads/*', + '/' . UPLOADS . '/*', '/wp-content/*', '/wp-content/plugins/*', '/wp-content/themes/stylesheet/*', @@ -333,7 +333,7 @@ function () { array( '/wp-*.php', '/wp-admin/*', - '/wp-content/uploads/*', + '/' . UPLOADS . '/*', '/wp-content/*', '/wp-content/plugins/*', '/wp-content/themes/stylesheet/*', @@ -363,7 +363,7 @@ function () { array( '/wp-*.php', '/wp-admin/*', - '/wp-content/uploads/*', + '/' . UPLOADS . '/*', '/wp-content/*', '/wp-content/plugins/*', '/wp-content/themes/stylesheet/*', @@ -409,7 +409,7 @@ function () { array( '/wp-*.php', '/wp-admin/*', - '/wp-content/uploads/*', + '/' . UPLOADS . '/*', '/wp-content/*', '/wp-content/plugins/*', '/wp-content/themes/stylesheet/*', @@ -467,7 +467,7 @@ function () { array( '/wp/wp-*.php', '/wp/wp-admin/*', - '/wp-content/uploads/*', + '/' . UPLOADS . '/*', '/wp-content/*', '/wp-content/plugins/*', '/wp-content/themes/stylesheet/*', diff --git a/tests/phpunit/tests/upload.php b/tests/phpunit/tests/upload.php index 46fcea7099097..8607e66cbc524 100644 --- a/tests/phpunit/tests/upload.php +++ b/tests/phpunit/tests/upload.php @@ -24,20 +24,18 @@ public function test_upload_dir_default() { $info = wp_upload_dir(); $subdir = date_format( date_create( 'now' ), '/Y/m' ); - $this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] ); - $this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] ); + $this->assertSame( _upload_get_url( $subdir ), $info['url'] ); + $this->assertSame( ABSPATH . UPLOADS . $subdir, $info['path'] ); $this->assertSame( $subdir, $info['subdir'] ); $this->assertFalse( $info['error'] ); } public function test_upload_dir_relative() { - // wp_upload_dir() with a relative upload path that is not 'wp-content/uploads'. - update_option( 'upload_path', 'foo/bar' ); $info = _wp_upload_dir(); $subdir = date_format( date_create( 'now' ), '/Y/m' ); - $this->assertSame( get_option( 'siteurl' ) . '/foo/bar' . $subdir, $info['url'] ); - $this->assertSame( ABSPATH . 'foo/bar' . $subdir, $info['path'] ); + $this->assertSame( _upload_get_url( $subdir ), $info['url'] ); + $this->assertSame( ABSPATH . UPLOADS . $subdir, $info['path'] ); $this->assertSame( $subdir, $info['subdir'] ); $this->assertFalse( $info['error'] ); } @@ -59,8 +57,9 @@ public function test_upload_dir_absolute() { $info = _wp_upload_dir(); $subdir = date_format( date_create( 'now' ), '/Y/m' ); - $this->assertSame( '/baz' . $subdir, $info['url'] ); - $this->assertSame( $path . $subdir, $info['path'] ); + // UPLOADS (always defined in bootstrap.php) overrides the options set above. + $this->assertSame( _upload_get_url( $subdir ), $info['url'] ); + $this->assertSame( ABSPATH . UPLOADS . $subdir, $info['path'] ); $this->assertSame( $subdir, $info['subdir'] ); $this->assertFalse( $info['error'] ); } @@ -71,8 +70,8 @@ public function test_upload_dir_no_yearnum() { // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options. $info = _wp_upload_dir(); - $this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads', $info['url'] ); - $this->assertSame( ABSPATH . 'wp-content/uploads', $info['path'] ); + $this->assertSame( get_option( 'siteurl' ) . '/' . UPLOADS, $info['url'] ); + $this->assertSame( ABSPATH . UPLOADS, $info['path'] ); $this->assertSame( '', $info['subdir'] ); $this->assertFalse( $info['error'] ); } @@ -85,14 +84,15 @@ public function test_upload_path_absolute() { $info = _wp_upload_dir(); $subdir = date_format( date_create( 'now' ), '/Y/m' ); - $this->assertSame( 'http://' . WP_TESTS_DOMAIN . '/asdf' . $subdir, $info['url'] ); - $this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] ); + // UPLOADS (always defined in bootstrap.php) overrides the options set above. + $this->assertSame( _upload_get_url( $subdir ), $info['url'] ); + $this->assertSame( ABSPATH . UPLOADS . $subdir, $info['path'] ); $this->assertSame( $subdir, $info['subdir'] ); $this->assertFalse( $info['error'] ); } public function test_upload_dir_empty() { - // Upload path setting is empty - it should default to 'wp-content/uploads'. + // Upload path setting is empty - it should default to UPLOADS. update_option( 'upload_path', '' ); // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options. @@ -100,8 +100,8 @@ public function test_upload_dir_empty() { $info = _wp_upload_dir(); $subdir = date_format( date_create( 'now' ), '/Y/m' ); - $this->assertSame( get_option( 'siteurl' ) . '/wp-content/uploads' . $subdir, $info['url'] ); - $this->assertSame( ABSPATH . 'wp-content/uploads' . $subdir, $info['path'] ); + $this->assertSame( _upload_get_url( $subdir ), $info['url'] ); + $this->assertSame( ABSPATH . UPLOADS . $subdir, $info['path'] ); $this->assertSame( $subdir, $info['subdir'] ); $this->assertFalse( $info['error'] ); }