Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions tests/phpunit/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';

Expand Down
11 changes: 11 additions & 0 deletions tests/phpunit/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
17 changes: 9 additions & 8 deletions tests/phpunit/tests/fonts/font-library/wpFontsDir.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.' );
Expand Down
68 changes: 34 additions & 34 deletions tests/phpunit/tests/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
)
);

Expand Down Expand Up @@ -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();
Expand All @@ -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 ) );
Expand Down Expand Up @@ -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();
Expand All @@ -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 );
Expand Down Expand Up @@ -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[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
}
Expand Down Expand Up @@ -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[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';

Expand Down Expand Up @@ -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[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';
}
Expand Down Expand Up @@ -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[] = '<figure><img src="' . $url . '" data-id="' . $i . '" /></figure>';

Expand Down Expand Up @@ -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[] = '<!-- wp:image {"id":' . $attachment_id . ',"sizeSlug":"large","linkDestination":"none"} --><figure class="wp-block-image size-large"><img src="' . $url . '" /></figure><!-- /wp:image -->';

Expand Down Expand Up @@ -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 ) );
}

Expand All @@ -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 ) );
}

Expand Down Expand Up @@ -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 ) );
}

Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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' );
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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, ' .
Expand All @@ -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,
Expand Down Expand Up @@ -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, ' .
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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, ' .
Expand All @@ -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' );
Expand Down Expand Up @@ -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 ) );
}
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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, ' .
Expand Down Expand Up @@ -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 = '<img width="999" height="999" ' .
'src="' . $uploads_url . 'test-image-testsize-999x999.jpg" ' .
Expand Down
Loading
Loading