diff --git a/.gitignore b/.gitignore index f96e379fbb304..94798d0a72702 100644 --- a/.gitignore +++ b/.gitignore @@ -88,6 +88,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 37ae4b2384b58..41cc5e7fd7f95 100644 --- a/tests/phpunit/includes/bootstrap.php +++ b/tests/phpunit/includes/bootstrap.php @@ -251,6 +251,24 @@ define( 'REST_TESTS_IMPOSSIBLY_HIGH_NUMBER', 99999999 ); +/* + * Tests that cover the upload_path and upload_url_path options run in a separate process + * with WP_TESTS_SKIP_UPLOADS_CONSTANT set, as UPLOADS overrides those options. + */ +if ( ! defined( 'UPLOADS' ) && '1' !== getenv( 'WP_TESTS_SKIP_UPLOADS_CONSTANT' ) ) { + define( 'UPLOADS', 'wp-content/test-suite-uploads' ); + + /* + * Delete the test uploads dir exactly once per run, via an env var (we need it cos each + * @runInSeparateProcess test re-executes this in its own process). + */ + $delete_tests_uploads_dir = ( false === getenv( 'WP_TESTS_UPLOADS_RUN_ID' ) ); + + if ( $delete_tests_uploads_dir ) { + putenv( 'WP_TESTS_UPLOADS_RUN_ID=' . getmypid() . '-' . uniqid() ); + } +} + $PHP_SELF = '/index.php'; $GLOBALS['PHP_SELF'] = '/index.php'; $_SERVER['PHP_SELF'] = '/index.php'; @@ -315,6 +333,23 @@ 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/class-wp-filesystem-base.php'; + require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php'; + + // Use the direct transport, as WP_Filesystem() may pick FTP and fail without credentials. + $uploads_test_run_fs = new WP_Filesystem_Direct( null ); + $uploads_test_run_fs->delete( $uploads_test_run_dir, true ); + } + + unset( $uploads_test_run_dir, $uploads_test_run_fs ); +} +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 6aab0c80b7201..3bcf8db87e893 100644 --- a/tests/phpunit/includes/functions.php +++ b/tests/phpunit/includes/functions.php @@ -337,6 +337,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..ce7f5d08ab295 100644 --- a/tests/phpunit/tests/fonts/font-library/wpFontsDir.php +++ b/tests/phpunit/tests/fonts/font-library/wpFontsDir.php @@ -52,10 +52,10 @@ public function test_fonts_dir_for_multisite() { $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", + 'url' => get_option( 'siteurl' ) . '/' . UPLOADS . "/sites/{$blog_id}/fonts", 'subdir' => '', 'basedir' => untrailingslashit( $main_site_upload_dir['basedir'] ) . "/sites/{$blog_id}/fonts", - 'baseurl' => untrailingslashit( $main_site_upload_dir['baseurl'] ) . "/sites/{$blog_id}/fonts", + 'baseurl' => get_option( 'siteurl' ) . '/' . UPLOADS . "/sites/{$blog_id}/fonts", 'error' => false, ); diff --git a/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index eea334c927626..05af011f5cf0a 100644 --- a/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/tests/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -447,7 +447,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 4cc8e35623609..c024efb075082 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -670,7 +670,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' ), ) ); @@ -1073,7 +1073,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(); @@ -1090,7 +1090,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 ) ); @@ -1124,7 +1124,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(); @@ -1141,7 +1141,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 ); @@ -1174,7 +1174,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[] = '
'; } @@ -1212,7 +1212,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[] = '
'; @@ -1254,7 +1254,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[] = '
'; } @@ -1297,7 +1297,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[] = '
'; @@ -1340,7 +1340,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[] = '
'; @@ -1823,7 +1823,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 ) ); } @@ -1841,7 +1841,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 ) ); } @@ -1869,7 +1869,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 ) ); } @@ -2399,7 +2399,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' ); @@ -2453,7 +2453,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' ); @@ -2544,7 +2544,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' ); @@ -2645,7 +2645,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, @@ -2678,7 +2678,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, ' . @@ -2694,7 +2694,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, @@ -2727,7 +2727,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, ' . @@ -2742,7 +2742,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, @@ -2776,10 +2776,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. @@ -2816,7 +2816,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, @@ -2849,7 +2849,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, ' . @@ -2871,7 +2871,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' ); @@ -3074,7 +3074,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 ) ); } @@ -3203,8 +3203,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 ); @@ -3301,11 +3301,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, ' . @@ -3434,7 +3434,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 static 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 6c9b00894b6c3..9a0a5094b2d47 100644 --- a/tests/phpunit/tests/media/getNextImageLink.php +++ b/tests/phpunit/tests/media/getNextImageLink.php @@ -31,7 +31,7 @@ public static 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 e3b670f2c8be0..6b86b00dd1315 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 b8a3ac6d1e9ba..eeca618f742a6 100644 --- a/tests/phpunit/tests/media/getPreviousImageLink.php +++ b/tests/phpunit/tests/media/getPreviousImageLink.php @@ -31,7 +31,7 @@ public static 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 d284138fd84e5..c288018e83559 100644 --- a/tests/phpunit/tests/media/nextImageLink.php +++ b/tests/phpunit/tests/media/nextImageLink.php @@ -30,7 +30,7 @@ public static 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 50528675bb0dc..df8a23d1ada18 100644 --- a/tests/phpunit/tests/media/previousImageLink.php +++ b/tests/phpunit/tests/media/previousImageLink.php @@ -30,7 +30,7 @@ public static 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 396018ea918dd..a35c45efc5112 100644 --- a/tests/phpunit/tests/multisite/msFilesRewriting.php +++ b/tests/phpunit/tests/multisite/msFilesRewriting.php @@ -15,28 +15,39 @@ class Tests_Multisite_msFilesRewriting extends WP_UnitTestCase { public function set_up() { parent::set_up(); update_site_option( 'ms_files_rewriting', 1 ); + + // Legacy ms-files subsite uploads ignore UPLOADS, so keep them in the test dir. + if ( ! defined( 'UPLOADBLOGSDIR' ) ) { + define( 'UPLOADBLOGSDIR', UPLOADS . '/blogs.dir' ); + } + ms_upload_constants(); + + // The main site ignores UPLOADS with ms-files rewriting, so keep its legacy path in the test dir. + update_option( 'upload_path', UPLOADS ); + + // Refresh the upload dir cache, which was populated before ms-files rewriting was enabled. + wp_upload_dir( null, false, true ); } 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'] ); switch_to_blog( $blog_id2 ); $info2 = wp_upload_dir(); $this->assertNotSame( $info, $info2 ); - $this->assertSame( get_option( 'siteurl' ) . '/wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . $date, $info2['url'] ); - $this->assertSame( ABSPATH . 'wp-content/blogs.dir/' . get_current_blog_id() . '/files/' . $date, $info2['path'] ); + $this->assertSame( get_option( 'siteurl' ) . '/' . UPLOADBLOGSDIR . '/' . get_current_blog_id() . '/files/' . $date, $info2['url'] ); + $this->assertSame( ABSPATH . UPLOADBLOGSDIR . '/' . get_current_blog_id() . '/files/' . $date, $info2['path'] ); $this->assertSame( '/' . $date, $info2['subdir'] ); $this->assertFalse( $info2['error'] ); restore_current_blog(); diff --git a/tests/phpunit/tests/multisite/site.php b/tests/phpunit/tests/multisite/site.php index d5f4630823dd4..3e40955c18920 100644 --- a/tests/phpunit/tests/multisite/site.php +++ b/tests/phpunit/tests/multisite/site.php @@ -565,12 +565,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'] ); @@ -578,15 +577,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..c1394c54881f4 100644 --- a/tests/phpunit/tests/upload.php +++ b/tests/phpunit/tests/upload.php @@ -24,43 +24,8 @@ 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( $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( $subdir, $info['subdir'] ); - $this->assertFalse( $info['error'] ); - } - - /** - * @ticket 5953 - */ - public function test_upload_dir_absolute() { - $path = get_temp_dir() . 'wp-unit-test'; - - // wp_upload_dir() with an absolute upload path. - update_option( 'upload_path', $path ); - - // Doesn't make sense to use an absolute file path without setting the url path. - update_option( 'upload_url_path', '/baz' ); - - // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options. - // It doesn't create the /year/month directories. - $info = _wp_upload_dir(); - $subdir = date_format( date_create( 'now' ), '/Y/m' ); - - $this->assertSame( '/baz' . $subdir, $info['url'] ); - $this->assertSame( $path . $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'] ); } @@ -71,28 +36,14 @@ 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'] ); } - public function test_upload_path_absolute() { - update_option( 'upload_url_path', 'http://' . WP_TESTS_DOMAIN . '/asdf' ); - - // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options. - // It doesn't create the /year/month directories. - $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'] ); - $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 +51,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'] ); } diff --git a/tests/phpunit/tests/upload/uploadDirOptions.php b/tests/phpunit/tests/upload/uploadDirOptions.php new file mode 100644 index 0000000000000..bf1a9da67e7f1 --- /dev/null +++ b/tests/phpunit/tests/upload/uploadDirOptions.php @@ -0,0 +1,90 @@ +assertSame( get_option( 'siteurl' ) . '/foo/bar' . $subdir, $info['url'] ); + $this->assertSame( ABSPATH . 'foo/bar' . $subdir, $info['path'] ); + $this->assertSame( $subdir, $info['subdir'] ); + $this->assertFalse( $info['error'] ); + } + + /** + * @ticket 5953 + */ + public function test_upload_dir_absolute() { + $path = get_temp_dir() . 'wp-unit-test'; + + // wp_upload_dir() with an absolute upload path. + update_option( 'upload_path', $path ); + + // Doesn't make sense to use an absolute file path without setting the url path. + update_option( 'upload_url_path', '/baz' ); + + // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options. + // It doesn't create the /year/month directories. + $info = _wp_upload_dir(); + $subdir = date_format( date_create( 'now' ), '/Y/m' ); + + $this->assertSame( '/baz' . $subdir, $info['url'] ); + $this->assertSame( $path . $subdir, $info['path'] ); + $this->assertSame( $subdir, $info['subdir'] ); + $this->assertFalse( $info['error'] ); + } + + public function test_upload_path_absolute() { + update_option( 'upload_url_path', 'http://' . WP_TESTS_DOMAIN . '/asdf' ); + + // Use `_wp_upload_dir()` directly to bypass caching and work with the changed options. + // It doesn't create the /year/month directories. + $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'] ); + $this->assertSame( $subdir, $info['subdir'] ); + $this->assertFalse( $info['error'] ); + } +}